bitops.h 4.8 KB

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