18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
38c2ecf20Sopenharmony_ci#define _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci/*
68c2ecf20Sopenharmony_ci * Compile time versions of __arch_hweightN()
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#define __const_hweight8(w)		\
98c2ecf20Sopenharmony_ci	((unsigned int)			\
108c2ecf20Sopenharmony_ci	 ((!!((w) & (1ULL << 0))) +	\
118c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 1))) +	\
128c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 2))) +	\
138c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 3))) +	\
148c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 4))) +	\
158c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 5))) +	\
168c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 6))) +	\
178c2ecf20Sopenharmony_ci	  (!!((w) & (1ULL << 7)))))
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define __const_hweight16(w) (__const_hweight8(w)  + __const_hweight8((w)  >> 8 ))
208c2ecf20Sopenharmony_ci#define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
218c2ecf20Sopenharmony_ci#define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * Generic interface.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci#define hweight8(w)  (__builtin_constant_p(w) ? __const_hweight8(w)  : __arch_hweight8(w))
278c2ecf20Sopenharmony_ci#define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w))
288c2ecf20Sopenharmony_ci#define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w))
298c2ecf20Sopenharmony_ci#define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * Interface for known constant arguments
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define HWEIGHT8(w)  (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight8(w))
358c2ecf20Sopenharmony_ci#define HWEIGHT16(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight16(w))
368c2ecf20Sopenharmony_ci#define HWEIGHT32(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight32(w))
378c2ecf20Sopenharmony_ci#define HWEIGHT64(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight64(w))
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * Type invariant interface to the compile time constant hweight functions.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#define HWEIGHT(w)   HWEIGHT64((u64)w)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#endif /* _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ */
45