bitops.h 4.5 KB

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