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 static inline void set_bit(int nr
, unsigned long *addr
)
34 addr
[nr
/ BITS_PER_LONG
] |= 1UL << (nr
% BITS_PER_LONG
);
37 static inline void clear_bit(int nr
, unsigned long *addr
)
39 addr
[nr
/ BITS_PER_LONG
] &= ~(1UL << (nr
% BITS_PER_LONG
));
42 static __always_inline
int test_bit(unsigned int nr
, const unsigned long *addr
)
44 return ((1UL << (nr
% BITS_PER_LONG
)) &
45 (((unsigned long *)addr
)[nr
/ BITS_PER_LONG
])) != 0;
48 static inline unsigned long hweight_long(unsigned long w
)
50 return sizeof(w
) == 4 ? hweight32(w
) : hweight64(w
);
53 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
56 * __ffs - find first bit in word.
57 * @word: The word to search
59 * Undefined if no bit exists, so code should check against 0 first.
61 static __always_inline
unsigned long __ffs(unsigned long word
)
65 #if BITS_PER_LONG == 64
66 if ((word
& 0xffffffff) == 0) {
71 if ((word
& 0xffff) == 0) {
75 if ((word
& 0xff) == 0) {
79 if ((word
& 0xf) == 0) {
83 if ((word
& 0x3) == 0) {
87 if ((word
& 0x1) == 0)
92 typedef const unsigned long __attribute__((__may_alias__
)) long_alias_t
;
95 * Find the first set bit in a memory region.
97 static inline unsigned long
98 find_first_bit(const unsigned long *addr
, unsigned long size
)
100 long_alias_t
*p
= (long_alias_t
*) addr
;
101 unsigned long result
= 0;
104 while (size
& ~(BITS_PER_LONG
-1)) {
107 result
+= BITS_PER_LONG
;
108 size
-= BITS_PER_LONG
;
113 tmp
= (*p
) & (~0UL >> (BITS_PER_LONG
- size
));
114 if (tmp
== 0UL) /* Are any bits set? */
115 return result
+ size
; /* Nope. */
117 return result
+ __ffs(tmp
);
121 * Find the next set bit in a memory region.
123 static inline unsigned long
124 find_next_bit(const unsigned long *addr
, unsigned long size
, unsigned long offset
)
126 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
127 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
133 offset
%= BITS_PER_LONG
;
136 tmp
&= (~0UL << offset
);
137 if (size
< BITS_PER_LONG
)
141 size
-= BITS_PER_LONG
;
142 result
+= BITS_PER_LONG
;
144 while (size
& ~(BITS_PER_LONG
-1)) {
147 result
+= BITS_PER_LONG
;
148 size
-= BITS_PER_LONG
;
155 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
156 if (tmp
== 0UL) /* Are any bits set? */
157 return result
+ size
; /* Nope. */
159 return result
+ __ffs(tmp
);
This page took 0.041202 seconds and 5 git commands to generate.