bitops.h 3.5 KB

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