xref: /OK3568_Linux_fs/u-boot/include/linux/bitops.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #ifndef _LINUX_BITOPS_H
2*4882a593Smuzhiyun #define _LINUX_BITOPS_H
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <asm/types.h>
5*4882a593Smuzhiyun #include <asm-generic/bitsperlong.h>
6*4882a593Smuzhiyun #include <linux/compiler.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifdef	__KERNEL__
9*4882a593Smuzhiyun #define BIT(nr)			(1UL << (nr))
10*4882a593Smuzhiyun #define BIT_ULL(nr)		(1ULL << (nr))
11*4882a593Smuzhiyun #define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
12*4882a593Smuzhiyun #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
13*4882a593Smuzhiyun #define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
14*4882a593Smuzhiyun #define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
15*4882a593Smuzhiyun #define BITS_PER_BYTE		8
16*4882a593Smuzhiyun #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
17*4882a593Smuzhiyun #endif
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Create a contiguous bitmask starting at bit position @l and ending at
21*4882a593Smuzhiyun  * position @h. For example
22*4882a593Smuzhiyun  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun #define GENMASK(h, l) \
25*4882a593Smuzhiyun 	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define GENMASK_ULL(h, l) \
28*4882a593Smuzhiyun 	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * ffs: find first bit set. This is defined the same way as
32*4882a593Smuzhiyun  * the libc and compiler builtin ffs routines, therefore
33*4882a593Smuzhiyun  * differs in spirit from the above ffz (man ffs).
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun 
generic_ffs(int x)36*4882a593Smuzhiyun static inline int generic_ffs(int x)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	int r = 1;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	if (!x)
41*4882a593Smuzhiyun 		return 0;
42*4882a593Smuzhiyun 	if (!(x & 0xffff)) {
43*4882a593Smuzhiyun 		x >>= 16;
44*4882a593Smuzhiyun 		r += 16;
45*4882a593Smuzhiyun 	}
46*4882a593Smuzhiyun 	if (!(x & 0xff)) {
47*4882a593Smuzhiyun 		x >>= 8;
48*4882a593Smuzhiyun 		r += 8;
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun 	if (!(x & 0xf)) {
51*4882a593Smuzhiyun 		x >>= 4;
52*4882a593Smuzhiyun 		r += 4;
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun 	if (!(x & 3)) {
55*4882a593Smuzhiyun 		x >>= 2;
56*4882a593Smuzhiyun 		r += 2;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 	if (!(x & 1)) {
59*4882a593Smuzhiyun 		x >>= 1;
60*4882a593Smuzhiyun 		r += 1;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 	return r;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun  * fls - find last (most-significant) bit set
67*4882a593Smuzhiyun  * @x: the word to search
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * This is defined the same way as ffs.
70*4882a593Smuzhiyun  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
71*4882a593Smuzhiyun  */
generic_fls(int x)72*4882a593Smuzhiyun static inline int generic_fls(int x)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	int r = 32;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if (!x)
77*4882a593Smuzhiyun 		return 0;
78*4882a593Smuzhiyun 	if (!(x & 0xffff0000u)) {
79*4882a593Smuzhiyun 		x <<= 16;
80*4882a593Smuzhiyun 		r -= 16;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun 	if (!(x & 0xff000000u)) {
83*4882a593Smuzhiyun 		x <<= 8;
84*4882a593Smuzhiyun 		r -= 8;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 	if (!(x & 0xf0000000u)) {
87*4882a593Smuzhiyun 		x <<= 4;
88*4882a593Smuzhiyun 		r -= 4;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 	if (!(x & 0xc0000000u)) {
91*4882a593Smuzhiyun 		x <<= 2;
92*4882a593Smuzhiyun 		r -= 2;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 	if (!(x & 0x80000000u)) {
95*4882a593Smuzhiyun 		x <<= 1;
96*4882a593Smuzhiyun 		r -= 1;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 	return r;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun  * hweightN: returns the hamming weight (i.e. the number
104*4882a593Smuzhiyun  * of bits set) of a N-bit word
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun 
generic_hweight32(unsigned int w)107*4882a593Smuzhiyun static inline unsigned int generic_hweight32(unsigned int w)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
110*4882a593Smuzhiyun 	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
111*4882a593Smuzhiyun 	res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
112*4882a593Smuzhiyun 	res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
113*4882a593Smuzhiyun 	return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
generic_hweight16(unsigned int w)116*4882a593Smuzhiyun static inline unsigned int generic_hweight16(unsigned int w)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
119*4882a593Smuzhiyun 	res = (res & 0x3333) + ((res >> 2) & 0x3333);
120*4882a593Smuzhiyun 	res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
121*4882a593Smuzhiyun 	return (res & 0x00FF) + ((res >> 8) & 0x00FF);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
generic_hweight8(unsigned int w)124*4882a593Smuzhiyun static inline unsigned int generic_hweight8(unsigned int w)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
127*4882a593Smuzhiyun 	res = (res & 0x33) + ((res >> 2) & 0x33);
128*4882a593Smuzhiyun 	return (res & 0x0F) + ((res >> 4) & 0x0F);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun #include <asm/bitops.h>
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /* linux/include/asm-generic/bitops/non-atomic.h */
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #ifndef PLATFORM__SET_BIT
136*4882a593Smuzhiyun # define __set_bit generic_set_bit
137*4882a593Smuzhiyun #endif
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun #ifndef PLATFORM__CLEAR_BIT
140*4882a593Smuzhiyun # define __clear_bit generic_clear_bit
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #ifndef PLATFORM_FFS
144*4882a593Smuzhiyun # define ffs generic_ffs
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun #ifndef PLATFORM_FLS
148*4882a593Smuzhiyun # define fls generic_fls
149*4882a593Smuzhiyun #endif
150*4882a593Smuzhiyun 
fls_long(unsigned long l)151*4882a593Smuzhiyun static inline unsigned fls_long(unsigned long l)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	if (sizeof(l) == 4)
154*4882a593Smuzhiyun 		return fls(l);
155*4882a593Smuzhiyun 	return fls64(l);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun  * __ffs64 - find first set bit in a 64 bit word
160*4882a593Smuzhiyun  * @word: The 64 bit word
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * On 64 bit arches this is a synomyn for __ffs
163*4882a593Smuzhiyun  * The result is not defined if no bits are set, so check that @word
164*4882a593Smuzhiyun  * is non-zero before calling this.
165*4882a593Smuzhiyun  */
__ffs64(u64 word)166*4882a593Smuzhiyun static inline unsigned long __ffs64(u64 word)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun #if BITS_PER_LONG == 32
169*4882a593Smuzhiyun 	if (((u32)word) == 0UL)
170*4882a593Smuzhiyun 		return __ffs((u32)(word >> 32)) + 32;
171*4882a593Smuzhiyun #elif BITS_PER_LONG != 64
172*4882a593Smuzhiyun #error BITS_PER_LONG not 32 or 64
173*4882a593Smuzhiyun #endif
174*4882a593Smuzhiyun 	return __ffs((unsigned long)word);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun  * __set_bit - Set a bit in memory
179*4882a593Smuzhiyun  * @nr: the bit to set
180*4882a593Smuzhiyun  * @addr: the address to start counting from
181*4882a593Smuzhiyun  *
182*4882a593Smuzhiyun  * Unlike set_bit(), this function is non-atomic and may be reordered.
183*4882a593Smuzhiyun  * If it's called on the same region of memory simultaneously, the effect
184*4882a593Smuzhiyun  * may be that only one operation succeeds.
185*4882a593Smuzhiyun  */
generic_set_bit(int nr,volatile unsigned long * addr)186*4882a593Smuzhiyun static inline void generic_set_bit(int nr, volatile unsigned long *addr)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
189*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	*p  |= mask;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
generic_clear_bit(int nr,volatile unsigned long * addr)194*4882a593Smuzhiyun static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	unsigned long mask = BIT_MASK(nr);
197*4882a593Smuzhiyun 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	*p &= ~mask;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun #endif
203