aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/asm-generic/bitops/hweight.h9
-rw-r--r--lib/Makefile1
-rw-r--r--lib/hweight.c54
3 files changed, 64 insertions, 0 deletions
diff --git a/include/asm-generic/bitops/hweight.h b/include/asm-generic/bitops/hweight.h
new file mode 100644
index 000000000000..8be6f17d6a5c
--- /dev/null
+++ b/include/asm-generic/bitops/hweight.h
@@ -0,0 +1,9 @@
1#ifndef _ASM_GENERIC_BITOPS_HWEIGHT_H_
2#define _ASM_GENERIC_BITOPS_HWEIGHT_H_
3
4extern unsigned int hweight32(unsigned int w);
5extern unsigned int hweight16(unsigned int w);
6extern unsigned int hweight8(unsigned int w);
7extern unsigned long hweight64(__u64 w);
8
9#endif /* _ASM_GENERIC_BITOPS_HWEIGHT_H_ */
diff --git a/lib/Makefile b/lib/Makefile
index f827e3c24ec0..b830c9a15541 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -23,6 +23,7 @@ lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
23lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o 23lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
24lib-$(CONFIG_SEMAPHORE_SLEEPERS) += semaphore-sleepers.o 24lib-$(CONFIG_SEMAPHORE_SLEEPERS) += semaphore-sleepers.o
25lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o 25lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
26lib-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
26obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o 27obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
27obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o 28obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
28 29
diff --git a/lib/hweight.c b/lib/hweight.c
new file mode 100644
index 000000000000..721a4b8b4fb8
--- /dev/null
+++ b/lib/hweight.c
@@ -0,0 +1,54 @@
1#include <linux/module.h>
2#include <asm/types.h>
3
4/**
5 * hweightN - returns the hamming weight of a N-bit word
6 * @x: the word to weigh
7 *
8 * The Hamming Weight of a number is the total number of bits set in it.
9 */
10
11unsigned int hweight32(unsigned int w)
12{
13 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
14 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
15 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
16 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
17 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
18}
19EXPORT_SYMBOL(hweight32);
20
21unsigned int hweight16(unsigned int w)
22{
23 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
24 res = (res & 0x3333) + ((res >> 2) & 0x3333);
25 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
26 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
27}
28EXPORT_SYMBOL(hweight16);
29
30unsigned int hweight8(unsigned int w)
31{
32 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
33 res = (res & 0x33) + ((res >> 2) & 0x33);
34 return (res & 0x0F) + ((res >> 4) & 0x0F);
35}
36EXPORT_SYMBOL(hweight8);
37
38unsigned long hweight64(__u64 w)
39{
40#if BITS_PER_LONG == 32
41 return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
42#elif BITS_PER_LONG == 64
43 u64 res;
44 res = (w & 0x5555555555555555ul) + ((w >> 1) & 0x5555555555555555ul);
45 res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
46 res = (res & 0x0F0F0F0F0F0F0F0Ful) + ((res >> 4) & 0x0F0F0F0F0F0F0F0Ful);
47 res = (res & 0x00FF00FF00FF00FFul) + ((res >> 8) & 0x00FF00FF00FF00FFul);
48 res = (res & 0x0000FFFF0000FFFFul) + ((res >> 16) & 0x0000FFFF0000FFFFul);
49 return (res & 0x00000000FFFFFFFFul) + ((res >> 32) & 0x00000000FFFFFFFFul);
50#else
51#error BITS_PER_LONG not defined
52#endif
53}
54EXPORT_SYMBOL(hweight64);