This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] For RTEMS-LIBBSD support, add bitcount routines


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]