util: Include bitscan.h directly
[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
36 /****************************************************************************
37 * generic bitset implementation
38 */
39
40 #define BITSET_WORD unsigned int
41 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
42
43 /* bitset declarations
44 */
45 #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
46 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
47
48 /* bitset operations
49 */
50 #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
51 #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
52 #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
53 #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
54
55 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
56 #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
57
58 /* single bit operations
59 */
60 #define BITSET_TEST(x, b) ((x)[BITSET_BITWORD(b)] & BITSET_BIT(b))
61 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
62 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
63
64 #define BITSET_MASK(b) ((b) == BITSET_WORDBITS ? ~0 : BITSET_BIT(b) - 1)
65 #define BITSET_RANGE(b, e) (BITSET_MASK((e) + 1) & ~BITSET_MASK(b))
66
67 /* bit range operations
68 */
69 #define BITSET_TEST_RANGE(x, b, e) \
70 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
71 ((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) : \
72 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
73 #define BITSET_SET_RANGE(x, b, e) \
74 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
75 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
76 (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
77 #define BITSET_CLEAR_RANGE(x, b, e) \
78 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
79 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
80 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
81
82 /* Get first bit set in a bitset.
83 */
84 static inline int
85 __bitset_ffs(const BITSET_WORD *x, int n)
86 {
87 int i;
88
89 for (i = 0; i < n; i++) {
90 if (x[i])
91 return ffs(x[i]) + BITSET_WORDBITS * i;
92 }
93
94 return 0;
95 }
96
97 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
98
99 static inline unsigned
100 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
101 const BITSET_WORD *set, unsigned size)
102 {
103 unsigned bit, word;
104
105 /* NOTE: The initial conditions for this function are very specific. At
106 * the start of the loop, the tmp variable must be set to *set and the
107 * initial i value set to 0. This way, if there is a bit set in the first
108 * word, we ignore the i-value and just grab that bit (so 0 is ok, even
109 * though 0 may be returned). If the first word is 0, then the value of
110 * `word` will be 0 and we will go on to look at the second word.
111 */
112 word = BITSET_BITWORD(i);
113 while (*tmp == 0) {
114 word++;
115
116 if (word >= BITSET_WORDS(size))
117 return size;
118
119 *tmp = set[word];
120 }
121
122 /* Find the next set bit in the non-zero word */
123 bit = ffs(*tmp) - 1;
124
125 /* Unset the bit */
126 *tmp &= ~(1ull << bit);
127
128 return word * BITSET_WORDBITS + bit;
129 }
130
131 #define BITSET_FOREACH_SET(__i, __tmp, __set, __size) \
132 for (__tmp = *(__set), __i = 0; \
133 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
134
135 #ifdef __cplusplus
136
137 /**
138 * Simple C++ wrapper of a bitset type of static size, with value semantics
139 * and basic bitwise arithmetic operators. The operators defined below are
140 * expected to have the same semantics as the same operator applied to other
141 * fundamental integer types. T is the name of the struct to instantiate
142 * it as, and N is the number of bits in the bitset.
143 */
144 #define DECLARE_BITSET_T(T, N) struct T { \
145 EXPLICIT_CONVERSION \
146 operator bool() const \
147 { \
148 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
149 if (words[i]) \
150 return true; \
151 return false; \
152 } \
153 \
154 T & \
155 operator=(int x) \
156 { \
157 const T c = {{ (BITSET_WORD)x }}; \
158 return *this = c; \
159 } \
160 \
161 friend bool \
162 operator==(const T &b, const T &c) \
163 { \
164 return BITSET_EQUAL(b.words, c.words); \
165 } \
166 \
167 friend bool \
168 operator!=(const T &b, const T &c) \
169 { \
170 return !(b == c); \
171 } \
172 \
173 friend bool \
174 operator==(const T &b, int x) \
175 { \
176 const T c = {{ (BITSET_WORD)x }}; \
177 return b == c; \
178 } \
179 \
180 friend bool \
181 operator!=(const T &b, int x) \
182 { \
183 return !(b == x); \
184 } \
185 \
186 friend T \
187 operator~(const T &b) \
188 { \
189 T c; \
190 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
191 c.words[i] = ~b.words[i]; \
192 return c; \
193 } \
194 \
195 T & \
196 operator|=(const T &b) \
197 { \
198 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
199 words[i] |= b.words[i]; \
200 return *this; \
201 } \
202 \
203 friend T \
204 operator|(const T &b, const T &c) \
205 { \
206 T d = b; \
207 d |= c; \
208 return d; \
209 } \
210 \
211 T & \
212 operator&=(const T &b) \
213 { \
214 for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
215 words[i] &= b.words[i]; \
216 return *this; \
217 } \
218 \
219 friend T \
220 operator&(const T &b, const T &c) \
221 { \
222 T d = b; \
223 d &= c; \
224 return d; \
225 } \
226 \
227 bool \
228 test(unsigned i) const \
229 { \
230 return BITSET_TEST(words, i); \
231 } \
232 \
233 T & \
234 set(unsigned i) \
235 { \
236 BITSET_SET(words, i); \
237 return *this; \
238 } \
239 \
240 T & \
241 clear(unsigned i) \
242 { \
243 BITSET_CLEAR(words, i); \
244 return *this; \
245 } \
246 \
247 BITSET_WORD words[BITSET_WORDS(N)]; \
248 }
249
250 #endif
251
252 #endif