Fixes for U-Boot charging
[u-boot-openmoko/mini2440.git] / include / linux / bitops.h
blob7d41ae62ccf7ead43fe4a468d6b58624410b7836
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
5 /*
6 * ffs: find first bit set. This is defined the same way as
7 * the libc and compiler builtin ffs routines, therefore
8 * differs in spirit from the above ffz (man ffs).
9 */
11 static inline int generic_ffs(int x)
13 int r = 1;
15 if (!x)
16 return 0;
17 if (!(x & 0xffff)) {
18 x >>= 16;
19 r += 16;
21 if (!(x & 0xff)) {
22 x >>= 8;
23 r += 8;
25 if (!(x & 0xf)) {
26 x >>= 4;
27 r += 4;
29 if (!(x & 3)) {
30 x >>= 2;
31 r += 2;
33 if (!(x & 1)) {
34 x >>= 1;
35 r += 1;
37 return r;
41 * hweightN: returns the hamming weight (i.e. the number
42 * of bits set) of a N-bit word
45 static inline unsigned int generic_hweight32(unsigned int w)
47 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
48 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
49 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
50 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
51 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
54 static inline unsigned int generic_hweight16(unsigned int w)
56 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
57 res = (res & 0x3333) + ((res >> 2) & 0x3333);
58 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
59 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
62 static inline unsigned int generic_hweight8(unsigned int w)
64 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
65 res = (res & 0x33) + ((res >> 2) & 0x33);
66 return (res & 0x0F) + ((res >> 4) & 0x0F);
69 #include <asm/bitops.h>
72 #endif