From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 106275 invoked by alias); 16 May 2017 19:05:02 -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 106262 invoked by uid 89); 16 May 2017 19:05:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-19.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 spammy=A53, Depending X-HELO: mail01.lgsinnovations.com Received: from mail01.lgsinnovations.com (HELO mail01.lgsinnovations.com) (184.75.234.233) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 16 May 2017 19:04:59 +0000 Subject: Re: [PATCH] For RTEMS-LIBBSD support, add bitcount routines To: References: <201705161823.v4GIMc16015657@mx0a-00116901.pphosted.com> From: Craig Howland Message-ID: Date: Tue, 16 May 2017 19:05:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <201705161823.v4GIMc16015657@mx0a-00116901.pphosted.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LGS-EX05.lgsdirect.com (135.22.48.227) To LGS-EX01.lgsdirect.com (135.22.77.164) X-IsSubscribed: yes X-SW-Source: 2017/txt/msg00329.txt.bz2 On 05/16/2017 02:23 PM, Kevin Kirspel wrote: > --- > 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 > ... > +#ifdef __LP64__ > +static __inline __uint64_t > +__bitcount64(__uint64_t _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__ */ > + Depending only upon LP64 is not sufficient in general to get all the sizes right. (ARM Cortex A53, for example, has 64-bit int, too--ILP64. So A53 would probably end up with two problems, bitcountl and bitcount.) One possible solution would be something similar to this size-agnostic endian function I use: #define __bswap(x) \ __builtin_choose_expr( \ sizeof(x) == sizeof(char), \ (x), \ __builtin_choose_expr( \ sizeof(x) == sizeof(short), \ __bswap16(x), \ __builtin_choose_expr( \ sizeof(x) == sizeof(int), \ __bswap32(x), \ __builtin_choose_expr( \ sizeof(x) == sizeof(long long), \ __bswap64(x), \ /* The void expression results in a compile-time error \ when assigning the result to something. */ \ (void) 0 \ ) \ ) \ ) \ ) One problem with it is that it is GCC-specific with the use of __builtin_choose_expr(), but that can be avoided, too, by using a contruct similar to that for fpclassify in math.h (use sizeof(int)==4 and sizeof(int)==8 for the tests, etc.)--the version in the else when the GNUC_PREREQ(4,4) check fails. Craig