1 | // |
2 | // Copyright 2015 The ANGLE Project Authors. All rights reserved. |
3 | // Use of this source code is governed by a BSD-style license that can be |
4 | // found in the LICENSE file. |
5 | // |
6 | // bitset_utils: |
7 | // Bitset-related helper classes, such as a fast iterator to scan for set bits. |
8 | // |
9 | |
10 | #ifndef COMMON_BITSETITERATOR_H_ |
11 | #define COMMON_BITSETITERATOR_H_ |
12 | |
13 | #include <stdint.h> |
14 | |
15 | #include <bitset> |
16 | |
17 | #include "common/angleutils.h" |
18 | #include "common/debug.h" |
19 | #include "common/mathutil.h" |
20 | #include "common/platform.h" |
21 | |
22 | namespace angle |
23 | { |
24 | |
25 | template <size_t N, typename BitsT, typename ParamT = std::size_t> |
26 | class BitSetT final |
27 | { |
28 | public: |
29 | class Reference final |
30 | { |
31 | public: |
32 | ~Reference() {} |
33 | Reference &operator=(bool x) |
34 | { |
35 | mParent->set(mBit, x); |
36 | return *this; |
37 | } |
38 | explicit operator bool() const { return mParent->test(mBit); } |
39 | |
40 | private: |
41 | friend class BitSetT; |
42 | |
43 | Reference(BitSetT *parent, ParamT bit) : mParent(parent), mBit(bit) {} |
44 | |
45 | BitSetT *mParent; |
46 | ParamT mBit; |
47 | }; |
48 | |
49 | class Iterator final |
50 | { |
51 | public: |
52 | Iterator(const BitSetT &bits); |
53 | Iterator &operator++(); |
54 | |
55 | bool operator==(const Iterator &other) const; |
56 | bool operator!=(const Iterator &other) const; |
57 | ParamT operator*() const; |
58 | |
59 | // These helper functions allow mutating an iterator in-flight. |
60 | // They only operate on later bits to ensure we don't iterate the same bit twice. |
61 | void resetLaterBit(std::size_t index) |
62 | { |
63 | ASSERT(index > mCurrentBit); |
64 | mBitsCopy.reset(index); |
65 | } |
66 | |
67 | void setLaterBit(std::size_t index) |
68 | { |
69 | ASSERT(index > mCurrentBit); |
70 | mBitsCopy.set(index); |
71 | } |
72 | |
73 | private: |
74 | std::size_t getNextBit(); |
75 | |
76 | BitSetT mBitsCopy; |
77 | std::size_t mCurrentBit; |
78 | }; |
79 | |
80 | BitSetT(); |
81 | constexpr explicit BitSetT(BitsT value); |
82 | |
83 | BitSetT(const BitSetT &other); |
84 | BitSetT &operator=(const BitSetT &other); |
85 | |
86 | bool operator==(const BitSetT &other) const; |
87 | bool operator!=(const BitSetT &other) const; |
88 | |
89 | constexpr bool operator[](ParamT pos) const; |
90 | Reference operator[](ParamT pos) { return Reference(this, pos); } |
91 | |
92 | bool test(ParamT pos) const; |
93 | |
94 | bool all() const; |
95 | bool any() const; |
96 | bool none() const; |
97 | std::size_t count() const; |
98 | |
99 | constexpr std::size_t size() const { return N; } |
100 | |
101 | BitSetT &operator&=(const BitSetT &other); |
102 | BitSetT &operator|=(const BitSetT &other); |
103 | BitSetT &operator^=(const BitSetT &other); |
104 | BitSetT operator~() const; |
105 | |
106 | BitSetT &operator&=(BitsT value); |
107 | BitSetT &operator|=(BitsT value); |
108 | BitSetT &operator^=(BitsT value); |
109 | |
110 | BitSetT operator<<(std::size_t pos) const; |
111 | BitSetT &operator<<=(std::size_t pos); |
112 | BitSetT operator>>(std::size_t pos) const; |
113 | BitSetT &operator>>=(std::size_t pos); |
114 | |
115 | BitSetT &set(); |
116 | BitSetT &set(ParamT pos, bool value = true); |
117 | |
118 | BitSetT &reset(); |
119 | BitSetT &reset(ParamT pos); |
120 | |
121 | BitSetT &flip(); |
122 | BitSetT &flip(ParamT pos); |
123 | |
124 | unsigned long to_ulong() const { return static_cast<unsigned long>(mBits); } |
125 | BitsT bits() const { return mBits; } |
126 | |
127 | Iterator begin() const { return Iterator(*this); } |
128 | Iterator end() const { return Iterator(BitSetT()); } |
129 | |
130 | private: |
131 | constexpr static BitsT Bit(ParamT x) |
132 | { |
133 | return (static_cast<BitsT>(1) << static_cast<size_t>(x)); |
134 | } |
135 | // Produces a mask of ones up to the "x"th bit. |
136 | constexpr static BitsT Mask(std::size_t x) |
137 | { |
138 | return ((Bit(static_cast<ParamT>(x - 1)) - 1) << 1) + 1; |
139 | } |
140 | |
141 | BitsT mBits; |
142 | }; |
143 | |
144 | template <size_t N> |
145 | class IterableBitSet : public std::bitset<N> |
146 | { |
147 | public: |
148 | IterableBitSet() {} |
149 | IterableBitSet(const std::bitset<N> &implicitBitSet) : std::bitset<N>(implicitBitSet) {} |
150 | |
151 | class Iterator final |
152 | { |
153 | public: |
154 | Iterator(const std::bitset<N> &bits); |
155 | Iterator &operator++(); |
156 | |
157 | bool operator==(const Iterator &other) const; |
158 | bool operator!=(const Iterator &other) const; |
159 | unsigned long operator*() const { return mCurrentBit; } |
160 | |
161 | // These helper functions allow mutating an iterator in-flight. |
162 | // They only operate on later bits to ensure we don't iterate the same bit twice. |
163 | void resetLaterBit(std::size_t index) |
164 | { |
165 | ASSERT(index > mCurrentBit); |
166 | mBits.reset(index - mOffset); |
167 | } |
168 | |
169 | void setLaterBit(std::size_t index) |
170 | { |
171 | ASSERT(index > mCurrentBit); |
172 | mBits.set(index - mOffset); |
173 | } |
174 | |
175 | private: |
176 | unsigned long getNextBit(); |
177 | |
178 | static constexpr size_t BitsPerWord = sizeof(uint32_t) * 8; |
179 | std::bitset<N> mBits; |
180 | unsigned long mCurrentBit; |
181 | unsigned long mOffset; |
182 | }; |
183 | |
184 | Iterator begin() const { return Iterator(*this); } |
185 | Iterator end() const { return Iterator(std::bitset<N>(0)); } |
186 | }; |
187 | |
188 | template <size_t N> |
189 | IterableBitSet<N>::Iterator::Iterator(const std::bitset<N> &bitset) |
190 | : mBits(bitset), mCurrentBit(0), mOffset(0) |
191 | { |
192 | if (mBits.any()) |
193 | { |
194 | mCurrentBit = getNextBit(); |
195 | } |
196 | else |
197 | { |
198 | mOffset = static_cast<unsigned long>(rx::roundUp(N, BitsPerWord)); |
199 | } |
200 | } |
201 | |
202 | template <size_t N> |
203 | ANGLE_INLINE typename IterableBitSet<N>::Iterator &IterableBitSet<N>::Iterator::operator++() |
204 | { |
205 | ASSERT(mBits.any()); |
206 | mBits.set(mCurrentBit - mOffset, 0); |
207 | mCurrentBit = getNextBit(); |
208 | return *this; |
209 | } |
210 | |
211 | template <size_t N> |
212 | bool IterableBitSet<N>::Iterator::operator==(const Iterator &other) const |
213 | { |
214 | return mOffset == other.mOffset && mBits == other.mBits; |
215 | } |
216 | |
217 | template <size_t N> |
218 | bool IterableBitSet<N>::Iterator::operator!=(const Iterator &other) const |
219 | { |
220 | return !(*this == other); |
221 | } |
222 | |
223 | template <size_t N> |
224 | unsigned long IterableBitSet<N>::Iterator::getNextBit() |
225 | { |
226 | // TODO(jmadill): Use 64-bit scan when possible. |
227 | static constexpr std::bitset<N> wordMask(std::numeric_limits<uint32_t>::max()); |
228 | |
229 | while (mOffset < N) |
230 | { |
231 | uint32_t wordBits = static_cast<uint32_t>((mBits & wordMask).to_ulong()); |
232 | if (wordBits != 0) |
233 | { |
234 | return gl::ScanForward(wordBits) + mOffset; |
235 | } |
236 | |
237 | mBits >>= BitsPerWord; |
238 | mOffset += BitsPerWord; |
239 | } |
240 | return 0; |
241 | } |
242 | |
243 | template <size_t N, typename BitsT, typename ParamT> |
244 | BitSetT<N, BitsT, ParamT>::BitSetT() : mBits(0) |
245 | { |
246 | static_assert(N > 0, "Bitset type cannot support zero bits." ); |
247 | static_assert(N <= sizeof(BitsT) * 8, "Bitset type cannot support a size this large." ); |
248 | } |
249 | |
250 | template <size_t N, typename BitsT, typename ParamT> |
251 | constexpr BitSetT<N, BitsT, ParamT>::BitSetT(BitsT value) : mBits(value & Mask(N)) |
252 | {} |
253 | |
254 | template <size_t N, typename BitsT, typename ParamT> |
255 | BitSetT<N, BitsT, ParamT>::BitSetT(const BitSetT &other) : mBits(other.mBits) |
256 | {} |
257 | |
258 | template <size_t N, typename BitsT, typename ParamT> |
259 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator=(const BitSetT &other) |
260 | { |
261 | mBits = other.mBits; |
262 | return *this; |
263 | } |
264 | |
265 | template <size_t N, typename BitsT, typename ParamT> |
266 | bool BitSetT<N, BitsT, ParamT>::operator==(const BitSetT &other) const |
267 | { |
268 | return mBits == other.mBits; |
269 | } |
270 | |
271 | template <size_t N, typename BitsT, typename ParamT> |
272 | bool BitSetT<N, BitsT, ParamT>::operator!=(const BitSetT &other) const |
273 | { |
274 | return mBits != other.mBits; |
275 | } |
276 | |
277 | template <size_t N, typename BitsT, typename ParamT> |
278 | constexpr bool BitSetT<N, BitsT, ParamT>::operator[](ParamT pos) const |
279 | { |
280 | return test(pos); |
281 | } |
282 | |
283 | template <size_t N, typename BitsT, typename ParamT> |
284 | bool BitSetT<N, BitsT, ParamT>::test(ParamT pos) const |
285 | { |
286 | return (mBits & Bit(pos)) != 0; |
287 | } |
288 | |
289 | template <size_t N, typename BitsT, typename ParamT> |
290 | bool BitSetT<N, BitsT, ParamT>::all() const |
291 | { |
292 | ASSERT(mBits == (mBits & Mask(N))); |
293 | return mBits == Mask(N); |
294 | } |
295 | |
296 | template <size_t N, typename BitsT, typename ParamT> |
297 | bool BitSetT<N, BitsT, ParamT>::any() const |
298 | { |
299 | ASSERT(mBits == (mBits & Mask(N))); |
300 | return (mBits != 0); |
301 | } |
302 | |
303 | template <size_t N, typename BitsT, typename ParamT> |
304 | bool BitSetT<N, BitsT, ParamT>::none() const |
305 | { |
306 | ASSERT(mBits == (mBits & Mask(N))); |
307 | return (mBits == 0); |
308 | } |
309 | |
310 | template <size_t N, typename BitsT, typename ParamT> |
311 | std::size_t BitSetT<N, BitsT, ParamT>::count() const |
312 | { |
313 | return gl::BitCount(mBits); |
314 | } |
315 | |
316 | template <size_t N, typename BitsT, typename ParamT> |
317 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator&=(const BitSetT &other) |
318 | { |
319 | mBits &= other.mBits; |
320 | return *this; |
321 | } |
322 | |
323 | template <size_t N, typename BitsT, typename ParamT> |
324 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator|=(const BitSetT &other) |
325 | { |
326 | mBits |= other.mBits; |
327 | return *this; |
328 | } |
329 | |
330 | template <size_t N, typename BitsT, typename ParamT> |
331 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator^=(const BitSetT &other) |
332 | { |
333 | mBits = mBits ^ other.mBits; |
334 | return *this; |
335 | } |
336 | |
337 | template <size_t N, typename BitsT, typename ParamT> |
338 | BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator~() const |
339 | { |
340 | return BitSetT<N, BitsT, ParamT>(~mBits & Mask(N)); |
341 | } |
342 | |
343 | template <size_t N, typename BitsT, typename ParamT> |
344 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator&=(BitsT value) |
345 | { |
346 | mBits &= value; |
347 | return *this; |
348 | } |
349 | |
350 | template <size_t N, typename BitsT, typename ParamT> |
351 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator|=(BitsT value) |
352 | { |
353 | mBits |= value & Mask(N); |
354 | return *this; |
355 | } |
356 | |
357 | template <size_t N, typename BitsT, typename ParamT> |
358 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator^=(BitsT value) |
359 | { |
360 | mBits ^= value & Mask(N); |
361 | return *this; |
362 | } |
363 | |
364 | template <size_t N, typename BitsT, typename ParamT> |
365 | BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator<<(std::size_t pos) const |
366 | { |
367 | return BitSetT<N, BitsT, ParamT>((mBits << pos) & Mask(N)); |
368 | } |
369 | |
370 | template <size_t N, typename BitsT, typename ParamT> |
371 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator<<=(std::size_t pos) |
372 | { |
373 | mBits = (mBits << pos & Mask(N)); |
374 | return *this; |
375 | } |
376 | |
377 | template <size_t N, typename BitsT, typename ParamT> |
378 | BitSetT<N, BitsT, ParamT> BitSetT<N, BitsT, ParamT>::operator>>(std::size_t pos) const |
379 | { |
380 | return BitSetT<N, BitsT, ParamT>(mBits >> pos); |
381 | } |
382 | |
383 | template <size_t N, typename BitsT, typename ParamT> |
384 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::operator>>=(std::size_t pos) |
385 | { |
386 | mBits = ((mBits >> pos) & Mask(N)); |
387 | return *this; |
388 | } |
389 | |
390 | template <size_t N, typename BitsT, typename ParamT> |
391 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::set() |
392 | { |
393 | mBits = Mask(N); |
394 | return *this; |
395 | } |
396 | |
397 | template <size_t N, typename BitsT, typename ParamT> |
398 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::set(ParamT pos, bool value) |
399 | { |
400 | if (value) |
401 | { |
402 | mBits |= Bit(pos) & Mask(N); |
403 | } |
404 | else |
405 | { |
406 | reset(pos); |
407 | } |
408 | return *this; |
409 | } |
410 | |
411 | template <size_t N, typename BitsT, typename ParamT> |
412 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::reset() |
413 | { |
414 | mBits = 0; |
415 | return *this; |
416 | } |
417 | |
418 | template <size_t N, typename BitsT, typename ParamT> |
419 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::reset(ParamT pos) |
420 | { |
421 | mBits &= ~Bit(pos); |
422 | return *this; |
423 | } |
424 | |
425 | template <size_t N, typename BitsT, typename ParamT> |
426 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::flip() |
427 | { |
428 | mBits ^= Mask(N); |
429 | return *this; |
430 | } |
431 | |
432 | template <size_t N, typename BitsT, typename ParamT> |
433 | BitSetT<N, BitsT, ParamT> &BitSetT<N, BitsT, ParamT>::flip(ParamT pos) |
434 | { |
435 | mBits ^= Bit(pos) & Mask(N); |
436 | return *this; |
437 | } |
438 | |
439 | template <size_t N, typename BitsT, typename ParamT> |
440 | BitSetT<N, BitsT, ParamT>::Iterator::Iterator(const BitSetT &bits) : mBitsCopy(bits), mCurrentBit(0) |
441 | { |
442 | if (bits.any()) |
443 | { |
444 | mCurrentBit = getNextBit(); |
445 | } |
446 | } |
447 | |
448 | template <size_t N, typename BitsT, typename ParamT> |
449 | ANGLE_INLINE typename BitSetT<N, BitsT, ParamT>::Iterator &BitSetT<N, BitsT, ParamT>::Iterator:: |
450 | operator++() |
451 | { |
452 | ASSERT(mBitsCopy.any()); |
453 | mBitsCopy.reset(static_cast<ParamT>(mCurrentBit)); |
454 | mCurrentBit = getNextBit(); |
455 | return *this; |
456 | } |
457 | |
458 | template <size_t N, typename BitsT, typename ParamT> |
459 | bool BitSetT<N, BitsT, ParamT>::Iterator::operator==(const Iterator &other) const |
460 | { |
461 | return mBitsCopy == other.mBitsCopy; |
462 | } |
463 | |
464 | template <size_t N, typename BitsT, typename ParamT> |
465 | bool BitSetT<N, BitsT, ParamT>::Iterator::operator!=(const Iterator &other) const |
466 | { |
467 | return !(*this == other); |
468 | } |
469 | |
470 | template <size_t N, typename BitsT, typename ParamT> |
471 | ParamT BitSetT<N, BitsT, ParamT>::Iterator::operator*() const |
472 | { |
473 | return static_cast<ParamT>(mCurrentBit); |
474 | } |
475 | |
476 | template <size_t N, typename BitsT, typename ParamT> |
477 | std::size_t BitSetT<N, BitsT, ParamT>::Iterator::getNextBit() |
478 | { |
479 | if (mBitsCopy.none()) |
480 | { |
481 | return 0; |
482 | } |
483 | |
484 | return gl::ScanForward(mBitsCopy.mBits); |
485 | } |
486 | |
487 | template <size_t N> |
488 | using BitSet32 = BitSetT<N, uint32_t>; |
489 | |
490 | // ScanForward for 64-bits requires a 64-bit implementation. |
491 | #if defined(ANGLE_IS_64_BIT_CPU) |
492 | template <size_t N> |
493 | using BitSet64 = BitSetT<N, uint64_t>; |
494 | #endif // defined(ANGLE_IS_64_BIT_CPU) |
495 | |
496 | namespace priv |
497 | { |
498 | |
499 | template <size_t N, typename T> |
500 | using EnableIfBitsFit = typename std::enable_if<N <= sizeof(T) * 8>::type; |
501 | |
502 | template <size_t N, typename Enable = void> |
503 | struct GetBitSet |
504 | { |
505 | using Type = IterableBitSet<N>; |
506 | }; |
507 | |
508 | // Prefer 64-bit bitsets on 64-bit CPUs. They seem faster than 32-bit. |
509 | #if defined(ANGLE_IS_64_BIT_CPU) |
510 | template <size_t N> |
511 | struct GetBitSet<N, EnableIfBitsFit<N, uint64_t>> |
512 | { |
513 | using Type = BitSet64<N>; |
514 | }; |
515 | #else |
516 | template <size_t N> |
517 | struct GetBitSet<N, EnableIfBitsFit<N, uint32_t>> |
518 | { |
519 | using Type = BitSet32<N>; |
520 | }; |
521 | #endif // defined(ANGLE_IS_64_BIT_CPU) |
522 | |
523 | } // namespace priv |
524 | |
525 | template <size_t N> |
526 | using BitSet = typename priv::GetBitSet<N>::Type; |
527 | |
528 | } // namespace angle |
529 | |
530 | template <size_t N, typename BitsT, typename ParamT> |
531 | inline angle::BitSetT<N, BitsT, ParamT> operator&(const angle::BitSetT<N, BitsT, ParamT> &lhs, |
532 | const angle::BitSetT<N, BitsT, ParamT> &rhs) |
533 | { |
534 | angle::BitSetT<N, BitsT, ParamT> result(lhs); |
535 | result &= rhs.bits(); |
536 | return result; |
537 | } |
538 | |
539 | template <size_t N, typename BitsT, typename ParamT> |
540 | inline angle::BitSetT<N, BitsT, ParamT> operator|(const angle::BitSetT<N, BitsT, ParamT> &lhs, |
541 | const angle::BitSetT<N, BitsT, ParamT> &rhs) |
542 | { |
543 | angle::BitSetT<N, BitsT, ParamT> result(lhs); |
544 | result |= rhs.bits(); |
545 | return result; |
546 | } |
547 | |
548 | template <size_t N, typename BitsT, typename ParamT> |
549 | inline angle::BitSetT<N, BitsT, ParamT> operator^(const angle::BitSetT<N, BitsT, ParamT> &lhs, |
550 | const angle::BitSetT<N, BitsT, ParamT> &rhs) |
551 | { |
552 | angle::BitSetT<N, BitsT, ParamT> result(lhs); |
553 | result ^= rhs.bits(); |
554 | return result; |
555 | } |
556 | |
557 | #endif // COMMON_BITSETITERATOR_H_ |
558 | |