bitops.h 3.5 KB

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