src/util: Remove out-of-range comparison
[mesa.git] / src / util / bitset.h
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
56 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
57 #define BITSET_BIT(b) (1u << ((b) % BITSET_WORDBITS))
58
59 /* single bit operations
60 */
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))
64
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))
67
68 /* bit range operations
69 */
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))
82
83 /* Get first bit set in a bitset.
84 */
85 static inline int
86 __bitset_ffs(const BITSET_WORD *x, int n)
87 {
88 int i;
89
90 for (i = 0; i < n; i++) {
91 if (x[i])
92 return ffs(x[i]) + BITSET_WORDBITS * i;
93 }
94
95 return 0;
96 }
97
98 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
99
100 static inline unsigned
101 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
102 const BITSET_WORD *set, unsigned size)
103 {
104 unsigned bit, word;
105
106 /* NOTE: The initial conditions for this function are very specific. At
107 * the start of the loop, the tmp variable must be set to *set and the
108 * initial i value set to 0. This way, if there is a bit set in the first
109 * word, we ignore the i-value and just grab that bit (so 0 is ok, even
110 * though 0 may be returned). If the first word is 0, then the value of
111 * `word` will be 0 and we will go on to look at the second word.
112 */
113 word = BITSET_BITWORD(i);
114 while (*tmp == 0) {
115 word++;
116
117 if (word >= BITSET_WORDS(size))
118 return size;
119
120 *tmp = set[word];
121 }
122
123 /* Find the next set bit in the non-zero word */
124 bit = ffs(*tmp) - 1;
125
126 /* Unset the bit */
127 *tmp &= ~(1ull << bit);
128
129 return word * BITSET_WORDBITS + bit;
130 }
131
132 /**
133 * Iterates over each set bit in a set
134 *
135 * @param __i iteration variable, bit number
136 * @param __set the bitset to iterate (will not be modified)
137 * @param __size number of bits in the set to consider
138 */
139 #define BITSET_FOREACH_SET(__i, __set, __size) \
140 for (BITSET_WORD __tmp = *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
141 for (__i = 0; \
142 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
143
144 #ifdef __cplusplus
145
146 /**
147 * Simple C++ wrapper of a bitset type of static size, with value semantics
148 * and basic bitwise arithmetic operators. The operators defined below are
149 * expected to have the same semantics as the same operator applied to other
150 * fundamental integer types. T is the name of the struct to instantiate
151 * it as, and N is the number of bits in the bitset.
152 */
153 #define DECLARE_BITSET_T(T, N) struct T { \
154 EXPLICIT_CONVERSION \
155 operator bool() const \
156 { \
157 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
158 if (words[i]) \
159 return true; \
160 return false; \
161 } \
162 \
163 T & \
164 operator=(int x) \
165 { \
166 const T c = {{ (BITSET_WORD)x }}; \
167 return *this = c; \
168 } \
169 \
170 friend bool \
171 operator==(const T &b, const T &c) \
172 { \
173 return BITSET_EQUAL(b.words, c.words); \
174 } \
175 \
176 friend bool \
177 operator!=(const T &b, const T &c) \
178 { \
179 return !(b == c); \
180 } \
181 \
182 friend bool \
183 operator==(const T &b, int x) \
184 { \
185 const T c = {{ (BITSET_WORD)x }}; \
186 return b == c; \
187 } \
188 \
189 friend bool \
190 operator!=(const T &b, int x) \
191 { \
192 return !(b == x); \
193 } \
194 \
195 friend T \
196 operator~(const T &b) \
197 { \
198 T c; \
199 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
200 c.words[i] = ~b.words[i]; \
201 return c; \
202 } \
203 \
204 T & \
205 operator|=(const T &b) \
206 { \
207 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
208 words[i] |= b.words[i]; \
209 return *this; \
210 } \
211 \
212 friend T \
213 operator|(const T &b, const T &c) \
214 { \
215 T d = b; \
216 d |= c; \
217 return d; \
218 } \
219 \
220 T & \
221 operator&=(const T &b) \
222 { \
223 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
224 words[i] &= b.words[i]; \
225 return *this; \
226 } \
227 \
228 friend T \
229 operator&(const T &b, const T &c) \
230 { \
231 T d = b; \
232 d &= c; \
233 return d; \
234 } \
235 \
236 bool \
237 test(unsigned i) const \
238 { \
239 return BITSET_TEST(words, i); \
240 } \
241 \
242 T & \
243 set(unsigned i) \
244 { \
245 BITSET_SET(words, i); \
246 return *this; \
247 } \
248 \
249 T & \
250 clear(unsigned i) \
251 { \
252 BITSET_CLEAR(words, i); \
253 return *this; \
254 } \
255 \
256 BITSET_WORD words[BITSET_WORDS(N)]; \
257 }
258
259 #endif
260
261 #endif