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