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 
15 #define __HAVE_ARCH_SW_HWEIGHT
16 
17 static __always_inline unsigned int __arch_hweight32(unsigned int w)
18 {
19 	unsigned int res;
20 
21 	asm (ALTERNATIVE("call __sw_hweight32", "popcntl %1, %0", X86_FEATURE_POPCNT)
22 			 : "="REG_OUT (res)
23 			 : REG_IN (w));
24 
25 	return res;
26 }
27 
28 static inline unsigned int __arch_hweight16(unsigned int w)
29 {
30 	return __arch_hweight32(w & 0xffff);
31 }
32 
33 static inline unsigned int __arch_hweight8(unsigned int w)
34 {
35 	return __arch_hweight32(w & 0xff);
36 }
37 
38 #ifdef CONFIG_X86_32
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
45 static __always_inline unsigned long __arch_hweight64(__u64 w)
46 {
47 	unsigned long res;
48 
49 	asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", X86_FEATURE_POPCNT)
50 			 : "="REG_OUT (res)
51 			 : REG_IN (w));
52 
53 	return res;
54 }
55 #endif /* CONFIG_X86_32 */
56 
57 #endif
58