1 #ifndef _PERF_LINUX_BITOPS_H_
2 #define _PERF_LINUX_BITOPS_H_
4 #include <linux/kernel.h>
5 #include <linux/compiler.h>
6 #include <asm/hweight.h>
9 #define __WORDSIZE (__SIZEOF_LONG__ * 8)
12 #define BITS_PER_LONG __WORDSIZE
13 #define BITS_PER_BYTE 8
14 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
15 #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64))
16 #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32))
18 #define for_each_set_bit(bit, addr, size) \
19 for ((bit) = find_first_bit((addr), (size)); \
21 (bit) = find_next_bit((addr), (size), (bit) + 1))
23 /* same as for_each_set_bit() but use bit as value to start with */
24 #define for_each_set_bit_from(bit, addr, size) \
25 for ((bit) = find_next_bit((addr), (size), (bit)); \
27 (bit) = find_next_bit((addr), (size), (bit) + 1))
29 static inline void set_bit(int nr, unsigned long *addr)
31 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
34 static inline void clear_bit(int nr, unsigned long *addr)
36 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
39 static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
41 return ((1UL << (nr % BITS_PER_LONG)) &
42 (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
45 static inline unsigned long hweight_long(unsigned long w)
47 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
50 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
53 * __ffs - find first bit in word.
54 * @word: The word to search
56 * Undefined if no bit exists, so code should check against 0 first.
58 static __always_inline unsigned long __ffs(unsigned long word)
62 #if BITS_PER_LONG == 64
63 if ((word & 0xffffffff) == 0) {
68 if ((word & 0xffff) == 0) {
72 if ((word & 0xff) == 0) {
76 if ((word & 0xf) == 0) {
80 if ((word & 0x3) == 0) {
84 if ((word & 0x1) == 0)
90 * Find the first set bit in a memory region.
92 static inline unsigned long
93 find_first_bit(const unsigned long *addr, unsigned long size)
95 const unsigned long *p = addr;
96 unsigned long result = 0;
99 while (size & ~(BITS_PER_LONG-1)) {
102 result += BITS_PER_LONG;
103 size -= BITS_PER_LONG;
108 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
109 if (tmp == 0UL) /* Are any bits set? */
110 return result + size; /* Nope. */
112 return result + __ffs(tmp);
116 * Find the next set bit in a memory region.
118 static inline unsigned long
119 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
121 const unsigned long *p = addr + BITOP_WORD(offset);
122 unsigned long result = offset & ~(BITS_PER_LONG-1);
128 offset %= BITS_PER_LONG;
131 tmp &= (~0UL << offset);
132 if (size < BITS_PER_LONG)
136 size -= BITS_PER_LONG;
137 result += BITS_PER_LONG;
139 while (size & ~(BITS_PER_LONG-1)) {
142 result += BITS_PER_LONG;
143 size -= BITS_PER_LONG;
150 tmp &= (~0UL >> (BITS_PER_LONG - size));
151 if (tmp == 0UL) /* Are any bits set? */
152 return result + size; /* Nope. */
154 return result + __ffs(tmp);