diff options
| author | Peter Zijlstra <a.p.zijlstra@chello.nl> | 2010-02-01 09:03:07 -0500 |
|---|---|---|
| committer | H. Peter Anvin <hpa@zytor.com> | 2010-04-06 18:52:11 -0400 |
| commit | 1527bc8b928dd1399c3d3467dd47d9ede210978a (patch) | |
| tree | fb391da915bdae9f933b8170ff61aa43c85ef9ae | |
| parent | 0fdf86754f70e813845af4abaa805165ce57a0bb (diff) | |
bitops: Optimize hweight() by making use of compile-time evaluation
Rename the extisting runtime hweight() implementations to
__arch_hweight(), rename the compile-time versions to __const_hweight()
and then have hweight() pick between them.
Suggested-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <20100318111929.GB11152@aftab>
Acked-by: H. Peter Anvin <hpa@zytor.com>
LKML-Reference: <1265028224.24455.154.camel@laptop>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
| -rw-r--r-- | arch/alpha/include/asm/bitops.h | 18 | ||||
| -rw-r--r-- | arch/ia64/include/asm/bitops.h | 11 | ||||
| -rw-r--r-- | arch/sparc/include/asm/bitops_64.h | 11 | ||||
| -rw-r--r-- | include/asm-generic/bitops/arch_hweight.h | 11 | ||||
| -rw-r--r-- | include/asm-generic/bitops/const_hweight.h | 42 | ||||
| -rw-r--r-- | include/asm-generic/bitops/hweight.h | 8 | ||||
| -rw-r--r-- | include/linux/bitops.h | 25 | ||||
| -rw-r--r-- | lib/hweight.c | 19 |
8 files changed, 87 insertions, 58 deletions
diff --git a/arch/alpha/include/asm/bitops.h b/arch/alpha/include/asm/bitops.h index 15f3ae25c511..296da1d5ed57 100644 --- a/arch/alpha/include/asm/bitops.h +++ b/arch/alpha/include/asm/bitops.h | |||
| @@ -405,29 +405,31 @@ static inline int fls(int x) | |||
| 405 | 405 | ||
| 406 | #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67) | 406 | #if defined(CONFIG_ALPHA_EV6) && defined(CONFIG_ALPHA_EV67) |
| 407 | /* Whee. EV67 can calculate it directly. */ | 407 | /* Whee. EV67 can calculate it directly. */ |
| 408 | static inline unsigned long hweight64(unsigned long w) | 408 | static inline unsigned long __arch_hweight64(unsigned long w) |
| 409 | { | 409 | { |
| 410 | return __kernel_ctpop(w); | 410 | return __kernel_ctpop(w); |
| 411 | } | 411 | } |
| 412 | 412 | ||
| 413 | static inline unsigned int hweight32(unsigned int w) | 413 | static inline unsigned int __arch_weight32(unsigned int w) |
| 414 | { | 414 | { |
| 415 | return hweight64(w); | 415 | return __arch_hweight64(w); |
| 416 | } | 416 | } |
| 417 | 417 | ||
| 418 | static inline unsigned int hweight16(unsigned int w) | 418 | static inline unsigned int __arch_hweight16(unsigned int w) |
| 419 | { | 419 | { |
| 420 | return hweight64(w & 0xffff); | 420 | return __arch_hweight64(w & 0xffff); |
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | static inline unsigned int hweight8(unsigned int w) | 423 | static inline unsigned int __arch_hweight8(unsigned int w) |
| 424 | { | 424 | { |
| 425 | return hweight64(w & 0xff); | 425 | return __arch_hweight64(w & 0xff); |
| 426 | } | 426 | } |
| 427 | #else | 427 | #else |
| 428 | #include <asm-generic/bitops/hweight.h> | 428 | #include <asm-generic/bitops/arch_hweight.h> |
| 429 | #endif | 429 | #endif |
| 430 | 430 | ||
| 431 | #include <asm-generic/bitops/const_hweight.h> | ||
| 432 | |||
| 431 | #endif /* __KERNEL__ */ | 433 | #endif /* __KERNEL__ */ |
| 432 | 434 | ||
| 433 | #include <asm-generic/bitops/find.h> | 435 | #include <asm-generic/bitops/find.h> |
diff --git a/arch/ia64/include/asm/bitops.h b/arch/ia64/include/asm/bitops.h index 6ebc229a1c51..9da3df6f1a52 100644 --- a/arch/ia64/include/asm/bitops.h +++ b/arch/ia64/include/asm/bitops.h | |||
| @@ -437,17 +437,18 @@ __fls (unsigned long x) | |||
| 437 | * hweightN: returns the hamming weight (i.e. the number | 437 | * hweightN: returns the hamming weight (i.e. the number |
| 438 | * of bits set) of a N-bit word | 438 | * of bits set) of a N-bit word |
| 439 | */ | 439 | */ |
| 440 | static __inline__ unsigned long | 440 | static __inline__ unsigned long __arch_hweight64(unsigned long x) |
| 441 | hweight64 (unsigned long x) | ||
| 442 | { | 441 | { |
| 443 | unsigned long result; | 442 | unsigned long result; |
| 444 | result = ia64_popcnt(x); | 443 | result = ia64_popcnt(x); |
| 445 | return result; | 444 | return result; |
| 446 | } | 445 | } |
| 447 | 446 | ||
| 448 | #define hweight32(x) (unsigned int) hweight64((x) & 0xfffffffful) | 447 | #define __arch_hweight32(x) ((unsigned int) __arch_hweight64((x) & 0xfffffffful)) |
| 449 | #define hweight16(x) (unsigned int) hweight64((x) & 0xfffful) | 448 | #define __arch_hweight16(x) ((unsigned int) __arch_hweight64((x) & 0xfffful)) |
| 450 | #define hweight8(x) (unsigned int) hweight64((x) & 0xfful) | 449 | #define __arch_hweight8(x) ((unsigned int) __arch_hweight64((x) & 0xfful)) |
| 450 | |||
| 451 | #include <asm-generic/bitops/const_hweight.h> | ||
| 451 | 452 | ||
| 452 | #endif /* __KERNEL__ */ | 453 | #endif /* __KERNEL__ */ |
| 453 | 454 | ||
diff --git a/arch/sparc/include/asm/bitops_64.h b/arch/sparc/include/asm/bitops_64.h index e72ac9cdfb98..766121a67a24 100644 --- a/arch/sparc/include/asm/bitops_64.h +++ b/arch/sparc/include/asm/bitops_64.h | |||
| @@ -44,7 +44,7 @@ extern void change_bit(unsigned long nr, volatile unsigned long *addr); | |||
| 44 | 44 | ||
| 45 | #ifdef ULTRA_HAS_POPULATION_COUNT | 45 | #ifdef ULTRA_HAS_POPULATION_COUNT |
| 46 | 46 | ||
| 47 | static inline unsigned int hweight64(unsigned long w) | 47 | static inline unsigned int __arch_hweight64(unsigned long w) |
| 48 | { | 48 | { |
| 49 | unsigned int res; | 49 | unsigned int res; |
| 50 | 50 | ||
| @@ -52,7 +52,7 @@ static inline unsigned int hweight64(unsigned long w) | |||
| 52 | return res; | 52 | return res; |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | static inline unsigned int hweight32(unsigned int w) | 55 | static inline unsigned int __arch_hweight32(unsigned int w) |
| 56 | { | 56 | { |
| 57 | unsigned int res; | 57 | unsigned int res; |
| 58 | 58 | ||
| @@ -60,7 +60,7 @@ static inline unsigned int hweight32(unsigned int w) | |||
| 60 | return res; | 60 | return res; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | static inline unsigned int hweight16(unsigned int w) | 63 | static inline unsigned int __arch_hweight16(unsigned int w) |
| 64 | { | 64 | { |
| 65 | unsigned int res; | 65 | unsigned int res; |
| 66 | 66 | ||
| @@ -68,7 +68,7 @@ static inline unsigned int hweight16(unsigned int w) | |||
| 68 | return res; | 68 | return res; |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | static inline unsigned int hweight8(unsigned int w) | 71 | static inline unsigned int __arch_hweight8(unsigned int w) |
| 72 | { | 72 | { |
| 73 | unsigned int res; | 73 | unsigned int res; |
| 74 | 74 | ||
| @@ -78,9 +78,10 @@ static inline unsigned int hweight8(unsigned int w) | |||
| 78 | 78 | ||
| 79 | #else | 79 | #else |
| 80 | 80 | ||
| 81 | #include <asm-generic/bitops/hweight.h> | 81 | #include <asm-generic/bitops/arch_hweight.h> |
| 82 | 82 | ||
| 83 | #endif | 83 | #endif |
| 84 | #include <asm-generic/bitops/const_hweight.h> | ||
| 84 | #include <asm-generic/bitops/lock.h> | 85 | #include <asm-generic/bitops/lock.h> |
| 85 | #endif /* __KERNEL__ */ | 86 | #endif /* __KERNEL__ */ |
| 86 | 87 | ||
diff --git a/include/asm-generic/bitops/arch_hweight.h b/include/asm-generic/bitops/arch_hweight.h new file mode 100644 index 000000000000..3a7be842cdce --- /dev/null +++ b/include/asm-generic/bitops/arch_hweight.h | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | #ifndef _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_ | ||
| 2 | #define _ASM_GENERIC_BITOPS_ARCH_HWEIGHT_H_ | ||
| 3 | |||
| 4 | #include <asm/types.h> | ||
| 5 | |||
| 6 | extern unsigned int __arch_hweight32(unsigned int w); | ||
| 7 | extern unsigned int __arch_hweight16(unsigned int w); | ||
| 8 | extern unsigned int __arch_hweight8(unsigned int w); | ||
| 9 | extern unsigned long __arch_hweight64(__u64 w); | ||
| 10 | |||
| 11 | #endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */ | ||
diff --git a/include/asm-generic/bitops/const_hweight.h b/include/asm-generic/bitops/const_hweight.h new file mode 100644 index 000000000000..fa2a50b7ee66 --- /dev/null +++ b/include/asm-generic/bitops/const_hweight.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #ifndef _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ | ||
| 2 | #define _ASM_GENERIC_BITOPS_CONST_HWEIGHT_H_ | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Compile time versions of __arch_hweightN() | ||
| 6 | */ | ||
| 7 | #define __const_hweight8(w) \ | ||
| 8 | ( (!!((w) & (1ULL << 0))) + \ | ||
| 9 | (!!((w) & (1ULL << 1))) + \ | ||
| 10 | (!!((w) & (1ULL << 2))) + \ | ||
| 11 | (!!((w) & (1ULL << 3))) + \ | ||
| 12 | (!!((w) & (1ULL << 4))) + \ | ||
| 13 | (!!((w) & (1ULL << 5))) + \ | ||
| 14 | (!!((w) & (1ULL << 6))) + \ | ||
| 15 | (!!((w) & (1ULL << 7))) ) | ||
| 16 | |||
| 17 | #define __const_hweight16(w) (__const_hweight8(w) + __const_hweight8((w) >> 8 )) | ||
| 18 | #define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16)) | ||
| 19 | #define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32)) | ||
| 20 | |||
| 21 | /* | ||
| 22 | * Generic interface. | ||
| 23 | */ | ||
| 24 | #define hweight8(w) (__builtin_constant_p(w) ? __const_hweight8(w) : __arch_hweight8(w)) | ||
| 25 | #define hweight16(w) (__builtin_constant_p(w) ? __const_hweight16(w) : __arch_hweight16(w)) | ||
| 26 | #define hweight32(w) (__builtin_constant_p(w) ? __const_hweight32(w) : __arch_hweight32(w)) | ||
| 27 | #define hweight64(w) (__builtin_constant_p(w) ? __const_hweight64(w) : __arch_hweight64(w)) | ||
| 28 | |||
| 29 | /* | ||
| 30 | * Interface for known constant arguments | ||
| 31 | */ | ||
| 32 | #define HWEIGHT8(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight8(w)) | ||
| 33 | #define HWEIGHT16(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight16(w)) | ||
| 34 | #define HWEIGHT32(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight32(w)) | ||
| 35 | #define HWEIGHT64(w) (BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + __const_hweight64(w)) | ||
| 36 | |||
| 37 | /* | ||
| 38 | * Type invariant interface to the compile time constant hweight functions. | ||
| 39 | */ | ||
