bitops.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _LINUX_BITOPS_H
  2. #define _LINUX_BITOPS_H
  3. #include <asm/types.h>
  4. #ifdef __KERNEL__
  5. #define BIT(nr) (1UL << (nr))
  6. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  7. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  8. #define BITS_PER_BYTE 8
  9. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  10. #endif
  11. /*
  12. * Create a contiguous bitmask starting at bit position @l and ending at
  13. * position @h. For example
  14. * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  15. */
  16. #define GENMASK(h, l) (((U32_C(1) << ((h) - (l) + 1)) - 1) << (l))
  17. #define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
  18. extern unsigned int __sw_hweight8(unsigned int w);
  19. extern unsigned int __sw_hweight16(unsigned int w);
  20. extern unsigned int __sw_hweight32(unsigned int w);
  21. extern unsigned long __sw_hweight64(__u64 w);
  22. /*
  23. * Include this here because some architectures need generic_ffs/fls in
  24. * scope
  25. */
  26. #include <asm/bitops.h>
  27. #define for_each_set_bit(bit, addr, size) \
  28. for ((bit) = find_first_bit((addr), (size)); \
  29. (bit) < (size); \
  30. (bit) = find_next_bit((addr), (size), (bit) + 1))
  31. /* same as for_each_set_bit() but use bit as value to start with */
  32. #define for_each_set_bit_from(bit, addr, size) \
  33. for ((bit) = find_next_bit((addr), (size), (bit)); \
  34. (bit) < (size); \
  35. (bit) = find_next_bit((addr), (size), (bit) + 1))
  36. #define for_each_clear_bit(bit, addr, size) \
  37. for ((bit) = find_first_zero_bit((addr), (size)); \
  38. (bit) < (size); \
  39. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  40. /* same as for_each_clear_bit() but use bit as value to start with */
  41. #define for_each_clear_bit_from(bit, addr, size) \
  42. for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
  43. (bit) < (size); \
  44. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  45. static __inline__ int get_bitmask_order(unsigned int count)
  46. {
  47. int order;
  48. order = fls(count);
  49. return order; /* We could be slightly more clever with -1 here... */
  50. }
  51. static __inline__ int get_count_order(unsigned int count)
  52. {
  53. int order;
  54. order = fls(count) - 1;
  55. if (count & (count - 1))
  56. order++;
  57. return order;
  58. }
  59. static inline unsigned long hweight_long(unsigned long w)
  60. {
  61. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  62. }
  63. /**
  64. * rol64 - rotate a 64-bit value left
  65. * @word: value to rotate
  66. * @shift: bits to roll
  67. */
  68. static inline __u64 rol64(__u64 word, unsigned int shift)
  69. {
  70. return (word << shift) | (word >> (64 - shift));
  71. }
  72. /**
  73. * ror64 - rotate a 64-bit value right
  74. * @word: value to rotate
  75. * @shift: bits to roll
  76. */
  77. static inline __u64 ror64(__u64 word, unsigned int shift)
  78. {
  79. return (word >> shift) | (word << (64 - shift));
  80. }
  81. /**
  82. * rol32 - rotate a 32-bit value left
  83. * @word: value to rotate
  84. * @shift: bits to roll
  85. */
  86. static inline __u32 rol32(__u32 word, unsigned int shift)
  87. {
  88. return (word << shift) | (word >> (32 - shift));
  89. }
  90. /**
  91. * ror32 - rotate a 32-bit value right
  92. * @word: value to rotate
  93. * @shift: bits to roll
  94. */
  95. static inline __u32 ror32(__u32 word, unsigned int shift)
  96. {
  97. return (word >> shift) | (word << (32 - shift));
  98. }
  99. /**
  100. * rol16 - rotate a 16-bit value left
  101. * @word: value to rotate
  102. * @shift: bits to roll
  103. */
  104. static inline __u16 rol16(__u16 word, unsigned int shift)
  105. {
  106. return (word << shift) | (word >> (16 - shift));
  107. }
  108. /**
  109. * ror16 - rotate a 16-bit value right
  110. * @word: value to rotate
  111. * @shift: bits to roll
  112. */
  113. static inline __u16 ror16(__u16 word, unsigned int shift)
  114. {
  115. return (word >> shift) | (word << (16 - shift));
  116. }
  117. /**
  118. * rol8 - rotate an 8-bit value left
  119. * @word: value to rotate
  120. * @shift: bits to roll
  121. */
  122. static inline __u8 rol8(__u8 word, unsigned int shift)
  123. {
  124. return (word << shift) | (word >> (8 - shift));
  125. }
  126. /**
  127. * ror8 - rotate an 8-bit value right
  128. * @word: value to rotate
  129. * @shift: bits to roll
  130. */
  131. static inline __u8 ror8(__u8 word, unsigned int shift)
  132. {
  133. return (word >> shift) | (word << (8 - shift));
  134. }
  135. /**
  136. * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
  137. * @value: value to sign extend
  138. * @index: 0 based bit index (0<=index<32) to sign bit
  139. */
  140. static inline __s32 sign_extend32(__u32 value, int index)
  141. {
  142. __u8 shift = 31 - index;
  143. return (__s32)(value << shift) >> shift;
  144. }
  145. static inline unsigned fls_long(unsigned long l)
  146. {
  147. if (sizeof(l) == 4)
  148. return fls(l);
  149. return fls64(l);
  150. }
  151. /**
  152. * __ffs64 - find first set bit in a 64 bit word
  153. * @word: The 64 bit word
  154. *
  155. * On 64 bit arches this is a synomyn for __ffs
  156. * The result is not defined if no bits are set, so check that @word
  157. * is non-zero before calling this.
  158. */
  159. static inline unsigned long __ffs64(u64 word)
  160. {
  161. #if BITS_PER_LONG == 32
  162. if (((u32)word) == 0UL)
  163. return __ffs((u32)(word >> 32)) + 32;
  164. #elif BITS_PER_LONG != 64
  165. #error BITS_PER_LONG not 32 or 64
  166. #endif
  167. return __ffs((unsigned long)word);
  168. }
  169. #ifdef __KERNEL__
  170. #ifndef find_last_bit
  171. /**
  172. * find_last_bit - find the last set bit in a memory region
  173. * @addr: The address to start the search at
  174. * @size: The maximum size to search
  175. *
  176. * Returns the bit number of the first set bit, or size.
  177. */
  178. extern unsigned long find_last_bit(const unsigned long *addr,
  179. unsigned long size);
  180. #endif
  181. #endif /* __KERNEL__ */
  182. #endif