• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ANDROID_CHANGE_BEGIN */
2 #if 0
3 #include <linux/bitops.h>
4 #else
5 #include "include/linux/bitops.h"
6 #endif
7 /* ANDROID_CHANGE_END */
8 
9 /**
10  * hweightN - returns the hamming weight of a N-bit word
11  * @x: the word to weigh
12  *
13  * The Hamming Weight of a number is the total number of bits set in it.
14  */
15 
hweight32(unsigned int w)16 unsigned int hweight32(unsigned int w)
17 {
18 	unsigned int res = w - ((w >> 1) & 0x55555555);
19 	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
20 	res = (res + (res >> 4)) & 0x0F0F0F0F;
21 	res = res + (res >> 8);
22 	return (res + (res >> 16)) & 0x000000FF;
23 }
24 
hweight64(__u64 w)25 unsigned long hweight64(__u64 w)
26 {
27 #if BITS_PER_LONG == 32
28 	return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
29 #elif BITS_PER_LONG == 64
30 	__u64 res = w - ((w >> 1) & 0x5555555555555555ul);
31 	res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
32 	res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
33 	res = res + (res >> 8);
34 	res = res + (res >> 16);
35 	return (res + (res >> 32)) & 0x00000000000000FFul;
36 #endif
37 }
38