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 #define BITSET_SIZE(x) (8 * sizeof(x)) // bitset size in bits
56
57 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
58 #define BITSET_BIT(b) (1u << ((b) % BITSET_WORDBITS))
59
60 /* single bit operations
61 */
62 #define BITSET_TEST(x, b) (((x)[BITSET_BITWORD(b)] & BITSET_BIT(b)) != 0)
63 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
64 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
65
66 #define BITSET_MASK(b) (((b) % BITSET_WORDBITS == 0) ? ~0 : BITSET_BIT(b) - 1)
67 #define BITSET_RANGE(b, e) ((BITSET_MASK((e) + 1)) & ~(BITSET_BIT(b) - 1))
68
69 /* logic bit operations
70 */
71 static inline void
__bitset_and(BITSET_WORD * r,const BITSET_WORD * x,const BITSET_WORD * y,unsigned n)72 __bitset_and(BITSET_WORD *r, const BITSET_WORD *x, const BITSET_WORD *y, unsigned n)
73 {
74 for (unsigned i = 0; i < n; i++)
75 r[i] = x[i] & y[i];
76 }
77
78 static inline void
__bitset_or(BITSET_WORD * r,const BITSET_WORD * x,const BITSET_WORD * y,unsigned n)79 __bitset_or(BITSET_WORD *r, const BITSET_WORD *x, const BITSET_WORD *y, unsigned n)
80 {
81 for (unsigned i = 0; i < n; i++)
82 r[i] = x[i] | y[i];
83 }
84
85 static inline void
__bitset_not(BITSET_WORD * x,unsigned n)86 __bitset_not(BITSET_WORD *x, unsigned n)
87 {
88 for (unsigned i = 0; i < n; i++)
89 x[i] = ~x[i];
90 }
91
92 static inline void
__bitset_andnot(BITSET_WORD * r,const BITSET_WORD * x,const BITSET_WORD * y,unsigned n)93 __bitset_andnot(BITSET_WORD *r, const BITSET_WORD *x, const BITSET_WORD *y, unsigned n)
94 {
95 for (unsigned i = 0; i < n; i++)
96 r[i] = x[i] & ~y[i];
97 }
98
99 #define BITSET_AND(r, x, y) \
100 do { \
101 STATIC_ASSERT(ARRAY_SIZE(r) == ARRAY_SIZE(x)); \
102 STATIC_ASSERT(ARRAY_SIZE(r) == ARRAY_SIZE(y)); \
103 __bitset_and(r, x, y, ARRAY_SIZE(r)); \
104 } while (0)
105
106 #define BITSET_OR(r, x, y) \
107 do { \
108 STATIC_ASSERT(ARRAY_SIZE(r) == ARRAY_SIZE(x)); \
109 STATIC_ASSERT(ARRAY_SIZE(r) == ARRAY_SIZE(y)); \
110 __bitset_or(r, x, y, ARRAY_SIZE(r)); \
111 } while (0)
112
113 #define BITSET_NOT(x) \
114 __bitset_not(x, ARRAY_SIZE(x))
115
116 #define BITSET_ANDNOT(r, x, y) \
117 do { \
118 assert(ARRAY_SIZE(r) == ARRAY_SIZE(x)); \
119 assert(ARRAY_SIZE(r) == ARRAY_SIZE(y)); \
120 __bitset_andnot(r, x, y, ARRAY_SIZE(r)); \
121 } while (0)
122
123 static inline void
__bitset_rotate_right(BITSET_WORD * x,unsigned amount,unsigned n)124 __bitset_rotate_right(BITSET_WORD *x, unsigned amount, unsigned n)
125 {
126 assert(amount < BITSET_WORDBITS);
127
128 if (amount == 0)
129 return;
130
131 for (unsigned i = 0; i < n - 1; i++) {
132 x[i] = (x[i] >> amount) | (x[i + 1] << (BITSET_WORDBITS - amount));
133 }
134
135 x[n - 1] = x[n - 1] >> amount;
136 }
137
138 static inline void
__bitset_rotate_left(BITSET_WORD * x,unsigned amount,unsigned n)139 __bitset_rotate_left(BITSET_WORD *x, unsigned amount, unsigned n)
140 {
141 assert(amount < BITSET_WORDBITS);
142
143 if (amount == 0)
144 return;
145
146 for (int i = n - 1; i > 0; i--) {
147 x[i] = (x[i] << amount) | (x[i - 1] >> (BITSET_WORDBITS - amount));
148 }
149
150 x[0] = x[0] << amount;
151 }
152
153 static inline void
__bitset_shr(BITSET_WORD * x,unsigned amount,unsigned n)154 __bitset_shr(BITSET_WORD *x, unsigned amount, unsigned n)
155 {
156 const unsigned int words = amount / BITSET_WORDBITS;
157
158 if (amount == 0)
159 return;
160
161 if (words) {
162 unsigned i;
163
164 for (i = 0; i < n - words; i++)
165 x[i] = x[i + words];
166
167 while (i < n)
168 x[i++] = 0;
169
170 amount %= BITSET_WORDBITS;
171 }
172
173 __bitset_rotate_right(x, amount, n);
174 }
175
176
177 static inline void
__bitset_shl(BITSET_WORD * x,unsigned amount,unsigned n)178 __bitset_shl(BITSET_WORD *x, unsigned amount, unsigned n)
179 {
180 const int words = amount / BITSET_WORDBITS;
181
182 if (amount == 0)
183 return;
184
185 if (words) {
186 int i;
187
188 for (i = n - 1; i >= words; i--) {
189 x[i] = x[i - words];
190 }
191
192 while (i >= 0) {
193 x[i--] = 0;
194 }
195
196 amount %= BITSET_WORDBITS;
197 }
198
199 __bitset_rotate_left(x, amount, n);
200 }
201
202 #define BITSET_SHR(x, n) \
203 __bitset_shr(x, n, ARRAY_SIZE(x));
204
205 #define BITSET_SHL(x, n) \
206 __bitset_shl(x, n, ARRAY_SIZE(x));
207
208 /* bit range operations
209 */
210 #define BITSET_TEST_RANGE_INSIDE_WORD(x, b, e) \
211 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
212 (((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) != 0) : \
213 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
214 #define BITSET_SET_RANGE_INSIDE_WORD(x, b, e) \
215 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
216 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
217 (assert (!"BITSET_SET_RANGE_INSIDE_WORD: bit range crosses word boundary"), 0))
218 #define BITSET_CLEAR_RANGE_INSIDE_WORD(x, b, e) \
219 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
220 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
221 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
222
223 static inline bool
__bitset_test_range(const BITSET_WORD * r,unsigned start,unsigned end)224 __bitset_test_range(const BITSET_WORD *r, unsigned start, unsigned end)
225 {
226 const unsigned size = end - start + 1;
227 const unsigned start_mod = start % BITSET_WORDBITS;
228
229 if (start_mod + size <= BITSET_WORDBITS) {
230 return BITSET_TEST_RANGE_INSIDE_WORD(r, start, end);
231 } else {
232 const unsigned first_size = BITSET_WORDBITS - start_mod;
233
234 return __bitset_test_range(r, start, start + first_size - 1) ||
235 __bitset_test_range(r, start + first_size, end);
236 }
237 }
238
239 #define BITSET_TEST_RANGE(x, b, e) \
240 __bitset_test_range(x, b, e)
241
242 static inline void
__bitset_set_range(BITSET_WORD * r,unsigned start,unsigned end)243 __bitset_set_range(BITSET_WORD *r, unsigned start, unsigned end)
244 {
245 const unsigned size = end - start + 1;
246 const unsigned start_mod = start % BITSET_WORDBITS;
247
248 if (start_mod + size <= BITSET_WORDBITS) {
249 BITSET_SET_RANGE_INSIDE_WORD(r, start, end);
250 } else {
251 const unsigned first_size = BITSET_WORDBITS - start_mod;
252
253 __bitset_set_range(r, start, start + first_size - 1);
254 __bitset_set_range(r, start + first_size, end);
255 }
256 }
257
258 #define BITSET_SET_RANGE(x, b, e) \
259 __bitset_set_range(x, b, e)
260
261 static inline void
__bitclear_clear_range(BITSET_WORD * r,unsigned start,unsigned end)262 __bitclear_clear_range(BITSET_WORD *r, unsigned start, unsigned end)
263 {
264 const unsigned size = end - start + 1;
265 const unsigned start_mod = start % BITSET_WORDBITS;
266
267 if (start_mod + size <= BITSET_WORDBITS) {
268 BITSET_CLEAR_RANGE_INSIDE_WORD(r, start, end);
269 } else {
270 const unsigned first_size = BITSET_WORDBITS - start_mod;
271
272 __bitclear_clear_range(r, start, start + first_size - 1);
273 __bitclear_clear_range(r, start + first_size, end);
274 }
275 }
276
277 #define BITSET_CLEAR_RANGE(x, b, e) \
278 __bitclear_clear_range(x, b, e)
279
280 static inline unsigned
__bitset_prefix_sum(const BITSET_WORD * x,unsigned b,unsigned n)281 __bitset_prefix_sum(const BITSET_WORD *x, unsigned b, unsigned n)
282 {
283 unsigned prefix = 0;
284
285 for (unsigned i = 0; i < n; i++) {
286 if ((i + 1) * BITSET_WORDBITS <= b) {
287 prefix += util_bitcount(x[i]);
288 } else {
289 prefix += util_bitcount(x[i] & BITFIELD_MASK(b - i * BITSET_WORDBITS));
290 break;
291 }
292 }
293 return prefix;
294 }
295
296 /* Count set bits in the bitset (compute the size/cardinality of the bitset).
297 * This is a special case of prefix sum, but this convenience method is more
298 * natural when applicable.
299 */
300
301 static inline unsigned
__bitset_count(const BITSET_WORD * x,unsigned n)302 __bitset_count(const BITSET_WORD *x, unsigned n)
303 {
304 return __bitset_prefix_sum(x, ~0, n);
305 }
306
307 #define BITSET_PREFIX_SUM(x, b) \
308 __bitset_prefix_sum(x, b, ARRAY_SIZE(x))
309
310 #define BITSET_COUNT(x) \
311 __bitset_count(x, ARRAY_SIZE(x))
312
313 /* Return true if the bitset has no bits set.
314 */
315 static inline bool
__bitset_is_empty(const BITSET_WORD * x,int n)316 __bitset_is_empty(const BITSET_WORD *x, int n)
317 {
318 for (int i = 0; i < n; i++) {
319 if (x[i])
320 return false;
321 }
322
323 return true;
324 }
325
326 /* Get first bit set in a bitset.
327 */
328 static inline int
__bitset_ffs(const BITSET_WORD * x,int n)329 __bitset_ffs(const BITSET_WORD *x, int n)
330 {
331 for (int i = 0; i < n; i++) {
332 if (x[i])
333 return ffs(x[i]) + BITSET_WORDBITS * i;
334 }
335
336 return 0;
337 }
338
339 /* Get the last bit set in a bitset.
340 */
341 static inline int
__bitset_last_bit(const BITSET_WORD * x,int n)342 __bitset_last_bit(const BITSET_WORD *x, int n)
343 {
344 for (int i = n - 1; i >= 0; i--) {
345 if (x[i])
346 return util_last_bit(x[i]) + BITSET_WORDBITS * i;
347 }
348
349 return 0;
350 }
351
352 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
353 #define BITSET_LAST_BIT(x) __bitset_last_bit(x, ARRAY_SIZE(x))
354 #define BITSET_LAST_BIT_SIZED(x, size) __bitset_last_bit(x, size)
355 #define BITSET_IS_EMPTY(x) __bitset_is_empty(x, ARRAY_SIZE(x))
356
357 static inline unsigned
__bitset_next_set(unsigned i,BITSET_WORD * tmp,const BITSET_WORD * set,unsigned size)358 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
359 const BITSET_WORD *set, unsigned size)
360 {
361 unsigned bit, word;
362
363 /* NOTE: The initial conditions for this function are very specific. At
364 * the start of the loop, the tmp variable must be set to *set and the
365 * initial i value set to 0. This way, if there is a bit set in the first
366 * word, we ignore the i-value and just grab that bit (so 0 is ok, even
367 * though 0 may be returned). If the first word is 0, then the value of
368 * `word` will be 0 and we will go on to look at the second word.
369 */
370 word = BITSET_BITWORD(i);
371 while (*tmp == 0) {
372 word++;
373
374 if (word >= BITSET_WORDS(size))
375 return size;
376
377 *tmp = set[word];
378 }
379
380 /* Find the next set bit in the non-zero word */
381 bit = ffs(*tmp) - 1;
382
383 /* Unset the bit */
384 *tmp &= ~(1ull << bit);
385
386 return word * BITSET_WORDBITS + bit;
387 }
388
389 /**
390 * Iterates over each set bit in a set
391 *
392 * @param __i iteration variable, bit number
393 * @param __set the bitset to iterate (will not be modified)
394 * @param __size number of bits in the set to consider
395 */
396 #define BITSET_FOREACH_SET(__i, __set, __size) \
397 for (BITSET_WORD __tmp = (__size) == 0 ? 0 : *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
398 for (__i = 0; \
399 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
400
401 static inline void
__bitset_next_range(unsigned * start,unsigned * end,const BITSET_WORD * set,unsigned size)402 __bitset_next_range(unsigned *start, unsigned *end, const BITSET_WORD *set,
403 unsigned size)
404 {
405 /* To find the next start, start searching from end. In the first iteration
406 * it will be at 0, in every subsequent iteration it will be at the first
407 * 0-bit after the range.
408 */
409 unsigned word = BITSET_BITWORD(*end);
410 if (word >= BITSET_WORDS(size)) {
411 *start = *end = size;
412 return;
413 }
414 BITSET_WORD tmp = set[word] & ~(BITSET_BIT(*end) - 1);
415 while (!tmp) {
416 word++;
417 if (word >= BITSET_WORDS(size)) {
418 *start = *end = size;
419 return;
420 }
421 tmp = set[word];
422 }
423
424 *start = word * BITSET_WORDBITS + ffs(tmp) - 1;
425
426 /* Now do the opposite to find end. Here we can start at start + 1, because
427 * we know that the bit at start is 1 and we're searching for the first
428 * 0-bit.
429 */
430 word = BITSET_BITWORD(*start + 1);
431 if (word >= BITSET_WORDS(size)) {
432 *end = size;
433 return;
434 }
435 tmp = set[word] | (BITSET_BIT(*start + 1) - 1);
436 while (~tmp == 0) {
437 word++;
438 if (word >= BITSET_WORDS(size)) {
439 *end = size;
440 return;
441 }
442 tmp = set[word];
443 }
444
445 /* Cap "end" at "size" in case there are extra bits past "size" set in the
446 * word. This is only necessary for "end" because we terminate the loop if
447 * "start" goes past "size".
448 */
449 *end = MIN2(word * BITSET_WORDBITS + ffs(~tmp) - 1, size);
450 }
451
452 /**
453 * Iterates over each contiguous range of set bits in a set
454 *
455 * @param __start the first 1 bit of the current range
456 * @param __end the bit after the last 1 bit of the current range
457 * @param __set the bitset to iterate (will not be modified)
458 * @param __size number of bits in the set to consider
459 */
460 #define BITSET_FOREACH_RANGE(__start, __end, __set, __size) \
461 for (__start = 0, __end = 0, \
462 __bitset_next_range(&__start, &__end, __set, __size); \
463 __start < __size; \
464 __bitset_next_range(&__start, &__end, __set, __size))
465
466
467 #ifdef __cplusplus
468
469 /**
470 * Simple C++ wrapper of a bitset type of static size, with value semantics
471 * and basic bitwise arithmetic operators. The operators defined below are
472 * expected to have the same semantics as the same operator applied to other
473 * fundamental integer types. T is the name of the struct to instantiate
474 * it as, and N is the number of bits in the bitset.
475 */
476 #define DECLARE_BITSET_T(T, N) struct T { \
477 explicit \
478 operator bool() const \
479 { \
480 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
481 if (words[i]) \
482 return true; \
483 return false; \
484 } \
485 \
486 T & \
487 operator=(int x) \
488 { \
489 const T c = {{ (BITSET_WORD)x }}; \
490 return *this = c; \
491 } \
492 \
493 friend bool \
494 operator==(const T &b, const T &c) \
495 { \
496 return BITSET_EQUAL(b.words, c.words); \
497 } \
498 \
499 friend bool \
500 operator!=(const T &b, const T &c) \
501 { \
502 return !(b == c); \
503 } \
504 \
505 friend bool \
506 operator==(const T &b, int x) \
507 { \
508 const T c = {{ (BITSET_WORD)x }}; \
509 return b == c; \
510 } \
511 \
512 friend bool \
513 operator!=(const T &b, int x) \
514 { \
515 return !(b == x); \
516 } \
517 \
518 friend T \
519 operator~(const T &b) \
520 { \
521 T c; \
522 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
523 c.words[i] = ~b.words[i]; \
524 return c; \
525 } \
526 \
527 T & \
528 operator|=(const T &b) \
529 { \
530 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
531 words[i] |= b.words[i]; \
532 return *this; \
533 } \
534 \
535 friend T \
536 operator|(const T &b, const T &c) \
537 { \
538 T d = b; \
539 d |= c; \
540 return d; \
541 } \
542 \
543 T & \
544 operator&=(const T &b) \
545 { \
546 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
547 words[i] &= b.words[i]; \
548 return *this; \
549 } \
550 \
551 friend T \
552 operator&(const T &b, const T &c) \
553 { \
554 T d = b; \
555 d &= c; \
556 return d; \
557 } \
558 \
559 bool \
560 test(unsigned i) const \
561 { \
562 return BITSET_TEST(words, i); \
563 } \
564 \
565 T & \
566 set(unsigned i) \
567 { \
568 BITSET_SET(words, i); \
569 return *this; \
570 } \
571 \
572 T & \
573 clear(unsigned i) \
574 { \
575 BITSET_CLEAR(words, i); \
576 return *this; \
577 } \
578 \
579 BITSET_WORD words[BITSET_WORDS(N)]; \
580 }
581
582 #endif
583
584 #endif
585