bit_set: Make constexpr

This commit is contained in:
GPUCode
2023-12-09 22:48:12 +02:00
parent 0afaa31df5
commit 5c2a0c4e37
2 changed files with 32 additions and 42 deletions

View File

@@ -83,14 +83,6 @@ class GMainWindow : public QMainWindow {
/// Max number of recently loaded items to keep track of /// Max number of recently loaded items to keep track of
static const int max_recent_files_item = 10; static const int max_recent_files_item = 10;
// TODO: Make use of this!
enum {
UI_IDLE,
UI_EMU_BOOTING,
UI_EMU_RUNNING,
UI_EMU_STOPPING,
};
public: public:
void filterBarSetChecked(bool state); void filterBarSetChecked(bool state);
void UpdateUITheme(); void UpdateUITheme();

View File

@@ -92,8 +92,6 @@ static inline int LeastSignificantSetBit(u64 val) {
// operation.) // operation.)
// - Counting set bits using .Count() - see comment on that method. // - Counting set bits using .Count() - see comment on that method.
// TODO: use constexpr when MSVC gets out of the Dark Ages
template <typename IntTy> template <typename IntTy>
class BitSet { class BitSet {
static_assert(!std::is_signed_v<IntTy>, "BitSet should not be used with signed types"); static_assert(!std::is_signed_v<IntTy>, "BitSet should not be used with signed types");
@@ -102,12 +100,12 @@ public:
// A reference to a particular bit, returned from operator[]. // A reference to a particular bit, returned from operator[].
class Ref { class Ref {
public: public:
Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {}
Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} constexpr Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {}
operator bool() const { constexpr operator bool() const {
return (m_bs->m_val & m_mask) != 0; return (m_bs->m_val & m_mask) != 0;
} }
bool operator=(bool set) { constexpr bool operator=(bool set) {
m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0);
return set; return set;
} }
@@ -120,26 +118,26 @@ public:
// A STL-like iterator is required to be able to use range-based for loops. // A STL-like iterator is required to be able to use range-based for loops.
class Iterator { class Iterator {
public: public:
Iterator(const Iterator& other) : m_val(other.m_val) {} constexpr Iterator(const Iterator& other) : m_val(other.m_val) {}
Iterator(IntTy val) : m_val(val) {} constexpr Iterator(IntTy val) : m_val(val) {}
int operator*() { constexpr int operator*() {
// This will never be called when m_val == 0, because that would be the end() iterator // This will never be called when m_val == 0, because that would be the end() iterator
return LeastSignificantSetBit(m_val); return LeastSignificantSetBit(m_val);
} }
Iterator& operator++() { constexpr Iterator& operator++() {
// Unset least significant set bit // Unset least significant set bit
m_val &= m_val - IntTy(1); m_val &= m_val - IntTy(1);
return *this; return *this;
} }
Iterator operator++(int _) { constexpr Iterator operator++(int) {
Iterator other(*this); Iterator other(*this);
++*this; ++*this;
return other; return other;
} }
bool operator==(Iterator other) const { constexpr bool operator==(Iterator other) const {
return m_val == other.m_val; return m_val == other.m_val;
} }
bool operator!=(Iterator other) const { constexpr bool operator!=(Iterator other) const {
return m_val != other.m_val; return m_val != other.m_val;
} }
@@ -147,59 +145,59 @@ public:
IntTy m_val; IntTy m_val;
}; };
BitSet() : m_val(0) {} constexpr BitSet() : m_val(0) {}
explicit BitSet(IntTy val) : m_val(val) {} constexpr explicit BitSet(IntTy val) : m_val(val) {}
BitSet(std::initializer_list<int> init) { constexpr BitSet(std::initializer_list<int> init) {
m_val = 0; m_val = 0;
for (int bit : init) for (int bit : init)
m_val |= (IntTy)1 << bit; m_val |= (IntTy)1 << bit;
} }
static BitSet AllTrue(std::size_t count) { constexpr static BitSet AllTrue(std::size_t count) {
return BitSet(count == sizeof(IntTy) * 8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1)); return BitSet(count == sizeof(IntTy) * 8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1));
} }
Ref operator[](std::size_t bit) { constexpr Ref operator[](std::size_t bit) {
return Ref(this, (IntTy)1 << bit); return Ref(this, (IntTy)1 << bit);
} }
const Ref operator[](std::size_t bit) const { constexpr const Ref operator[](std::size_t bit) const {
return (*const_cast<BitSet*>(this))[bit]; return (*const_cast<BitSet*>(this))[bit];
} }
bool operator==(BitSet other) const { constexpr bool operator==(BitSet other) const {
return m_val == other.m_val; return m_val == other.m_val;
} }
bool operator!=(BitSet other) const { constexpr bool operator!=(BitSet other) const {
return m_val != other.m_val; return m_val != other.m_val;
} }
bool operator<(BitSet other) const { constexpr bool operator<(BitSet other) const {
return m_val < other.m_val; return m_val < other.m_val;
} }
bool operator>(BitSet other) const { constexpr bool operator>(BitSet other) const {
return m_val > other.m_val; return m_val > other.m_val;
} }
BitSet operator|(BitSet other) const { constexpr BitSet operator|(BitSet other) const {
return BitSet(m_val | other.m_val); return BitSet(m_val | other.m_val);
} }
BitSet operator&(BitSet other) const { constexpr BitSet operator&(BitSet other) const {
return BitSet(m_val & other.m_val); return BitSet(m_val & other.m_val);
} }
BitSet operator^(BitSet other) const { constexpr BitSet operator^(BitSet other) const {
return BitSet(m_val ^ other.m_val); return BitSet(m_val ^ other.m_val);
} }
BitSet operator~() const { constexpr BitSet operator~() const {
return BitSet(~m_val); return BitSet(~m_val);
} }
BitSet& operator|=(BitSet other) { constexpr BitSet& operator|=(BitSet other) {
return *this = *this | other; return *this = *this | other;
} }
BitSet& operator&=(BitSet other) { constexpr BitSet& operator&=(BitSet other) {
return *this = *this & other; return *this = *this & other;
} }
BitSet& operator^=(BitSet other) { constexpr BitSet& operator^=(BitSet other) {
return *this = *this ^ other; return *this = *this ^ other;
} }
operator u32() = delete; operator u32() = delete;
operator bool() { constexpr operator bool() {
return m_val != 0; return m_val != 0;
} }
@@ -207,14 +205,14 @@ public:
// Dolphin's official builds do not currently assume POPCNT support on x86, // Dolphin's official builds do not currently assume POPCNT support on x86,
// so slower explicit bit twiddling is generated. Still should generally // so slower explicit bit twiddling is generated. Still should generally
// be faster than a loop. // be faster than a loop.
unsigned int Count() const { constexpr u32 Count() const {
return CountSetBits(m_val); return CountSetBits(m_val);
} }
Iterator begin() const { constexpr Iterator begin() const {
return Iterator(m_val); return Iterator(m_val);
} }
Iterator end() const { constexpr Iterator end() const {
return Iterator(0); return Iterator(0);
} }