xref: /linux-6.15/include/linux/bitops.h (revision 00a1a053)
11da177e4SLinus Torvalds #ifndef _LINUX_BITOPS_H
21da177e4SLinus Torvalds #define _LINUX_BITOPS_H
31da177e4SLinus Torvalds #include <asm/types.h>
41da177e4SLinus Torvalds 
5d05be13bSJiri Slaby #ifdef	__KERNEL__
693043eceSJiri Slaby #define BIT(nr)			(1UL << (nr))
7bfd1ff63SSrinivas Pandruvada #define BIT_ULL(nr)		(1ULL << (nr))
8d05be13bSJiri Slaby #define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
9d05be13bSJiri Slaby #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
10bfd1ff63SSrinivas Pandruvada #define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
11bfd1ff63SSrinivas Pandruvada #define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
12d05be13bSJiri Slaby #define BITS_PER_BYTE		8
13ede9c697SEric Dumazet #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
14d05be13bSJiri Slaby #endif
15d05be13bSJiri Slaby 
1610ef6b0dSChen, Gong /*
1710ef6b0dSChen, Gong  * Create a contiguous bitmask starting at bit position @l and ending at
1810ef6b0dSChen, Gong  * position @h. For example
1910ef6b0dSChen, Gong  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
2010ef6b0dSChen, Gong  */
2110ef6b0dSChen, Gong #define GENMASK(h, l)		(((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
2210ef6b0dSChen, Gong #define GENMASK_ULL(h, l)	(((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
2310ef6b0dSChen, Gong 
244677d4a5SBorislav Petkov extern unsigned int __sw_hweight8(unsigned int w);
254677d4a5SBorislav Petkov extern unsigned int __sw_hweight16(unsigned int w);
264677d4a5SBorislav Petkov extern unsigned int __sw_hweight32(unsigned int w);
274677d4a5SBorislav Petkov extern unsigned long __sw_hweight64(__u64 w);
284677d4a5SBorislav Petkov 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * Include this here because some architectures need generic_ffs/fls in
311da177e4SLinus Torvalds  * scope
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds #include <asm/bitops.h>
341da177e4SLinus Torvalds 
35984b3f57SAkinobu Mita #define for_each_set_bit(bit, addr, size) \
363e037454SShannon Nelson 	for ((bit) = find_first_bit((addr), (size));		\
373e037454SShannon Nelson 	     (bit) < (size);					\
381e2ad28fSRobert Richter 	     (bit) = find_next_bit((addr), (size), (bit) + 1))
391e2ad28fSRobert Richter 
401e2ad28fSRobert Richter /* same as for_each_set_bit() but use bit as value to start with */
41307b1cd7SAkinobu Mita #define for_each_set_bit_from(bit, addr, size) \
421e2ad28fSRobert Richter 	for ((bit) = find_next_bit((addr), (size), (bit));	\
431e2ad28fSRobert Richter 	     (bit) < (size);					\
443e037454SShannon Nelson 	     (bit) = find_next_bit((addr), (size), (bit) + 1))
453e037454SShannon Nelson 
4603f4a822SAkinobu Mita #define for_each_clear_bit(bit, addr, size) \
4703f4a822SAkinobu Mita 	for ((bit) = find_first_zero_bit((addr), (size));	\
4803f4a822SAkinobu Mita 	     (bit) < (size);					\
4903f4a822SAkinobu Mita 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
5003f4a822SAkinobu Mita 
5103f4a822SAkinobu Mita /* same as for_each_clear_bit() but use bit as value to start with */
5203f4a822SAkinobu Mita #define for_each_clear_bit_from(bit, addr, size) \
5303f4a822SAkinobu Mita 	for ((bit) = find_next_zero_bit((addr), (size), (bit));	\
5403f4a822SAkinobu Mita 	     (bit) < (size);					\
5503f4a822SAkinobu Mita 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
5603f4a822SAkinobu Mita 
571da177e4SLinus Torvalds static __inline__ int get_bitmask_order(unsigned int count)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	int order;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	order = fls(count);
621da177e4SLinus Torvalds 	return order;	/* We could be slightly more clever with -1 here... */
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
6594605effSSiddha, Suresh B static __inline__ int get_count_order(unsigned int count)
6694605effSSiddha, Suresh B {
6794605effSSiddha, Suresh B 	int order;
6894605effSSiddha, Suresh B 
6994605effSSiddha, Suresh B 	order = fls(count) - 1;
7094605effSSiddha, Suresh B 	if (count & (count - 1))
7194605effSSiddha, Suresh B 		order++;
7294605effSSiddha, Suresh B 	return order;
7394605effSSiddha, Suresh B }
7494605effSSiddha, Suresh B 
751da177e4SLinus Torvalds static inline unsigned long hweight_long(unsigned long w)
761da177e4SLinus Torvalds {
77e9bebd6fSAkinobu Mita 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
781da177e4SLinus Torvalds }
799f41699eSPeter Zijlstra 
8045f8bde0SRobert P. J. Day /**
81f2ea0f5fSAlexey Dobriyan  * rol64 - rotate a 64-bit value left
82f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
83f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
84f2ea0f5fSAlexey Dobriyan  */
85f2ea0f5fSAlexey Dobriyan static inline __u64 rol64(__u64 word, unsigned int shift)
86f2ea0f5fSAlexey Dobriyan {
87f2ea0f5fSAlexey Dobriyan 	return (word << shift) | (word >> (64 - shift));
88f2ea0f5fSAlexey Dobriyan }
89f2ea0f5fSAlexey Dobriyan 
90f2ea0f5fSAlexey Dobriyan /**
91f2ea0f5fSAlexey Dobriyan  * ror64 - rotate a 64-bit value right
92f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
93f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
94f2ea0f5fSAlexey Dobriyan  */
95f2ea0f5fSAlexey Dobriyan static inline __u64 ror64(__u64 word, unsigned int shift)
96f2ea0f5fSAlexey Dobriyan {
97f2ea0f5fSAlexey Dobriyan 	return (word >> shift) | (word << (64 - shift));
98f2ea0f5fSAlexey Dobriyan }
99f2ea0f5fSAlexey Dobriyan 
100f2ea0f5fSAlexey Dobriyan /**
1011da177e4SLinus Torvalds  * rol32 - rotate a 32-bit value left
1021da177e4SLinus Torvalds  * @word: value to rotate
1031da177e4SLinus Torvalds  * @shift: bits to roll
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds static inline __u32 rol32(__u32 word, unsigned int shift)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	return (word << shift) | (word >> (32 - shift));
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
11045f8bde0SRobert P. J. Day /**
1111da177e4SLinus Torvalds  * ror32 - rotate a 32-bit value right
1121da177e4SLinus Torvalds  * @word: value to rotate
1131da177e4SLinus Torvalds  * @shift: bits to roll
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds static inline __u32 ror32(__u32 word, unsigned int shift)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	return (word >> shift) | (word << (32 - shift));
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1203afe3925SHarvey Harrison /**
1213afe3925SHarvey Harrison  * rol16 - rotate a 16-bit value left
1223afe3925SHarvey Harrison  * @word: value to rotate
1233afe3925SHarvey Harrison  * @shift: bits to roll
1243afe3925SHarvey Harrison  */
1253afe3925SHarvey Harrison static inline __u16 rol16(__u16 word, unsigned int shift)
1263afe3925SHarvey Harrison {
1273afe3925SHarvey Harrison 	return (word << shift) | (word >> (16 - shift));
1283afe3925SHarvey Harrison }
1293afe3925SHarvey Harrison 
1303afe3925SHarvey Harrison /**
1313afe3925SHarvey Harrison  * ror16 - rotate a 16-bit value right
1323afe3925SHarvey Harrison  * @word: value to rotate
1333afe3925SHarvey Harrison  * @shift: bits to roll
1343afe3925SHarvey Harrison  */
1353afe3925SHarvey Harrison static inline __u16 ror16(__u16 word, unsigned int shift)
1363afe3925SHarvey Harrison {
1373afe3925SHarvey Harrison 	return (word >> shift) | (word << (16 - shift));
1383afe3925SHarvey Harrison }
1393afe3925SHarvey Harrison 
1403afe3925SHarvey Harrison /**
1413afe3925SHarvey Harrison  * rol8 - rotate an 8-bit value left
1423afe3925SHarvey Harrison  * @word: value to rotate
1433afe3925SHarvey Harrison  * @shift: bits to roll
1443afe3925SHarvey Harrison  */
1453afe3925SHarvey Harrison static inline __u8 rol8(__u8 word, unsigned int shift)
1463afe3925SHarvey Harrison {
1473afe3925SHarvey Harrison 	return (word << shift) | (word >> (8 - shift));
1483afe3925SHarvey Harrison }
1493afe3925SHarvey Harrison 
1503afe3925SHarvey Harrison /**
1513afe3925SHarvey Harrison  * ror8 - rotate an 8-bit value right
1523afe3925SHarvey Harrison  * @word: value to rotate
1533afe3925SHarvey Harrison  * @shift: bits to roll
1543afe3925SHarvey Harrison  */
1553afe3925SHarvey Harrison static inline __u8 ror8(__u8 word, unsigned int shift)
1563afe3925SHarvey Harrison {
1573afe3925SHarvey Harrison 	return (word >> shift) | (word << (8 - shift));
1583afe3925SHarvey Harrison }
1593afe3925SHarvey Harrison 
1607919a57bSAndreas Herrmann /**
1617919a57bSAndreas Herrmann  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
1627919a57bSAndreas Herrmann  * @value: value to sign extend
1637919a57bSAndreas Herrmann  * @index: 0 based bit index (0<=index<32) to sign bit
1647919a57bSAndreas Herrmann  */
1657919a57bSAndreas Herrmann static inline __s32 sign_extend32(__u32 value, int index)
1667919a57bSAndreas Herrmann {
1677919a57bSAndreas Herrmann 	__u8 shift = 31 - index;
1687919a57bSAndreas Herrmann 	return (__s32)(value << shift) >> shift;
1697919a57bSAndreas Herrmann }
1707919a57bSAndreas Herrmann 
171962749afSAndrew Morton static inline unsigned fls_long(unsigned long l)
172962749afSAndrew Morton {
173962749afSAndrew Morton 	if (sizeof(l) == 4)
174962749afSAndrew Morton 		return fls(l);
175962749afSAndrew Morton 	return fls64(l);
176962749afSAndrew Morton }
177962749afSAndrew Morton 
178952043acSSteven Whitehouse /**
179952043acSSteven Whitehouse  * __ffs64 - find first set bit in a 64 bit word
180952043acSSteven Whitehouse  * @word: The 64 bit word
181952043acSSteven Whitehouse  *
182952043acSSteven Whitehouse  * On 64 bit arches this is a synomyn for __ffs
183952043acSSteven Whitehouse  * The result is not defined if no bits are set, so check that @word
184952043acSSteven Whitehouse  * is non-zero before calling this.
185952043acSSteven Whitehouse  */
186952043acSSteven Whitehouse static inline unsigned long __ffs64(u64 word)
187952043acSSteven Whitehouse {
188952043acSSteven Whitehouse #if BITS_PER_LONG == 32
189952043acSSteven Whitehouse 	if (((u32)word) == 0UL)
190952043acSSteven Whitehouse 		return __ffs((u32)(word >> 32)) + 32;
191952043acSSteven Whitehouse #elif BITS_PER_LONG != 64
192952043acSSteven Whitehouse #error BITS_PER_LONG not 32 or 64
193952043acSSteven Whitehouse #endif
194952043acSSteven Whitehouse 	return __ffs((unsigned long)word);
195952043acSSteven Whitehouse }
196952043acSSteven Whitehouse 
19764970b68SAlexander van Heukelum #ifdef __KERNEL__
19877b9bd9cSAlexander van Heukelum 
199*00a1a053STheodore Ts'o #ifndef set_mask_bits
200*00a1a053STheodore Ts'o #define set_mask_bits(ptr, _mask, _bits)	\
201*00a1a053STheodore Ts'o ({								\
202*00a1a053STheodore Ts'o 	const typeof(*ptr) mask = (_mask), bits = (_bits);	\
203*00a1a053STheodore Ts'o 	typeof(*ptr) old, new;					\
204*00a1a053STheodore Ts'o 								\
205*00a1a053STheodore Ts'o 	do {							\
206*00a1a053STheodore Ts'o 		old = ACCESS_ONCE(*ptr);			\
207*00a1a053STheodore Ts'o 		new = (old & ~mask) | bits;			\
208*00a1a053STheodore Ts'o 	} while (cmpxchg(ptr, old, new) != old);		\
209*00a1a053STheodore Ts'o 								\
210*00a1a053STheodore Ts'o 	new;							\
211*00a1a053STheodore Ts'o })
212*00a1a053STheodore Ts'o #endif
213*00a1a053STheodore Ts'o 
21419de85efSAkinobu Mita #ifndef find_last_bit
215ab53d472SRusty Russell /**
216ab53d472SRusty Russell  * find_last_bit - find the last set bit in a memory region
217ab53d472SRusty Russell  * @addr: The address to start the search at
218ab53d472SRusty Russell  * @size: The maximum size to search
219ab53d472SRusty Russell  *
220ab53d472SRusty Russell  * Returns the bit number of the first set bit, or size.
221ab53d472SRusty Russell  */
222ab53d472SRusty Russell extern unsigned long find_last_bit(const unsigned long *addr,
223ab53d472SRusty Russell 				   unsigned long size);
22419de85efSAkinobu Mita #endif
225ab53d472SRusty Russell 
22664970b68SAlexander van Heukelum #endif /* __KERNEL__ */
2271da177e4SLinus Torvalds #endif
228