xref: /linux-6.15/include/linux/bitops.h (revision 2c57a0e2)
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  */
2100b4d9a1SMaxime COQUELIN #define GENMASK(h, l) \
2200b4d9a1SMaxime COQUELIN 	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
2300b4d9a1SMaxime COQUELIN 
2400b4d9a1SMaxime COQUELIN #define GENMASK_ULL(h, l) \
2500b4d9a1SMaxime COQUELIN 	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
2610ef6b0dSChen, Gong 
274677d4a5SBorislav Petkov extern unsigned int __sw_hweight8(unsigned int w);
284677d4a5SBorislav Petkov extern unsigned int __sw_hweight16(unsigned int w);
294677d4a5SBorislav Petkov extern unsigned int __sw_hweight32(unsigned int w);
304677d4a5SBorislav Petkov extern unsigned long __sw_hweight64(__u64 w);
314677d4a5SBorislav Petkov 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * Include this here because some architectures need generic_ffs/fls in
341da177e4SLinus Torvalds  * scope
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds #include <asm/bitops.h>
371da177e4SLinus Torvalds 
38984b3f57SAkinobu Mita #define for_each_set_bit(bit, addr, size) \
393e037454SShannon Nelson 	for ((bit) = find_first_bit((addr), (size));		\
403e037454SShannon Nelson 	     (bit) < (size);					\
411e2ad28fSRobert Richter 	     (bit) = find_next_bit((addr), (size), (bit) + 1))
421e2ad28fSRobert Richter 
431e2ad28fSRobert Richter /* same as for_each_set_bit() but use bit as value to start with */
44307b1cd7SAkinobu Mita #define for_each_set_bit_from(bit, addr, size) \
451e2ad28fSRobert Richter 	for ((bit) = find_next_bit((addr), (size), (bit));	\
461e2ad28fSRobert Richter 	     (bit) < (size);					\
473e037454SShannon Nelson 	     (bit) = find_next_bit((addr), (size), (bit) + 1))
483e037454SShannon Nelson 
4903f4a822SAkinobu Mita #define for_each_clear_bit(bit, addr, size) \
5003f4a822SAkinobu Mita 	for ((bit) = find_first_zero_bit((addr), (size));	\
5103f4a822SAkinobu Mita 	     (bit) < (size);					\
5203f4a822SAkinobu Mita 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
5303f4a822SAkinobu Mita 
5403f4a822SAkinobu Mita /* same as for_each_clear_bit() but use bit as value to start with */
5503f4a822SAkinobu Mita #define for_each_clear_bit_from(bit, addr, size) \
5603f4a822SAkinobu Mita 	for ((bit) = find_next_zero_bit((addr), (size), (bit));	\
5703f4a822SAkinobu Mita 	     (bit) < (size);					\
5803f4a822SAkinobu Mita 	     (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
5903f4a822SAkinobu Mita 
601da177e4SLinus Torvalds static __inline__ int get_bitmask_order(unsigned int count)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	int order;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	order = fls(count);
651da177e4SLinus Torvalds 	return order;	/* We could be slightly more clever with -1 here... */
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
6894605effSSiddha, Suresh B static __inline__ int get_count_order(unsigned int count)
6994605effSSiddha, Suresh B {
7094605effSSiddha, Suresh B 	int order;
7194605effSSiddha, Suresh B 
7294605effSSiddha, Suresh B 	order = fls(count) - 1;
7394605effSSiddha, Suresh B 	if (count & (count - 1))
7494605effSSiddha, Suresh B 		order++;
7594605effSSiddha, Suresh B 	return order;
7694605effSSiddha, Suresh B }
7794605effSSiddha, Suresh B 
781da177e4SLinus Torvalds static inline unsigned long hweight_long(unsigned long w)
791da177e4SLinus Torvalds {
80e9bebd6fSAkinobu Mita 	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
811da177e4SLinus Torvalds }
829f41699eSPeter Zijlstra 
8345f8bde0SRobert P. J. Day /**
84f2ea0f5fSAlexey Dobriyan  * rol64 - rotate a 64-bit value left
85f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
86f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
87f2ea0f5fSAlexey Dobriyan  */
88f2ea0f5fSAlexey Dobriyan static inline __u64 rol64(__u64 word, unsigned int shift)
89f2ea0f5fSAlexey Dobriyan {
90f2ea0f5fSAlexey Dobriyan 	return (word << shift) | (word >> (64 - shift));
91f2ea0f5fSAlexey Dobriyan }
92f2ea0f5fSAlexey Dobriyan 
93f2ea0f5fSAlexey Dobriyan /**
94f2ea0f5fSAlexey Dobriyan  * ror64 - rotate a 64-bit value right
95f2ea0f5fSAlexey Dobriyan  * @word: value to rotate
96f2ea0f5fSAlexey Dobriyan  * @shift: bits to roll
97f2ea0f5fSAlexey Dobriyan  */
98f2ea0f5fSAlexey Dobriyan static inline __u64 ror64(__u64 word, unsigned int shift)
99f2ea0f5fSAlexey Dobriyan {
100f2ea0f5fSAlexey Dobriyan 	return (word >> shift) | (word << (64 - shift));
101f2ea0f5fSAlexey Dobriyan }
102f2ea0f5fSAlexey Dobriyan 
103f2ea0f5fSAlexey Dobriyan /**
1041da177e4SLinus Torvalds  * rol32 - rotate a 32-bit value left
1051da177e4SLinus Torvalds  * @word: value to rotate
1061da177e4SLinus Torvalds  * @shift: bits to roll
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds static inline __u32 rol32(__u32 word, unsigned int shift)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	return (word << shift) | (word >> (32 - shift));
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
11345f8bde0SRobert P. J. Day /**
1141da177e4SLinus Torvalds  * ror32 - rotate a 32-bit value right
1151da177e4SLinus Torvalds  * @word: value to rotate
1161da177e4SLinus Torvalds  * @shift: bits to roll
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds static inline __u32 ror32(__u32 word, unsigned int shift)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	return (word >> shift) | (word << (32 - shift));
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1233afe3925SHarvey Harrison /**
1243afe3925SHarvey Harrison  * rol16 - rotate a 16-bit value left
1253afe3925SHarvey Harrison  * @word: value to rotate
1263afe3925SHarvey Harrison  * @shift: bits to roll
1273afe3925SHarvey Harrison  */
1283afe3925SHarvey Harrison static inline __u16 rol16(__u16 word, unsigned int shift)
1293afe3925SHarvey Harrison {
1303afe3925SHarvey Harrison 	return (word << shift) | (word >> (16 - shift));
1313afe3925SHarvey Harrison }
1323afe3925SHarvey Harrison 
1333afe3925SHarvey Harrison /**
1343afe3925SHarvey Harrison  * ror16 - rotate a 16-bit value right
1353afe3925SHarvey Harrison  * @word: value to rotate
1363afe3925SHarvey Harrison  * @shift: bits to roll
1373afe3925SHarvey Harrison  */
1383afe3925SHarvey Harrison static inline __u16 ror16(__u16 word, unsigned int shift)
1393afe3925SHarvey Harrison {
1403afe3925SHarvey Harrison 	return (word >> shift) | (word << (16 - shift));
1413afe3925SHarvey Harrison }
1423afe3925SHarvey Harrison 
1433afe3925SHarvey Harrison /**
1443afe3925SHarvey Harrison  * rol8 - rotate an 8-bit value left
1453afe3925SHarvey Harrison  * @word: value to rotate
1463afe3925SHarvey Harrison  * @shift: bits to roll
1473afe3925SHarvey Harrison  */
1483afe3925SHarvey Harrison static inline __u8 rol8(__u8 word, unsigned int shift)
1493afe3925SHarvey Harrison {
1503afe3925SHarvey Harrison 	return (word << shift) | (word >> (8 - shift));
1513afe3925SHarvey Harrison }
1523afe3925SHarvey Harrison 
1533afe3925SHarvey Harrison /**
1543afe3925SHarvey Harrison  * ror8 - rotate an 8-bit value right
1553afe3925SHarvey Harrison  * @word: value to rotate
1563afe3925SHarvey Harrison  * @shift: bits to roll
1573afe3925SHarvey Harrison  */
1583afe3925SHarvey Harrison static inline __u8 ror8(__u8 word, unsigned int shift)
1593afe3925SHarvey Harrison {
1603afe3925SHarvey Harrison 	return (word >> shift) | (word << (8 - shift));
1613afe3925SHarvey Harrison }
1623afe3925SHarvey Harrison 
1637919a57bSAndreas Herrmann /**
1647919a57bSAndreas Herrmann  * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
1657919a57bSAndreas Herrmann  * @value: value to sign extend
1667919a57bSAndreas Herrmann  * @index: 0 based bit index (0<=index<32) to sign bit
1677919a57bSAndreas Herrmann  */
1687919a57bSAndreas Herrmann static inline __s32 sign_extend32(__u32 value, int index)
1697919a57bSAndreas Herrmann {
1707919a57bSAndreas Herrmann 	__u8 shift = 31 - index;
1717919a57bSAndreas Herrmann 	return (__s32)(value << shift) >> shift;
1727919a57bSAndreas Herrmann }
1737919a57bSAndreas Herrmann 
174962749afSAndrew Morton static inline unsigned fls_long(unsigned long l)
175962749afSAndrew Morton {
176962749afSAndrew Morton 	if (sizeof(l) == 4)
177962749afSAndrew Morton 		return fls(l);
178962749afSAndrew Morton 	return fls64(l);
179962749afSAndrew Morton }
180962749afSAndrew Morton 
181952043acSSteven Whitehouse /**
182952043acSSteven Whitehouse  * __ffs64 - find first set bit in a 64 bit word
183952043acSSteven Whitehouse  * @word: The 64 bit word
184952043acSSteven Whitehouse  *
185952043acSSteven Whitehouse  * On 64 bit arches this is a synomyn for __ffs
186952043acSSteven Whitehouse  * The result is not defined if no bits are set, so check that @word
187952043acSSteven Whitehouse  * is non-zero before calling this.
188952043acSSteven Whitehouse  */
189952043acSSteven Whitehouse static inline unsigned long __ffs64(u64 word)
190952043acSSteven Whitehouse {
191952043acSSteven Whitehouse #if BITS_PER_LONG == 32
192952043acSSteven Whitehouse 	if (((u32)word) == 0UL)
193952043acSSteven Whitehouse 		return __ffs((u32)(word >> 32)) + 32;
194952043acSSteven Whitehouse #elif BITS_PER_LONG != 64
195952043acSSteven Whitehouse #error BITS_PER_LONG not 32 or 64
196952043acSSteven Whitehouse #endif
197952043acSSteven Whitehouse 	return __ffs((unsigned long)word);
198952043acSSteven Whitehouse }
199952043acSSteven Whitehouse 
20064970b68SAlexander van Heukelum #ifdef __KERNEL__
20177b9bd9cSAlexander van Heukelum 
20200a1a053STheodore Ts'o #ifndef set_mask_bits
20300a1a053STheodore Ts'o #define set_mask_bits(ptr, _mask, _bits)	\
20400a1a053STheodore Ts'o ({								\
20500a1a053STheodore Ts'o 	const typeof(*ptr) mask = (_mask), bits = (_bits);	\
20600a1a053STheodore Ts'o 	typeof(*ptr) old, new;					\
20700a1a053STheodore Ts'o 								\
20800a1a053STheodore Ts'o 	do {							\
20900a1a053STheodore Ts'o 		old = ACCESS_ONCE(*ptr);			\
21000a1a053STheodore Ts'o 		new = (old & ~mask) | bits;			\
21100a1a053STheodore Ts'o 	} while (cmpxchg(ptr, old, new) != old);		\
21200a1a053STheodore Ts'o 								\
21300a1a053STheodore Ts'o 	new;							\
21400a1a053STheodore Ts'o })
21500a1a053STheodore Ts'o #endif
21600a1a053STheodore Ts'o 
21719de85efSAkinobu Mita #ifndef find_last_bit
218ab53d472SRusty Russell /**
219ab53d472SRusty Russell  * find_last_bit - find the last set bit in a memory region
220ab53d472SRusty Russell  * @addr: The address to start the search at
221*2c57a0e2SYury Norov  * @size: The number of bits to search
222ab53d472SRusty Russell  *
223*2c57a0e2SYury Norov  * Returns the bit number of the last set bit, or size.
224ab53d472SRusty Russell  */
225ab53d472SRusty Russell extern unsigned long find_last_bit(const unsigned long *addr,
226ab53d472SRusty Russell 				   unsigned long size);
22719de85efSAkinobu Mita #endif
228ab53d472SRusty Russell 
22964970b68SAlexander van Heukelum #endif /* __KERNEL__ */
2301da177e4SLinus Torvalds #endif
231