* [PATCH v2] For RTEMS-LIBBSD support, add bitcount routines
@ 2017-05-17 13:06 Kevin Kirspel
2017-05-19 5:52 ` Sebastian Huber
0 siblings, 1 reply; 3+ messages in thread
From: Kevin Kirspel @ 2017-05-17 13:06 UTC (permalink / raw)
To: newlib; +Cc: Kevin Kirspel
---
newlib/libc/sys/rtems/include/machine/types.h | 58 +++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/newlib/libc/sys/rtems/include/machine/types.h b/newlib/libc/sys/rtems/include/machine/types.h
index b28f923..0b0a100 100644
--- a/newlib/libc/sys/rtems/include/machine/types.h
+++ b/newlib/libc/sys/rtems/include/machine/types.h
@@ -83,3 +83,61 @@ typedef __uintmax_t rman_res_t;
#endif
#endif /* __BSD_VISIBLE */
+
+#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);
+}
+
+#if __SIZEOF_INT__ == 8
+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 /* __SIZEOF_INT__ == 8 */
+static __inline __uint64_t
+__bitcount64(__uint64_t _x)
+{
+ return (__bitcount32(_x >> 32) + __bitcount32(_x));
+}
+
+#define __bitcountl(x) __bitcount32((unsigned long)(x))
+#endif /* __SIZEOF_INT__ == 8 */
+#define __bitcount(x) __bitcount32((unsigned int)(x))
+#endif /* __POPCNT__ */
--
1.9.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] For RTEMS-LIBBSD support, add bitcount routines
2017-05-17 13:06 [PATCH v2] For RTEMS-LIBBSD support, add bitcount routines Kevin Kirspel
@ 2017-05-19 5:52 ` Sebastian Huber
2017-05-22 6:39 ` Sebastian Huber
0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Huber @ 2017-05-19 5:52 UTC (permalink / raw)
To: Kevin Kirspel, newlib
On 17/05/17 15:06, Kevin Kirspel wrote:
> ---
> newlib/libc/sys/rtems/include/machine/types.h | 58 +++++++++++++++++++++++++++
> 1 file changed, 58 insertions(+)
>
> diff --git a/newlib/libc/sys/rtems/include/machine/types.h b/newlib/libc/sys/rtems/include/machine/types.h
> index b28f923..0b0a100 100644
> --- a/newlib/libc/sys/rtems/include/machine/types.h
> +++ b/newlib/libc/sys/rtems/include/machine/types.h
> @@ -83,3 +83,61 @@ typedef __uintmax_t rman_res_t;
> #endif
>
> #endif /* __BSD_VISIBLE */
> +
> +#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);
> +}
> +
> +#if __SIZEOF_INT__ == 8
> +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 /* __SIZEOF_INT__ == 8 */
> +static __inline __uint64_t
> +__bitcount64(__uint64_t _x)
> +{
> + return (__bitcount32(_x >> 32) + __bitcount32(_x));
> +}
The __bitcount64() implementation should be based on the word size of
the architecture. Usually this is indicated by sizeof long.
> +
> +#define __bitcountl(x) __bitcount32((unsigned long)(x))
> +#endif /* __SIZEOF_INT__ == 8 */
> +#define __bitcount(x) __bitcount32((unsigned int)(x))
> +#endif /* __POPCNT__ */
Selection of __bitcountl() should be based on sizeof long. Selection of
__bitcount() should be based on sizeof int. Maybe it makes sense to add
a test for this to the RTEMS testsuite and do a check of the
pre-processor output on the arm-rtems4.12, sparc64-rtems4.12 and
m32c-rtems4.12 targets.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] For RTEMS-LIBBSD support, add bitcount routines
2017-05-19 5:52 ` Sebastian Huber
@ 2017-05-22 6:39 ` Sebastian Huber
0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Huber @ 2017-05-22 6:39 UTC (permalink / raw)
To: Kevin Kirspel, newlib
I will send an alternative version today since there are some cyclic
dependency problems with this definition location.
--
Sebastian Huber, embedded brains GmbH
Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone : +49 89 189 47 41-16
Fax : +49 89 189 47 41-09
E-Mail : sebastian.huber@embedded-brains.de
PGP : Public key available on request.
Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-05-22 6:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-17 13:06 [PATCH v2] For RTEMS-LIBBSD support, add bitcount routines Kevin Kirspel
2017-05-19 5:52 ` Sebastian Huber
2017-05-22 6:39 ` Sebastian Huber
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).