bitops.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /*
  78. * Find the first set bit in a memory region.
  79. */
  80. static inline unsigned long
  81. find_first_bit(const unsigned long *addr, unsigned long size)
  82. {
  83. const unsigned long *p = addr;
  84. unsigned long result = 0;
  85. unsigned long tmp;
  86. while (size & ~(BITS_PER_LONG-1)) {
  87. if ((tmp = *(p++)))
  88. goto found;
  89. result += BITS_PER_LONG;
  90. size -= BITS_PER_LONG;
  91. }
  92. if (!size)
  93. return result;
  94. tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
  95. if (tmp == 0UL) /* Are any bits set? */
  96. return result + size; /* Nope. */
  97. found:
  98. return result + __ffs(tmp);
  99. }
  100. /*
  101. * Find the next set bit in a memory region.
  102. */
  103. static inline unsigned long
  104. find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
  105. {
  106. const unsigned long *p = addr + BITOP_WORD(offset);
  107. unsigned long result = offset & ~(BITS_PER_LONG-1);
  108. unsigned long tmp;
  109. if (offset >= size)
  110. return size;
  111. size -= result;
  112. offset %= BITS_PER_LONG;
  113. if (offset) {
  114. tmp = *(p++);
  115. tmp &= (~0UL << offset);
  116. if (size < BITS_PER_LONG)
  117. goto found_first;
  118. if (tmp)
  119. goto found_middle;
  120. size -= BITS_PER_LONG;
  121. result += BITS_PER_LONG;
  122. }
  123. while (size & ~(BITS_PER_LONG-1)) {
  124. if ((tmp = *(p++)))
  125. goto found_middle;
  126. result += BITS_PER_LONG;
  127. size -= BITS_PER_LONG;
  128. }
  129. if (!size)
  130. return result;
  131. tmp = *p;
  132. found_first:
  133. tmp &= (~0UL >> (BITS_PER_LONG - size));
  134. if (tmp == 0UL) /* Are any bits set? */
  135. return result + size; /* Nope. */
  136. found_middle:
  137. return result + __ffs(tmp);
  138. }
  139. #endif