1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
6 #define BIT(nr) (1UL << (nr))
7 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
9 #define BITS_PER_BYTE 8
10 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
14 * Create a contiguous bitmask starting at bit position @l and ending at
15 * position @h. For example
16 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
18 #define GENMASK(h, l) (((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
19 #define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
21 extern unsigned int __sw_hweight8(unsigned int w);
22 extern unsigned int __sw_hweight16(unsigned int w);
23 extern unsigned int __sw_hweight32(unsigned int w);
24 extern unsigned long __sw_hweight64(__u64 w);
27 * Include this here because some architectures need generic_ffs/fls in
30 #include <asm/bitops.h>
32 #define for_each_set_bit(bit, addr, size) \
33 for ((bit) = find_first_bit((addr), (size)); \
35 (bit) = find_next_bit((addr), (size), (bit) + 1))
37 /* same as for_each_set_bit() but use bit as value to start with */
38 #define for_each_set_bit_from(bit, addr, size) \
39 for ((bit) = find_next_bit((addr), (size), (bit)); \
41 (bit) = find_next_bit((addr), (size), (bit) + 1))
43 #define for_each_clear_bit(bit, addr, size) \
44 for ((bit) = find_first_zero_bit((addr), (size)); \
46 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
48 /* same as for_each_clear_bit() but use bit as value to start with */
49 #define for_each_clear_bit_from(bit, addr, size) \
50 for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
52 (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
54 static __inline__ int get_bitmask_order(unsigned int count)
59 return order; /* We could be slightly more clever with -1 here... */
62 static __inline__ int get_count_order(unsigned int count)
66 order = fls(count) - 1;
67 if (count & (count - 1))
72 static inline unsigned long hweight_long(unsigned long w)
74 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
78 * rol64 - rotate a 64-bit value left
79 * @word: value to rotate
80 * @shift: bits to roll
82 static inline __u64 rol64(__u64 word, unsigned int shift)
84 return (word << shift) | (word >> (64 - shift));
88 * ror64 - rotate a 64-bit value right
89 * @word: value to rotate
90 * @shift: bits to roll
92 static inline __u64 ror64(__u64 word, unsigned int shift)
94 return (word >> shift) | (word << (64 - shift));
98 * rol32 - rotate a 32-bit value left
99 * @word: value to rotate
100 * @shift: bits to roll
102 static inline __u32 rol32(__u32 word, unsigned int shift)
104 return (word << shift) | (word >> (32 - shift));
108 * ror32 - rotate a 32-bit value right
109 * @word: value to rotate
110 * @shift: bits to roll
112 static inline __u32 ror32(__u32 word, unsigned int shift)
114 return (word >> shift) | (word << (32 - shift));
118 * rol16 - rotate a 16-bit value left
119 * @word: value to rotate
120 * @shift: bits to roll
122 static inline __u16 rol16(__u16 word, unsigned int shift)
124 return (word << shift) | (word >> (16 - shift));
128 * ror16 - rotate a 16-bit value right
129 * @word: value to rotate
130 * @shift: bits to roll
132 static inline __u16 ror16(__u16 word, unsigned int shift)
134 return (word >> shift) | (word << (16 - shift));
138 * rol8 - rotate an 8-bit value left
139 * @word: value to rotate
140 * @shift: bits to roll
142 static inline __u8 rol8(__u8 word, unsigned int shift)
144 return (word << shift) | (word >> (8 - shift));
148 * ror8 - rotate an 8-bit value right
149 * @word: value to rotate
150 * @shift: bits to roll
152 static inline __u8 ror8(__u8 word, unsigned int shift)
154 return (word >> shift) | (word << (8 - shift));
158 * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
159 * @value: value to sign extend
160 * @index: 0 based bit index (0<=index<32) to sign bit
162 static inline __s32 sign_extend32(__u32 value, int index)
164 __u8 shift = 31 - index;
165 return (__s32)(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 find_last_bit
198 * find_last_bit - find the last set bit in a memory region
199 * @addr: The address to start the search at
200 * @size: The maximum size to search
202 * Returns the bit number of the first set bit, or size.
204 extern unsigned long find_last_bit(const unsigned long *addr,
208 #endif /* __KERNEL__ */