Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/util/bitset.h
4545 views
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
static inline unsigned
84
__bitset_prefix_sum(const BITSET_WORD *x, unsigned b, unsigned n)
85
{
86
unsigned prefix = 0;
87
88
for (unsigned i = 0; i < n; i++) {
89
if ((i + 1) * BITSET_WORDBITS <= b) {
90
prefix += util_bitcount(x[i]);
91
} else {
92
prefix += util_bitcount(x[i] & BITFIELD_MASK(b - i * BITSET_WORDBITS));
93
break;
94
}
95
}
96
return prefix;
97
}
98
99
/* Count set bits in the bitset (compute the size/cardinality of the bitset).
100
* This is a special case of prefix sum, but this convenience method is more
101
* natural when applicable.
102
*/
103
104
static inline unsigned
105
__bitset_count(const BITSET_WORD *x, unsigned n)
106
{
107
return __bitset_prefix_sum(x, ~0, n);
108
}
109
110
#define BITSET_PREFIX_SUM(x, b) \
111
__bitset_prefix_sum(x, b, ARRAY_SIZE(x))
112
113
#define BITSET_COUNT(x) \
114
__bitset_count(x, ARRAY_SIZE(x))
115
116
/* Get first bit set in a bitset.
117
*/
118
static inline int
119
__bitset_ffs(const BITSET_WORD *x, int n)
120
{
121
int i;
122
123
for (i = 0; i < n; i++) {
124
if (x[i])
125
return ffs(x[i]) + BITSET_WORDBITS * i;
126
}
127
128
return 0;
129
}
130
131
/* Get the last bit set in a bitset.
132
*/
133
static inline int
134
__bitset_last_bit(const BITSET_WORD *x, int n)
135
{
136
for (int i = n - 1; i >= 0; i--) {
137
if (x[i])
138
return util_last_bit(x[i]) + BITSET_WORDBITS * i;
139
}
140
141
return 0;
142
}
143
144
#define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
145
#define BITSET_LAST_BIT(x) __bitset_last_bit(x, ARRAY_SIZE(x))
146
#define BITSET_LAST_BIT_SIZED(x, size) __bitset_last_bit(x, size)
147
148
static inline unsigned
149
__bitset_next_set(unsigned i, BITSET_WORD *tmp,
150
const BITSET_WORD *set, unsigned size)
151
{
152
unsigned bit, word;
153
154
/* NOTE: The initial conditions for this function are very specific. At
155
* the start of the loop, the tmp variable must be set to *set and the
156
* initial i value set to 0. This way, if there is a bit set in the first
157
* word, we ignore the i-value and just grab that bit (so 0 is ok, even
158
* though 0 may be returned). If the first word is 0, then the value of
159
* `word` will be 0 and we will go on to look at the second word.
160
*/
161
word = BITSET_BITWORD(i);
162
while (*tmp == 0) {
163
word++;
164
165
if (word >= BITSET_WORDS(size))
166
return size;
167
168
*tmp = set[word];
169
}
170
171
/* Find the next set bit in the non-zero word */
172
bit = ffs(*tmp) - 1;
173
174
/* Unset the bit */
175
*tmp &= ~(1ull << bit);
176
177
return word * BITSET_WORDBITS + bit;
178
}
179
180
/**
181
* Iterates over each set bit in a set
182
*
183
* @param __i iteration variable, bit number
184
* @param __set the bitset to iterate (will not be modified)
185
* @param __size number of bits in the set to consider
186
*/
187
#define BITSET_FOREACH_SET(__i, __set, __size) \
188
for (BITSET_WORD __tmp = (__size) == 0 ? 0 : *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
189
for (__i = 0; \
190
(__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
191
192
static inline void
193
__bitset_next_range(unsigned *start, unsigned *end, const BITSET_WORD *set,
194
unsigned size)
195
{
196
/* To find the next start, start searching from end. In the first iteration
197
* it will be at 0, in every subsequent iteration it will be at the first
198
* 0-bit after the range.
199
*/
200
unsigned word = BITSET_BITWORD(*end);
201
if (word >= BITSET_WORDS(size)) {
202
*start = *end = size;
203
return;
204
}
205
BITSET_WORD tmp = set[word] & ~(BITSET_BIT(*end) - 1);
206
while (!tmp) {
207
word++;
208
if (word >= BITSET_WORDS(size)) {
209
*start = *end = size;
210
return;
211
}
212
tmp = set[word];
213
}
214
215
*start = word * BITSET_WORDBITS + ffs(tmp) - 1;
216
217
/* Now do the opposite to find end. Here we can start at start + 1, because
218
* we know that the bit at start is 1 and we're searching for the first
219
* 0-bit.
220
*/
221
word = BITSET_BITWORD(*start + 1);
222
if (word >= BITSET_WORDS(size)) {
223
*end = size;
224
return;
225
}
226
tmp = set[word] | (BITSET_BIT(*start + 1) - 1);
227
while (~tmp == 0) {
228
word++;
229
if (word >= BITSET_WORDS(size)) {
230
*end = size;
231
return;
232
}
233
tmp = set[word];
234
}
235
236
/* Cap "end" at "size" in case there are extra bits past "size" set in the
237
* word. This is only necessary for "end" because we terminate the loop if
238
* "start" goes past "size".
239
*/
240
*end = MIN2(word * BITSET_WORDBITS + ffs(~tmp) - 1, size);
241
}
242
243
/**
244
* Iterates over each contiguous range of set bits in a set
245
*
246
* @param __start the first 1 bit of the current range
247
* @param __end the bit after the last 1 bit of the current range
248
* @param __set the bitset to iterate (will not be modified)
249
* @param __size number of bits in the set to consider
250
*/
251
#define BITSET_FOREACH_RANGE(__start, __end, __set, __size) \
252
for (__start = 0, __end = 0, \
253
__bitset_next_range(&__start, &__end, __set, __size); \
254
__start < __size; \
255
__bitset_next_range(&__start, &__end, __set, __size))
256
257
258
#ifdef __cplusplus
259
260
/**
261
* Simple C++ wrapper of a bitset type of static size, with value semantics
262
* and basic bitwise arithmetic operators. The operators defined below are
263
* expected to have the same semantics as the same operator applied to other
264
* fundamental integer types. T is the name of the struct to instantiate
265
* it as, and N is the number of bits in the bitset.
266
*/
267
#define DECLARE_BITSET_T(T, N) struct T { \
268
EXPLICIT_CONVERSION \
269
operator bool() const \
270
{ \
271
for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
272
if (words[i]) \
273
return true; \
274
return false; \
275
} \
276
\
277
T & \
278
operator=(int x) \
279
{ \
280
const T c = {{ (BITSET_WORD)x }}; \
281
return *this = c; \
282
} \
283
\
284
friend bool \
285
operator==(const T &b, const T &c) \
286
{ \
287
return BITSET_EQUAL(b.words, c.words); \
288
} \
289
\
290
friend bool \
291
operator!=(const T &b, const T &c) \
292
{ \
293
return !(b == c); \
294
} \
295
\
296
friend bool \
297
operator==(const T &b, int x) \
298
{ \
299
const T c = {{ (BITSET_WORD)x }}; \
300
return b == c; \
301
} \
302
\
303
friend bool \
304
operator!=(const T &b, int x) \
305
{ \
306
return !(b == x); \
307
} \
308
\
309
friend T \
310
operator~(const T &b) \
311
{ \
312
T c; \
313
for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
314
c.words[i] = ~b.words[i]; \
315
return c; \
316
} \
317
\
318
T & \
319
operator|=(const T &b) \
320
{ \
321
for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
322
words[i] |= b.words[i]; \
323
return *this; \
324
} \
325
\
326
friend T \
327
operator|(const T &b, const T &c) \
328
{ \
329
T d = b; \
330
d |= c; \
331
return d; \
332
} \
333
\
334
T & \
335
operator&=(const T &b) \
336
{ \
337
for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
338
words[i] &= b.words[i]; \
339
return *this; \
340
} \
341
\
342
friend T \
343
operator&(const T &b, const T &c) \
344
{ \
345
T d = b; \
346
d &= c; \
347
return d; \
348
} \
349
\
350
bool \
351
test(unsigned i) const \
352
{ \
353
return BITSET_TEST(words, i); \
354
} \
355
\
356
T & \
357
set(unsigned i) \
358
{ \
359
BITSET_SET(words, i); \
360
return *this; \
361
} \
362
\
363
T & \
364
clear(unsigned i) \
365
{ \
366
BITSET_CLEAR(words, i); \
367
return *this; \
368
} \
369
\
370
BITSET_WORD words[BITSET_WORDS(N)]; \
371
}
372
373
#endif
374
375
#endif
376
377