bitops.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  7. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  8. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
  9. #define BITS_PER_BYTE 8
  10. #endif
  11. /*
  12. * Include this here because some architectures need generic_ffs/fls in
  13. * scope
  14. */
  15. #include <asm/bitops.h>
  16. #define for_each_bit(bit, addr, size) \
  17. for ((bit) = find_first_bit((addr), (size)); \
  18. (bit) < (size); \
  19. (bit) = find_next_bit((addr), (size), (bit) + 1))
  20. static __inline__ int get_bitmask_order(unsigned int count)
  21. {
  22. int order;
  23. order = fls(count);
  24. return order; /* We could be slightly more clever with -1 here... */
  25. }
  26. static __inline__ int get_count_order(unsigned int count)
  27. {
  28. int order;
  29. order = fls(count) - 1;
  30. if (count & (count - 1))
  31. order++;
  32. return order;
  33. }
  34. static inline unsigned long hweight_long(unsigned long w)
  35. {
  36. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  37. }
  38. /**
  39. * rol32 - rotate a 32-bit value left
  40. * @word: value to rotate
  41. * @shift: bits to roll
  42. */
  43. static inline __u32 rol32(__u32 word, unsigned int shift)
  44. {
  45. return (word << shift) | (word >> (32 - shift));
  46. }
  47. /**
  48. * ror32 - rotate a 32-bit value right
  49. * @word: value to rotate
  50. * @shift: bits to roll
  51. */
  52. static inline __u32 ror32(__u32 word, unsigned int shift)
  53. {
  54. return (word >> shift) | (word << (32 - shift));
  55. }
  56. /**
  57. * rol16 - rotate a 16-bit value left
  58. * @word: value to rotate
  59. * @shift: bits to roll
  60. */
  61. static inline __u16 rol16(__u16 word, unsigned int shift)
  62. {
  63. return (word << shift) | (word >> (16 - shift));
  64. }
  65. /**
  66. * ror16 - rotate a 16-bit value right
  67. * @word: value to rotate
  68. * @shift: bits to roll
  69. */
  70. static inline __u16 ror16(__u16 word, unsigned int shift)
  71. {
  72. return (word >> shift) | (word << (16 - shift));
  73. }
  74. /**
  75. * rol8 - rotate an 8-bit value left
  76. * @word: value to rotate
  77. * @shift: bits to roll
  78. */
  79. static inline __u8 rol8(__u8 word, unsigned int shift)
  80. {
  81. return (word << shift) | (word >> (8 - shift));
  82. }
  83. /**
  84. * ror8 - rotate an 8-bit value right
  85. * @word: value to rotate
  86. * @shift: bits to roll
  87. */
  88. static inline __u8 ror8(__u8 word, unsigned int shift)
  89. {
  90. return (word >> shift) | (word << (8 - shift));
  91. }
  92. static inline unsigned fls_long(unsigned long l)
  93. {
  94. if (sizeof(l) == 4)
  95. return fls(l);
  96. return fls64(l);
  97. }
  98. #ifdef __KERNEL__
  99. #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
  100. extern unsigned long __find_first_bit(const unsigned long *addr,
  101. unsigned long size);
  102. /**
  103. * find_first_bit - find the first set bit in a memory region
  104. * @addr: The address to start the search at
  105. * @size: The maximum size to search
  106. *
  107. * Returns the bit number of the first set bit.
  108. */
  109. static __always_inline unsigned long
  110. find_first_bit(const unsigned long *addr, unsigned long size)
  111. {
  112. /* Avoid a function call if the bitmap size is a constant */
  113. /* and not bigger than BITS_PER_LONG. */
  114. /* insert a sentinel so that __ffs returns size if there */
  115. /* are no set bits in the bitmap */
  116. if (__builtin_constant_p(size) && (size < BITS_PER_LONG))
  117. return __ffs((*addr) | (1ul << size));
  118. /* the result of __ffs(0) is undefined, so it needs to be */
  119. /* handled separately */
  120. if (__builtin_constant_p(size) && (size == BITS_PER_LONG))
  121. return ((*addr) == 0) ? BITS_PER_LONG : __ffs(*addr);
  122. /* size is not constant or too big */
  123. return __find_first_bit(addr, size);
  124. }
  125. extern unsigned long __find_first_zero_bit(const unsigned long *addr,
  126. unsigned long size);
  127. /**
  128. * find_first_zero_bit - find the first cleared bit in a memory region
  129. * @addr: The address to start the search at
  130. * @size: The maximum size to search
  131. *
  132. * Returns the bit number of the first cleared bit.
  133. */
  134. static __always_inline unsigned long
  135. find_first_zero_bit(const unsigned long *addr, unsigned long size)
  136. {
  137. /* Avoid a function call if the bitmap size is a constant */
  138. /* and not bigger than BITS_PER_LONG. */
  139. /* insert a sentinel so that __ffs returns size if there */
  140. /* are no set bits in the bitmap */
  141. if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
  142. return __ffs(~(*addr) | (1ul << size));
  143. }
  144. /* the result of __ffs(0) is undefined, so it needs to be */
  145. /* handled separately */
  146. if (__builtin_constant_p(size) && (size == BITS_PER_LONG))
  147. return (~(*addr) == 0) ? BITS_PER_LONG : __ffs(~(*addr));
  148. /* size is not constant or too big */
  149. return __find_first_zero_bit(addr, size);
  150. }
  151. #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
  152. #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
  153. extern unsigned long __find_next_bit(const unsigned long *addr,
  154. unsigned long size, unsigned long offset);
  155. /**
  156. * find_next_bit - find the next set bit in a memory region
  157. * @addr: The address to base the search on
  158. * @offset: The bitnumber to start searching at
  159. * @size: The bitmap size in bits
  160. */
  161. static __always_inline unsigned long
  162. find_next_bit(const unsigned long *addr, unsigned long size,
  163. unsigned long offset)
  164. {
  165. unsigned long value;
  166. /* Avoid a function call if the bitmap size is a constant */
  167. /* and not bigger than BITS_PER_LONG. */
  168. /* insert a sentinel so that __ffs returns size if there */
  169. /* are no set bits in the bitmap */
  170. if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
  171. value = (*addr) & ((~0ul) << offset);
  172. value |= (1ul << size);
  173. return __ffs(value);
  174. }
  175. /* the result of __ffs(0) is undefined, so it needs to be */
  176. /* handled separately */
  177. if (__builtin_constant_p(size) && (size == BITS_PER_LONG)) {
  178. value = (*addr) & ((~0ul) << offset);
  179. return (value == 0) ? BITS_PER_LONG : __ffs(value);
  180. }
  181. /* size is not constant or too big */
  182. return __find_next_bit(addr, size, offset);
  183. }
  184. extern unsigned long __find_next_zero_bit(const unsigned long *addr,
  185. unsigned long size, unsigned long offset);
  186. /**
  187. * find_next_zero_bit - find the next cleared bit in a memory region
  188. * @addr: The address to base the search on
  189. * @offset: The bitnumber to start searching at
  190. * @size: The bitmap size in bits
  191. */
  192. static __always_inline unsigned long
  193. find_next_zero_bit(const unsigned long *addr, unsigned long size,
  194. unsigned long offset)
  195. {
  196. unsigned long value;
  197. /* Avoid a function call if the bitmap size is a constant */
  198. /* and not bigger than BITS_PER_LONG. */
  199. /* insert a sentinel so that __ffs returns size if there */
  200. /* are no set bits in the bitmap */
  201. if (__builtin_constant_p(size) && (size < BITS_PER_LONG)) {
  202. value = (~(*addr)) & ((~0ul) << offset);
  203. value |= (1ul << size);
  204. return __ffs(value);
  205. }
  206. /* the result of __ffs(0) is undefined, so it needs to be */
  207. /* handled separately */
  208. if (__builtin_constant_p(size) && (size == BITS_PER_LONG)) {
  209. value = (~(*addr)) & ((~0ul) << offset);
  210. return (value == 0) ? BITS_PER_LONG : __ffs(value);
  211. }
  212. /* size is not constant or too big */
  213. return __find_next_zero_bit(addr, size, offset);
  214. }
  215. #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
  216. #endif /* __KERNEL__ */
  217. #endif