]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
macro: add hweight*() helpers
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 25 Feb 2021 12:48:52 +0000 (13:48 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 25 Feb 2021 12:49:29 +0000 (13:49 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/macro.h

index c98ca5900354b368200afe910082a540b2b4c129..a54fe72b3a2add84eb841f5963b9df11e07c97ce 100644 (file)
@@ -708,4 +708,29 @@ enum {
                _min1 < _min2 ? _min1 : _min2; \
        })
 
+#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
+
+/*
+ * Compile time versions of __arch_hweightN()
+ */
+#define __const_hweight8(w)            \
+       ((unsigned int)                 \
+        ((!!((w) & (1ULL << 0))) +     \
+         (!!((w) & (1ULL << 1))) +     \
+         (!!((w) & (1ULL << 2))) +     \
+         (!!((w) & (1ULL << 3))) +     \
+         (!!((w) & (1ULL << 4))) +     \
+         (!!((w) & (1ULL << 5))) +     \
+         (!!((w) & (1ULL << 6))) +     \
+         (!!((w) & (1ULL << 7)))))
+
+#define __const_hweight16(w) (__const_hweight8(w)  + __const_hweight8((w)  >> 8 ))
+#define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
+#define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
+
+#define hweight8(w) __const_hweight8(w)
+#define hweight16(w) __const_hweight16(w)
+#define hweight32(w) __const_hweight32(w)
+#define hweight64(w) __const_hweight64(w)
+
 #endif /* __LXC_MACRO_H */