diff options
| -rw-r--r-- | lib/hweight.c | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/lib/hweight.c b/lib/hweight.c index 721a4b8b4fb8..438257671708 100644 --- a/lib/hweight.c +++ b/lib/hweight.c | |||
| @@ -10,28 +10,28 @@ | |||
| 10 | 10 | ||
| 11 | unsigned int hweight32(unsigned int w) | 11 | unsigned int hweight32(unsigned int w) |
| 12 | { | 12 | { |
| 13 | unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); | 13 | unsigned int res = w - ((w >> 1) & 0x55555555); |
| 14 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); | 14 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 15 | res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); | 15 | res = (res + (res >> 4)) & 0x0F0F0F0F; |
| 16 | res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); | 16 | res = res + (res >> 8); |
| 17 | return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); | 17 | return (res + (res >> 16)) & 0x000000FF; |
| 18 | } | 18 | } |
| 19 | EXPORT_SYMBOL(hweight32); | 19 | EXPORT_SYMBOL(hweight32); |
| 20 | 20 | ||
| 21 | unsigned int hweight16(unsigned int w) | 21 | unsigned int hweight16(unsigned int w) |
| 22 | { | 22 | { |
| 23 | unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); | 23 | unsigned int res = w - ((w >> 1) & 0x5555); |
| 24 | res = (res & 0x3333) + ((res >> 2) & 0x3333); | 24 | res = (res & 0x3333) + ((res >> 2) & 0x3333); |
| 25 | res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); | 25 | res = (res + (res >> 4)) & 0x0F0F; |
| 26 | return (res & 0x00FF) + ((res >> 8) & 0x00FF); | 26 | return (res + (res >> 8)) & 0x00FF; |
| 27 | } | 27 | } |
| 28 | EXPORT_SYMBOL(hweight16); | 28 | EXPORT_SYMBOL(hweight16); |
| 29 | 29 | ||
| 30 | unsigned int hweight8(unsigned int w) | 30 | unsigned int hweight8(unsigned int w) |
| 31 | { | 31 | { |
| 32 | unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); | 32 | unsigned int res = w - ((w >> 1) & 0x55); |
| 33 | res = (res & 0x33) + ((res >> 2) & 0x33); | 33 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 34 | return (res & 0x0F) + ((res >> 4) & 0x0F); | 34 | return (res + (res >> 4)) & 0x0F; |
| 35 | } | 35 | } |
| 36 | EXPORT_SYMBOL(hweight8); | 36 | EXPORT_SYMBOL(hweight8); |
| 37 | 37 | ||
| @@ -40,13 +40,12 @@ unsigned long hweight64(__u64 w) | |||
| 40 | #if BITS_PER_LONG == 32 | 40 | #if BITS_PER_LONG == 32 |
| 41 | return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w); | 41 | return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w); |
| 42 | #elif BITS_PER_LONG == 64 | 42 | #elif BITS_PER_LONG == 64 |
| 43 | u64 res; | 43 | __u64 res = w - ((w >> 1) & 0x5555555555555555ul); |
| 44 | res = (w & 0x5555555555555555ul) + ((w >> 1) & 0x5555555555555555ul); | ||
| 45 | res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul); | 44 | res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul); |
| 46 | res = (res & 0x0F0F0F0F0F0F0F0Ful) + ((res >> 4) & 0x0F0F0F0F0F0F0F0Ful); | 45 | res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful; |
| 47 | res = (res & 0x00FF00FF00FF00FFul) + ((res >> 8) & 0x00FF00FF00FF00FFul); | 46 | res = res + (res >> 8); |
| 48 | res = (res & 0x0000FFFF0000FFFFul) + ((res >> 16) & 0x0000FFFF0000FFFFul); | 47 | res = res + (res >> 16); |
| 49 | return (res & 0x00000000FFFFFFFFul) + ((res >> 32) & 0x00000000FFFFFFFFul); | 48 | return (res + (res >> 32)) & 0x00000000000000FFul; |
| 50 | #else | 49 | #else |
| 51 | #error BITS_PER_LONG not defined | 50 | #error BITS_PER_LONG not defined |
| 52 | #endif | 51 | #endif |
