1 #ifndef _TOOLS_LINUX_BITOPS_H_
2 #define _TOOLS_LINUX_BITOPS_H_
5 #include <linux/kernel.h>
6 #include <linux/compiler.h>
7 #include <linux/page.h>
10 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
14 # define BITS_PER_LONG __WORDSIZE
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)
26 static inline void __set_bit(int nr, volatile unsigned long *addr)
28 unsigned long mask = BIT_MASK(nr);
29 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
34 static inline void set_bit(long nr, volatile unsigned long *addr)
36 unsigned long mask = BIT_MASK(nr);
37 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
39 __atomic_or_fetch(p, mask, __ATOMIC_RELAXED);
42 static inline void __clear_bit(int nr, volatile unsigned long *addr)
44 unsigned long mask = BIT_MASK(nr);
45 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
50 static inline void clear_bit(long nr, volatile unsigned long *addr)
52 unsigned long mask = BIT_MASK(nr);
53 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
55 __atomic_and_fetch(p, ~mask, __ATOMIC_RELAXED);
58 static inline int test_bit(long nr, const volatile unsigned long *addr)
60 unsigned long mask = BIT_MASK(nr);
61 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
63 return (*p & mask) != 0;
66 static inline int __test_and_set_bit(int nr, unsigned long *addr)
68 unsigned long mask = BIT_MASK(nr);
69 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
75 return (old & mask) != 0;
78 static inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
80 unsigned long mask = BIT_MASK(nr);
81 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
84 old = __atomic_fetch_or(p, mask, __ATOMIC_RELAXED);
86 return (old & mask) != 0;
89 static inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
91 unsigned long mask = BIT_MASK(nr);
92 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
95 old = __atomic_fetch_and(p, ~mask, __ATOMIC_RELAXED);
97 return (old & mask) != 0;
100 static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
102 unsigned long mask = BIT_MASK(nr);
103 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
105 __atomic_and_fetch(p, ~mask, __ATOMIC_RELEASE);
108 static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
110 unsigned long mask = BIT_MASK(nr);
111 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
114 old = __atomic_fetch_or(p, mask, __ATOMIC_ACQUIRE);
116 return (old & mask) != 0;
119 #define for_each_set_bit(bit, addr, size) \
120 for ((bit) = find_first_bit((addr), (size)); \
122 (bit) = find_next_bit((addr), (size), (bit) + 1))
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)); \
128 (bit) = find_next_bit((addr), (size), (bit) + 1))
130 static inline unsigned long hweight_long(unsigned long w)
132 return __builtin_popcountl(w);
135 static inline unsigned long hweight64(u64 w)
137 return __builtin_popcount((u32) w) +
138 __builtin_popcount(w >> 32);
141 static inline unsigned long hweight32(u32 w)
143 return __builtin_popcount(w);
146 static inline unsigned long hweight8(unsigned long w)
148 return __builtin_popcountl(w);
152 * rol64 - rotate a 64-bit value left
153 * @word: value to rotate
154 * @shift: bits to roll
156 static inline __u64 rol64(__u64 word, unsigned int shift)
158 return (word << shift) | (word >> (64 - shift));
162 * ror64 - rotate a 64-bit value right
163 * @word: value to rotate
164 * @shift: bits to roll
166 static inline __u64 ror64(__u64 word, unsigned int shift)
168 return (word >> shift) | (word << (64 - shift));
172 * rol32 - rotate a 32-bit value left
173 * @word: value to rotate
174 * @shift: bits to roll
176 static inline __u32 rol32(__u32 word, unsigned int shift)
178 return (word << shift) | (word >> ((-shift) & 31));
182 * ror32 - rotate a 32-bit value right
183 * @word: value to rotate
184 * @shift: bits to roll
186 static inline __u32 ror32(__u32 word, unsigned int shift)
188 return (word >> shift) | (word << (32 - shift));
192 * rol16 - rotate a 16-bit value left
193 * @word: value to rotate
194 * @shift: bits to roll
196 static inline __u16 rol16(__u16 word, unsigned int shift)
198 return (word << shift) | (word >> (16 - shift));
202 * ror16 - rotate a 16-bit value right
203 * @word: value to rotate
204 * @shift: bits to roll
206 static inline __u16 ror16(__u16 word, unsigned int shift)
208 return (word >> shift) | (word << (16 - shift));
212 * rol8 - rotate an 8-bit value left
213 * @word: value to rotate
214 * @shift: bits to roll
216 static inline __u8 rol8(__u8 word, unsigned int shift)
218 return (word << shift) | (word >> (8 - shift));
222 * ror8 - rotate an 8-bit value right
223 * @word: value to rotate
224 * @shift: bits to roll
226 static inline __u8 ror8(__u8 word, unsigned int shift)
228 return (word >> shift) | (word << (8 - shift));
231 static inline unsigned long __fls(unsigned long word)
233 return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
236 static inline int fls(int x)
238 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
241 static inline int fls64(__u64 x)
243 #if BITS_PER_LONG == 32
248 #elif BITS_PER_LONG == 64
255 static inline unsigned fls_long(unsigned long l)
262 static inline unsigned long __ffs(unsigned long word)
264 return __builtin_ctzl(word);
267 static inline unsigned long __ffs64(u64 word)
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
275 return __ffs((unsigned long)word);
278 #define ffz(x) __ffs(~(x))
280 static inline __attribute__((const))
281 unsigned long rounddown_pow_of_two(unsigned long n)
283 return 1UL << (fls_long(n) - 1);