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_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)
25 static inline void __set_bit(int nr, volatile unsigned long *addr)
27 unsigned long mask = BIT_MASK(nr);
28 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
33 static inline void set_bit(long nr, volatile unsigned long *addr)
35 unsigned long mask = BIT_MASK(nr);
36 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
38 __atomic_or_fetch(p, mask, __ATOMIC_RELAXED);
41 static inline void __clear_bit(int nr, volatile unsigned long *addr)
43 unsigned long mask = BIT_MASK(nr);
44 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
49 static inline void clear_bit(long nr, volatile unsigned long *addr)
51 unsigned long mask = BIT_MASK(nr);
52 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
54 __atomic_and_fetch(p, ~mask, __ATOMIC_RELAXED);
57 static inline int test_bit(long nr, const volatile unsigned long *addr)
59 unsigned long mask = BIT_MASK(nr);
60 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
62 return (*p & mask) != 0;
65 static inline int __test_and_set_bit(int nr, unsigned long *addr)
67 unsigned long mask = BIT_MASK(nr);
68 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
74 return (old & mask) != 0;
77 static inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
79 unsigned long mask = BIT_MASK(nr);
80 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
83 old = __atomic_fetch_or(p, mask, __ATOMIC_RELAXED);
85 return (old & mask) != 0;
88 static inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
90 unsigned long mask = BIT_MASK(nr);
91 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
94 old = __atomic_fetch_and(p, ~mask, __ATOMIC_RELAXED);
96 return (old & mask) != 0;
99 static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
101 unsigned long mask = BIT_MASK(nr);
102 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
104 __atomic_and_fetch(p, ~mask, __ATOMIC_RELEASE);
107 static inline bool test_and_set_bit_lock(long nr, volatile unsigned long *addr)
109 unsigned long mask = BIT_MASK(nr);
110 unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
113 old = __atomic_fetch_or(p, mask, __ATOMIC_ACQUIRE);
115 return (old & mask) != 0;
118 #define for_each_set_bit(bit, addr, size) \
119 for ((bit) = find_first_bit((addr), (size)); \
121 (bit) = find_next_bit((addr), (size), (bit) + 1))
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)); \
127 (bit) = find_next_bit((addr), (size), (bit) + 1))
129 static inline unsigned long hweight_long(unsigned long w)
131 return __builtin_popcountl(w);
134 static inline unsigned long hweight64(u64 w)
136 return __builtin_popcount((u32) w) +
137 __builtin_popcount(w >> 32);
140 static inline unsigned long hweight8(unsigned long w)
142 return __builtin_popcountl(w);
146 * rol64 - rotate a 64-bit value left
147 * @word: value to rotate
148 * @shift: bits to roll
150 static inline __u64 rol64(__u64 word, unsigned int shift)
152 return (word << shift) | (word >> (64 - shift));
156 * ror64 - rotate a 64-bit value right
157 * @word: value to rotate
158 * @shift: bits to roll
160 static inline __u64 ror64(__u64 word, unsigned int shift)
162 return (word >> shift) | (word << (64 - shift));
166 * rol32 - rotate a 32-bit value left
167 * @word: value to rotate
168 * @shift: bits to roll
170 static inline __u32 rol32(__u32 word, unsigned int shift)
172 return (word << shift) | (word >> ((-shift) & 31));
176 * ror32 - rotate a 32-bit value right
177 * @word: value to rotate
178 * @shift: bits to roll
180 static inline __u32 ror32(__u32 word, unsigned int shift)
182 return (word >> shift) | (word << (32 - shift));
186 * rol16 - rotate a 16-bit value left
187 * @word: value to rotate
188 * @shift: bits to roll
190 static inline __u16 rol16(__u16 word, unsigned int shift)
192 return (word << shift) | (word >> (16 - shift));
196 * ror16 - rotate a 16-bit value right
197 * @word: value to rotate
198 * @shift: bits to roll
200 static inline __u16 ror16(__u16 word, unsigned int shift)
202 return (word >> shift) | (word << (16 - shift));
206 * rol8 - rotate an 8-bit value left
207 * @word: value to rotate
208 * @shift: bits to roll
210 static inline __u8 rol8(__u8 word, unsigned int shift)
212 return (word << shift) | (word >> (8 - shift));
216 * ror8 - rotate an 8-bit value right
217 * @word: value to rotate
218 * @shift: bits to roll
220 static inline __u8 ror8(__u8 word, unsigned int shift)
222 return (word >> shift) | (word << (8 - shift));
225 static inline unsigned long __fls(unsigned long word)
227 return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
230 static inline int fls(int x)
232 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
235 static inline int fls64(__u64 x)
237 #if BITS_PER_LONG == 32
242 #elif BITS_PER_LONG == 64
249 static inline unsigned fls_long(unsigned long l)
256 static inline unsigned long __ffs(unsigned long word)
258 return __builtin_ctzl(word);
261 static inline unsigned long __ffs64(u64 word)
263 #if BITS_PER_LONG == 32
264 if (((u32)word) == 0UL)
265 return __ffs((u32)(word >> 32)) + 32;
266 #elif BITS_PER_LONG != 64
267 #error BITS_PER_LONG not 32 or 64
269 return __ffs((unsigned long)word);
272 #define ffz(x) __ffs(~(x))
274 static inline __attribute__((const))
275 unsigned long rounddown_pow_of_two(unsigned long n)
277 return 1UL << (fls_long(n) - 1);