bitops.h 3.3 KB

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