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
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:
void filterBarSetChecked(bool state);
void UpdateUITheme();

View File

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