xref: /rk3399_rockchip-uboot/include/linux/bitops.h (revision b15cbc0be004d352d4dd661546847bd52fbf8a28)
1*b15cbc0bSwdenk #ifndef _LINUX_BITOPS_H
2*b15cbc0bSwdenk #define _LINUX_BITOPS_H
3*b15cbc0bSwdenk 
4*b15cbc0bSwdenk 
5*b15cbc0bSwdenk /*
6*b15cbc0bSwdenk  * ffs: find first bit set. This is defined the same way as
7*b15cbc0bSwdenk  * the libc and compiler builtin ffs routines, therefore
8*b15cbc0bSwdenk  * differs in spirit from the above ffz (man ffs).
9*b15cbc0bSwdenk  */
10*b15cbc0bSwdenk 
11*b15cbc0bSwdenk static inline int generic_ffs(int x)
12*b15cbc0bSwdenk {
13*b15cbc0bSwdenk 	int r = 1;
14*b15cbc0bSwdenk 
15*b15cbc0bSwdenk 	if (!x)
16*b15cbc0bSwdenk 		return 0;
17*b15cbc0bSwdenk 	if (!(x & 0xffff)) {
18*b15cbc0bSwdenk 		x >>= 16;
19*b15cbc0bSwdenk 		r += 16;
20*b15cbc0bSwdenk 	}
21*b15cbc0bSwdenk 	if (!(x & 0xff)) {
22*b15cbc0bSwdenk 		x >>= 8;
23*b15cbc0bSwdenk 		r += 8;
24*b15cbc0bSwdenk 	}
25*b15cbc0bSwdenk 	if (!(x & 0xf)) {
26*b15cbc0bSwdenk 		x >>= 4;
27*b15cbc0bSwdenk 		r += 4;
28*b15cbc0bSwdenk 	}
29*b15cbc0bSwdenk 	if (!(x & 3)) {
30*b15cbc0bSwdenk 		x >>= 2;
31*b15cbc0bSwdenk 		r += 2;
32*b15cbc0bSwdenk 	}
33*b15cbc0bSwdenk 	if (!(x & 1)) {
34*b15cbc0bSwdenk 		x >>= 1;
35*b15cbc0bSwdenk 		r += 1;
36*b15cbc0bSwdenk 	}
37*b15cbc0bSwdenk 	return r;
38*b15cbc0bSwdenk }
39*b15cbc0bSwdenk 
40*b15cbc0bSwdenk /*
41*b15cbc0bSwdenk  * hweightN: returns the hamming weight (i.e. the number
42*b15cbc0bSwdenk  * of bits set) of a N-bit word
43*b15cbc0bSwdenk  */
44*b15cbc0bSwdenk 
45*b15cbc0bSwdenk static inline unsigned int generic_hweight32(unsigned int w)
46*b15cbc0bSwdenk {
47*b15cbc0bSwdenk         unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
48*b15cbc0bSwdenk         res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
49*b15cbc0bSwdenk         res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
50*b15cbc0bSwdenk         res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
51*b15cbc0bSwdenk         return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
52*b15cbc0bSwdenk }
53*b15cbc0bSwdenk 
54*b15cbc0bSwdenk static inline unsigned int generic_hweight16(unsigned int w)
55*b15cbc0bSwdenk {
56*b15cbc0bSwdenk         unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
57*b15cbc0bSwdenk         res = (res & 0x3333) + ((res >> 2) & 0x3333);
58*b15cbc0bSwdenk         res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
59*b15cbc0bSwdenk         return (res & 0x00FF) + ((res >> 8) & 0x00FF);
60*b15cbc0bSwdenk }
61*b15cbc0bSwdenk 
62*b15cbc0bSwdenk static inline unsigned int generic_hweight8(unsigned int w)
63*b15cbc0bSwdenk {
64*b15cbc0bSwdenk         unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
65*b15cbc0bSwdenk         res = (res & 0x33) + ((res >> 2) & 0x33);
66*b15cbc0bSwdenk         return (res & 0x0F) + ((res >> 4) & 0x0F);
67*b15cbc0bSwdenk }
68*b15cbc0bSwdenk 
69*b15cbc0bSwdenk #include <asm/bitops.h>
70*b15cbc0bSwdenk 
71*b15cbc0bSwdenk 
72*b15cbc0bSwdenk #endif
73