BitSet.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: CC0-1.0
  2. #pragma once
  3. #include <bit>
  4. #include <cstddef>
  5. #include <initializer_list>
  6. #include <type_traits>
  7. #include "Common/CommonTypes.h"
  8. namespace Common
  9. {
  10. // Similar to std::bitset, this is a class which encapsulates a bitset, i.e.
  11. // using the set bits of an integer to represent a set of integers. Like that
  12. // class, it acts like an array of bools:
  13. // BitSet32 bs;
  14. // bs[1] = true;
  15. // but also like the underlying integer ([0] = least significant bit):
  16. // BitSet32 bs2 = ...;
  17. // bs = (bs ^ bs2) & BitSet32(0xffff);
  18. // The following additional functionality is provided:
  19. // - Construction using an initializer list.
  20. // BitSet bs { 1, 2, 4, 8 };
  21. // - Efficiently iterating through the set bits:
  22. // for (int i : bs)
  23. // [i is the *index* of a set bit]
  24. // (This uses the appropriate CPU instruction to find the next set bit in one
  25. // operation.)
  26. // - Counting set bits using .Count() - see comment on that method.
  27. template <typename IntTy>
  28. class BitSet
  29. {
  30. static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types");
  31. public:
  32. // A reference to a particular bit, returned from operator[].
  33. class Ref
  34. {
  35. public:
  36. constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {}
  37. constexpr Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {}
  38. constexpr operator bool() const { return (m_bs->m_val & m_mask) != 0; }
  39. bool operator=(bool set)
  40. {
  41. m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0);
  42. return set;
  43. }
  44. private:
  45. BitSet* m_bs;
  46. IntTy m_mask;
  47. };
  48. // A STL-like iterator is required to be able to use range-based for loops.
  49. class Iterator
  50. {
  51. public:
  52. constexpr Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {}
  53. constexpr Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {}
  54. Iterator& operator=(Iterator other)
  55. {
  56. new (this) Iterator(other);
  57. return *this;
  58. }
  59. Iterator& operator++()
  60. {
  61. if (m_val == 0)
  62. {
  63. m_bit = -1;
  64. }
  65. else
  66. {
  67. int bit = std::countr_zero(m_val);
  68. m_val &= ~(IntTy{1} << bit);
  69. m_bit = bit;
  70. }
  71. return *this;
  72. }
  73. Iterator operator++(int)
  74. {
  75. Iterator other(*this);
  76. ++*this;
  77. return other;
  78. }
  79. constexpr int operator*() const { return m_bit; }
  80. constexpr bool operator==(Iterator other) const { return m_bit == other.m_bit; }
  81. constexpr bool operator!=(Iterator other) const { return m_bit != other.m_bit; }
  82. private:
  83. IntTy m_val;
  84. int m_bit;
  85. };
  86. constexpr BitSet() = default;
  87. constexpr explicit BitSet(IntTy val) : m_val(val) {}
  88. constexpr BitSet(std::initializer_list<int> init)
  89. {
  90. for (int bit : init)
  91. m_val |= IntTy{1} << bit;
  92. }
  93. constexpr static BitSet AllTrue(size_t count)
  94. {
  95. return BitSet(count == sizeof(IntTy) * 8 ? ~IntTy{0} : ((IntTy{1} << count) - 1));
  96. }
  97. Ref operator[](size_t bit) { return Ref(this, IntTy{1} << bit); }
  98. constexpr const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; }
  99. constexpr bool operator==(BitSet other) const { return m_val == other.m_val; }
  100. constexpr bool operator!=(BitSet other) const { return m_val != other.m_val; }
  101. constexpr bool operator<(BitSet other) const { return m_val < other.m_val; }
  102. constexpr bool operator>(BitSet other) const { return m_val > other.m_val; }
  103. constexpr BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); }
  104. constexpr BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); }
  105. constexpr BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); }
  106. constexpr BitSet operator~() const { return BitSet(~m_val); }
  107. constexpr BitSet operator<<(IntTy shift) const { return BitSet(m_val << shift); }
  108. constexpr BitSet operator>>(IntTy shift) const { return BitSet(m_val >> shift); }
  109. constexpr explicit operator bool() const { return m_val != 0; }
  110. BitSet& operator|=(BitSet other) { return *this = *this | other; }
  111. BitSet& operator&=(BitSet other) { return *this = *this & other; }
  112. BitSet& operator^=(BitSet other) { return *this = *this ^ other; }
  113. BitSet& operator<<=(IntTy shift) { return *this = *this << shift; }
  114. BitSet& operator>>=(IntTy shift) { return *this = *this >> shift; }
  115. // Warning: Even though on modern CPUs this is a single fast instruction,
  116. // Dolphin's official builds do not currently assume POPCNT support on x86,
  117. // so slower explicit bit twiddling is generated. Still should generally
  118. // be faster than a loop.
  119. constexpr unsigned int Count() const { return std::popcount(m_val); }
  120. constexpr Iterator begin() const { return ++Iterator(m_val, 0); }
  121. constexpr Iterator end() const { return Iterator(m_val, -1); }
  122. IntTy m_val{};
  123. };
  124. } // namespace Common
  125. using BitSet8 = Common::BitSet<u8>;
  126. using BitSet16 = Common::BitSet<u16>;
  127. using BitSet32 = Common::BitSet<u32>;
  128. using BitSet64 = Common::BitSet<u64>;