1#ifndef _PERF_LINUX_BITOPS_H_
2#define _PERF_LINUX_BITOPS_H_
3
4#include <linux/kernel.h>
5#include <linux/compiler.h>
6#include <asm/hweight.h>
7
8#ifndef __WORDSIZE
9#define __WORDSIZE (__SIZEOF_LONG__ * 8)
10#endif
11
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
19#define for_each_set_bit(bit, addr, size) \
20 for ((bit) = find_first_bit((addr), (size)); \
21 (bit) < (size); \
22 (bit) = find_next_bit((addr), (size), (bit) + 1))
23
24
25#define for_each_set_bit_from(bit, addr, size) \
26 for ((bit) = find_next_bit((addr), (size), (bit)); \
27 (bit) < (size); \
28 (bit) = find_next_bit((addr), (size), (bit) + 1))
29
30static inline void set_bit(int nr, unsigned long *addr)
31{
32 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
33}
34
35static inline void clear_bit(int nr, unsigned long *addr)
36{
37 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
38}
39
40static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
41{
42 return ((1UL << (nr % BITS_PER_LONG)) &
43 (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
44}
45
46static inline unsigned long hweight_long(unsigned long w)
47{
48 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
49}
50
51#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
52
53
54
55
56
57
58
59static __always_inline unsigned long __ffs(unsigned long word)
60{
61 int num = 0;
62
63#if BITS_PER_LONG == 64
64 if ((word & 0xffffffff) == 0) {
65 num += 32;
66 word >>= 32;
67 }
68#endif
69 if ((word & 0xffff) == 0) {
70 num += 16;
71 word >>= 16;
72 }
73 if ((word & 0xff) == 0) {
74 num += 8;
75 word >>= 8;
76 }
77 if ((word & 0xf) == 0) {
78 num += 4;
79 word >>= 4;
80 }
81 if ((word & 0x3) == 0) {
82 num += 2;
83 word >>= 2;
84 }
85 if ((word & 0x1) == 0)
86 num += 1;
87 return num;
88}
89
90typedef const unsigned long __attribute__((__may_alias__)) long_alias_t;
91
92
93
94
95static inline unsigned long
96find_first_bit(const unsigned long *addr, unsigned long size)
97{
98 long_alias_t *p = (long_alias_t *) addr;
99 unsigned long result = 0;
100 unsigned long tmp;
101
102 while (size & ~(BITS_PER_LONG-1)) {
103 if ((tmp = *(p++)))
104 goto found;
105 result += BITS_PER_LONG;
106 size -= BITS_PER_LONG;
107 }
108 if (!size)
109 return result;
110
111 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
112 if (tmp == 0UL)
113 return result + size;
114found:
115 return result + __ffs(tmp);
116}
117
118
119
120
121static inline unsigned long
122find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
123{
124 const unsigned long *p = addr + BITOP_WORD(offset);
125 unsigned long result = offset & ~(BITS_PER_LONG-1);
126 unsigned long tmp;
127
128 if (offset >= size)
129 return size;
130 size -= result;
131 offset %= BITS_PER_LONG;
132 if (offset) {
133 tmp = *(p++);
134 tmp &= (~0UL << offset);
135 if (size < BITS_PER_LONG)
136 goto found_first;
137 if (tmp)
138 goto found_middle;
139 size -= BITS_PER_LONG;
140 result += BITS_PER_LONG;
141 }
142 while (size & ~(BITS_PER_LONG-1)) {
143 if ((tmp = *(p++)))
144 goto found_middle;
145 result += BITS_PER_LONG;
146 size -= BITS_PER_LONG;
147 }
148 if (!size)
149 return result;
150 tmp = *p;
151
152found_first:
153 tmp &= (~0UL >> (BITS_PER_LONG - size));
154 if (tmp == 0UL)
155 return result + size;
156found_middle:
157 return result + __ffs(tmp);
158}
159
160#endif
161