bitops.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 BIT_WORD(nr) ((nr) / BITS_PER_LONG)
  16. #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
  17. #define for_each_set_bit(bit, addr, size) \
  18. for ((bit) = find_first_bit((addr), (size)); \
  19. (bit) < (size); \
  20. (bit) = find_next_bit((addr), (size), (bit) + 1))
  21. /* same as for_each_set_bit() but use bit as value to start with */
  22. #define for_each_set_bit_from(bit, addr, size) \
  23. for ((bit) = find_next_bit((addr), (size), (bit)); \
  24. (bit) < (size); \
  25. (bit) = find_next_bit((addr), (size), (bit) + 1))
  26. static inline void set_bit(int nr, unsigned long *addr)
  27. {
  28. addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
  29. }
  30. static inline void clear_bit(int nr, unsigned long *addr)
  31. {
  32. addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
  33. }
  34. static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
  35. {
  36. return ((1UL << (nr % BITS_PER_LONG)) &
  37. (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
  38. }
  39. static inline unsigned long hweight_long(unsigned long w)
  40. {
  41. return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
  42. }
  43. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  44. #include <asm-generic/bitops/__ffs.h>
  45. typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
  46. /*
  47. * Find the first set bit in a memory region.
  48. */
  49. static inline unsigned long
  50. find_first_bit(const unsigned long *addr, unsigned long size)
  51. {
  52. long_alias_t *p = (long_alias_t *) addr;
  53. unsigned long result = 0;
  54. unsigned long tmp;
  55. while (size & ~(BITS_PER_LONG-1)) {
  56. if ((tmp = *(p++)))
  57. goto found;
  58. result += BITS_PER_LONG;
  59. size -= BITS_PER_LONG;
  60. }
  61. if (!size)
  62. return result;
  63. tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
  64. if (tmp == 0UL) /* Are any bits set? */
  65. return result + size; /* Nope. */
  66. found:
  67. return result + __ffs(tmp);
  68. }
  69. /*
  70. * Find the next set bit in a memory region.
  71. */
  72. static inline unsigned long
  73. find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
  74. {
  75. const unsigned long *p = addr + BITOP_WORD(offset);
  76. unsigned long result = offset & ~(BITS_PER_LONG-1);
  77. unsigned long tmp;
  78. if (offset >= size)
  79. return size;
  80. size -= result;
  81. offset %= BITS_PER_LONG;
  82. if (offset) {
  83. tmp = *(p++);
  84. tmp &= (~0UL << offset);
  85. if (size < BITS_PER_LONG)
  86. goto found_first;
  87. if (tmp)
  88. goto found_middle;
  89. size -= BITS_PER_LONG;
  90. result += BITS_PER_LONG;
  91. }
  92. while (size & ~(BITS_PER_LONG-1)) {
  93. if ((tmp = *(p++)))
  94. goto found_middle;
  95. result += BITS_PER_LONG;
  96. size -= BITS_PER_LONG;
  97. }
  98. if (!size)
  99. return result;
  100. tmp = *p;
  101. found_first:
  102. tmp &= (~0UL >> (BITS_PER_LONG - size));
  103. if (tmp == 0UL) /* Are any bits set? */
  104. return result + size; /* Nope. */
  105. found_middle:
  106. return result + __ffs(tmp);
  107. }
  108. #endif