• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (e=end is inclusive)
209  */
210 #define BITSET_GET_RANGE_INSIDE_WORD(x, b, e) \
211    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
212    (((x)[BITSET_BITWORD(b)] >> (b % BITSET_WORDBITS)) & \
213    BITSET_MASK((e) - (b) + 1)) : \
214    (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
215 #define BITSET_TEST_RANGE_INSIDE_WORD(x, b, e, mask) \
216    (BITSET_GET_RANGE_INSIDE_WORD(x, b, e) == (mask))
217 #define BITSET_SET_RANGE_INSIDE_WORD(x, b, e) \
218    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
219    ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
220    (assert (!"BITSET_SET_RANGE_INSIDE_WORD: bit range crosses word boundary"), 0))
221 #define BITSET_CLEAR_RANGE_INSIDE_WORD(x, b, e) \
222    (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
223    ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
224    (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
225 
226 static inline bool
__bitset_test_range(const BITSET_WORD * r,unsigned start,unsigned end)227 __bitset_test_range(const BITSET_WORD *r, unsigned start, unsigned end)
228 {
229    const unsigned size = end - start + 1;
230    const unsigned start_mod = start % BITSET_WORDBITS;
231 
232    if (start_mod + size <= BITSET_WORDBITS) {
233       return !BITSET_TEST_RANGE_INSIDE_WORD(r, start, end, 0);
234    } else {
235       const unsigned first_size = BITSET_WORDBITS - start_mod;
236 
237       return __bitset_test_range(r, start, start + first_size - 1) ||
238              __bitset_test_range(r, start + first_size, end);
239    }
240 }
241 
242 #define BITSET_TEST_RANGE(x, b, e) \
243    __bitset_test_range(x, b, e)
244 
245 static inline void
__bitset_set_range(BITSET_WORD * r,unsigned start,unsigned end)246 __bitset_set_range(BITSET_WORD *r, unsigned start, unsigned end)
247 {
248    const unsigned size = end - start + 1;
249    const unsigned start_mod = start % BITSET_WORDBITS;
250 
251    if (start_mod + size <= BITSET_WORDBITS) {
252       BITSET_SET_RANGE_INSIDE_WORD(r, start, end);
253    } else {
254       const unsigned first_size = BITSET_WORDBITS - start_mod;
255 
256       __bitset_set_range(r, start, start + first_size - 1);
257       __bitset_set_range(r, start + first_size, end);
258    }
259 }
260 
261 #define BITSET_SET_RANGE(x, b, e) \
262    __bitset_set_range(x, b, e)
263 
264 static inline void
__bitclear_clear_range(BITSET_WORD * r,unsigned start,unsigned end)265 __bitclear_clear_range(BITSET_WORD *r, unsigned start, unsigned end)
266 {
267    const unsigned size = end - start + 1;
268    const unsigned start_mod = start % BITSET_WORDBITS;
269 
270    if (start_mod + size <= BITSET_WORDBITS) {
271       BITSET_CLEAR_RANGE_INSIDE_WORD(r, start, end);
272    } else {
273       const unsigned first_size = BITSET_WORDBITS - start_mod;
274 
275       __bitclear_clear_range(r, start, start + first_size - 1);
276       __bitclear_clear_range(r, start + first_size, end);
277    }
278 }
279 
280 #define BITSET_CLEAR_RANGE(x, b, e) \
281    __bitclear_clear_range(x, b, e)
282 
283 static inline unsigned
__bitset_prefix_sum(const BITSET_WORD * x,unsigned b,unsigned n)284 __bitset_prefix_sum(const BITSET_WORD *x, unsigned b, unsigned n)
285 {
286    unsigned prefix = 0;
287 
288    for (unsigned i = 0; i < n; i++) {
289       if ((i + 1) * BITSET_WORDBITS <= b) {
290          prefix += util_bitcount(x[i]);
291       } else {
292          prefix += util_bitcount(x[i] & BITFIELD_MASK(b - i * BITSET_WORDBITS));
293          break;
294       }
295    }
296    return prefix;
297 }
298 
299 /* Count set bits in the bitset (compute the size/cardinality of the bitset).
300  * This is a special case of prefix sum, but this convenience method is more
301  * natural when applicable.
302  */
303 
304 static inline unsigned
__bitset_count(const BITSET_WORD * x,unsigned n)305 __bitset_count(const BITSET_WORD *x, unsigned n)
306 {
307    return __bitset_prefix_sum(x, ~0, n);
308 }
309 
310 #define BITSET_PREFIX_SUM(x, b) \
311    __bitset_prefix_sum(x, b, ARRAY_SIZE(x))
312 
313 #define BITSET_COUNT(x) \
314    __bitset_count(x, ARRAY_SIZE(x))
315 
316 /* Return true if the bitset has no bits set.
317  */
318 static inline bool
__bitset_is_empty(const BITSET_WORD * x,int n)319 __bitset_is_empty(const BITSET_WORD *x, int n)
320 {
321    for (int i = 0; i < n; i++) {
322       if (x[i])
323          return false;
324    }
325 
326    return true;
327 }
328 
329 /* Get first bit set in a bitset.
330  */
331 static inline int
__bitset_ffs(const BITSET_WORD * x,int n)332 __bitset_ffs(const BITSET_WORD *x, int n)
333 {
334    for (int i = 0; i < n; i++) {
335       if (x[i])
336          return ffs(x[i]) + BITSET_WORDBITS * i;
337    }
338 
339    return 0;
340 }
341 
342 /* Get the last bit set in a bitset.
343  */
344 static inline int
__bitset_last_bit(const BITSET_WORD * x,int n)345 __bitset_last_bit(const BITSET_WORD *x, int n)
346 {
347    for (int i = n - 1; i >= 0; i--) {
348       if (x[i])
349          return util_last_bit(x[i]) + BITSET_WORDBITS * i;
350    }
351 
352    return 0;
353 }
354 
355 /* Get the last bit set in a bitset before last_bit.
356  */
357 static inline int
__bitset_last_bit_before(const BITSET_WORD * x,int last_bit)358 __bitset_last_bit_before(const BITSET_WORD *x, int last_bit)
359 {
360    int n = last_bit / BITSET_WORDBITS;
361    int reminder = last_bit % BITSET_WORDBITS;
362    if (reminder) {
363       BITSET_WORD last = x[n] & BITFIELD_MASK(reminder);
364       if (last)
365          return util_last_bit(last) + n * BITSET_WORDBITS;
366    }
367    return __bitset_last_bit(x, n);
368 }
369 
370 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
371 #define BITSET_LAST_BIT(x) __bitset_last_bit(x, ARRAY_SIZE(x))
372 #define BITSET_LAST_BIT_SIZED(x, size) __bitset_last_bit(x, size)
373 #define BITSET_LAST_BIT_BEFORE(x, last_bit) __bitset_last_bit_before(x, last_bit)
374 #define BITSET_IS_EMPTY(x) __bitset_is_empty(x, ARRAY_SIZE(x))
375 
376 static inline unsigned
__bitset_next_set(unsigned i,BITSET_WORD * tmp,const BITSET_WORD * set,unsigned size)377 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
378                   const BITSET_WORD *set, unsigned size)
379 {
380    unsigned bit, word;
381 
382    /* NOTE: The initial conditions for this function are very specific.  At
383     * the start of the loop, the tmp variable must be set to *set and the
384     * initial i value set to 0.  This way, if there is a bit set in the first
385     * word, we ignore the i-value and just grab that bit (so 0 is ok, even
386     * though 0 may be returned).  If the first word is 0, then the value of
387     * `word` will be 0 and we will go on to look at the second word.
388     */
389    word = BITSET_BITWORD(i);
390    while (*tmp == 0) {
391       word++;
392 
393       if (word >= BITSET_WORDS(size))
394          return size;
395 
396       *tmp = set[word];
397    }
398 
399    /* Find the next set bit in the non-zero word */
400    bit = ffs(*tmp) - 1;
401 
402    /* Unset the bit */
403    *tmp &= ~(1ull << bit);
404 
405    return word * BITSET_WORDBITS + bit;
406 }
407 
408 /**
409  * Iterates over each set bit in a set
410  *
411  * @param __i    iteration variable, bit number
412  * @param __set  the bitset to iterate (will not be modified)
413  * @param __size number of bits in the set to consider
414  */
415 #define BITSET_FOREACH_SET(__i, __set, __size) \
416    for (BITSET_WORD __tmp = (__size) == 0 ? 0 : *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
417       for (__i = 0; \
418            (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
419 
420 static inline void
__bitset_next_range(unsigned * start,unsigned * end,const BITSET_WORD * set,unsigned size)421 __bitset_next_range(unsigned *start, unsigned *end, const BITSET_WORD *set,
422                     unsigned size)
423 {
424    /* To find the next start, start searching from end. In the first iteration
425     * it will be at 0, in every subsequent iteration it will be at the first
426     * 0-bit after the range.
427     */
428    unsigned word = BITSET_BITWORD(*end);
429    if (word >= BITSET_WORDS(size)) {
430       *start = *end = size;
431       return;
432    }
433    BITSET_WORD tmp = set[word] & ~(BITSET_BIT(*end) - 1);
434    while (!tmp) {
435       word++;
436       if (word >= BITSET_WORDS(size)) {
437          *start = *end = size;
438          return;
439       }
440       tmp = set[word];
441    }
442 
443    *start = word * BITSET_WORDBITS + ffs(tmp) - 1;
444 
445    /* Now do the opposite to find end. Here we can start at start + 1, because
446     * we know that the bit at start is 1 and we're searching for the first
447     * 0-bit.
448     */
449    word = BITSET_BITWORD(*start + 1);
450    if (word >= BITSET_WORDS(size)) {
451       *end = size;
452       return;
453    }
454    tmp = set[word] | (BITSET_BIT(*start + 1) - 1);
455    while (~tmp == 0) {
456       word++;
457       if (word >= BITSET_WORDS(size)) {
458          *end = size;
459          return;
460       }
461       tmp = set[word];
462    }
463 
464    /* Cap "end" at "size" in case there are extra bits past "size" set in the
465     * word. This is only necessary for "end" because we terminate the loop if
466     * "start" goes past "size".
467     */
468    *end = MIN2(word * BITSET_WORDBITS + ffs(~tmp) - 1, size);
469 }
470 
471 /**
472  * Iterates over each contiguous range of set bits in a set
473  *
474  * @param __start the first 1 bit of the current range
475  * @param __end   the bit after the last 1 bit of the current range
476  * @param __set   the bitset to iterate (will not be modified)
477  * @param __size  number of bits in the set to consider
478  */
479 #define BITSET_FOREACH_RANGE(__start, __end, __set, __size) \
480    for (__start = 0, __end = 0, \
481         __bitset_next_range(&__start, &__end, __set, __size); \
482         __start < __size; \
483         __bitset_next_range(&__start, &__end, __set, __size))
484 
485 
486 #ifdef __cplusplus
487 
488 /**
489  * Simple C++ wrapper of a bitset type of static size, with value semantics
490  * and basic bitwise arithmetic operators.  The operators defined below are
491  * expected to have the same semantics as the same operator applied to other
492  * fundamental integer types.  T is the name of the struct to instantiate
493  * it as, and N is the number of bits in the bitset.
494  */
495 #define DECLARE_BITSET_T(T, N) struct T {                       \
496       explicit                                                  \
497       operator bool() const                                     \
498       {                                                         \
499          for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
500             if (words[i])                                       \
501                return true;                                     \
502          return false;                                          \
503       }                                                         \
504                                                                 \
505       T &                                                       \
506       operator=(int x)                                          \
507       {                                                         \
508          const T c = {{ (BITSET_WORD)x }};                      \
509          return *this = c;                                      \
510       }                                                         \
511                                                                 \
512       friend bool                                               \
513       operator==(const T &b, const T &c)                        \
514       {                                                         \
515          return BITSET_EQUAL(b.words, c.words);                 \
516       }                                                         \
517                                                                 \
518       friend bool                                               \
519       operator!=(const T &b, const T &c)                        \
520       {                                                         \
521          return !(b == c);                                      \
522       }                                                         \
523                                                                 \
524       friend bool                                               \
525       operator==(const T &b, int x)                             \
526       {                                                         \
527          const T c = {{ (BITSET_WORD)x }};                      \
528          return b == c;                                         \
529       }                                                         \
530                                                                 \
531       friend bool                                               \
532       operator!=(const T &b, int x)                             \
533       {                                                         \
534          return !(b == x);                                      \
535       }                                                         \
536                                                                 \
537       friend T                                                  \
538       operator~(const T &b)                                     \
539       {                                                         \
540          T c;                                                   \
541          for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
542             c.words[i] = ~b.words[i];                           \
543          return c;                                              \
544       }                                                         \
545                                                                 \
546       T &                                                       \
547       operator|=(const T &b)                                    \
548       {                                                         \
549          for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
550             words[i] |= b.words[i];                             \
551          return *this;                                          \
552       }                                                         \
553                                                                 \
554       friend T                                                  \
555       operator|(const T &b, const T &c)                         \
556       {                                                         \
557          T d = b;                                               \
558          d |= c;                                                \
559          return d;                                              \
560       }                                                         \
561                                                                 \
562       T &                                                       \
563       operator&=(const T &b)                                    \
564       {                                                         \
565          for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
566             words[i] &= b.words[i];                             \
567          return *this;                                          \
568       }                                                         \
569                                                                 \
570       friend T                                                  \
571       operator&(const T &b, const T &c)                         \
572       {                                                         \
573          T d = b;                                               \
574          d &= c;                                                \
575          return d;                                              \
576       }                                                         \
577                                                                 \
578       bool                                                      \
579       test(unsigned i) const                                    \
580       {                                                         \
581          return BITSET_TEST(words, i);                          \
582       }                                                         \
583                                                                 \
584       T &                                                       \
585       set(unsigned i)                                           \
586       {                                                         \
587          BITSET_SET(words, i);                                  \
588          return *this;                                          \
589       }                                                         \
590                                                                 \
591       T &                                                       \
592       clear(unsigned i)                                         \
593       {                                                         \
594          BITSET_CLEAR(words, i);                                \
595          return *this;                                          \
596       }                                                         \
597                                                                 \
598       BITSET_WORD words[BITSET_WORDS(N)];                       \
599    }
600 
601 #endif
602 
603 #endif
604