]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/bitops.h
Add upstream files
[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 hweight64(u64 w)
124 {
125         return __builtin_popcount((u32) w) +
126                __builtin_popcount(w >> 32);
127 }
128
129 static inline unsigned long hweight8(unsigned long w)
130 {
131         return __builtin_popcountl(w);
132 }
133
134 /**
135  * rol64 - rotate a 64-bit value left
136  * @word: value to rotate
137  * @shift: bits to roll
138  */
139 static inline __u64 rol64(__u64 word, unsigned int shift)
140 {
141         return (word << shift) | (word >> (64 - shift));
142 }
143
144 /**
145  * ror64 - rotate a 64-bit value right
146  * @word: value to rotate
147  * @shift: bits to roll
148  */
149 static inline __u64 ror64(__u64 word, unsigned int shift)
150 {
151         return (word >> shift) | (word << (64 - shift));
152 }
153
154 /**
155  * rol32 - rotate a 32-bit value left
156  * @word: value to rotate
157  * @shift: bits to roll
158  */
159 static inline __u32 rol32(__u32 word, unsigned int shift)
160 {
161         return (word << shift) | (word >> ((-shift) & 31));
162 }
163
164 /**
165  * ror32 - rotate a 32-bit value right
166  * @word: value to rotate
167  * @shift: bits to roll
168  */
169 static inline __u32 ror32(__u32 word, unsigned int shift)
170 {
171         return (word >> shift) | (word << (32 - shift));
172 }
173
174 /**
175  * rol16 - rotate a 16-bit value left
176  * @word: value to rotate
177  * @shift: bits to roll
178  */
179 static inline __u16 rol16(__u16 word, unsigned int shift)
180 {
181         return (word << shift) | (word >> (16 - shift));
182 }
183
184 /**
185  * ror16 - rotate a 16-bit value right
186  * @word: value to rotate
187  * @shift: bits to roll
188  */
189 static inline __u16 ror16(__u16 word, unsigned int shift)
190 {
191         return (word >> shift) | (word << (16 - shift));
192 }
193
194 /**
195  * rol8 - rotate an 8-bit value left
196  * @word: value to rotate
197  * @shift: bits to roll
198  */
199 static inline __u8 rol8(__u8 word, unsigned int shift)
200 {
201         return (word << shift) | (word >> (8 - shift));
202 }
203
204 /**
205  * ror8 - rotate an 8-bit value right
206  * @word: value to rotate
207  * @shift: bits to roll
208  */
209 static inline __u8 ror8(__u8 word, unsigned int shift)
210 {
211         return (word >> shift) | (word << (8 - shift));
212 }
213
214 static inline unsigned long __fls(unsigned long word)
215 {
216         return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
217 }
218
219 static inline int fls(int x)
220 {
221         return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
222 }
223
224 static inline int fls64(__u64 x)
225 {
226 #if BITS_PER_LONG == 32
227         __u32 h = x >> 32;
228         if (h)
229                 return fls(h) + 32;
230         return fls(x);
231 #elif BITS_PER_LONG == 64
232         if (x == 0)
233                 return 0;
234         return __fls(x) + 1;
235 #endif
236 }
237
238 static inline unsigned fls_long(unsigned long l)
239 {
240         if (sizeof(l) == 4)
241                 return fls(l);
242         return fls64(l);
243 }
244
245 static inline unsigned long __ffs(unsigned long word)
246 {
247         return __builtin_ctzl(word);
248 }
249
250 static inline unsigned long __ffs64(u64 word)
251 {
252 #if BITS_PER_LONG == 32
253         if (((u32)word) == 0UL)
254                 return __ffs((u32)(word >> 32)) + 32;
255 #elif BITS_PER_LONG != 64
256 #error BITS_PER_LONG not 32 or 64
257 #endif
258         return __ffs((unsigned long)word);
259 }
260
261 #define ffz(x)  __ffs(~(x))
262
263 static inline __attribute__((const))
264 unsigned long rounddown_pow_of_two(unsigned long n)
265 {
266         return 1UL << (fls_long(n) - 1);
267 }
268
269 #endif