bitops.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef _LINUX_BITOPS_H
  2. #define _LINUX_BITOPS_H
  3. #ifndef USE_HOSTCC
  4. #include <asm/types.h>
  5. #include <asm-generic/bitsperlong.h>
  6. #include <linux/compiler.h>
  7. #include <linux/kernel.h>
  8. #ifdef __KERNEL__
  9. #define BIT(nr) (1UL << (nr))
  10. #define BIT_ULL(nr) (1ULL << (nr))
  11. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  12. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  13. #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
  14. #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
  15. #define BITS_PER_BYTE 8
  16. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  17. #endif
  18. /*
  19. * Create a contiguous bitmask starting at bit position @l and ending at
  20. * position @h. For example
  21. * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
  22. */
  23. #ifdef CONFIG_SANDBOX
  24. #define GENMASK(h, l) \
  25. (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h))))
  26. #else
  27. #define GENMASK(h, l) \
  28. (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
  29. #endif
  30. #define GENMASK_ULL(h, l) \
  31. (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
  32. /*
  33. * ffs: find first bit set. This is defined the same way as
  34. * the libc and compiler builtin ffs routines, therefore
  35. * differs in spirit from the above ffz (man ffs).
  36. */
  37. static inline int generic_ffs(int x)
  38. {
  39. int r = 1;
  40. if (!x)
  41. return 0;
  42. if (!(x & 0xffff)) {
  43. x >>= 16;
  44. r += 16;
  45. }
  46. if (!(x & 0xff)) {
  47. x >>= 8;
  48. r += 8;
  49. }
  50. if (!(x & 0xf)) {
  51. x >>= 4;
  52. r += 4;
  53. }
  54. if (!(x & 3)) {
  55. x >>= 2;
  56. r += 2;
  57. }
  58. if (!(x & 1)) {
  59. x >>= 1;
  60. r += 1;
  61. }
  62. return r;
  63. }
  64. /**
  65. * fls - find last (most-significant) bit set
  66. * @x: the word to search
  67. *
  68. * This is defined the same way as ffs.
  69. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  70. */
  71. static inline int generic_fls(int x)
  72. {
  73. int r = 32;
  74. if (!x)
  75. return 0;
  76. if (!(x & 0xffff0000u)) {
  77. x <<= 16;
  78. r -= 16;
  79. }
  80. if (!(x & 0xff000000u)) {
  81. x <<= 8;
  82. r -= 8;
  83. }
  84. if (!(x & 0xf0000000u)) {
  85. x <<= 4;
  86. r -= 4;
  87. }
  88. if (!(x & 0xc0000000u)) {
  89. x <<= 2;
  90. r -= 2;
  91. }
  92. if (!(x & 0x80000000u)) {
  93. x <<= 1;
  94. r -= 1;
  95. }
  96. return r;
  97. }
  98. /*
  99. * hweightN: returns the hamming weight (i.e. the number
  100. * of bits set) of a N-bit word
  101. */
  102. static inline unsigned int generic_hweight32(unsigned int w)
  103. {
  104. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  105. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  106. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  107. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  108. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  109. }
  110. static inline unsigned int generic_hweight16(unsigned int w)
  111. {
  112. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  113. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  114. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  115. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  116. }
  117. static inline unsigned int generic_hweight8(unsigned int w)
  118. {
  119. unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
  120. res = (res & 0x33) + ((res >> 2) & 0x33);
  121. return (res & 0x0F) + ((res >> 4) & 0x0F);
  122. }
  123. static inline unsigned long generic_hweight64(__u64 w)
  124. {
  125. return generic_hweight32((unsigned int)(w >> 32)) +
  126. generic_hweight32((unsigned int)w);
  127. }
  128. static inline unsigned long hweight_long(unsigned long w)
  129. {
  130. return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
  131. }
  132. #include <asm/bitops.h>
  133. /* linux/include/asm-generic/bitops/non-atomic.h */
  134. #ifndef PLATFORM__SET_BIT
  135. # define __set_bit generic_set_bit
  136. #endif
  137. #ifndef PLATFORM__CLEAR_BIT
  138. # define __clear_bit generic_clear_bit
  139. #endif
  140. #ifndef PLATFORM_FFS
  141. # define ffs generic_ffs
  142. #endif
  143. #ifndef PLATFORM_FLS
  144. # define fls generic_fls
  145. #endif
  146. static inline unsigned fls_long(unsigned long l)
  147. {
  148. if (sizeof(l) == 4)
  149. return fls(l);
  150. return fls64(l);
  151. }
  152. /**
  153. * __ffs64 - find first set bit in a 64 bit word
  154. * @word: The 64 bit word
  155. *
  156. * On 64 bit arches this is a synomyn for __ffs
  157. * The result is not defined if no bits are set, so check that @word
  158. * is non-zero before calling this.
  159. */
  160. static inline unsigned long __ffs64(u64 word)
  161. {
  162. #if BITS_PER_LONG == 32
  163. if (((u32)word) == 0UL)
  164. return __ffs((u32)(word >> 32)) + 32;
  165. #elif BITS_PER_LONG != 64
  166. #error BITS_PER_LONG not 32 or 64
  167. #endif
  168. return __ffs((unsigned long)word);
  169. }
  170. /**
  171. * __set_bit - Set a bit in memory
  172. * @nr: the bit to set
  173. * @addr: the address to start counting from
  174. *
  175. * Unlike set_bit(), this function is non-atomic and may be reordered.
  176. * If it's called on the same region of memory simultaneously, the effect
  177. * may be that only one operation succeeds.
  178. */
  179. static inline void generic_set_bit(int nr, volatile unsigned long *addr)
  180. {
  181. unsigned long mask = BIT_MASK(nr);
  182. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  183. *p |= mask;
  184. }
  185. static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
  186. {
  187. unsigned long mask = BIT_MASK(nr);
  188. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  189. *p &= ~mask;
  190. }
  191. #endif /* !USE_HOSTCC */
  192. #endif