bit_set.h 6.9 KB

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