From: Christian Brauner Date: Thu, 25 Feb 2021 12:48:52 +0000 (+0100) Subject: macro: add hweight*() helpers X-Git-Tag: lxc-5.0.0~267^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=265dc73af65742b65ed4bcf025f1e7c5d5fd23b1;p=thirdparty%2Flxc.git macro: add hweight*() helpers Signed-off-by: Christian Brauner --- diff --git a/src/lxc/macro.h b/src/lxc/macro.h index c98ca5900..a54fe72b3 100644 --- a/src/lxc/macro.h +++ b/src/lxc/macro.h @@ -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 */