bitops.h 7.2 KB

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