public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Craig Howland <howland@LGSInnovations.com>
To: <newlib@sourceware.org>
Subject: Re: [PATCH] For RTEMS-LIBBSD support, add bitcount routines
Date: Tue, 16 May 2017 19:05:00 -0000	[thread overview]
Message-ID: <e49202b4-8eec-af40-535d-8f9ca56bf4a8@LGSInnovations.com> (raw)
In-Reply-To: <201705161823.v4GIMc16015657@mx0a-00116901.pphosted.com>

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

  reply	other threads:[~2017-05-16 19:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-16 18:23 Kevin Kirspel
2017-05-16 19:05 ` Craig Howland [this message]
2017-05-17  5:22   ` Sebastian Huber
2017-05-17  5:11 ` Sebastian Huber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e49202b4-8eec-af40-535d-8f9ca56bf4a8@LGSInnovations.com \
    --to=howland@lgsinnovations.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).