bitops.h 5.6 KB

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