bitops.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_PER_BYTE 8
  9. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  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_set_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. * Clearly slow versions of the hweightN() functions, their benefit is
  40. * of course compile time evaluation of constant arguments.
  41. */
  42. #define HWEIGHT8(w) \
  43. ( BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + \
  44. (!!((w) & (1ULL << 0))) + \
  45. (!!((w) & (1ULL << 1))) + \
  46. (!!((w) & (1ULL << 2))) + \
  47. (!!((w) & (1ULL << 3))) + \
  48. (!!((w) & (1ULL << 4))) + \
  49. (!!((w) & (1ULL << 5))) + \
  50. (!!((w) & (1ULL << 6))) + \
  51. (!!((w) & (1ULL << 7))) )
  52. #define HWEIGHT16(w) (HWEIGHT8(w) + HWEIGHT8((w) >> 8))
  53. #define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16))
  54. #define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32))
  55. /*
  56. * Type invariant version that simply casts things to the
  57. * largest type.
  58. */
  59. #define HWEIGHT(w) HWEIGHT64((u64)(w))
  60. /**
  61. * rol32 - rotate a 32-bit value left
  62. * @word: value to rotate
  63. * @shift: bits to roll
  64. */
  65. static inline __u32 rol32(__u32 word, unsigned int shift)
  66. {
  67. return (word << shift) | (word >> (32 - shift));
  68. }
  69. /**
  70. * ror32 - rotate a 32-bit value right
  71. * @word: value to rotate
  72. * @shift: bits to roll
  73. */
  74. static inline __u32 ror32(__u32 word, unsigned int shift)
  75. {
  76. return (word >> shift) | (word << (32 - shift));
  77. }
  78. /**
  79. * rol16 - rotate a 16-bit value left
  80. * @word: value to rotate
  81. * @shift: bits to roll
  82. */
  83. static inline __u16 rol16(__u16 word, unsigned int shift)
  84. {
  85. return (word << shift) | (word >> (16 - shift));
  86. }
  87. /**
  88. * ror16 - rotate a 16-bit value right
  89. * @word: value to rotate
  90. * @shift: bits to roll
  91. */
  92. static inline __u16 ror16(__u16 word, unsigned int shift)
  93. {
  94. return (word >> shift) | (word << (16 - shift));
  95. }
  96. /**
  97. * rol8 - rotate an 8-bit value left
  98. * @word: value to rotate
  99. * @shift: bits to roll
  100. */
  101. static inline __u8 rol8(__u8 word, unsigned int shift)
  102. {
  103. return (word << shift) | (word >> (8 - shift));
  104. }
  105. /**
  106. * ror8 - rotate an 8-bit value right
  107. * @word: value to rotate
  108. * @shift: bits to roll
  109. */
  110. static inline __u8 ror8(__u8 word, unsigned int shift)
  111. {
  112. return (word >> shift) | (word << (8 - shift));
  113. }
  114. static inline unsigned fls_long(unsigned long l)
  115. {
  116. if (sizeof(l) == 4)
  117. return fls(l);
  118. return fls64(l);
  119. }
  120. /**
  121. * __ffs64 - find first set bit in a 64 bit word
  122. * @word: The 64 bit word
  123. *
  124. * On 64 bit arches this is a synomyn for __ffs
  125. * The result is not defined if no bits are set, so check that @word
  126. * is non-zero before calling this.
  127. */
  128. static inline unsigned long __ffs64(u64 word)
  129. {
  130. #if BITS_PER_LONG == 32
  131. if (((u32)word) == 0UL)
  132. return __ffs((u32)(word >> 32)) + 32;
  133. #elif BITS_PER_LONG != 64
  134. #error BITS_PER_LONG not 32 or 64
  135. #endif
  136. return __ffs((unsigned long)word);
  137. }
  138. #ifdef __KERNEL__
  139. #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
  140. /**
  141. * find_first_bit - find the first set bit in a memory region
  142. * @addr: The address to start the search at
  143. * @size: The maximum size to search
  144. *
  145. * Returns the bit number of the first set bit.
  146. */
  147. extern unsigned long find_first_bit(const unsigned long *addr,
  148. unsigned long size);
  149. /**
  150. * find_first_zero_bit - find the first cleared bit in a memory region
  151. * @addr: The address to start the search at
  152. * @size: The maximum size to search
  153. *
  154. * Returns the bit number of the first cleared bit.
  155. */
  156. extern unsigned long find_first_zero_bit(const unsigned long *addr,
  157. unsigned long size);
  158. #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
  159. #ifdef CONFIG_GENERIC_FIND_LAST_BIT
  160. /**
  161. * find_last_bit - find the last set bit in a memory region
  162. * @addr: The address to start the search at
  163. * @size: The maximum size to search
  164. *
  165. * Returns the bit number of the first set bit, or size.
  166. */
  167. extern unsigned long find_last_bit(const unsigned long *addr,
  168. unsigned long size);
  169. #endif /* CONFIG_GENERIC_FIND_LAST_BIT */
  170. #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
  171. /**
  172. * find_next_bit - find the next set bit in a memory region
  173. * @addr: The address to base the search on
  174. * @offset: The bitnumber to start searching at
  175. * @size: The bitmap size in bits
  176. */
  177. extern unsigned long find_next_bit(const unsigned long *addr,
  178. unsigned long size, unsigned long offset);
  179. /**
  180. * find_next_zero_bit - find the next cleared bit in a memory region
  181. * @addr: The address to base the search on
  182. * @offset: The bitnumber to start searching at
  183. * @size: The bitmap size in bits
  184. */
  185. extern unsigned long find_next_zero_bit(const unsigned long *addr,
  186. unsigned long size,
  187. unsigned long offset);
  188. #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
  189. #endif /* __KERNEL__ */
  190. #endif