]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/bitops.h
hweight32()
[bcachefs-tools-debian] / include / linux / bitops.h
1 #ifndef _TOOLS_LINUX_BITOPS_H_
2 #define _TOOLS_LINUX_BITOPS_H_
3
4 #include <asm/types.h>
5 #include <linux/kernel.h>
6 #include <linux/compiler.h>
7 #include <linux/page.h>
8
9 #ifndef __WORDSIZE
10 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
11 #endif
12
13 #ifndef BITS_PER_LONG
14 # define BITS_PER_LONG __WORDSIZE
15 #endif
16
17 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
18 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
19 #define BITS_PER_BYTE           8
20 #define BITS_TO_LONGS(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
21 #define BITS_TO_U64(nr)         DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
22 #define BITS_TO_U32(nr)         DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
23 #define BITS_TO_BYTES(nr)       DIV_ROUND_UP(nr, BITS_PER_BYTE)
24
25 static inline void __set_bit(int nr, volatile unsigned long *addr)
26 {
27         unsigned long mask = BIT_MASK(nr);
28         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
29
30         *p  |= mask;
31 }
32
33 static inline void set_bit(long nr, volatile unsigned long *addr)
34 {
35         unsigned long mask = BIT_MASK(nr);
36         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
37
38         __atomic_or_fetch(p, mask, __ATOMIC_RELAXED);
39 }
40
41 static inline void __clear_bit(int nr, volatile unsigned long *addr)
42 {
43         unsigned long mask = BIT_MASK(nr);
44         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
45
46         *p &= ~mask;
47 }
48
49 static inline void clear_bit(long nr, volatile unsigned long *addr)
50 {
51         unsigned long mask = BIT_MASK(nr);
52         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
53
54         __atomic_and_fetch(p, ~mask, __ATOMIC_RELAXED);
55 }
56
57 static inline int test_bit(long nr, const volatile unsigned long *addr)
58 {
59         unsigned long mask = BIT_MASK(nr);
60         unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
61
62         return (*p & mask) != 0;
63 }
64
65 static inline int __test_and_set_bit(int nr, unsigned long *addr)
66 {
67         unsigned long mask = BIT_MASK(nr);
68         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
69         unsigned long old;
70
71         old = *p;
72         *p = old | mask;
73
74         return (old & mask) != 0;
75 }
76
77 static inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
78 {
79         unsigned long mask = BIT_MASK(nr);
80         unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
81         unsigned long old;
82
83         old = __atomic_fetch_or(p, mask, __ATOMIC_RELAXED);
84
85         return (old & mask) != 0;
86 }
87
88 static inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
89 {
90         unsigned long mask = BIT_MASK(nr);
91         unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
92         unsigned long old;
93
94         old = __atomic_fetch_and(p, ~mask, __ATOMIC_RELAXED);
95
96         return (old & mask) != 0;
97 }
98
99 static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
100 {
101         unsigned long mask = BIT_MASK(nr);
102         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
103
104         __atomic_and_fetch(p, ~mask, __ATOMIC_RELEASE);
105 }
106
107 static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
108 {
109         unsigned long mask = BIT_MASK(nr);
110         unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
111         unsigned long old;
112
113         old = __atomic_fetch_or(p, mask, __ATOMIC_ACQUIRE);
114
115         return (old & mask) != 0;
116 }
117
118 #define for_each_set_bit(bit, addr, size) \
119         for ((bit) = find_first_bit((addr), (size));            \
120              (bit) < (size);                                    \
121              (bit) = find_next_bit((addr), (size), (bit) + 1))
122
123 /* same as for_each_set_bit() but use bit as value to start with */
124 #define for_each_set_bit_from(bit, addr, size) \
125         for ((bit) = find_next_bit((addr), (size), (bit));      \
126              (bit) < (size);                                    \
127              (bit) = find_next_bit((addr), (size), (bit) + 1))
128
129 static inline unsigned long hweight_long(unsigned long w)
130 {
131         return __builtin_popcountl(w);
132 }
133
134 static inline unsigned long hweight64(u64 w)
135 {
136         return __builtin_popcount((u32) w) +
137                __builtin_popcount(w >> 32);
138 }
139
140 static inline unsigned long hweight32(u32 w)
141 {
142         return __builtin_popcount(w);
143 }
144
145 static inline unsigned long hweight8(unsigned long w)
146 {
147         return __builtin_popcountl(w);
148 }
149
150 /**
151  * rol64 - rotate a 64-bit value left
152  * @word: value to rotate
153  * @shift: bits to roll
154  */
155 static inline __u64 rol64(__u64 word, unsigned int shift)
156 {
157         return (word << shift) | (word >> (64 - shift));
158 }
159
160 /**
161  * ror64 - rotate a 64-bit value right
162  * @word: value to rotate
163  * @shift: bits to roll
164  */
165 static inline __u64 ror64(__u64 word, unsigned int shift)
166 {
167         return (word >> shift) | (word << (64 - shift));
168 }
169
170 /**
171  * rol32 - rotate a 32-bit value left
172  * @word: value to rotate
173  * @shift: bits to roll
174  */
175 static inline __u32 rol32(__u32 word, unsigned int shift)
176 {
177         return (word << shift) | (word >> ((-shift) & 31));
178 }
179
180 /**
181  * ror32 - rotate a 32-bit value right
182  * @word: value to rotate
183  * @shift: bits to roll
184  */
185 static inline __u32 ror32(__u32 word, unsigned int shift)
186 {
187         return (word >> shift) | (word << (32 - shift));
188 }
189
190 /**
191  * rol16 - rotate a 16-bit value left
192  * @word: value to rotate
193  * @shift: bits to roll
194  */
195 static inline __u16 rol16(__u16 word, unsigned int shift)
196 {
197         return (word << shift) | (word >> (16 - shift));
198 }
199
200 /**
201  * ror16 - rotate a 16-bit value right
202  * @word: value to rotate
203  * @shift: bits to roll
204  */
205 static inline __u16 ror16(__u16 word, unsigned int shift)
206 {
207         return (word >> shift) | (word << (16 - shift));
208 }
209
210 /**
211  * rol8 - rotate an 8-bit value left
212  * @word: value to rotate
213  * @shift: bits to roll
214  */
215 static inline __u8 rol8(__u8 word, unsigned int shift)
216 {
217         return (word << shift) | (word >> (8 - shift));
218 }
219
220 /**
221  * ror8 - rotate an 8-bit value right
222  * @word: value to rotate
223  * @shift: bits to roll
224  */
225 static inline __u8 ror8(__u8 word, unsigned int shift)
226 {
227         return (word >> shift) | (word << (8 - shift));
228 }
229
230 static inline unsigned long __fls(unsigned long word)
231 {
232         return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
233 }
234
235 static inline int fls(int x)
236 {
237         return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
238 }
239
240 static inline int fls64(__u64 x)
241 {
242 #if BITS_PER_LONG == 32
243         __u32 h = x >> 32;
244         if (h)
245                 return fls(h) + 32;
246         return fls(x);
247 #elif BITS_PER_LONG == 64
248         if (x == 0)
249                 return 0;
250         return __fls(x) + 1;
251 #endif
252 }
253
254 static inline unsigned fls_long(unsigned long l)
255 {
256         if (sizeof(l) == 4)
257                 return fls(l);
258         return fls64(l);
259 }
260
261 static inline unsigned long __ffs(unsigned long word)
262 {
263         return __builtin_ctzl(word);
264 }
265
266 static inline unsigned long __ffs64(u64 word)
267 {
268 #if BITS_PER_LONG == 32
269         if (((u32)word) == 0UL)
270                 return __ffs((u32)(word >> 32)) + 32;
271 #elif BITS_PER_LONG != 64
272 #error BITS_PER_LONG not 32 or 64
273 #endif
274         return __ffs((unsigned long)word);
275 }
276
277 #define ffz(x)  __ffs(~(x))
278
279 static inline __attribute__((const))
280 unsigned long rounddown_pow_of_two(unsigned long n)
281 {
282         return 1UL << (fls_long(n) - 1);
283 }
284
285 #endif