012764afc42a725fd8722d136117734a1e20dfaa
[mesa.git] / src / util / bitset.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2006 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file bitset.h
27 * \brief Bitset of arbitrary size definitions.
28 * \author Michal Krol
29 */
30
31 #ifndef BITSET_H
32 #define BITSET_H
33
34 #include "util/bitscan.h"
35 #include "util/macros.h"
36
37 /****************************************************************************
38 * generic bitset implementation
39 */
40
41 #define BITSET_WORD unsigned int
42 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
43
44 /* bitset declarations
45 */
46 #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
47 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
48
49 /* bitset operations
50 */
51 #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
52 #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
53 #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
54 #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
55
56 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
57 #define BITSET_BIT(b) (1u << ((b) % BITSET_WORDBITS))
58
59 /* single bit operations
60 */
61 #define BITSET_TEST(x, b) (((x)[BITSET_BITWORD(b)] & BITSET_BIT(b)) != 0)
62 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
63 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
64
65 #define BITSET_MASK(b) (((b) % BITSET_WORDBITS == 0) ? ~0 : BITSET_BIT(b) - 1)
66 #define BITSET_RANGE(b, e) ((BITSET_MASK((e) + 1)) & ~(BITSET_BIT(b) - 1))
67
68 /* bit range operations
69 */
70 #define BITSET_TEST_RANGE(x, b, e) \
71 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
72 (((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) != 0) : \
73 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
74 #define BITSET_SET_RANGE(x, b, e) \
75 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
76 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
77 (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
78 #define BITSET_CLEAR_RANGE(x, b, e) \
79 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
80 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
81 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
82
83 /* Get first bit set in a bitset.
84 */
85 static inline int
86 __bitset_ffs(const BITSET_WORD *x, int n)
87 {
88 int i;
89
90 for (i = 0; i < n; i++) {
91 if (x[i])
92 return ffs(x[i]) + BITSET_WORDBITS * i;
93 }
94
95 return 0;
96 }
97
98 /* Get the last bit set in a bitset.
99 */
100 static inline int
101 __bitset_last_bit(const BITSET_WORD *x, int n)
102 {
103 for (int i = n - 1; i >= 0; i--) {
104 if (x[i])
105 return util_last_bit(x[i]) + BITSET_WORDBITS * i;
106 }
107
108 return 0;
109 }
110
111 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
112 #define BITSET_LAST_BIT(x, size) __bitset_last_bit(x, size)
113
114 static inline unsigned
115 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
116 const BITSET_WORD *set, unsigned size)
117 {
118 unsigned bit, word;
119
120 /* NOTE: The initial conditions for this function are very specific. At
121 * the start of the loop, the tmp variable must be set to *set and the
122 * initial i value set to 0. This way, if there is a bit set in the first
123 * word, we ignore the i-value and just grab that bit (so 0 is ok, even
124 * though 0 may be returned). If the first word is 0, then the value of
125 * `word` will be 0 and we will go on to look at the second word.
126 */
127 word = BITSET_BITWORD(i);
128 while (*tmp == 0) {
129 word++;
130
131 if (word >= BITSET_WORDS(size))
132 return size;
133
134 *tmp = set[word];
135 }
136
137 /* Find the next set bit in the non-zero word */
138 bit = ffs(*tmp) - 1;
139
140 /* Unset the bit */
141 *tmp &= ~(1ull << bit);
142
143 return word * BITSET_WORDBITS + bit;
144 }
145
146 /**
147 * Iterates over each set bit in a set
148 *
149 * @param __i iteration variable, bit number
150 * @param __set the bitset to iterate (will not be modified)
151 * @param __size number of bits in the set to consider
152 */
153 #define BITSET_FOREACH_SET(__i, __set, __size) \
154 for (BITSET_WORD __tmp = *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
155 for (__i = 0; \
156 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
157
158 #ifdef __cplusplus
159
160 /**
161 * Simple C++ wrapper of a bitset type of static size, with value semantics
162 * and basic bitwise arithmetic operators. The operators defined below are
163 * expected to have the same semantics as the same operator applied to other
164 * fundamental integer types. T is the name of the struct to instantiate
165 * it as, and N is the number of bits in the bitset.
166 */
167 #define DECLARE_BITSET_T(T, N) struct T { \
168 EXPLICIT_CONVERSION \
169 operator bool() const \
170 { \
171 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
172 if (words[i]) \
173 return true; \
174 return false; \
175 } \
176 \
177 T & \
178 operator=(int x) \
179 { \
180 const T c = {{ (BITSET_WORD)x }}; \
181 return *this = c; \
182 } \
183 \
184 friend bool \
185 operator==(const T &b, const T &c) \
186 { \
187 return BITSET_EQUAL(b.words, c.words); \
188 } \
189 \
190 friend bool \
191 operator!=(const T &b, const T &c) \
192 { \
193 return !(b == c); \
194 } \
195 \
196 friend bool \
197 operator==(const T &b, int x) \
198 { \
199 const T c = {{ (BITSET_WORD)x }}; \
200 return b == c; \
201 } \
202 \
203 friend bool \
204 operator!=(const T &b, int x) \
205 { \
206 return !(b == x); \
207 } \
208 \
209 friend T \
210 operator~(const T &b) \
211 { \
212 T c; \
213 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
214 c.words[i] = ~b.words[i]; \
215 return c; \
216 } \
217 \
218 T & \
219 operator|=(const T &b) \
220 { \
221 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
222 words[i] |= b.words[i]; \
223 return *this; \
224 } \
225 \
226 friend T \
227 operator|(const T &b, const T &c) \
228 { \
229 T d = b; \
230 d |= c; \
231 return d; \
232 } \
233 \
234 T & \
235 operator&=(const T &b) \
236 { \
237 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
238 words[i] &= b.words[i]; \
239 return *this; \
240 } \
241 \
242 friend T \
243 operator&(const T &b, const T &c) \
244 { \
245 T d = b; \
246 d &= c; \
247 return d; \
248 } \
249 \
250 bool \
251 test(unsigned i) const \
252 { \
253 return BITSET_TEST(words, i); \
254 } \
255 \
256 T & \
257 set(unsigned i) \
258 { \
259 BITSET_SET(words, i); \
260 return *this; \
261 } \
262 \
263 T & \
264 clear(unsigned i) \
265 { \
266 BITSET_CLEAR(words, i); \
267 return *this; \
268 } \
269 \
270 BITSET_WORD words[BITSET_WORDS(N)]; \
271 }
272
273 #endif
274
275 #endif