bitops.h 6.7 KB

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