bitops.h 4.3 KB

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