BitSet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // This file is under the public domain.
  2. #pragma once
  3. #include <cstddef>
  4. #include <initializer_list>
  5. #include <type_traits>
  6. #include "CommonTypes.h"
  7. // Helper functions:
  8. #ifdef _WIN32
  9. template <typename T>
  10. static inline int CountSetBits(T v)
  11. {
  12. // from https://graphics.stanford.edu/~seander/bithacks.html
  13. // GCC has this built in, but MSVC's intrinsic will only emit the actual
  14. // POPCNT instruction, which we're not depending on
  15. v = v - ((v >> 1) & (T)~(T)0/3);
  16. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  17. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  18. return (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8;
  19. }
  20. static inline int LeastSignificantSetBit(u8 val)
  21. {
  22. unsigned long index;
  23. _BitScanForward(&index, val);
  24. return (int)index;
  25. }
  26. static inline int LeastSignificantSetBit(u16 val)
  27. {
  28. unsigned long index;
  29. _BitScanForward(&index, val);
  30. return (int)index;
  31. }
  32. static inline int LeastSignificantSetBit(u32 val)
  33. {
  34. unsigned long index;
  35. _BitScanForward(&index, val);
  36. return (int)index;
  37. }
  38. static inline int LeastSignificantSetBit(u64 val)
  39. {
  40. unsigned long index;
  41. _BitScanForward64(&index, val);
  42. return (int)index;
  43. }
  44. #else
  45. static inline int CountSetBits(u8 val) { return __builtin_popcount(val); }
  46. static inline int CountSetBits(u16 val) { return __builtin_popcount(val); }
  47. static inline int CountSetBits(u32 val) { return __builtin_popcount(val); }
  48. static inline int CountSetBits(u64 val) { return __builtin_popcountll(val); }
  49. static inline int LeastSignificantSetBit(u8 val) { return __builtin_ctz(val); }
  50. static inline int LeastSignificantSetBit(u16 val) { return __builtin_ctz(val); }
  51. static inline int LeastSignificantSetBit(u32 val) { return __builtin_ctz(val); }
  52. static inline int LeastSignificantSetBit(u64 val) { return __builtin_ctzll(val); }
  53. #endif
  54. // namespace avoids conflict with OS X Carbon; don't use BitSet<T> directly
  55. namespace BS
  56. {
  57. // Similar to std::bitset, this is a class which encapsulates a bitset, i.e.
  58. // using the set bits of an integer to represent a set of integers. Like that
  59. // class, it acts like an array of bools:
  60. // BitSet32 bs;
  61. // bs[1] = true;
  62. // but also like the underlying integer ([0] = least significant bit):
  63. // BitSet32 bs2 = ...;
  64. // bs = (bs ^ bs2) & BitSet32(0xffff);
  65. // The following additional functionality is provided:
  66. // - Construction using an initializer list.
  67. // BitSet bs { 1, 2, 4, 8 };
  68. // - Efficiently iterating through the set bits:
  69. // for (int i : bs)
  70. // [i is the *index* of a set bit]
  71. // (This uses the appropriate CPU instruction to find the next set bit in one
  72. // operation.)
  73. // - Counting set bits using .Count() - see comment on that method.
  74. // TODO: use constexpr when MSVC gets out of the Dark Ages
  75. template <typename IntTy>
  76. class BitSet
  77. {
  78. static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types");
  79. public:
  80. // A reference to a particular bit, returned from operator[].
  81. class Ref
  82. {
  83. public:
  84. Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {}
  85. Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {}
  86. operator bool() const { return (m_bs->m_val & m_mask) != 0; }
  87. bool operator=(bool set)
  88. {
  89. m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0);
  90. return set;
  91. }
  92. private:
  93. BitSet* m_bs;
  94. IntTy m_mask;
  95. };
  96. // A STL-like iterator is required to be able to use range-based for loops.
  97. class Iterator
  98. {
  99. public:
  100. Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {}
  101. Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {}
  102. Iterator& operator=(Iterator other) { new (this) Iterator(other); return *this; }
  103. int operator*() { return m_bit; }
  104. Iterator& operator++()
  105. {
  106. if (m_val == 0)
  107. {
  108. m_bit = -1;
  109. }
  110. else
  111. {
  112. int bit = LeastSignificantSetBit(m_val);
  113. m_val &= ~(1 << bit);
  114. m_bit = bit;
  115. }
  116. return *this;
  117. }
  118. Iterator operator++(int _)
  119. {
  120. Iterator other(*this);
  121. ++*this;
  122. return other;
  123. }
  124. bool operator==(Iterator other) const { return m_bit == other.m_bit; }
  125. bool operator!=(Iterator other) const { return m_bit != other.m_bit; }
  126. private:
  127. IntTy m_val;
  128. int m_bit;
  129. };
  130. BitSet() : m_val(0) {}
  131. explicit BitSet(IntTy val) : m_val(val) {}
  132. BitSet(std::initializer_list<int> init)
  133. {
  134. m_val = 0;
  135. for (int bit : init)
  136. m_val |= (IntTy)1 << bit;
  137. }
  138. static BitSet AllTrue(size_t count)
  139. {
  140. return BitSet(count == sizeof(IntTy)*8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1));
  141. }
  142. Ref operator[](size_t bit) { return Ref(this, (IntTy)1 << bit); }
  143. const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; }
  144. bool operator==(BitSet other) const { return m_val == other.m_val; }
  145. bool operator!=(BitSet other) const { return m_val != other.m_val; }
  146. BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); }
  147. BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); }
  148. BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); }
  149. BitSet operator~() const { return BitSet(~m_val); }
  150. BitSet& operator|=(BitSet other) { return *this = *this | other; }
  151. BitSet& operator&=(BitSet other) { return *this = *this & other; }
  152. BitSet& operator^=(BitSet other) { return *this = *this ^ other; }
  153. operator u32() = delete;
  154. operator bool() { return m_val != 0; }
  155. // Warning: Even though on modern CPUs this is a single fast instruction,
  156. // Dolphin's official builds do not currently assume POPCNT support on x86,
  157. // so slower explicit bit twiddling is generated. Still should generally
  158. // be faster than a loop.
  159. unsigned int Count() const { return CountSetBits(m_val); }
  160. Iterator begin() const { Iterator it(m_val, 0); return ++it; }
  161. Iterator end() const { return Iterator(m_val, -1); }
  162. IntTy m_val;
  163. };
  164. }
  165. typedef BS::BitSet<u8> BitSet8;
  166. typedef BS::BitSet<u16> BitSet16;
  167. typedef BS::BitSet<u32> BitSet32;
  168. typedef BS::BitSet<u64> BitSet64;