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