1 #ifndef _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
2 #define _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
3 
4 /*
5  * Compile time versions of __arch_hweightN()
6  */
7 #define __const_hweight8(w)		\
8 	((unsigned int)			\
9 	 ((!!((w) & (1ULL << 0))) +	\
10 	  (!!((w) & (1ULL << 1))) +	\
11 	  (!!((w) & (1ULL << 2))) +	\
12 	  (!!((w) & (1ULL << 3))) +	\
13 	  (!!((w) & (1ULL << 4))) +	\
14 	  (!!((w) & (1ULL << 5))) +	\
15 	  (!!((w) & (1ULL << 6))) +	\
16 	  (!!((w) & (1ULL << 7)))))
17 
18 #define __const_hweight16(w) (__const_hweight8(w)  + __const_hweight8((w)  >> 8 ))
19 #define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
20 #define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
21 
22 /*
23  * Generic interface.
24  */
25 #define hweight8(w)  (__builtin_constant_p(w) ? __const_hweight8(w)  : __arch_hweight8(w))
26 #define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
27 #define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
28 #define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
29 
30 /*
31  * Interface for known constant arguments
32  */
33 #define HWEIGHT8(w)  (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight8(w))
34 #define HWEIGHT16(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight16(w))
35 #define HWEIGHT32(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight32(w))
36 #define HWEIGHT64(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight64(w))
37 
38 /*
39  * Type invariant interface to the compile time constant hweight functions.
40  */
41 #define HWEIGHT(w)   HWEIGHT64((u64)w)
42 
43 #endif /* _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ */
44