1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
4 #include <linux/bits.h>
6 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
8 extern unsigned int __sw_hweight8(unsigned int w);
9 extern unsigned int __sw_hweight16(unsigned int w);
10 extern unsigned int __sw_hweight32(unsigned int w);
11 extern unsigned long __sw_hweight64(__u64 w);
14 * Include this here because some architectures need generic_ffs/fls in
17 #include <asm/bitops.h>
19 #define for_each_set_bit(bit, addr, size) \
20 for ((bit) = find_first_bit((addr), (size)); \
22 (bit) = find_next_bit((addr), (size), (bit) + 1))
24 /* same as for_each_set_bit() but use bit as value to start with */
25 #define for_each_set_bit_from(bit, addr, size) \
26 for ((bit) = find_next_bit((addr), (size), (bit)); \
28 (bit) = find_next_bit((addr), (size), (bit) + 1))
30 #define for_each_clear_bit(bit, addr, size) \
31 for ((bit) = find_first_zero_bit((addr), (size)); \
33 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
35 /* same as for_each_clear_bit() but use bit as value to start with */
36 #define for_each_clear_bit_from(bit, addr, size) \
37 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
39 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
41 static inline int get_bitmask_order(unsigned int count)
46 return order; /* We could be slightly more clever with -1 here... */
49 static inline int get_count_order(unsigned int count)
53 order = fls(count) - 1;
54 if (count & (count - 1))
59 static __always_inline unsigned long hweight_long(unsigned long w)
61 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
65 * rol64 - rotate a 64-bit value left
66 * @word: value to rotate
67 * @shift: bits to roll
69 static inline __u64 rol64(__u64 word, unsigned int shift)
71 return (word << (shift & 63)) | (word >> ((-shift) & 63));
75 * ror64 - rotate a 64-bit value right
76 * @word: value to rotate
77 * @shift: bits to roll
79 static inline __u64 ror64(__u64 word, unsigned int shift)
81 return (word >> (shift & 63)) | (word << ((-shift) & 63));
85 * rol32 - rotate a 32-bit value left
86 * @word: value to rotate
87 * @shift: bits to roll
89 static inline __u32 rol32(__u32 word, unsigned int shift)
91 return (word << (shift & 31)) | (word >> ((-shift) & 31));
95 * ror32 - rotate a 32-bit value right
96 * @word: value to rotate
97 * @shift: bits to roll
99 static inline __u32 ror32(__u32 word, unsigned int shift)
101 return (word >> (shift & 31)) | (word << ((-shift) & 31));
105 * rol16 - rotate a 16-bit value left
106 * @word: value to rotate
107 * @shift: bits to roll
109 static inline __u16 rol16(__u16 word, unsigned int shift)
111 return (word << (shift & 15)) | (word >> ((-shift) & 15));
115 * ror16 - rotate a 16-bit value right
116 * @word: value to rotate
117 * @shift: bits to roll
119 static inline __u16 ror16(__u16 word, unsigned int shift)
121 return (word >> (shift & 15)) | (word << ((-shift) & 15));
125 * rol8 - rotate an 8-bit value left
126 * @word: value to rotate
127 * @shift: bits to roll
129 static inline __u8 rol8(__u8 word, unsigned int shift)
131 return (word << (shift & 7)) | (word >> ((-shift) & 7));
135 * ror8 - rotate an 8-bit value right
136 * @word: value to rotate
137 * @shift: bits to roll
139 static inline __u8 ror8(__u8 word, unsigned int shift)
141 return (word >> (shift & 7)) | (word << ((-shift) & 7));
145 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
146 * @value: value to sign extend
147 * @index: 0 based bit index (0<=index<32) to sign bit
149 * This is safe to use for 16- and 8-bit types as well.
151 static inline __s32 sign_extend32(__u32 value, int index)
153 __u8 shift = 31 - index;
154 return (__s32)(value << shift) >> shift;
158 * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
159 * @value: value to sign extend
160 * @index: 0 based bit index (0<=index<64) to sign bit
162 static inline __s64 sign_extend64(__u64 value, int index)
164 __u8 shift = 63 - index;
165 return (__s64)(value << shift) >> shift;
168 static inline unsigned fls_long(unsigned long l)
176 * __ffs64 - find first set bit in a 64 bit word
177 * @word: The 64 bit word
179 * On 64 bit arches this is a synomyn for __ffs
180 * The result is not defined if no bits are set, so check that @word
181 * is non-zero before calling this.
183 static inline unsigned long __ffs64(u64 word)
185 #if BITS_PER_LONG == 32
186 if (((u32)word) == 0UL)
187 return __ffs((u32)(word >> 32)) + 32;
188 #elif BITS_PER_LONG != 64
189 #error BITS_PER_LONG not 32 or 64
191 return __ffs((unsigned long)word);
196 #ifndef set_mask_bits
197 #define set_mask_bits(ptr, _mask, _bits) \
199 const typeof(*ptr) mask = (_mask), bits = (_bits); \
200 typeof(*ptr) old, new; \
203 old = ACCESS_ONCE(*ptr); \
204 new = (old & ~mask) | bits; \
205 } while (cmpxchg(ptr, old, new) != old); \
211 #ifndef find_last_bit
213 * find_last_bit - find the last set bit in a memory region
214 * @addr: The address to start the search at
215 * @size: The number of bits to search
217 * Returns the bit number of the last set bit, or size.
219 extern unsigned long find_last_bit(const unsigned long *addr,
223 #endif /* __KERNEL__ */