Merge branch 'next/drivers' into HEAD
[deliverable/linux.git] / tools / perf / util / include / linux / bitops.h
CommitLineData
5a116dd2
FW
1#ifndef _PERF_LINUX_BITOPS_H_
2#define _PERF_LINUX_BITOPS_H_
3
fb72014d 4#include <linux/kernel.h>
a860a608 5#include <linux/compiler.h>
fb72014d 6#include <asm/hweight.h>
5a116dd2 7
3f34f6c0
IT
8#ifndef __WORDSIZE
9#define __WORDSIZE (__SIZEOF_LONG__ * 8)
10#endif
11
fb72014d
ACM
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))
80c0120a
DA
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))
ee3d2504 17
b1e5a9be
RR
18#define for_each_set_bit(bit, addr, size) \
19 for ((bit) = find_first_bit((addr), (size)); \
20 (bit) < (size); \
21 (bit) = find_next_bit((addr), (size), (bit) + 1))
22
23/* same as for_each_set_bit() but use bit as value to start with */
307b1cd7 24#define for_each_set_bit_from(bit, addr, size) \
b1e5a9be
RR
25 for ((bit) = find_next_bit((addr), (size), (bit)); \
26 (bit) < (size); \
27 (bit) = find_next_bit((addr), (size), (bit) + 1))
28
5a116dd2
FW
29static inline void set_bit(int nr, unsigned long *addr)
30{
31 addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG);
32}
33
baa2f6ce
ACM
34static inline void clear_bit(int nr, unsigned long *addr)
35{
36 addr[nr / BITS_PER_LONG] &= ~(1UL << (nr % BITS_PER_LONG));
37}
38
5a116dd2
FW
39static __always_inline int test_bit(unsigned int nr, const unsigned long *addr)
40{
41 return ((1UL << (nr % BITS_PER_LONG)) &
42 (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0;
43}
44
fb72014d
ACM
45static inline unsigned long hweight_long(unsigned long w)
46{
47 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
48}
5a116dd2 49
b1e5a9be
RR
50#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
51
52/**
53 * __ffs - find first bit in word.
54 * @word: The word to search
55 *
56 * Undefined if no bit exists, so code should check against 0 first.
57 */
58static __always_inline unsigned long __ffs(unsigned long word)
59{
60 int num = 0;
61
62#if BITS_PER_LONG == 64
63 if ((word & 0xffffffff) == 0) {
64 num += 32;
65 word >>= 32;
66 }
67#endif
68 if ((word & 0xffff) == 0) {
69 num += 16;
70 word >>= 16;
71 }
72 if ((word & 0xff) == 0) {
73 num += 8;
74 word >>= 8;
75 }
76 if ((word & 0xf) == 0) {
77 num += 4;
78 word >>= 4;
79 }
80 if ((word & 0x3) == 0) {
81 num += 2;
82 word >>= 2;
83 }
84 if ((word & 0x1) == 0)
85 num += 1;
86 return num;
87}
88
89/*
90 * Find the first set bit in a memory region.
91 */
92static inline unsigned long
93find_first_bit(const unsigned long *addr, unsigned long size)
94{
95 const unsigned long *p = addr;
96 unsigned long result = 0;
97 unsigned long tmp;
98
99 while (size & ~(BITS_PER_LONG-1)) {
100 if ((tmp = *(p++)))
101 goto found;
102 result += BITS_PER_LONG;
103 size -= BITS_PER_LONG;
104 }
105 if (!size)
106 return result;
107
108 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
109 if (tmp == 0UL) /* Are any bits set? */
110 return result + size; /* Nope. */
111found:
112 return result + __ffs(tmp);
113}
114
115/*
116 * Find the next set bit in a memory region.
117 */
118static inline unsigned long
119find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
120{
121 const unsigned long *p = addr + BITOP_WORD(offset);
122 unsigned long result = offset & ~(BITS_PER_LONG-1);
123 unsigned long tmp;
124
125 if (offset >= size)
126 return size;
127 size -= result;
128 offset %= BITS_PER_LONG;
129 if (offset) {
130 tmp = *(p++);
131 tmp &= (~0UL << offset);
132 if (size < BITS_PER_LONG)
133 goto found_first;
134 if (tmp)
135 goto found_middle;
136 size -= BITS_PER_LONG;
137 result += BITS_PER_LONG;
138 }
139 while (size & ~(BITS_PER_LONG-1)) {
140 if ((tmp = *(p++)))
141 goto found_middle;
142 result += BITS_PER_LONG;
143 size -= BITS_PER_LONG;
144 }
145 if (!size)
146 return result;
147 tmp = *p;
148
149found_first:
150 tmp &= (~0UL >> (BITS_PER_LONG - size));
151 if (tmp == 0UL) /* Are any bits set? */
152 return result + size; /* Nope. */
153found_middle:
154 return result + __ffs(tmp);
155}
156
5a116dd2 157#endif
This page took 0.164435 seconds and 5 git commands to generate.