]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
d61931d8 BP |
2 | #ifndef _ASM_X86_HWEIGHT_H |
3 | #define _ASM_X86_HWEIGHT_H | |
4 | ||
cd4d09ec BP |
5 | #include <asm/cpufeatures.h> |
6 | ||
d61931d8 | 7 | #ifdef CONFIG_64BIT |
f5967101 BP |
8 | /* popcnt %edi, %eax */ |
9 | #define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc7" | |
d61931d8 | 10 | /* popcnt %rdi, %rax */ |
c59bd568 | 11 | #define POPCNT64 ".byte 0xf3,0x48,0x0f,0xb8,0xc7" |
d61931d8 BP |
12 | #define REG_IN "D" |
13 | #define REG_OUT "a" | |
14 | #else | |
15 | /* popcnt %eax, %eax */ | |
c59bd568 | 16 | #define POPCNT32 ".byte 0xf3,0x0f,0xb8,0xc0" |
d61931d8 BP |
17 | #define REG_IN "a" |
18 | #define REG_OUT "a" | |
19 | #endif | |
20 | ||
f5967101 BP |
21 | #define __HAVE_ARCH_SW_HWEIGHT |
22 | ||
d14edb16 | 23 | static __always_inline unsigned int __arch_hweight32(unsigned int w) |
d61931d8 | 24 | { |
f5967101 | 25 | unsigned int res; |
d61931d8 | 26 | |
c59bd568 | 27 | asm (ALTERNATIVE("call __sw_hweight32", POPCNT32, X86_FEATURE_POPCNT) |
f5967101 BP |
28 | : "="REG_OUT (res) |
29 | : REG_IN (w)); | |
d61931d8 BP |
30 | |
31 | return res; | |
32 | } | |
33 | ||
34 | static inline unsigned int __arch_hweight16(unsigned int w) | |
35 | { | |
36 | return __arch_hweight32(w & 0xffff); | |
37 | } | |
38 | ||
39 | static inline unsigned int __arch_hweight8(unsigned int w) | |
40 | { | |
41 | return __arch_hweight32(w & 0xff); | |
42 | } | |
43 | ||
d14edb16 | 44 | #ifdef CONFIG_X86_32 |
d61931d8 BP |
45 | static inline unsigned long __arch_hweight64(__u64 w) |
46 | { | |
d61931d8 BP |
47 | return __arch_hweight32((u32)w) + |
48 | __arch_hweight32((u32)(w >> 32)); | |
d14edb16 | 49 | } |
d61931d8 | 50 | #else |
d14edb16 DV |
51 | static __always_inline unsigned long __arch_hweight64(__u64 w) |
52 | { | |
f5967101 | 53 | unsigned long res; |
d14edb16 | 54 | |
c59bd568 | 55 | asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT) |
f5967101 BP |
56 | : "="REG_OUT (res) |
57 | : REG_IN (w)); | |
d61931d8 BP |
58 | |
59 | return res; | |
60 | } | |
d14edb16 | 61 | #endif /* CONFIG_X86_32 */ |
d61931d8 BP |
62 | |
63 | #endif |