Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 31 May 2023 16:48:33 +0200
From: Jens Gustedt <Jens.Gustedt@...ia.fr>
To: musl@...ts.openwall.com
Subject: [C23 stdbit.h 1/2] implement low level popcount

---
 src/internal/atomic.h | 45 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/src/internal/atomic.h b/src/internal/atomic.h
index 96c1552d..39c47ed5 100644
--- a/src/internal/atomic.h
+++ b/src/internal/atomic.h
@@ -330,4 +330,49 @@ static inline int a_clz_32(uint32_t x)
 }
 #endif
 
+#ifndef a_popcount_14
+#define a_popcount_14 a_popcount_14
+static inline int a_popcount_14(uint64_t x)
+{
+	x &= UINT64_C(0x3FFF);
+	return (x * UINT64_C(0x200040008001) & UINT64_C(0x111111111111111)) % 0xf;
+}
+#endif
+
+#ifndef a_popcount_8
+#define a_popcount_8 a_popcount_8
+static inline int a_popcount_8(uint64_t x)
+{
+	x &= UINT64_C(0xFF);
+	return a_popcount_14(x);
+}
+#endif
+
+#ifndef a_popcount_16
+#define a_popcount_16 a_popcount_16
+static inline int a_popcount_16(uint64_t x)
+{
+	x &= UINT64_C(0xFFFF);
+	return a_popcount_14(x) + a_popcount_14(x>>14);
+}
+#endif
+
+#ifndef a_popcount_32
+#define a_popcount_32 a_popcount_32
+static inline int a_popcount_32(uint64_t x)
+{
+	x &= UINT64_C(0xFFFFFFFF);
+	return a_popcount_14(x) + a_popcount_14(x>>14) + a_popcount_14(x>>28);
+}
+#endif
+
+#ifndef a_popcount_64
+#define a_popcount_64 a_popcount_64
+static inline int a_popcount_64(uint64_t x)
+{
+	return a_popcount_14(x) + a_popcount_14(x>>14) + a_popcount_14(x>>28)
+	  + a_popcount_14(x>>42) + a_popcount_14(x>>56);
+}
+#endif
+
 #endif
-- 
2.34.1

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.