diff options
author | Borislav Petkov <borislav.petkov@amd.com> | 2010-03-05 11:34:46 -0500 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2010-04-06 18:52:11 -0400 |
commit | d61931d89be506372d01a90d1755f6d0a9fafe2d (patch) | |
tree | 652c34238edcb6c558163abc3cd9d6ce7c5f91a5 | |
parent | 1527bc8b928dd1399c3d3467dd47d9ede210978a (diff) |
x86: Add optimized popcnt variants
Add support for the hardware version of the Hamming weight function,
popcnt, present in CPUs which advertize it under CPUID, Function
0x0000_0001_ECX[23]. On CPUs which don't support it, we fallback to the
default lib/hweight.c sw versions.
A synthetic benchmark comparing popcnt with __sw_hweight64 showed almost
a 3x speedup on a F10h machine.
Signed-off-by: Borislav Petkov <borislav.petkov@amd.com>
LKML-Reference: <20100318112015.GC11152@aftab>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r-- | arch/x86/Kconfig | 5 | ||||
-rw-r--r-- | arch/x86/include/asm/alternative.h | 9 | ||||
-rw-r--r-- | arch/x86/include/asm/arch_hweight.h | 59 | ||||
-rw-r--r-- | arch/x86/include/asm/bitops.h | 4 | ||||
-rw-r--r-- | include/asm-generic/bitops/arch_hweight.h | 22 | ||||
-rw-r--r-- | lib/Makefile | 3 | ||||
-rw-r--r-- | lib/hweight.c | 20 | ||||
-rw-r--r-- | scripts/Makefile.lib | 4 |
8 files changed, 108 insertions, 18 deletions
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0eacb1ffb421..89d8c54cdd37 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig | |||
@@ -238,6 +238,11 @@ config X86_32_LAZY_GS | |||
238 | def_bool y | 238 | def_bool y |
239 | depends on X86_32 && !CC_STACKPROTECTOR | 239 | depends on X86_32 && !CC_STACKPROTECTOR |
240 | 240 | ||
241 | config ARCH_HWEIGHT_CFLAGS | ||
242 | string | ||
243 | default "-fcall-saved-ecx -fcall-saved-edx" if X86_32 | ||
244 | default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx -fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10 -fcall-saved-r11" if X86_64 | ||
245 | |||
241 | config KTIME_SCALAR | 246 | config KTIME_SCALAR |
242 | def_bool X86_32 | 247 | def_bool X86_32 |
243 | source "init/Kconfig" | 248 | source "init/Kconfig" |
diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index b09ec55650b3..67dae51e7fd0 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h | |||
@@ -39,9 +39,6 @@ | |||
39 | #define LOCK_PREFIX "" | 39 | #define LOCK_PREFIX "" |
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | /* This must be included *after* the definition of LOCK_PREFIX */ | ||
43 | #include <asm/cpufeature.h> | ||
44 | |||
45 | struct alt_instr { | 42 | struct alt_instr { |
46 | u8 *instr; /* original instruction */ | 43 | u8 *instr; /* original instruction */ |
47 | u8 *replacement; | 44 | u8 *replacement; |
@@ -96,6 +93,12 @@ static inline int alternatives_text_reserved(void *start, void *end) | |||
96 | ".previous" | 93 | ".previous" |
97 | 94 | ||
98 | /* | 95 | /* |
96 | * This must be included *after* the definition of ALTERNATIVE due to | ||
97 | * <asm/arch_hweight.h> | ||
98 | */ | ||
99 | #include <asm/cpufeature.h> | ||
100 | |||
101 | /* | ||
99 | * Alternative instructions for different CPU types or capabilities. | 102 | * Alternative instructions for different CPU types or capabilities. |
100 | * | 103 | * |
101 | * This allows to use optimized instructions even on generic binary | 104 | * This allows to use optimized instructions even on generic binary |
diff --git a/arch/x86/include/asm/arch_hweight.h b/arch/x86/include/asm/arch_hweight.h new file mode 100644 index 000000000000..d1fc3c219ae6 --- /dev/null +++ b/arch/x86/include/asm/arch_hweight.h | |||
@@ -0,0 +1,59 @@ | |||
1 | #ifndef _ASM_X86_HWEIGHT_H | ||
2 | #define _ASM_X86_HWEIGHT_H | ||
3 | |||
4 | #ifdef CONFIG_64BIT | ||
5 | /* popcnt %rdi, %rax */ | ||
6 | #define POPCNT ".byte 0xf3,0x48,0x0f,0xb8,0xc7" | ||
7 | #define REG_IN "D" | ||
8 | #define REG_OUT "a" | ||
9 | #else | ||
10 | /* popcnt %eax, %eax */ | ||
11 | #define POPCNT ".byte 0xf3,0x0f,0xb8,0xc0" | ||
12 | #define REG_IN "a" | ||
13 | #define REG_OUT "a" | ||
14 | #endif | ||
15 | |||
16 | /* | ||
17 | * __sw_hweightXX are called from within the alternatives below | ||
18 | * and callee-clobbered registers need to be taken care of. See | ||
19 | * ARCH_HWEIGHT_CFLAGS in <arch/x86/Kconfig> for the respective | ||
20 | * compiler switches. | ||
21 | */ | ||
22 | static inline unsigned int __arch_hweight32(unsigned int w) | ||
23 | { | ||
24 | unsigned int res = 0; | ||
25 | |||
26 | asm (ALTERNATIVE("call __sw_hweight32", POPCNT, X86_FEATURE_POPCNT) | ||
27 | : "="REG_OUT (res) | ||
28 | : REG_IN (w)); | ||
29 | |||
30 | return res; | ||
31 | } | ||
32 | |||
33 | static inline unsigned int __arch_hweight16(unsigned int w) | ||
34 | { | ||
35 | return __arch_hweight32(w & 0xffff); | ||
36 | } | ||
37 | |||
38 | static inline unsigned int __arch_hweight8(unsigned int w) | ||
39 | { | ||
40 | return __arch_hweight32(w & 0xff); | ||
41 | } | ||
42 | |||
43 | static inline unsigned long __arch_hweight64(__u64 w) | ||
44 | { | ||
45 | unsigned long res = 0; | ||
46 | |||
47 | #ifdef CONFIG_X86_32 | ||
48 | return __arch_hweight32((u32)w) + | ||
49 | __arch_hweight32((u32)(w >> 32)); | ||
50 | #else | ||
51 | asm (ALTERNATIVE("call __sw_hweight64", POPCNT, X86_FEATURE_POPCNT) | ||
52 | : "="REG_OUT (res) | ||
53 | : REG_IN (w)); | ||
54 | #endif /* CONFIG_X86_32 */ | ||
55 | |||
56 | return res; | ||
57 | } | ||
58 | |||
59 | #endif | ||
diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 02b47a603fc8..545776efeb16 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h | |||
@@ -444,7 +444,9 @@ static inline int fls(int x) | |||
444 | 444 | ||
445 | #define ARCH_HAS_FAST_MULTIPLIER 1 | 445 | #define ARCH_HAS_FAST_MULTIPLIER 1 |
446 | 446 | ||
447 | #include <asm-generic/bitops/hweight.h> | 447 | #include <asm/arch_hweight.h> |
448 | |||
449 | #include <asm-generic/bitops/const_hweight.h> | ||
448 | 450 | ||
449 | #endif /* __KERNEL__ */ | 451 | #endif /* __KERNEL__ */ |
450 | 452 | ||
diff --git a/include/asm-generic/bitops/arch_hweight.h b/include/asm-generic/bitops/arch_hweight.h index 3a7be842cdce..9a81c1e9436c 100644 --- a/include/asm-generic/bitops/arch_hweight.h +++ b/include/asm-generic/bitops/arch_hweight.h | |||
@@ -3,9 +3,23 @@ | |||
3 | 3 | ||
4 | #include <asm/types.h> | 4 | #include <asm/types.h> |
5 | 5 | ||
6 | extern unsigned int __arch_hweight32(unsigned int w); | 6 | inline unsigned int __arch_hweight32(unsigned int w) |
7 | extern unsigned int __arch_hweight16(unsigned int w); | 7 | { |
8 | extern unsigned int __arch_hweight8(unsigned int w); | 8 | return __sw_hweight32(w); |
9 | extern unsigned long __arch_hweight64(__u64 w); | 9 | } |
10 | 10 | ||
11 | inline unsigned int __arch_hweight16(unsigned int w) | ||
12 | { | ||
13 | return __sw_hweight16(w); | ||
14 | } | ||
15 | |||
16 | inline unsigned int __arch_hweight8(unsigned int w) | ||
17 | { | ||
18 | return __sw_hweight8(w); | ||
19 | } | ||
20 | |||
21 | inline unsigned long __arch_hweight64(__u64 w) | ||
22 | { | ||
23 | return __sw_hweight64(w); | ||
24 | } | ||
11 | #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */ | 25 | #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */ |
diff --git a/lib/Makefile b/lib/Makefile index 2e152aed7198..abe63a8ad143 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -39,7 +39,10 @@ lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o | |||
39 | lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o | 39 | lib-$(CONFIG_GENERIC_FIND_FIRST_BIT) += find_next_bit.o |
40 | lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o | 40 | lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o |
41 | obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o | 41 | obj-$(CONFIG_GENERIC_FIND_LAST_BIT) += find_last_bit.o |
42 | |||
43 | CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) | ||
42 | obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o | 44 | obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o |
45 | |||
43 | obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o | 46 | obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o |
44 | obj-$(CONFIG_BTREE) += btree.o | 47 | obj-$(CONFIG_BTREE) += btree.o |
45 | obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o | 48 | obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o |
diff --git a/lib/hweight.c b/lib/hweight.c index a6927e76840f..3c79d50814cf 100644 --- a/lib/hweight.c +++ b/lib/hweight.c | |||
@@ -9,7 +9,7 @@ | |||
9 | * The Hamming Weight of a number is the total number of bits set in it. | 9 | * The Hamming Weight of a number is the total number of bits set in it. |
10 | */ | 10 | */ |
11 | 11 | ||
12 | unsigned int __arch_hweight32(unsigned int w) | 12 | unsigned int __sw_hweight32(unsigned int w) |
13 | { | 13 | { |
14 | #ifdef ARCH_HAS_FAST_MULTIPLIER | 14 | #ifdef ARCH_HAS_FAST_MULTIPLIER |
15 | w -= (w >> 1) & 0x55555555; | 15 | w -= (w >> 1) & 0x55555555; |
@@ -24,30 +24,30 @@ unsigned int __arch_hweight32(unsigned int w) | |||
24 | return (res + (res >> 16)) & 0x000000FF; | 24 | return (res + (res >> 16)) & 0x000000FF; |
25 | #endif | 25 | #endif |
26 | } | 26 | } |
27 | EXPORT_SYMBOL(__arch_hweight32); | 27 | EXPORT_SYMBOL(__sw_hweight32); |
28 | 28 | ||
29 | unsigned int __arch_hweight16(unsigned int w) | 29 | unsigned int __sw_hweight16(unsigned int w) |
30 | { | 30 | { |
31 | unsigned int res = w - ((w >> 1) & 0x5555); | 31 | unsigned int res = w - ((w >> 1) & 0x5555); |
32 | res = (res & 0x3333) + ((res >> 2) & 0x3333); | 32 | res = (res & 0x3333) + ((res >> 2) & 0x3333); |
33 | res = (res + (res >> 4)) & 0x0F0F; | 33 | res = (res + (res >> 4)) & 0x0F0F; |
34 | return (res + (res >> 8)) & 0x00FF; | 34 | return (res + (res >> 8)) & 0x00FF; |
35 | } | 35 | } |
36 | EXPORT_SYMBOL(__arch_hweight16); | 36 | EXPORT_SYMBOL(__sw_hweight16); |
37 | 37 | ||
38 | unsigned int __arch_hweight8(unsigned int w) | 38 | unsigned int __sw_hweight8(unsigned int w) |
39 | { | 39 | { |
40 | unsigned int res = w - ((w >> 1) & 0x55); | 40 | unsigned int res = w - ((w >> 1) & 0x55); |
41 | res = (res & 0x33) + ((res >> 2) & 0x33); | 41 | res = (res & 0x33) + ((res >> 2) & 0x33); |
42 | return (res + (res >> 4)) & 0x0F; | 42 | return (res + (res >> 4)) & 0x0F; |
43 | } | 43 | } |
44 | EXPORT_SYMBOL(__arch_hweight8); | 44 | EXPORT_SYMBOL(__sw_hweight8); |
45 | 45 | ||
46 | unsigned long __arch_hweight64(__u64 w) | 46 | unsigned long __sw_hweight64(__u64 w) |
47 | { | 47 | { |
48 | #if BITS_PER_LONG == 32 | 48 | #if BITS_PER_LONG == 32 |
49 | return __arch_hweight32((unsigned int)(w >> 32)) + | 49 | return __sw_hweight32((unsigned int)(w >> 32)) + |
50 | __arch_hweight32((unsigned int)w); | 50 | __sw_hweight32((unsigned int)w); |
51 | #elif BITS_PER_LONG == 64 | 51 | #elif BITS_PER_LONG == 64 |
52 | #ifdef ARCH_HAS_FAST_MULTIPLIER | 52 | #ifdef ARCH_HAS_FAST_MULTIPLIER |
53 | w -= (w >> 1) & 0x5555555555555555ul; | 53 | w -= (w >> 1) & 0x5555555555555555ul; |
@@ -64,4 +64,4 @@ unsigned long __arch_hweight64(__u64 w) | |||
64 | #endif | 64 | #endif |
65 | #endif | 65 | #endif |
66 | } | 66 | } |
67 | EXPORT_SYMBOL(__arch_hweight64); | 67 | EXPORT_SYMBOL(__sw_hweight64); |
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index f9bdf264473d..cbcd654215e6 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib | |||
@@ -245,3 +245,7 @@ quiet_cmd_lzo = LZO $@ | |||
245 | cmd_lzo = (cat $(filter-out FORCE,$^) | \ | 245 | cmd_lzo = (cat $(filter-out FORCE,$^) | \ |
246 | lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \ | 246 | lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \ |
247 | (rm -f $@ ; false) | 247 | (rm -f $@ ; false) |
248 | |||
249 | # misc stuff | ||
250 | # --------------------------------------------------------------------------- | ||
251 | quote:=" | ||