bitops.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef _GENERIC_BITOPS_H_
  2. #define _GENERIC_BITOPS_H_
  3. #include <unistd.h>
  4. #include <stdint.h>
  5. #include "gcc_builtin.h"
  6. #ifndef __WORDSIZE
  7. #define __WORDSIZE (__SIZEOF_LONG__ * 8)
  8. #endif
  9. #ifndef BITS_PER_LONG
  10. # define BITS_PER_LONG __WORDSIZE
  11. #endif
  12. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  13. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  14. #define BITS_PER_BYTE 8
  15. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  16. #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
  17. #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
  18. #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
  19. extern unsigned int __sw_hweight8(unsigned int w);
  20. extern unsigned int __sw_hweight16(unsigned int w);
  21. extern unsigned int __sw_hweight32(unsigned int w);
  22. extern unsigned long __sw_hweight64(uint64_t w);
  23. #define ffz(x) __ffs(~(x))
  24. /**
  25. * fls - find last (most-significant) bit set
  26. * @x: the word to search
  27. *
  28. * This is defined the same way as ffs.
  29. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  30. */
  31. /**
  32. * __ffs - find first bit in word.
  33. * @word: The word to search
  34. *
  35. * Undefined if no bit exists, so code should check against 0 first.
  36. */
  37. static inline unsigned long __ffs(unsigned long word)
  38. {
  39. int num = 0;
  40. #if __BITS_PER_LONG == 64
  41. if ((word & 0xffffffff) == 0) {
  42. num += 32;
  43. word >>= 32;
  44. }
  45. #endif
  46. if ((word & 0xffff) == 0) {
  47. num += 16;
  48. word >>= 16;
  49. }
  50. if ((word & 0xff) == 0) {
  51. num += 8;
  52. word >>= 8;
  53. }
  54. if ((word & 0xf) == 0) {
  55. num += 4;
  56. word >>= 4;
  57. }
  58. if ((word & 0x3) == 0) {
  59. num += 2;
  60. word >>= 2;
  61. }
  62. if ((word & 0x1) == 0)
  63. num += 1;
  64. return num;
  65. }
  66. static inline int fls(int x)
  67. {
  68. return 32 - __builtin_clz(x);
  69. }
  70. /**
  71. * fls64 - find last set bit in a 64-bit word
  72. * @x: the word to search
  73. *
  74. * This is defined in a similar way as the libc and compiler builtin
  75. * ffsll, but returns the position of the most significant set bit.
  76. *
  77. * fls64(value) returns 0 if value is 0 or the position of the last
  78. * set bit if value is nonzero. The last (most significant) bit is
  79. * at position 64.
  80. */
  81. #if BITS_PER_LONG == 32
  82. static inline int fls64(uint64_t x)
  83. {
  84. uint32_t h = x >> 32;
  85. if (h)
  86. return fls(h) + 32;
  87. return fls(x);
  88. }
  89. #elif BITS_PER_LONG == 64
  90. static inline int fls64(uint64_t x)
  91. {
  92. return 64 - __builtin_clzll(x);
  93. }
  94. #else
  95. #error BITS_PER_LONG not 32 or 64
  96. #endif
  97. /**
  98. * hweightN - returns the hamming weight of a N-bit word
  99. * @x: the word to weigh
  100. *
  101. * The Hamming Weight of a number is the total number of bits set in it.
  102. */
  103. static inline unsigned int hweight32(unsigned int w)
  104. {
  105. unsigned int res = w - ((w >> 1) & 0x55555555);
  106. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  107. res = (res + (res >> 4)) & 0x0F0F0F0F;
  108. res = res + (res >> 8);
  109. return (res + (res >> 16)) & 0x000000FF;
  110. }
  111. static inline unsigned int hweight16(unsigned int w)
  112. {
  113. unsigned int res = w - ((w >> 1) & 0x5555);
  114. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  115. res = (res + (res >> 4)) & 0x0F0F;
  116. return (res + (res >> 8)) & 0x00FF;
  117. }
  118. static inline unsigned int hweight8(unsigned int w)
  119. {
  120. unsigned int res = w - ((w >> 1) & 0x55);
  121. res = (res & 0x33) + ((res >> 2) & 0x33);
  122. return (res + (res >> 4)) & 0x0F;
  123. }
  124. static inline unsigned long hweight64(uint64_t w)
  125. {
  126. #if BITS_PER_LONG == 32
  127. return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
  128. #elif BITS_PER_LONG == 64
  129. #ifdef ARCH_HAS_FAST_MULTIPLIER
  130. w -= (w >> 1) & 0x5555555555555555ul;
  131. w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul);
  132. w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful;
  133. return (w * 0x0101010101010101ul) >> 56;
  134. #else
  135. uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
  136. res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
  137. res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
  138. res = res + (res >> 8);
  139. res = res + (res >> 16);
  140. return (res + (res >> 32)) & 0x00000000000000FFul;
  141. #endif
  142. #endif
  143. }
  144. #define for_each_set_bit(bit, addr, size) \
  145. for ((bit) = find_first_bit((addr), (size)); \
  146. (bit) < (size); \
  147. (bit) = find_next_bit((addr), (size), (bit) + 1))
  148. #define for_each_clear_bit(bit, addr, size) \
  149. for ((bit) = find_first_zero_bit((addr), (size)); \
  150. (bit) < (size); \
  151. (bit) = find_next_zero_bit((addr), (size), (bit) + 1))
  152. /* same as for_each_set_bit() but use bit as value to start with */
  153. #define for_each_set_bit_from(bit, addr, size) \
  154. for ((bit) = find_next_bit((addr), (size), (bit)); \
  155. (bit) < (size); \
  156. (bit) = find_next_bit((addr), (size), (bit) + 1))
  157. static inline unsigned long hweight_long(unsigned long w)
  158. {
  159. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  160. }
  161. static inline unsigned fls_long(unsigned long l)
  162. {
  163. if (sizeof(l) == 4)
  164. return fls(l);
  165. return fls64(l);
  166. }
  167. /**
  168. * rol32 - rotate a 32-bit value left
  169. * @word: value to rotate
  170. * @shift: bits to roll
  171. */
  172. static inline uint32_t rol32(uint32_t word, unsigned int shift)
  173. {
  174. return (word << shift) | (word >> ((-shift) & 31));
  175. }
  176. #endif