1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_HWEIGHT_H
3 #define _ASM_X86_HWEIGHT_H
4 
5 #include <asm/cpufeatures.h>
6 
7 #ifdef CONFIG_64BIT
8 #define REG_IN "D"
9 #define REG_OUT "a"
10 #else
11 #define REG_IN "a"
12 #define REG_OUT "a"
13 #endif
14 
__arch_hweight32(unsigned int w)15 static __always_inline unsigned int __arch_hweight32(unsigned int w)
16 {
17 	unsigned int res;
18 
19 	asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE
20 				"call __sw_hweight32",
21 				"popcntl %[val], %[cnt]", X86_FEATURE_POPCNT)
22 			 : [cnt] "=" REG_OUT (res), ASM_CALL_CONSTRAINT
23 			 : [val] REG_IN (w));
24 
25 	return res;
26 }
27 
__arch_hweight16(unsigned int w)28 static inline unsigned int __arch_hweight16(unsigned int w)
29 {
30 	return __arch_hweight32(w & 0xffff);
31 }
32 
__arch_hweight8(unsigned int w)33 static inline unsigned int __arch_hweight8(unsigned int w)
34 {
35 	return __arch_hweight32(w & 0xff);
36 }
37 
38 #ifdef CONFIG_X86_32
__arch_hweight64(__u64 w)39 static inline unsigned long __arch_hweight64(__u64 w)
40 {
41 	return  __arch_hweight32((u32)w) +
42 		__arch_hweight32((u32)(w >> 32));
43 }
44 #else
__arch_hweight64(__u64 w)45 static __always_inline unsigned long __arch_hweight64(__u64 w)
46 {
47 	unsigned long res;
48 
49 	asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE
50 				"call __sw_hweight64",
51 				"popcntq %[val], %[cnt]", X86_FEATURE_POPCNT)
52 			 : [cnt] "=" REG_OUT (res), ASM_CALL_CONSTRAINT
53 			 : [val] REG_IN (w));
54 
55 	return res;
56 }
57 #endif /* CONFIG_X86_32 */
58 
59 #endif
60