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))
17 #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE)
18 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
19 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
21 #define for_each_set_bit(bit, addr, size) \
22 for ((bit) = find_first_bit((addr), (size)); \
24 (bit) = find_next_bit((addr), (size), (bit) + 1))
26 /* same as for_each_set_bit() but use bit as value to start with */
27 #define for_each_set_bit_from(bit, addr, size) \
28 for ((bit) = find_next_bit((addr), (size), (bit)); \
30 (bit) = find_next_bit((addr), (size), (bit) + 1))
32 #include <asm-generic/bitops/atomic.h>
34 static inline unsigned long hweight_long(unsigned long w)
36 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
39 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
41 #include <asm-generic/bitops/__ffs.h>
43 typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
46 * Find the first set bit in a memory region.
48 static inline unsigned long
49 find_first_bit(const unsigned long *addr, unsigned long size)
51 long_alias_t *p = (long_alias_t *) addr;
52 unsigned long result = 0;
55 while (size & ~(BITS_PER_LONG-1)) {
58 result += BITS_PER_LONG;
59 size -= BITS_PER_LONG;
64 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
65 if (tmp == 0UL) /* Are any bits set? */
66 return result + size; /* Nope. */
68 return result + __ffs(tmp);
72 * Find the next set bit in a memory region.
74 static inline unsigned long
75 find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
77 const unsigned long *p = addr + BITOP_WORD(offset);
78 unsigned long result = offset & ~(BITS_PER_LONG-1);
84 offset %= BITS_PER_LONG;
87 tmp &= (~0UL << offset);
88 if (size < BITS_PER_LONG)
92 size -= BITS_PER_LONG;
93 result += BITS_PER_LONG;
95 while (size & ~(BITS_PER_LONG-1)) {
98 result += BITS_PER_LONG;
99 size -= BITS_PER_LONG;
106 tmp &= (~0UL >> (BITS_PER_LONG - size));
107 if (tmp == 0UL) /* Are any bits set? */
108 return result + size; /* Nope. */
110 return result + __ffs(tmp);