bitops.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 __always_inline unsigned long hweight_long(unsigned long w)
  57. {
  58. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  59. }
  60. /**
  61. * rol64 - rotate a 64-bit value left
  62. * @word: value to rotate
  63. * @shift: bits to roll
  64. */
  65. static inline __u64 rol64(__u64 word, unsigned int shift)
  66. {
  67. return (word << shift) | (word >> (64 - shift));
  68. }
  69. /**
  70. * ror64 - rotate a 64-bit value right
  71. * @word: value to rotate
  72. * @shift: bits to roll
  73. */
  74. static inline __u64 ror64(__u64 word, unsigned int shift)
  75. {
  76. return (word >> shift) | (word << (64 - shift));
  77. }
  78. /**
  79. * rol32 - rotate a 32-bit value left
  80. * @word: value to rotate
  81. * @shift: bits to roll
  82. */
  83. static inline __u32 rol32(__u32 word, unsigned int shift)
  84. {
  85. return (word << shift) | (word >> ((-shift) & 31));
  86. }
  87. /**
  88. * ror32 - rotate a 32-bit value right
  89. * @word: value to rotate
  90. * @shift: bits to roll
  91. */
  92. static inline __u32 ror32(__u32 word, unsigned int shift)
  93. {
  94. return (word >> shift) | (word << (32 - shift));
  95. }
  96. /**
  97. * rol16 - rotate a 16-bit value left
  98. * @word: value to rotate
  99. * @shift: bits to roll
  100. */
  101. static inline __u16 rol16(__u16 word, unsigned int shift)
  102. {
  103. return (word << shift) | (word >> (16 - shift));
  104. }
  105. /**
  106. * ror16 - rotate a 16-bit value right
  107. * @word: value to rotate
  108. * @shift: bits to roll
  109. */
  110. static inline __u16 ror16(__u16 word, unsigned int shift)
  111. {
  112. return (word >> shift) | (word << (16 - shift));
  113. }
  114. /**
  115. * rol8 - rotate an 8-bit value left
  116. * @word: value to rotate
  117. * @shift: bits to roll
  118. */
  119. static inline __u8 rol8(__u8 word, unsigned int shift)
  120. {
  121. return (word << shift) | (word >> (8 - shift));
  122. }
  123. /**
  124. * ror8 - rotate an 8-bit value right
  125. * @word: value to rotate
  126. * @shift: bits to roll
  127. */
  128. static inline __u8 ror8(__u8 word, unsigned int shift)
  129. {
  130. return (word >> shift) | (word << (8 - shift));
  131. }
  132. /**
  133. * sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
  134. * @value: value to sign extend
  135. * @index: 0 based bit index (0<=index<32) to sign bit
  136. *
  137. * This is safe to use for 16- and 8-bit types as well.
  138. */
  139. static inline __s32 sign_extend32(__u32 value, int index)
  140. {
  141. __u8 shift = 31 - index;
  142. return (__s32)(value << shift) >> shift;
  143. }
  144. /**
  145. * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
  146. * @value: value to sign extend
  147. * @index: 0 based bit index (0<=index<64) to sign bit
  148. */
  149. static inline __s64 sign_extend64(__u64 value, int index)
  150. {
  151. __u8 shift = 63 - index;
  152. return (__s64)(value << shift) >> shift;
  153. }
  154. static inline unsigned fls_long(unsigned long l)
  155. {
  156. if (sizeof(l) == 4)
  157. return fls(l);
  158. return fls64(l);
  159. }
  160. static inline int get_count_order(unsigned int count)
  161. {
  162. int order;
  163. order = fls(count) - 1;
  164. if (count & (count - 1))
  165. order++;
  166. return order;
  167. }
  168. /**
  169. * get_count_order_long - get order after rounding @l up to power of 2
  170. * @l: parameter
  171. *
  172. * it is same as get_count_order() but with long type parameter
  173. */
  174. static inline int get_count_order_long(unsigned long l)
  175. {
  176. if (l == 0UL)
  177. return -1;
  178. else if (l & (l - 1UL))
  179. return (int)fls_long(l);
  180. else
  181. return (int)fls_long(l) - 1;
  182. }
  183. /**
  184. * __ffs64 - find first set bit in a 64 bit word
  185. * @word: The 64 bit word
  186. *
  187. * On 64 bit arches this is a synomyn for __ffs
  188. * The result is not defined if no bits are set, so check that @word
  189. * is non-zero before calling this.
  190. */
  191. static inline unsigned long __ffs64(u64 word)
  192. {
  193. #if BITS_PER_LONG == 32
  194. if (((u32)word) == 0UL)
  195. return __ffs((u32)(word >> 32)) + 32;
  196. #elif BITS_PER_LONG != 64
  197. #error BITS_PER_LONG not 32 or 64
  198. #endif
  199. return __ffs((unsigned long)word);
  200. }
  201. #ifdef __KERNEL__
  202. #ifndef set_mask_bits
  203. #define set_mask_bits(ptr, _mask, _bits) \
  204. ({ \
  205. const typeof(*ptr) mask = (_mask), bits = (_bits); \
  206. typeof(*ptr) old, new; \
  207. \
  208. do { \
  209. old = ACCESS_ONCE(*ptr); \
  210. new = (old & ~mask) | bits; \
  211. } while (cmpxchg(ptr, old, new) != old); \
  212. \
  213. new; \
  214. })
  215. #endif
  216. #ifndef bit_clear_unless
  217. #define bit_clear_unless(ptr, _clear, _test) \
  218. ({ \
  219. const typeof(*ptr) clear = (_clear), test = (_test); \
  220. typeof(*ptr) old, new; \
  221. \
  222. do { \
  223. old = ACCESS_ONCE(*ptr); \
  224. new = old & ~clear; \
  225. } while (!(old & test) && \
  226. cmpxchg(ptr, old, new) != old); \
  227. \
  228. !(old & test); \
  229. })
  230. #endif
  231. #ifndef find_last_bit
  232. /**
  233. * find_last_bit - find the last set bit in a memory region
  234. * @addr: The address to start the search at
  235. * @size: The number of bits to search
  236. *
  237. * Returns the bit number of the last set bit, or size.
  238. */
  239. extern unsigned long find_last_bit(const unsigned long *addr,
  240. unsigned long size);
  241. #endif
  242. #endif /* __KERNEL__ */
  243. #endif