bitops.h 5.0 KB

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