]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/bitops.h
Update bcachefs sources to 99750eab4d bcachefs: Persist stripe blocks_used
[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 void clear_bit_unlock(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
93         __atomic_and_fetch(p, ~mask, __ATOMIC_RELEASE);
94 }
95
96 static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
97 {
98         unsigned long mask = BIT_MASK(nr);
99         unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
100         unsigned long old;
101
102         old = __atomic_fetch_or(p, mask, __ATOMIC_ACQUIRE);
103
104         return (old & mask) != 0;
105 }
106
107 #define for_each_set_bit(bit, addr, size) \
108         for ((bit) = find_first_bit((addr), (size));            \
109              (bit) < (size);                                    \
110              (bit) = find_next_bit((addr), (size), (bit) + 1))
111
112 /* same as for_each_set_bit() but use bit as value to start with */
113 #define for_each_set_bit_from(bit, addr, size) \
114         for ((bit) = find_next_bit((addr), (size), (bit));      \
115              (bit) < (size);                                    \
116              (bit) = find_next_bit((addr), (size), (bit) + 1))
117
118 static inline unsigned long hweight_long(unsigned long w)
119 {
120         return __builtin_popcountl(w);
121 }
122
123 static inline unsigned long hweight8(unsigned long w)
124 {
125         return __builtin_popcountl(w);
126 }
127
128 /**
129  * rol64 - rotate a 64-bit value left
130  * @word: value to rotate
131  * @shift: bits to roll
132  */
133 static inline __u64 rol64(__u64 word, unsigned int shift)
134 {
135         return (word << shift) | (word >> (64 - shift));
136 }
137
138 /**
139  * ror64 - rotate a 64-bit value right
140  * @word: value to rotate
141  * @shift: bits to roll
142  */
143 static inline __u64 ror64(__u64 word, unsigned int shift)
144 {
145         return (word >> shift) | (word << (64 - shift));
146 }
147
148 /**
149  * rol32 - rotate a 32-bit value left
150  * @word: value to rotate
151  * @shift: bits to roll
152  */
153 static inline __u32 rol32(__u32 word, unsigned int shift)
154 {
155         return (word << shift) | (word >> ((-shift) & 31));
156 }
157
158 /**
159  * ror32 - rotate a 32-bit value right
160  * @word: value to rotate
161  * @shift: bits to roll
162  */
163 static inline __u32 ror32(__u32 word, unsigned int shift)
164 {
165         return (word >> shift) | (word << (32 - shift));
166 }
167
168 /**
169  * rol16 - rotate a 16-bit value left
170  * @word: value to rotate
171  * @shift: bits to roll
172  */
173 static inline __u16 rol16(__u16 word, unsigned int shift)
174 {
175         return (word << shift) | (word >> (16 - shift));
176 }
177
178 /**
179  * ror16 - rotate a 16-bit value right
180  * @word: value to rotate
181  * @shift: bits to roll
182  */
183 static inline __u16 ror16(__u16 word, unsigned int shift)
184 {
185         return (word >> shift) | (word << (16 - shift));
186 }
187
188 /**
189  * rol8 - rotate an 8-bit value left
190  * @word: value to rotate
191  * @shift: bits to roll
192  */
193 static inline __u8 rol8(__u8 word, unsigned int shift)
194 {
195         return (word << shift) | (word >> (8 - shift));
196 }
197
198 /**
199  * ror8 - rotate an 8-bit value right
200  * @word: value to rotate
201  * @shift: bits to roll
202  */
203 static inline __u8 ror8(__u8 word, unsigned int shift)
204 {
205         return (word >> shift) | (word << (8 - shift));
206 }
207
208 static inline unsigned long __fls(unsigned long word)
209 {
210         return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
211 }
212
213 static inline int fls(int x)
214 {
215         return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
216 }
217
218 static inline int fls64(__u64 x)
219 {
220 #if BITS_PER_LONG == 32
221         __u32 h = x >> 32;
222         if (h)
223                 return fls(h) + 32;
224         return fls(x);
225 #elif BITS_PER_LONG == 64
226         if (x == 0)
227                 return 0;
228         return __fls(x) + 1;
229 #endif
230 }
231
232 static inline unsigned fls_long(unsigned long l)
233 {
234         if (sizeof(l) == 4)
235                 return fls(l);
236         return fls64(l);
237 }
238
239 static inline unsigned long __ffs(unsigned long word)
240 {
241         return __builtin_ctzl(word);
242 }
243
244 static inline unsigned long __ffs64(u64 word)
245 {
246 #if BITS_PER_LONG == 32
247         if (((u32)word) == 0UL)
248                 return __ffs((u32)(word >> 32)) + 32;
249 #elif BITS_PER_LONG != 64
250 #error BITS_PER_LONG not 32 or 64
251 #endif
252         return __ffs((unsigned long)word);
253 }
254
255 #define ffz(x)  __ffs(~(x))
256
257 static inline __attribute__((const))
258 unsigned long rounddown_pow_of_two(unsigned long n)
259 {
260         return 1UL << (fls_long(n) - 1);
261 }
262
263 #endif