1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
4 #include <linux/bits.h>
6 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
7 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
9 extern unsigned int __sw_hweight8(unsigned int w);
10 extern unsigned int __sw_hweight16(unsigned int w);
11 extern unsigned int __sw_hweight32(unsigned int w);
12 extern unsigned long __sw_hweight64(__u64 w);
15 * Include this here because some architectures need generic_ffs/fls in
18 #include <asm/bitops.h>
20 #define for_each_set_bit(bit, addr, size) \
21 for ((bit) = find_first_bit((addr), (size)); \
23 (bit) = find_next_bit((addr), (size), (bit) + 1))
25 /* same as for_each_set_bit() but use bit as value to start with */
26 #define for_each_set_bit_from(bit, addr, size) \
27 for ((bit) = find_next_bit((addr), (size), (bit)); \
29 (bit) = find_next_bit((addr), (size), (bit) + 1))
31 #define for_each_clear_bit(bit, addr, size) \
32 for ((bit) = find_first_zero_bit((addr), (size)); \
34 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
36 /* same as for_each_clear_bit() but use bit as value to start with */
37 #define for_each_clear_bit_from(bit, addr, size) \
38 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
40 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
42 static inline int get_bitmask_order(unsigned int count)
47 return order; /* We could be slightly more clever with -1 here... */
50 static inline int get_count_order(unsigned int count)
54 order = fls(count) - 1;
55 if (count & (count - 1))
60 static __always_inline unsigned long hweight_long(unsigned long w)
62 return sizeof(w) == 4 ? hweight32(w) : hweight64((__u64)w);
66 * rol64 - rotate a 64-bit value left
67 * @word: value to rotate
68 * @shift: bits to roll
70 static inline __u64 rol64(__u64 word, unsigned int shift)
72 return (word << (shift & 63)) | (word >> ((-shift) & 63));
76 * ror64 - rotate a 64-bit value right
77 * @word: value to rotate
78 * @shift: bits to roll
80 static inline __u64 ror64(__u64 word, unsigned int shift)
82 return (word >> (shift & 63)) | (word << ((-shift) & 63));
86 * rol32 - rotate a 32-bit value left
87 * @word: value to rotate
88 * @shift: bits to roll
90 static inline __u32 rol32(__u32 word, unsigned int shift)
92 return (word << (shift & 31)) | (word >> ((-shift) & 31));
96 * ror32 - rotate a 32-bit value right
97 * @word: value to rotate
98 * @shift: bits to roll
100 static inline __u32 ror32(__u32 word, unsigned int shift)
102 return (word >> (shift & 31)) | (word << ((-shift) & 31));
106 * rol16 - rotate a 16-bit value left
107 * @word: value to rotate
108 * @shift: bits to roll
110 static inline __u16 rol16(__u16 word, unsigned int shift)
112 return (word << (shift & 15)) | (word >> ((-shift) & 15));
116 * ror16 - rotate a 16-bit value right
117 * @word: value to rotate
118 * @shift: bits to roll
120 static inline __u16 ror16(__u16 word, unsigned int shift)
122 return (word >> (shift & 15)) | (word << ((-shift) & 15));
126 * rol8 - rotate an 8-bit value left
127 * @word: value to rotate
128 * @shift: bits to roll
130 static inline __u8 rol8(__u8 word, unsigned int shift)
132 return (word << (shift & 7)) | (word >> ((-shift) & 7));
136 * ror8 - rotate an 8-bit value right
137 * @word: value to rotate
138 * @shift: bits to roll
140 static inline __u8 ror8(__u8 word, unsigned int shift)
142 return (word >> (shift & 7)) | (word << ((-shift) & 7));
146 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
147 * @value: value to sign extend
148 * @index: 0 based bit index (0<=index<32) to sign bit
150 * This is safe to use for 16- and 8-bit types as well.
152 static inline __s32 sign_extend32(__u32 value, int index)
154 __u8 shift = 31 - index;
155 return (__s32)(value << shift) >> shift;
159 * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
160 * @value: value to sign extend
161 * @index: 0 based bit index (0<=index<64) to sign bit
163 static inline __s64 sign_extend64(__u64 value, int index)
165 __u8 shift = 63 - index;
166 return (__s64)(value << shift) >> shift;
169 static inline unsigned fls_long(unsigned long l)
177 * __ffs64 - find first set bit in a 64 bit word
178 * @word: The 64 bit word
180 * On 64 bit arches this is a synomyn for __ffs
181 * The result is not defined if no bits are set, so check that @word
182 * is non-zero before calling this.
184 static inline unsigned long __ffs64(u64 word)
186 #if BITS_PER_LONG == 32
187 if (((u32)word) == 0UL)
188 return __ffs((u32)(word >> 32)) + 32;
189 #elif BITS_PER_LONG != 64
190 #error BITS_PER_LONG not 32 or 64
192 return __ffs((unsigned long)word);
197 #ifndef set_mask_bits
198 #define set_mask_bits(ptr, _mask, _bits) \
200 const typeof(*ptr) mask = (_mask), bits = (_bits); \
201 typeof(*ptr) old, new; \
204 old = ACCESS_ONCE(*ptr); \
205 new = (old & ~mask) | bits; \
206 } while (cmpxchg(ptr, old, new) != old); \
212 #ifndef find_last_bit
214 * find_last_bit - find the last set bit in a memory region
215 * @addr: The address to start the search at
216 * @size: The number of bits to search
218 * Returns the bit number of the last set bit, or size.
220 extern unsigned long find_last_bit(const unsigned long *addr,
224 #endif /* __KERNEL__ */