bitops.h 3.7 KB

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