bitops.h 5.6 KB

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