1*c2c66affSColin Finck #ifndef _LINUX_BITOPS_H
2*c2c66affSColin Finck #define _LINUX_BITOPS_H
3*c2c66affSColin Finck
4*c2c66affSColin Finck #include <ntifs.h>
5*c2c66affSColin Finck #include <linux/types.h>
6*c2c66affSColin Finck
7*c2c66affSColin Finck #ifdef __KERNEL__
8*c2c66affSColin Finck #define BIT(nr) (1 << (nr))
9*c2c66affSColin Finck #define BIT_MASK(nr) (1 << ((nr) % BITS_PER_LONG))
10*c2c66affSColin Finck #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
11*c2c66affSColin Finck #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
12*c2c66affSColin Finck #define BITS_PER_BYTE 8
13*c2c66affSColin Finck #endif
14*c2c66affSColin Finck
15*c2c66affSColin Finck /*
16*c2c66affSColin Finck * Include this here because some architectures need generic_ffs/fls in
17*c2c66affSColin Finck * scope
18*c2c66affSColin Finck */
19*c2c66affSColin Finck
20*c2c66affSColin Finck /**
21*c2c66affSColin Finck * find_first_zero_bit - find the first zero bit in a memory region
22*c2c66affSColin Finck * @addr: The address to start the search at
23*c2c66affSColin Finck * @size: The maximum size to search
24*c2c66affSColin Finck *
25*c2c66affSColin Finck * Returns the bit number of the first zero bit, not the number of the byte
26*c2c66affSColin Finck * containing a bit.
27*c2c66affSColin Finck */
28*c2c66affSColin Finck #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
29*c2c66affSColin Finck
30*c2c66affSColin Finck /**
31*c2c66affSColin Finck * find_next_zero_bit - find the first zero bit in a memory region
32*c2c66affSColin Finck * @addr: The address to base the search on
33*c2c66affSColin Finck * @offset: The bit number to start searching at
34*c2c66affSColin Finck * @size: The maximum size to search
35*c2c66affSColin Finck */
36*c2c66affSColin Finck int find_next_zero_bit(const unsigned long *addr, int size, int offset);
37*c2c66affSColin Finck
38*c2c66affSColin Finck /**
39*c2c66affSColin Finck * __ffs - find first bit in word.
40*c2c66affSColin Finck * @word: The word to search
41*c2c66affSColin Finck *
42*c2c66affSColin Finck * Undefined if no bit exists, so code should check against 0 first.
43*c2c66affSColin Finck */
__ffs(unsigned long word)44*c2c66affSColin Finck static inline unsigned long __ffs(unsigned long word)
45*c2c66affSColin Finck {
46*c2c66affSColin Finck int num = 0;
47*c2c66affSColin Finck
48*c2c66affSColin Finck #if BITS_PER_LONG == 64
49*c2c66affSColin Finck if ((word & 0xffffffff) == 0) {
50*c2c66affSColin Finck num += 32;
51*c2c66affSColin Finck word >>= 32;
52*c2c66affSColin Finck }
53*c2c66affSColin Finck #endif
54*c2c66affSColin Finck if ((word & 0xffff) == 0) {
55*c2c66affSColin Finck num += 16;
56*c2c66affSColin Finck word >>= 16;
57*c2c66affSColin Finck }
58*c2c66affSColin Finck if ((word & 0xff) == 0) {
59*c2c66affSColin Finck num += 8;
60*c2c66affSColin Finck word >>= 8;
61*c2c66affSColin Finck }
62*c2c66affSColin Finck if ((word & 0xf) == 0) {
63*c2c66affSColin Finck num += 4;
64*c2c66affSColin Finck word >>= 4;
65*c2c66affSColin Finck }
66*c2c66affSColin Finck if ((word & 0x3) == 0) {
67*c2c66affSColin Finck num += 2;
68*c2c66affSColin Finck word >>= 2;
69*c2c66affSColin Finck }
70*c2c66affSColin Finck if ((word & 0x1) == 0)
71*c2c66affSColin Finck num += 1;
72*c2c66affSColin Finck return num;
73*c2c66affSColin Finck }
74*c2c66affSColin Finck
75*c2c66affSColin Finck /**
76*c2c66affSColin Finck * find_first_bit - find the first set bit in a memory region
77*c2c66affSColin Finck * @addr: The address to start the search at
78*c2c66affSColin Finck * @size: The maximum size to search
79*c2c66affSColin Finck *
80*c2c66affSColin Finck * Returns the bit number of the first set bit, not the number of the byte
81*c2c66affSColin Finck * containing a bit.
82*c2c66affSColin Finck */
find_first_bit(const unsigned long * addr,unsigned size)83*c2c66affSColin Finck static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
84*c2c66affSColin Finck {
85*c2c66affSColin Finck unsigned x = 0;
86*c2c66affSColin Finck
87*c2c66affSColin Finck while (x < size) {
88*c2c66affSColin Finck unsigned long val = *addr++;
89*c2c66affSColin Finck if (val)
90*c2c66affSColin Finck return __ffs(val) + x;
91*c2c66affSColin Finck x += (sizeof(*addr)<<3);
92*c2c66affSColin Finck }
93*c2c66affSColin Finck return x;
94*c2c66affSColin Finck }
95*c2c66affSColin Finck
96*c2c66affSColin Finck /**
97*c2c66affSColin Finck * find_next_bit - find the next set bit in a memory region
98*c2c66affSColin Finck * @addr: The address to base the search on
99*c2c66affSColin Finck * @offset: The bitnumber to start searching at
100*c2c66affSColin Finck * @size: The maximum size to search
101*c2c66affSColin Finck */
102*c2c66affSColin Finck
103*c2c66affSColin Finck /*
104*c2c66affSColin Finck * ffz - find first zero in word.
105*c2c66affSColin Finck * @word: The word to search
106*c2c66affSColin Finck *
107*c2c66affSColin Finck * Undefined if no zero exists, so code should check against ~0UL first.
108*c2c66affSColin Finck */
109*c2c66affSColin Finck #define ffz(x) __ffs(~(x))
110*c2c66affSColin Finck
111*c2c66affSColin Finck
112*c2c66affSColin Finck /**
113*c2c66affSColin Finck * ffs - find first bit set
114*c2c66affSColin Finck * @x: the word to search
115*c2c66affSColin Finck *
116*c2c66affSColin Finck * This is defined the same way as
117*c2c66affSColin Finck * the libc and compiler builtin ffs routines, therefore
118*c2c66affSColin Finck * differs in spirit from the above ffz (man ffs).
119*c2c66affSColin Finck */
ffs(int x)120*c2c66affSColin Finck static inline int ffs(int x)
121*c2c66affSColin Finck {
122*c2c66affSColin Finck int r = 1;
123*c2c66affSColin Finck
124*c2c66affSColin Finck if (!x)
125*c2c66affSColin Finck return 0;
126*c2c66affSColin Finck if (!(x & 0xffff)) {
127*c2c66affSColin Finck x >>= 16;
128*c2c66affSColin Finck r += 16;
129*c2c66affSColin Finck }
130*c2c66affSColin Finck if (!(x & 0xff)) {
131*c2c66affSColin Finck x >>= 8;
132*c2c66affSColin Finck r += 8;
133*c2c66affSColin Finck }
134*c2c66affSColin Finck if (!(x & 0xf)) {
135*c2c66affSColin Finck x >>= 4;
136*c2c66affSColin Finck r += 4;
137*c2c66affSColin Finck }
138*c2c66affSColin Finck if (!(x & 3)) {
139*c2c66affSColin Finck x >>= 2;
140*c2c66affSColin Finck r += 2;
141*c2c66affSColin Finck }
142*c2c66affSColin Finck if (!(x & 1)) {
143*c2c66affSColin Finck x >>= 1;
144*c2c66affSColin Finck r += 1;
145*c2c66affSColin Finck }
146*c2c66affSColin Finck return r;
147*c2c66affSColin Finck }
148*c2c66affSColin Finck
149*c2c66affSColin Finck /**
150*c2c66affSColin Finck * fls - find last (most-significant) bit set
151*c2c66affSColin Finck * @x: the word to search
152*c2c66affSColin Finck *
153*c2c66affSColin Finck * This is defined the same way as ffs.
154*c2c66affSColin Finck * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
155*c2c66affSColin Finck */
156*c2c66affSColin Finck
fls(int x)157*c2c66affSColin Finck static inline int fls(int x)
158*c2c66affSColin Finck {
159*c2c66affSColin Finck int r = 32;
160*c2c66affSColin Finck
161*c2c66affSColin Finck if (!x)
162*c2c66affSColin Finck return 0;
163*c2c66affSColin Finck if (!(x & 0xffff0000u)) {
164*c2c66affSColin Finck x <<= 16;
165*c2c66affSColin Finck r -= 16;
166*c2c66affSColin Finck }
167*c2c66affSColin Finck if (!(x & 0xff000000u)) {
168*c2c66affSColin Finck x <<= 8;
169*c2c66affSColin Finck r -= 8;
170*c2c66affSColin Finck }
171*c2c66affSColin Finck if (!(x & 0xf0000000u)) {
172*c2c66affSColin Finck x <<= 4;
173*c2c66affSColin Finck r -= 4;
174*c2c66affSColin Finck }
175*c2c66affSColin Finck if (!(x & 0xc0000000u)) {
176*c2c66affSColin Finck x <<= 2;
177*c2c66affSColin Finck r -= 2;
178*c2c66affSColin Finck }
179*c2c66affSColin Finck if (!(x & 0x80000000u)) {
180*c2c66affSColin Finck x <<= 1;
181*c2c66affSColin Finck r -= 1;
182*c2c66affSColin Finck }
183*c2c66affSColin Finck return r;
184*c2c66affSColin Finck }
185*c2c66affSColin Finck
fls64(__u64 x)186*c2c66affSColin Finck static inline int fls64(__u64 x)
187*c2c66affSColin Finck {
188*c2c66affSColin Finck __u32 h = (__u32) (x >> 32);
189*c2c66affSColin Finck if (h)
190*c2c66affSColin Finck return fls(h) + 32;
191*c2c66affSColin Finck return fls((int)x);
192*c2c66affSColin Finck }
193*c2c66affSColin Finck
194*c2c66affSColin Finck #define for_each_bit(bit, addr, size) \
195*c2c66affSColin Finck for ((bit) = find_first_bit((addr), (size)); \
196*c2c66affSColin Finck (bit) < (size); \
197*c2c66affSColin Finck (bit) = find_next_bit((addr), (size), (bit) + 1))
198*c2c66affSColin Finck
199*c2c66affSColin Finck
get_bitmask_order(unsigned int count)200*c2c66affSColin Finck static __inline int get_bitmask_order(unsigned int count)
201*c2c66affSColin Finck {
202*c2c66affSColin Finck int order;
203*c2c66affSColin Finck
204*c2c66affSColin Finck order = fls(count);
205*c2c66affSColin Finck return order; /* We could be slightly more clever with -1 here... */
206*c2c66affSColin Finck }
207*c2c66affSColin Finck
get_count_order(unsigned int count)208*c2c66affSColin Finck static __inline int get_count_order(unsigned int count)
209*c2c66affSColin Finck {
210*c2c66affSColin Finck int order;
211*c2c66affSColin Finck
212*c2c66affSColin Finck order = fls(count) - 1;
213*c2c66affSColin Finck if (count & (count - 1))
214*c2c66affSColin Finck order++;
215*c2c66affSColin Finck return order;
216*c2c66affSColin Finck }
217*c2c66affSColin Finck
218*c2c66affSColin Finck
219*c2c66affSColin Finck /**
220*c2c66affSColin Finck * rol32 - rotate a 32-bit value left
221*c2c66affSColin Finck * @word: value to rotate
222*c2c66affSColin Finck * @shift: bits to roll
223*c2c66affSColin Finck */
rol32(__u32 word,unsigned int shift)224*c2c66affSColin Finck static inline __u32 rol32(__u32 word, unsigned int shift)
225*c2c66affSColin Finck {
226*c2c66affSColin Finck return (word << shift) | (word >> (32 - shift));
227*c2c66affSColin Finck }
228*c2c66affSColin Finck
229*c2c66affSColin Finck /**
230*c2c66affSColin Finck * ror32 - rotate a 32-bit value right
231*c2c66affSColin Finck * @word: value to rotate
232*c2c66affSColin Finck * @shift: bits to roll
233*c2c66affSColin Finck */
ror32(__u32 word,unsigned int shift)234*c2c66affSColin Finck static inline __u32 ror32(__u32 word, unsigned int shift)
235*c2c66affSColin Finck {
236*c2c66affSColin Finck return (word >> shift) | (word << (32 - shift));
237*c2c66affSColin Finck }
238*c2c66affSColin Finck
fls_long(unsigned long l)239*c2c66affSColin Finck static inline unsigned fls_long(unsigned long l)
240*c2c66affSColin Finck {
241*c2c66affSColin Finck if (sizeof(l) == 4)
242*c2c66affSColin Finck return fls(l);
243*c2c66affSColin Finck return fls64(l);
244*c2c66affSColin Finck }
245*c2c66affSColin Finck
246*c2c66affSColin Finck /*
247*c2c66affSColin Finck * hweightN: returns the hamming weight (i.e. the number
248*c2c66affSColin Finck * of bits set) of a N-bit word
249*c2c66affSColin Finck */
250*c2c66affSColin Finck
hweight32(unsigned long w)251*c2c66affSColin Finck static inline unsigned long hweight32(unsigned long w)
252*c2c66affSColin Finck {
253*c2c66affSColin Finck unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
254*c2c66affSColin Finck res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
255*c2c66affSColin Finck res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
256*c2c66affSColin Finck res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
257*c2c66affSColin Finck return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
258*c2c66affSColin Finck }
259*c2c66affSColin Finck
hweight64(__u64 w)260*c2c66affSColin Finck static inline unsigned long hweight64(__u64 w)
261*c2c66affSColin Finck {
262*c2c66affSColin Finck #if BITS_PER_LONG < 64
263*c2c66affSColin Finck return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
264*c2c66affSColin Finck #else
265*c2c66affSColin Finck u64 res;
266*c2c66affSColin Finck res = (w & 0x5555555555555555U) + ((w >> 1) & 0x5555555555555555U);
267*c2c66affSColin Finck res = (res & 0x3333333333333333U) + ((res >> 2) & 0x3333333333333333U);
268*c2c66affSColin Finck res = (res & 0x0F0F0F0F0F0F0F0FU) + ((res >> 4) & 0x0F0F0F0F0F0F0F0FU);
269*c2c66affSColin Finck res = (res & 0x00FF00FF00FF00FFU) + ((res >> 8) & 0x00FF00FF00FF00FFU);
270*c2c66affSColin Finck res = (res & 0x0000FFFF0000FFFFU) + ((res >> 16) & 0x0000FFFF0000FFFFU);
271*c2c66affSColin Finck return (res & 0x00000000FFFFFFFFU) + ((res >> 32) & 0x00000000FFFFFFFFU);
272*c2c66affSColin Finck #endif
273*c2c66affSColin Finck }
274*c2c66affSColin Finck
hweight_long(unsigned long w)275*c2c66affSColin Finck static inline unsigned long hweight_long(unsigned long w)
276*c2c66affSColin Finck {
277*c2c66affSColin Finck return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
278*c2c66affSColin Finck }
279*c2c66affSColin Finck
280*c2c66affSColin Finck #endif
281