bitops.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _PERF_LINUX_BITOPS_H_
  2. #define _PERF_LINUX_BITOPS_H_
  3. #include <linux/kernel.h>
  4. #include <linux/compiler.h>
  5. #include <asm/hweight.h>
  6. #ifndef __WORDSIZE
  7. #define __WORDSIZE (__SIZEOF_LONG__ * 8)
  8. #endif
  9. #define BITS_PER_LONG __WORDSIZE
  10. #define BITS_PER_BYTE 8
  11. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  12. #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
  13. #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
  14. #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
  15. #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  16. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  17. #define for_each_set_bit(bit, addr, size) \
  18. for ((bit) = find_first_bit((addr), (size)); \
  19. (bit) < (size); \
  20. (bit) = find_next_bit((addr), (size), (bit) + 1))
  21. /* same as for_each_set_bit() but use bit as value to start with */
  22. #define for_each_set_bit_from(bit, addr, size) \
  23. for ((bit) = find_next_bit((addr), (size), (bit)); \
  24. (bit) < (size); \
  25. (bit) = find_next_bit((addr), (size), (bit) + 1))
  26. static inline void set_bit(int nr, unsigned long *addr)
  27. {
  28. addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
  29. }
  30. static inline void clear_bit(int nr, unsigned long *addr)
  31. {
  32. addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
  33. }
  34. static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
  35. {
  36. return ((1UL << (nr % BITS_PER_LONG)) &
  37. (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
  38. }
  39. static inline unsigned long hweight_long(unsigned long w)
  40. {
  41. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  42. }
  43. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  44. /**
  45. * __ffs - find first bit in word.
  46. * @word: The word to search
  47. *
  48. * Undefined if no bit exists, so code should check against 0 first.
  49. */
  50. static __always_inline unsigned long __ffs(unsigned long word)
  51. {
  52. int num = 0;
  53. #if BITS_PER_LONG == 64
  54. if ((word & 0xffffffff) == 0) {
  55. num += 32;
  56. word >>= 32;
  57. }
  58. #endif
  59. if ((word & 0xffff) == 0) {
  60. num += 16;
  61. word >>= 16;
  62. }
  63. if ((word & 0xff) == 0) {
  64. num += 8;
  65. word >>= 8;
  66. }
  67. if ((word & 0xf) == 0) {
  68. num += 4;
  69. word >>= 4;
  70. }
  71. if ((word & 0x3) == 0) {
  72. num += 2;
  73. word >>= 2;
  74. }
  75. if ((word & 0x1) == 0)
  76. num += 1;
  77. return num;
  78. }
  79. typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
  80. /*
  81. * Find the first set bit in a memory region.
  82. */
  83. static inline unsigned long
  84. find_first_bit(const unsigned long *addr, unsigned long size)
  85. {
  86. long_alias_t *p = (long_alias_t *) addr;
  87. unsigned long result = 0;
  88. unsigned long tmp;
  89. while (size & ~(BITS_PER_LONG-1)) {
  90. if ((tmp = *(p++)))
  91. goto found;
  92. result += BITS_PER_LONG;
  93. size -= BITS_PER_LONG;
  94. }
  95. if (!size)
  96. return result;
  97. tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
  98. if (tmp == 0UL) /* Are any bits set? */
  99. return result + size; /* Nope. */
  100. found:
  101. return result + __ffs(tmp);
  102. }
  103. /*
  104. * Find the next set bit in a memory region.
  105. */
  106. static inline unsigned long
  107. find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
  108. {
  109. const unsigned long *p = addr + BITOP_WORD(offset);
  110. unsigned long result = offset & ~(BITS_PER_LONG-1);
  111. unsigned long tmp;
  112. if (offset >= size)
  113. return size;
  114. size -= result;
  115. offset %= BITS_PER_LONG;
  116. if (offset) {
  117. tmp = *(p++);
  118. tmp &= (~0UL << offset);
  119. if (size < BITS_PER_LONG)
  120. goto found_first;
  121. if (tmp)
  122. goto found_middle;
  123. size -= BITS_PER_LONG;
  124. result += BITS_PER_LONG;
  125. }
  126. while (size & ~(BITS_PER_LONG-1)) {
  127. if ((tmp = *(p++)))
  128. goto found_middle;
  129. result += BITS_PER_LONG;
  130. size -= BITS_PER_LONG;
  131. }
  132. if (!size)
  133. return result;
  134. tmp = *p;
  135. found_first:
  136. tmp &= (~0UL >> (BITS_PER_LONG - size));
  137. if (tmp == 0UL) /* Are any bits set? */
  138. return result + size; /* Nope. */
  139. found_middle:
  140. return result + __ffs(tmp);
  141. }
  142. #endif