1 #ifndef __NET_BITOPS_H__
2 #define __NET_BITOPS_H__
3 #include "net_defs.h"
4 #include "byteorder.h"
5 
6 #define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
7 #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
8 #define BITS_PER_BYTE		8
9 #ifndef BITS_TO_LONGS /* Older kernels define this already */
10 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
11 #endif
12 
13 
14 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
15 #define BITMAP_LAST_WORD_MASK(nbits)					\
16 (									\
17 	((nbits) % BITS_PER_LONG) ?					\
18 		(1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL		\
19 )
20 
21 extern int bitmap_empty(const unsigned long *bitmap, int bits);
22 extern unsigned int __sw_hweight16(unsigned int w);
23 #define hweight16(w) __sw_hweight16(w)
24 extern unsigned int hweight32(unsigned int w);
25 extern unsigned long hweight64(__u64 w);
26 
__clear_bit(int nr,volatile unsigned long * addr)27 static inline void __clear_bit(int nr, volatile unsigned long *addr)
28 {
29 	unsigned long mask = BIT_MASK(nr);
30 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
31 
32 	*p &= ~mask;
33 }
34 
__set_bit(int nr,volatile unsigned long * addr)35 static inline void __set_bit(int nr, volatile unsigned long *addr)
36 {
37 	unsigned long mask = BIT_MASK(nr);
38 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
39 
40 	*p  |= mask;
41 }
42 
43 static __INLINE void
set_bit(unsigned long nr,volatile void * addr)44 set_bit(unsigned long nr, volatile void * addr)
45 {
46 	int *m = ((int *) addr) + (nr >> 5);
47 
48 	*m |= 1 << (nr & 31);
49 }
50 
51 static __INLINE void
clear_bit(unsigned long nr,volatile void * addr)52 clear_bit(unsigned long nr, volatile void * addr)
53 {
54 	int *m = ((int *) addr) + (nr >> 5);
55 
56 	*m &= ~(1 << (nr & 31));
57 }
58 
59 static inline int
test_bit(unsigned long nr,volatile void * addr)60 test_bit(unsigned long nr, volatile void * addr)
61 {
62 	unsigned long mask = 1 << (nr & 0x1f);
63 	int *m = ((int *) addr) + (nr >> 5);
64 
65 	return ((*m) & mask) != 0;
66 }
67 
68 static inline int
test_and_set_bit(unsigned long nr,volatile void * addr)69 test_and_set_bit(unsigned long nr, volatile void * addr)
70 {
71     unsigned long mask = 1 << (nr & 0x1f);
72 	int *m = ((int *) addr) + (nr >> 5);
73 	int old = *m;
74 
75 	*m = old | mask;
76 	return (old & mask) != 0;
77 }
78 
79 static inline int
test_and_clear_bit(unsigned long nr,volatile void * addr)80 test_and_clear_bit(unsigned long nr, volatile void * addr)
81 {
82 	unsigned long mask = 1 << (nr & 0x1f);
83 	int *m = ((int *) addr) + (nr >> 5);
84 	int old = *m;
85 
86 	*m = old & ~mask;
87 	return (old & mask) != 0;
88 }
89 
90 int fls(int x);
91 
92 /**
93  * __ffs - find first bit in word.
94  * @word: The word to search
95  *
96  * Undefined if no bit exists, so code should check against 0 first.
97  */
__ffs(unsigned long word)98 static inline unsigned long __ffs(unsigned long word)
99 {
100 	int num = 0;
101 
102 #if BITS_PER_LONG == 64
103 	if ((word & 0xffffffff) == 0) {
104 		num += 32;
105 		word >>= 32;
106 	}
107 #endif
108 	if ((word & 0xffff) == 0) {
109 		num += 16;
110 		word >>= 16;
111 	}
112 	if ((word & 0xff) == 0) {
113 		num += 8;
114 		word >>= 8;
115 	}
116 	if ((word & 0xf) == 0) {
117 		num += 4;
118 		word >>= 4;
119 	}
120 	if ((word & 0x3) == 0) {
121 		num += 2;
122 		word >>= 2;
123 	}
124 	if ((word & 0x1) == 0)
125 		num += 1;
126 	return num;
127 }
128 
__ffs64(u64 word)129 static inline unsigned long __ffs64(u64 word)
130 {
131 #if BITS_PER_LONG == 32
132 	if (((u32)word) == 0UL)
133 		return __ffs((u32)(word >> 32)) + 32;
134 #elif BITS_PER_LONG != 64
135 #error BITS_PER_LONG not 32 or 64
136 #endif
137 	return __ffs((unsigned long)word);
138 }
139 
140 #if 0
141 static inline int ffs(int x)
142 {
143 	int r = 1;
144 
145 	if (!x)
146 		return 0;
147 	if (!(x & 0xffff)) {
148 		x >>= 16;
149 		r += 16;
150 	}
151 	if (!(x & 0xff)) {
152 		x >>= 8;
153 		r += 8;
154 	}
155 	if (!(x & 0xf)) {
156 		x >>= 4;
157 		r += 4;
158 	}
159 	if (!(x & 3)) {
160 		x >>= 2;
161 		r += 2;
162 	}
163 	if (!(x & 1)) {
164 		x >>= 1;
165 		r += 1;
166 	}
167 	return r;
168 }
169 #endif
170 
fls64(__u64 x)171 static __INLINE int fls64(__u64 x)
172 {
173 	__u32 h = x >> 32;
174 	if (h)
175 		return fls(h) + 32;
176 	return fls(x);
177 }
178 
179 /**
180  * rol32 - rotate a 32-bit value left
181  * @word: value to rotate
182  * @shift: bits to roll
183  */
rol32(__u32 word,unsigned int shift)184 static __INLINE __u32 rol32(__u32 word, unsigned int shift)
185 {
186 	return (word << shift) | (word >> (32 - shift));
187 }
188 
189 /**
190  * ror32 - rotate a 32-bit value right
191  * @word: value to rotate
192  * @shift: bits to roll
193  */
ror32(__u32 word,unsigned int shift)194 static __INLINE __u32 ror32(__u32 word, unsigned int shift)
195 {
196 	return (word >> shift) | (word << (32 - shift));
197 }
198 
199 #endif
200 
201