bitops.h 4.1 KB

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