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)) 7*bfd1ff63SSrinivas 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) 10*bfd1ff63SSrinivas Pandruvada #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG)) 11*bfd1ff63SSrinivas 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 164677d4a5SBorislav Petkov extern unsigned int __sw_hweight8(unsigned int w); 174677d4a5SBorislav Petkov extern unsigned int __sw_hweight16(unsigned int w); 184677d4a5SBorislav Petkov extern unsigned int __sw_hweight32(unsigned int w); 194677d4a5SBorislav Petkov extern unsigned long __sw_hweight64(__u64 w); 204677d4a5SBorislav Petkov 211da177e4SLinus Torvalds /* 221da177e4SLinus Torvalds * Include this here because some architectures need generic_ffs/fls in 231da177e4SLinus Torvalds * scope 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds #include <asm/bitops.h> 261da177e4SLinus Torvalds 27984b3f57SAkinobu Mita #define for_each_set_bit(bit, addr, size) \ 283e037454SShannon Nelson for ((bit) = find_first_bit((addr), (size)); \ 293e037454SShannon Nelson (bit) < (size); \ 301e2ad28fSRobert Richter (bit) = find_next_bit((addr), (size), (bit) + 1)) 311e2ad28fSRobert Richter 321e2ad28fSRobert Richter /* same as for_each_set_bit() but use bit as value to start with */ 33307b1cd7SAkinobu Mita #define for_each_set_bit_from(bit, addr, size) \ 341e2ad28fSRobert Richter for ((bit) = find_next_bit((addr), (size), (bit)); \ 351e2ad28fSRobert Richter (bit) < (size); \ 363e037454SShannon Nelson (bit) = find_next_bit((addr), (size), (bit) + 1)) 373e037454SShannon Nelson 3803f4a822SAkinobu Mita #define for_each_clear_bit(bit, addr, size) \ 3903f4a822SAkinobu Mita for ((bit) = find_first_zero_bit((addr), (size)); \ 4003f4a822SAkinobu Mita (bit) < (size); \ 4103f4a822SAkinobu Mita (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) 4203f4a822SAkinobu Mita 4303f4a822SAkinobu Mita /* same as for_each_clear_bit() but use bit as value to start with */ 4403f4a822SAkinobu Mita #define for_each_clear_bit_from(bit, addr, size) \ 4503f4a822SAkinobu Mita for ((bit) = find_next_zero_bit((addr), (size), (bit)); \ 4603f4a822SAkinobu Mita (bit) < (size); \ 4703f4a822SAkinobu Mita (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) 4803f4a822SAkinobu Mita 491da177e4SLinus Torvalds static __inline__ int get_bitmask_order(unsigned int count) 501da177e4SLinus Torvalds { 511da177e4SLinus Torvalds int order; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds order = fls(count); 541da177e4SLinus Torvalds return order; /* We could be slightly more clever with -1 here... */ 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 5794605effSSiddha, Suresh B static __inline__ int get_count_order(unsigned int count) 5894605effSSiddha, Suresh B { 5994605effSSiddha, Suresh B int order; 6094605effSSiddha, Suresh B 6194605effSSiddha, Suresh B order = fls(count) - 1; 6294605effSSiddha, Suresh B if (count & (count - 1)) 6394605effSSiddha, Suresh B order++; 6494605effSSiddha, Suresh B return order; 6594605effSSiddha, Suresh B } 6694605effSSiddha, Suresh B 671da177e4SLinus Torvalds static inline unsigned long hweight_long(unsigned long w) 681da177e4SLinus Torvalds { 69e9bebd6fSAkinobu Mita return sizeof(w) == 4 ? hweight32(w) : hweight64(w); 701da177e4SLinus Torvalds } 719f41699eSPeter Zijlstra 7245f8bde0SRobert P. J. Day /** 73f2ea0f5fSAlexey Dobriyan * rol64 - rotate a 64-bit value left 74f2ea0f5fSAlexey Dobriyan * @word: value to rotate 75f2ea0f5fSAlexey Dobriyan * @shift: bits to roll 76f2ea0f5fSAlexey Dobriyan */ 77f2ea0f5fSAlexey Dobriyan static inline __u64 rol64(__u64 word, unsigned int shift) 78f2ea0f5fSAlexey Dobriyan { 79f2ea0f5fSAlexey Dobriyan return (word << shift) | (word >> (64 - shift)); 80f2ea0f5fSAlexey Dobriyan } 81f2ea0f5fSAlexey Dobriyan 82f2ea0f5fSAlexey Dobriyan /** 83f2ea0f5fSAlexey Dobriyan * ror64 - rotate a 64-bit value right 84f2ea0f5fSAlexey Dobriyan * @word: value to rotate 85f2ea0f5fSAlexey Dobriyan * @shift: bits to roll 86f2ea0f5fSAlexey Dobriyan */ 87f2ea0f5fSAlexey Dobriyan static inline __u64 ror64(__u64 word, unsigned int shift) 88f2ea0f5fSAlexey Dobriyan { 89f2ea0f5fSAlexey Dobriyan return (word >> shift) | (word << (64 - shift)); 90f2ea0f5fSAlexey Dobriyan } 91f2ea0f5fSAlexey Dobriyan 92f2ea0f5fSAlexey Dobriyan /** 931da177e4SLinus Torvalds * rol32 - rotate a 32-bit value left 941da177e4SLinus Torvalds * @word: value to rotate 951da177e4SLinus Torvalds * @shift: bits to roll 961da177e4SLinus Torvalds */ 971da177e4SLinus Torvalds static inline __u32 rol32(__u32 word, unsigned int shift) 981da177e4SLinus Torvalds { 991da177e4SLinus Torvalds return (word << shift) | (word >> (32 - shift)); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 10245f8bde0SRobert P. J. Day /** 1031da177e4SLinus Torvalds * ror32 - rotate a 32-bit value right 1041da177e4SLinus Torvalds * @word: value to rotate 1051da177e4SLinus Torvalds * @shift: bits to roll 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds static inline __u32 ror32(__u32 word, unsigned int shift) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds return (word >> shift) | (word << (32 - shift)); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1123afe3925SHarvey Harrison /** 1133afe3925SHarvey Harrison * rol16 - rotate a 16-bit value left 1143afe3925SHarvey Harrison * @word: value to rotate 1153afe3925SHarvey Harrison * @shift: bits to roll 1163afe3925SHarvey Harrison */ 1173afe3925SHarvey Harrison static inline __u16 rol16(__u16 word, unsigned int shift) 1183afe3925SHarvey Harrison { 1193afe3925SHarvey Harrison return (word << shift) | (word >> (16 - shift)); 1203afe3925SHarvey Harrison } 1213afe3925SHarvey Harrison 1223afe3925SHarvey Harrison /** 1233afe3925SHarvey Harrison * ror16 - rotate a 16-bit value right 1243afe3925SHarvey Harrison * @word: value to rotate 1253afe3925SHarvey Harrison * @shift: bits to roll 1263afe3925SHarvey Harrison */ 1273afe3925SHarvey Harrison static inline __u16 ror16(__u16 word, unsigned int shift) 1283afe3925SHarvey Harrison { 1293afe3925SHarvey Harrison return (word >> shift) | (word << (16 - shift)); 1303afe3925SHarvey Harrison } 1313afe3925SHarvey Harrison 1323afe3925SHarvey Harrison /** 1333afe3925SHarvey Harrison * rol8 - rotate an 8-bit value left 1343afe3925SHarvey Harrison * @word: value to rotate 1353afe3925SHarvey Harrison * @shift: bits to roll 1363afe3925SHarvey Harrison */ 1373afe3925SHarvey Harrison static inline __u8 rol8(__u8 word, unsigned int shift) 1383afe3925SHarvey Harrison { 1393afe3925SHarvey Harrison return (word << shift) | (word >> (8 - shift)); 1403afe3925SHarvey Harrison } 1413afe3925SHarvey Harrison 1423afe3925SHarvey Harrison /** 1433afe3925SHarvey Harrison * ror8 - rotate an 8-bit value right 1443afe3925SHarvey Harrison * @word: value to rotate 1453afe3925SHarvey Harrison * @shift: bits to roll 1463afe3925SHarvey Harrison */ 1473afe3925SHarvey Harrison static inline __u8 ror8(__u8 word, unsigned int shift) 1483afe3925SHarvey Harrison { 1493afe3925SHarvey Harrison return (word >> shift) | (word << (8 - shift)); 1503afe3925SHarvey Harrison } 1513afe3925SHarvey Harrison 1527919a57bSAndreas Herrmann /** 1537919a57bSAndreas Herrmann * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit 1547919a57bSAndreas Herrmann * @value: value to sign extend 1557919a57bSAndreas Herrmann * @index: 0 based bit index (0<=index<32) to sign bit 1567919a57bSAndreas Herrmann */ 1577919a57bSAndreas Herrmann static inline __s32 sign_extend32(__u32 value, int index) 1587919a57bSAndreas Herrmann { 1597919a57bSAndreas Herrmann __u8 shift = 31 - index; 1607919a57bSAndreas Herrmann return (__s32)(value << shift) >> shift; 1617919a57bSAndreas Herrmann } 1627919a57bSAndreas Herrmann 163962749afSAndrew Morton static inline unsigned fls_long(unsigned long l) 164962749afSAndrew Morton { 165962749afSAndrew Morton if (sizeof(l) == 4) 166962749afSAndrew Morton return fls(l); 167962749afSAndrew Morton return fls64(l); 168962749afSAndrew Morton } 169962749afSAndrew Morton 170952043acSSteven Whitehouse /** 171952043acSSteven Whitehouse * __ffs64 - find first set bit in a 64 bit word 172952043acSSteven Whitehouse * @word: The 64 bit word 173952043acSSteven Whitehouse * 174952043acSSteven Whitehouse * On 64 bit arches this is a synomyn for __ffs 175952043acSSteven Whitehouse * The result is not defined if no bits are set, so check that @word 176952043acSSteven Whitehouse * is non-zero before calling this. 177952043acSSteven Whitehouse */ 178952043acSSteven Whitehouse static inline unsigned long __ffs64(u64 word) 179952043acSSteven Whitehouse { 180952043acSSteven Whitehouse #if BITS_PER_LONG == 32 181952043acSSteven Whitehouse if (((u32)word) == 0UL) 182952043acSSteven Whitehouse return __ffs((u32)(word >> 32)) + 32; 183952043acSSteven Whitehouse #elif BITS_PER_LONG != 64 184952043acSSteven Whitehouse #error BITS_PER_LONG not 32 or 64 185952043acSSteven Whitehouse #endif 186952043acSSteven Whitehouse return __ffs((unsigned long)word); 187952043acSSteven Whitehouse } 188952043acSSteven Whitehouse 18964970b68SAlexander van Heukelum #ifdef __KERNEL__ 19077b9bd9cSAlexander van Heukelum 19119de85efSAkinobu Mita #ifndef find_last_bit 192ab53d472SRusty Russell /** 193ab53d472SRusty Russell * find_last_bit - find the last set bit in a memory region 194ab53d472SRusty Russell * @addr: The address to start the search at 195ab53d472SRusty Russell * @size: The maximum size to search 196ab53d472SRusty Russell * 197ab53d472SRusty Russell * Returns the bit number of the first set bit, or size. 198ab53d472SRusty Russell */ 199ab53d472SRusty Russell extern unsigned long find_last_bit(const unsigned long *addr, 200ab53d472SRusty Russell unsigned long size); 20119de85efSAkinobu Mita #endif 202ab53d472SRusty Russell 20364970b68SAlexander van Heukelum #endif /* __KERNEL__ */ 2041da177e4SLinus Torvalds #endif 205