xref: /openbmc/u-boot/include/linux/bitops.h (revision affae2bf)
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 
4 
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  */
10 
11 static inline int generic_ffs(int x)
12 {
13 	int r = 1;
14 
15 	if (!x)
16 		return 0;
17 	if (!(x & 0xffff)) {
18 		x >>= 16;
19 		r += 16;
20 	}
21 	if (!(x & 0xff)) {
22 		x >>= 8;
23 		r += 8;
24 	}
25 	if (!(x & 0xf)) {
26 		x >>= 4;
27 		r += 4;
28 	}
29 	if (!(x & 3)) {
30 		x >>= 2;
31 		r += 2;
32 	}
33 	if (!(x & 1)) {
34 		x >>= 1;
35 		r += 1;
36 	}
37 	return r;
38 }
39 
40 /*
41  * hweightN: returns the hamming weight (i.e. the number
42  * of bits set) of a N-bit word
43  */
44 
45 static inline unsigned int generic_hweight32(unsigned int w)
46 {
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);
52 }
53 
54 static inline unsigned int generic_hweight16(unsigned int w)
55 {
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);
60 }
61 
62 static inline unsigned int generic_hweight8(unsigned int w)
63 {
64         unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
65         res = (res & 0x33) + ((res >> 2) & 0x33);
66         return (res & 0x0F) + ((res >> 4) & 0x0F);
67 }
68 
69 #include <asm/bitops.h>
70 
71 
72 #endif
73