From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 102182 invoked by alias); 16 May 2017 18:23:01 -0000 Mailing-List: contact newlib-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-owner@sourceware.org Received: (qmail 102152 invoked by uid 89); 16 May 2017 18:23:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=H*r:8.16.0, SIMD, H*r:sk:mx0a-00 X-HELO: mx0a-00116901.pphosted.com Received: from mx0a-00116901.pphosted.com (HELO mx0a-00116901.pphosted.com) (67.231.145.58) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 16 May 2017 18:22:58 +0000 Received: from pps.filterd (m0046982.ppops.net [127.0.0.1]) by mx0a-00116901.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v4GIMc16015657 for ; Tue, 16 May 2017 11:23:00 -0700 Message-Id: <201705161823.v4GIMc16015657@mx0a-00116901.pphosted.com> From: Kevin Kirspel To: CC: Kevin Kirspel Subject: [PATCH] For RTEMS-LIBBSD support, add bitcount routines Date: Tue, 16 May 2017 18:23:00 -0000 MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-05-16_05:,, signatures=0 X-SW-Source: 2017/txt/msg00328.txt.bz2 --- newlib/libc/include/sys/types.h | 66 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/newlib/libc/include/sys/types.h b/newlib/libc/include/sys/types.h index 65ff520..83f891e 100644 --- a/newlib/libc/include/sys/types.h +++ b/newlib/libc/include/sys/types.h @@ -53,6 +53,64 @@ typedef __int64_t quad_t; typedef quad_t * qaddr_t; #endif +#ifdef __POPCNT__ +#define __bitcount64(x) __builtin_popcountll((__uint64_t)(x)) +#define __bitcount32(x) __builtin_popcount((__uint32_t)(x)) +#define __bitcount16(x) __builtin_popcount((__uint16_t)(x)) +#define __bitcountl(x) __builtin_popcountl((unsigned long)(x)) +#define __bitcount(x) __builtin_popcount((unsigned int)(x)) +#else /* __POPCNT__ */ +/* + * Population count algorithm using SWAR approach + * - "SIMD Within A Register". + */ +static __inline __uint16_t +__bitcount16(__uint16_t _x) +{ + _x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1); + _x = (_x & 0x3333) + ((_x & 0xcccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f; + _x = (_x + (_x >> 8)) & 0x00ff; + return (_x); +} + +static __inline __uint32_t +__bitcount32(__uint32_t _x) +{ + _x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1); + _x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f0f0f; + _x = (_x + (_x >> 8)); + _x = (_x + (_x >> 16)) & 0x000000ff; + return (_x); +} + +#ifdef __LP64__ +static __inline __uint64_t +__bitcount64(__uint64_t _x) +{ + _x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1); + _x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2); + _x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f; + _x = (_x + (_x >> 8)); + _x = (_x + (_x >> 16)); + _x = (_x + (_x >> 32)) & 0x000000ff; + return (_x); +} + +#define __bitcountl(x) __bitcount64((unsigned long)(x)) +#else /* __LP64__ */ +static __inline __uint64_t +__bitcount64(__uint64_t _x) +{ + return (__bitcount32(_x >> 32) + __bitcount32(_x)); +} + +#define __bitcountl(x) __bitcount32((unsigned long)(x)) +#endif /* __LP64__ */ +#define __bitcount(x) __bitcount32((unsigned int)(x)) +#endif /* __POPCNT__ */ + #endif /* __rtems__ || __XMK__ */ #ifndef __need_inttypes -- 1.9.1