bitops.h 4.0 KB

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