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 hweight32(u32 w)
142 return __builtin_popcount(w);
145 static inline unsigned long hweight8(unsigned long w)
147 return __builtin_popcountl(w);
151 * rol64 - rotate a 64-bit value left
152 * @word: value to rotate
153 * @shift: bits to roll
155 static inline __u64 rol64(__u64 word, unsigned int shift)
157 return (word << shift) | (word >> (64 - shift));
161 * ror64 - rotate a 64-bit value right
162 * @word: value to rotate
163 * @shift: bits to roll
165 static inline __u64 ror64(__u64 word, unsigned int shift)
167 return (word >> shift) | (word << (64 - shift));
171 * rol32 - rotate a 32-bit value left
172 * @word: value to rotate
173 * @shift: bits to roll
175 static inline __u32 rol32(__u32 word, unsigned int shift)
177 return (word << shift) | (word >> ((-shift) & 31));
181 * ror32 - rotate a 32-bit value right
182 * @word: value to rotate
183 * @shift: bits to roll
185 static inline __u32 ror32(__u32 word, unsigned int shift)
187 return (word >> shift) | (word << (32 - shift));
191 * rol16 - rotate a 16-bit value left
192 * @word: value to rotate
193 * @shift: bits to roll
195 static inline __u16 rol16(__u16 word, unsigned int shift)
197 return (word << shift) | (word >> (16 - shift));
201 * ror16 - rotate a 16-bit value right
202 * @word: value to rotate
203 * @shift: bits to roll
205 static inline __u16 ror16(__u16 word, unsigned int shift)
207 return (word >> shift) | (word << (16 - shift));
211 * rol8 - rotate an 8-bit value left
212 * @word: value to rotate
213 * @shift: bits to roll
215 static inline __u8 rol8(__u8 word, unsigned int shift)
217 return (word << shift) | (word >> (8 - shift));
221 * ror8 - rotate an 8-bit value right
222 * @word: value to rotate
223 * @shift: bits to roll
225 static inline __u8 ror8(__u8 word, unsigned int shift)
227 return (word >> shift) | (word << (8 - shift));
230 static inline unsigned long __fls(unsigned long word)
232 return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
235 static inline int fls(int x)
237 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
240 static inline int fls64(__u64 x)
242 #if BITS_PER_LONG == 32
247 #elif BITS_PER_LONG == 64
254 static inline unsigned fls_long(unsigned long l)
261 static inline unsigned long __ffs(unsigned long word)
263 return __builtin_ctzl(word);
266 static inline unsigned long __ffs64(u64 word)
268 #if BITS_PER_LONG == 32
269 if (((u32)word) == 0UL)
270 return __ffs((u32)(word >> 32)) + 32;
271 #elif BITS_PER_LONG != 64
272 #error BITS_PER_LONG not 32 or 64
274 return __ffs((unsigned long)word);
277 #define ffz(x) __ffs(~(x))
279 static inline __attribute__((const))
280 unsigned long rounddown_pow_of_two(unsigned long n)
282 return 1UL << (fls_long(n) - 1);