1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_HWEIGHT_H
3 #define _ASM_X86_HWEIGHT_H
5 #include <asm/cpufeatures.h>
8 /* popcnt %edi, %eax */
9 #define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc7"
10 /* popcnt %rdi, %rax */
11 #define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7"
15 /* popcnt %eax, %eax */
16 #define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0"
21 #define __HAVE_ARCH_SW_HWEIGHT
23 static __always_inline unsigned int __arch_hweight32(unsigned int w)
27 asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT)
34 static inline unsigned int __arch_hweight16(unsigned int w)
36 return __arch_hweight32(w & 0xffff);
39 static inline unsigned int __arch_hweight8(unsigned int w)
41 return __arch_hweight32(w & 0xff);
45 static inline unsigned long __arch_hweight64(__u64 w)
47 return __arch_hweight32((u32)w) +
48 __arch_hweight32((u32)(w >> 32));
51 static __always_inline unsigned long __arch_hweight64(__u64 w)
55 asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
61 #endif /* CONFIG_X86_32 */