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