1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21527bc8bSPeter Zijlstra #ifndef _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
31527bc8bSPeter Zijlstra #define _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
41527bc8bSPeter Zijlstra 
51527bc8bSPeter Zijlstra /*
61527bc8bSPeter Zijlstra  * Compile time versions of __arch_hweightN()
71527bc8bSPeter Zijlstra  */
81527bc8bSPeter Zijlstra #define __const_hweight8(w)		\
9c32fa99fSPaul Walmsley 	((unsigned int)			\
101527bc8bSPeter Zijlstra 	 ((!!((w) & (1ULL << 0))) +	\
111527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 1))) +	\
121527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 2))) +	\
131527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 3))) +	\
141527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 4))) +	\
151527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 5))) +	\
161527bc8bSPeter Zijlstra 	  (!!((w) & (1ULL << 6))) +	\
17c32fa99fSPaul Walmsley 	  (!!((w) & (1ULL << 7)))))
181527bc8bSPeter Zijlstra 
191527bc8bSPeter Zijlstra #define __const_hweight16(w) (__const_hweight8(w)  + __const_hweight8((w)  >> 8 ))
201527bc8bSPeter Zijlstra #define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
211527bc8bSPeter Zijlstra #define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
221527bc8bSPeter Zijlstra 
231527bc8bSPeter Zijlstra /*
241527bc8bSPeter Zijlstra  * Generic interface.
251527bc8bSPeter Zijlstra  */
261527bc8bSPeter Zijlstra #define hweight8(w)  (__builtin_constant_p(w) ? __const_hweight8(w)  : __arch_hweight8(w))
271527bc8bSPeter Zijlstra #define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
281527bc8bSPeter Zijlstra #define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
291527bc8bSPeter Zijlstra #define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
301527bc8bSPeter Zijlstra 
311527bc8bSPeter Zijlstra /*
321527bc8bSPeter Zijlstra  * Interface for known constant arguments
331527bc8bSPeter Zijlstra  */
341527bc8bSPeter Zijlstra #define HWEIGHT8(w)  (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight8(w))
351527bc8bSPeter Zijlstra #define HWEIGHT16(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight16(w))
361527bc8bSPeter Zijlstra #define HWEIGHT32(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight32(w))
371527bc8bSPeter Zijlstra #define HWEIGHT64(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight64(w))
381527bc8bSPeter Zijlstra 
391527bc8bSPeter Zijlstra /*
401527bc8bSPeter Zijlstra  * Type invariant interface to the compile time constant hweight functions.
411527bc8bSPeter Zijlstra  */
421527bc8bSPeter Zijlstra #define HWEIGHT(w)   HWEIGHT64((u64)w)
431527bc8bSPeter Zijlstra 
441527bc8bSPeter Zijlstra #endif /* _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ */
45