public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Sebastian Huber <sebastian.huber@embedded-brains.de>
To: newlib@sourceware.org
Subject: [PATCH 5/8] Add __bitcount*() to RTEMS <machine/types.h>
Date: Mon, 22 May 2017 12:04:00 -0000	[thread overview]
Message-ID: <1495454672-9131-5-git-send-email-sebastian.huber@embedded-brains.de> (raw)
In-Reply-To: <1495454672-9131-1-git-send-email-sebastian.huber@embedded-brains.de>

Use a dedicated header file <machine/_bitcount.h> to avoid cyclic header
dependencies in future changes.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
---
 newlib/libc/sys/rtems/include/machine/_bitcount.h | 90 +++++++++++++++++++++++
 newlib/libc/sys/rtems/include/machine/types.h     |  2 +
 2 files changed, 92 insertions(+)
 create mode 100644 newlib/libc/sys/rtems/include/machine/_bitcount.h

diff --git a/newlib/libc/sys/rtems/include/machine/_bitcount.h b/newlib/libc/sys/rtems/include/machine/_bitcount.h
new file mode 100644
index 0000000..ec324d2
--- /dev/null
+++ b/newlib/libc/sys/rtems/include/machine/_bitcount.h
@@ -0,0 +1,90 @@
+/*-
+ * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _MACHINE__BITCOUNT_H
+#define	_MACHINE__BITCOUNT_H
+
+#ifdef __POPCNT__
+#define	__bitcount64(x)	__builtin_popcountll((__uint64_t)(x))
+#define	__bitcount32(x)	__builtin_popcountl((__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__ */
+static __inline __uint16_t
+__bitcount16(__uint16_t _x)
+{
+	_x = (_x & 0x5555u) + ((_x & 0xaaaau) >> 1);
+	_x = (_x & 0x3333u) + ((_x & 0xccccu) >> 2);
+	_x = (_x + (_x >> 4)) & 0x0f0fu;
+	_x = (_x + (_x >> 8)) & 0x00ffu;
+	return (_x);
+}
+
+static __inline __uint32_t
+__bitcount32(__uint32_t _x)
+{
+	_x = (_x & 0x55555555u) + ((_x & 0xaaaaaaaau) >> 1);
+	_x = (_x & 0x33333333u) + ((_x & 0xccccccccu) >> 2);
+	_x = (_x + (_x >> 4)) & 0x0f0f0f0fu;
+	_x = (_x + (_x >> 8));
+	_x = (_x + (_x >> 16)) & 0x000000ffu;
+	return (_x);
+}
+
+static __inline __uint64_t
+__bitcount64(__uint64_t _x)
+{
+#if __SIZEOF_LONG__ == 8
+	_x = (_x & 0x5555555555555555u) + ((_x & 0xaaaaaaaaaaaaaaaau) >> 1);
+	_x = (_x & 0x3333333333333333u) + ((_x & 0xccccccccccccccccu) >> 2);
+	_x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0fu;
+	_x = (_x + (_x >> 8));
+	_x = (_x + (_x >> 16));
+	_x = (_x + (_x >> 32)) & 0x000000ffu;
+	return (_x);
+#else
+	return (__bitcount32(_x >> 32) + __bitcount32(_x));
+#endif
+}
+
+#if __SIZEOF_LONG__ == 8
+#define	__bitcountl(x)	__bitcount64((unsigned long)(x))
+#else
+#define	__bitcountl(x)	__bitcount32((unsigned long)(x))
+#endif
+
+#if __SIZEOF_INT__ == 8
+#define	__bitcount(x)	__bitcount64((unsigned int)(x))
+#else
+#define	__bitcount(x)	__bitcount32((unsigned int)(x))
+#endif
+#endif /* __POPCNT__ */
+
+#endif /* _MACHINE__BITCOUNT_H */
diff --git a/newlib/libc/sys/rtems/include/machine/types.h b/newlib/libc/sys/rtems/include/machine/types.h
index b28f923..11ca37f 100644
--- a/newlib/libc/sys/rtems/include/machine/types.h
+++ b/newlib/libc/sys/rtems/include/machine/types.h
@@ -28,6 +28,8 @@
 #error "must be included via <sys/types.h>"
 #endif /* !_SYS_TYPES_H */
 
+#include <machine/_bitcount.h>
+
 #if __BSD_VISIBLE
 
 #ifndef _ACCMODE_T_DECLARED
-- 
1.8.4.5

  parent reply	other threads:[~2017-05-22 12:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-22 12:04 [PATCH 1/8] Add generic <machine/_align.h> for RTEMS Sebastian Huber
2017-05-22 12:04 ` [PATCH 8/8] Add de-facto standard <sys/ioctl.h> " Sebastian Huber
2017-05-22 12:04 ` [PATCH 6/8] FreeBSD compatibility for RTEMS <sys/cpuset.h> Sebastian Huber
2017-05-22 12:04 ` [PATCH 3/8] Increase MSIZE for RTEMS Sebastian Huber
2017-05-22 12:04 ` [PATCH 2/8] FreeBSD compatibility for RTEMS <sys/param.h> Sebastian Huber
2017-05-22 12:04 ` [PATCH 4/8] Move ARM access.c from machine to sys Sebastian Huber
2017-05-22 12:04 ` Sebastian Huber [this message]
2017-05-22 12:09 ` [PATCH 1/8] Add generic <machine/_align.h> for RTEMS 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=1495454672-9131-5-git-send-email-sebastian.huber@embedded-brains.de \
    --to=sebastian.huber@embedded-brains.de \
    --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).