2 * Mesa 3-D graphics library
4 * Copyright (C) 2006 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * \brief Bitset of arbitrary size definitions.
34 #include "util/bitscan.h"
35 #include "util/macros.h"
37 /****************************************************************************
38 * generic bitset implementation
41 #define BITSET_WORD unsigned int
42 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
44 /* bitset declarations
46 #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
47 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
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) )
56 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
57 #define BITSET_BIT(b) (1u << ((b) % BITSET_WORDBITS))
59 /* single bit operations
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))
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))
68 /* bit range operations
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))
83 /* Get first bit set in a bitset.
86 __bitset_ffs(const BITSET_WORD
*x
, int n
)
90 for (i
= 0; i
< n
; i
++) {
92 return ffs(x
[i
]) + BITSET_WORDBITS
* i
;
98 /* Get the last bit set in a bitset.
101 __bitset_last_bit(const BITSET_WORD
*x
, int n
)
103 for (int i
= n
- 1; i
>= 0; i
--) {
105 return util_last_bit(x
[i
]) + BITSET_WORDBITS
* i
;
111 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
112 #define BITSET_LAST_BIT(x, size) __bitset_last_bit(x, size)
114 static inline unsigned
115 __bitset_next_set(unsigned i
, BITSET_WORD
*tmp
,
116 const BITSET_WORD
*set
, unsigned size
)
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.
127 word
= BITSET_BITWORD(i
);
131 if (word
>= BITSET_WORDS(size
))
137 /* Find the next set bit in the non-zero word */
141 *tmp
&= ~(1ull << bit
);
143 return word
* BITSET_WORDBITS
+ bit
;
147 * Iterates over each set bit in a set
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
153 #define BITSET_FOREACH_SET(__i, __set, __size) \
154 for (BITSET_WORD __tmp = *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
156 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
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.
167 #define DECLARE_BITSET_T(T, N) struct T { \
168 EXPLICIT_CONVERSION \
169 operator bool() const \
171 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
180 const T c = {{ (BITSET_WORD)x }}; \
185 operator==(const T &b, const T &c) \
187 return BITSET_EQUAL(b.words, c.words); \
191 operator!=(const T &b, const T &c) \
197 operator==(const T &b, int x) \
199 const T c = {{ (BITSET_WORD)x }}; \
204 operator!=(const T &b, int x) \
210 operator~(const T &b) \
213 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
214 c.words[i] = ~b.words[i]; \
219 operator|=(const T &b) \
221 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
222 words[i] |= b.words[i]; \
227 operator|(const T &b, const T &c) \
235 operator&=(const T &b) \
237 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
238 words[i] &= b.words[i]; \
243 operator&(const T &b, const T &c) \
251 test(unsigned i) const \
253 return BITSET_TEST(words, i); \
259 BITSET_SET(words, i); \
266 BITSET_CLEAR(words, i); \
270 BITSET_WORD words[BITSET_WORDS(N)]; \