bit_set.h 6.5 KB

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