mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-11-18 01:38:08 -06:00
BitSet: Some small additions
Also add requirements for using range functions. And micro-optimize testing if a bit is set (GCC and MSVC struggle to emit `bt` or `tbz`/`tbnz` with `bs & (1 << n)` compared to `(bs >> n) & 1`).
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <bit>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <initializer_list>
|
||||
#include <type_traits>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
|
||||
@@ -29,28 +29,26 @@ namespace Common
|
||||
// operation.)
|
||||
// - Counting set bits using .Count() - see comment on that method.
|
||||
|
||||
template <typename IntTy>
|
||||
template <std::unsigned_integral IntTy>
|
||||
class BitSet
|
||||
{
|
||||
static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types");
|
||||
|
||||
public:
|
||||
// A reference to a particular bit, returned from operator[].
|
||||
class Ref
|
||||
{
|
||||
public:
|
||||
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 Ref(Ref&& other) : m_bs(other.m_bs), m_shift(other.m_shift) {}
|
||||
constexpr Ref(BitSet* bs, size_t shift) : m_bs(bs), m_shift(shift) {}
|
||||
constexpr operator bool() const { return (m_bs->m_val >> m_shift) & 1; }
|
||||
constexpr Ref& operator=(bool set)
|
||||
{
|
||||
m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0);
|
||||
return set;
|
||||
m_bs->m_val = (m_bs->m_val & ~(!set << m_shift)) | (set << m_shift);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
BitSet* m_bs;
|
||||
IntTy m_mask;
|
||||
size_t m_shift;
|
||||
};
|
||||
|
||||
// A STL-like iterator is required to be able to use range-based for loops.
|
||||
@@ -58,6 +56,9 @@ public:
|
||||
{
|
||||
public:
|
||||
constexpr Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {}
|
||||
using value_type = int;
|
||||
using difference_type = int;
|
||||
constexpr Iterator() : m_val(0), m_bit(-1) {}
|
||||
constexpr Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {}
|
||||
Iterator& operator=(Iterator other)
|
||||
{
|
||||
@@ -101,13 +102,15 @@ public:
|
||||
m_val |= IntTy{1} << bit;
|
||||
}
|
||||
|
||||
constexpr static BitSet AllTrue() { return BitSet(~IntTy{0}); }
|
||||
|
||||
constexpr static BitSet AllTrue(size_t count)
|
||||
{
|
||||
return BitSet(count == sizeof(IntTy) * 8 ? ~IntTy{0} : ((IntTy{1} << count) - 1));
|
||||
}
|
||||
|
||||
Ref operator[](size_t bit) { return Ref(this, IntTy{1} << bit); }
|
||||
constexpr const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; }
|
||||
constexpr Ref operator[](size_t bit) { return Ref(this, bit); }
|
||||
constexpr Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; }
|
||||
constexpr bool operator==(BitSet other) const { return m_val == other.m_val; }
|
||||
constexpr bool operator!=(BitSet other) const { return m_val != other.m_val; }
|
||||
constexpr bool operator<(BitSet other) const { return m_val < other.m_val; }
|
||||
@@ -131,6 +134,13 @@ public:
|
||||
constexpr unsigned int Count() const { return std::popcount(m_val); }
|
||||
constexpr Iterator begin() const { return ++Iterator(m_val, 0); }
|
||||
constexpr Iterator end() const { return Iterator(m_val, -1); }
|
||||
|
||||
template <std::unsigned_integral IntTyTo>
|
||||
constexpr BitSet<IntTyTo> Cast()
|
||||
{
|
||||
return BitSet<IntTyTo>((IntTyTo)m_val);
|
||||
}
|
||||
|
||||
IntTy m_val{};
|
||||
};
|
||||
} // namespace Common
|
||||
|
||||
@@ -1338,7 +1338,8 @@ BitSet8 Jit64::ComputeStaticGQRs(const PPCAnalyst::CodeBlock& cb) const
|
||||
|
||||
BitSet32 Jit64::CallerSavedRegistersInUse(BitSet32 additional_registers) const
|
||||
{
|
||||
BitSet32 in_use = gpr.RegistersInUse() | (fpr.RegistersInUse() << 16) | additional_registers;
|
||||
BitSet32 in_use = gpr.RegistersInUse().Cast<u32>() | (fpr.RegistersInUse().Cast<u32>() << 16) |
|
||||
additional_registers;
|
||||
return in_use & ABI_ALL_CALLER_SAVED;
|
||||
}
|
||||
|
||||
|
||||
@@ -396,9 +396,9 @@ void RegCache::PreloadRegisters(BitSet32 to_preload)
|
||||
}
|
||||
}
|
||||
|
||||
BitSet32 RegCache::RegistersInUse() const
|
||||
BitSet16 RegCache::RegistersInUse() const
|
||||
{
|
||||
BitSet32 result;
|
||||
BitSet16 result;
|
||||
for (size_t i = 0; i < m_xregs.size(); i++)
|
||||
{
|
||||
if (!m_xregs[i].IsFree())
|
||||
|
||||
@@ -178,7 +178,7 @@ public:
|
||||
|
||||
RCForkGuard Fork();
|
||||
void Discard(BitSet32 pregs);
|
||||
void Flush(BitSet32 pregs = BitSet32::AllTrue(32), FlushMode mode = FlushMode::Full,
|
||||
void Flush(BitSet32 pregs = BitSet32::AllTrue(), FlushMode mode = FlushMode::Full,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers = IgnoreDiscardedRegisters::No);
|
||||
void Reset(BitSet32 pregs);
|
||||
BitSet32 RegistersRevertable() const;
|
||||
@@ -187,7 +187,7 @@ public:
|
||||
bool IsAllUnlocked() const;
|
||||
|
||||
void PreloadRegisters(BitSet32 pregs);
|
||||
BitSet32 RegistersInUse() const;
|
||||
BitSet16 RegistersInUse() const;
|
||||
|
||||
protected:
|
||||
friend class RCOpArg;
|
||||
|
||||
@@ -348,8 +348,8 @@ void Arm64GPRCache::ResetCRRegisters(BitSet8 regs)
|
||||
void Arm64GPRCache::Flush(FlushMode mode, ARM64Reg tmp_reg,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers)
|
||||
{
|
||||
FlushRegisters(BitSet32(0xFFFFFFFF), mode, tmp_reg, ignore_discarded_registers);
|
||||
FlushCRRegisters(BitSet8(0xFF), mode, tmp_reg, ignore_discarded_registers);
|
||||
FlushRegisters(BitSet32::AllTrue(), mode, tmp_reg, ignore_discarded_registers);
|
||||
FlushCRRegisters(BitSet8::AllTrue(), mode, tmp_reg, ignore_discarded_registers);
|
||||
}
|
||||
|
||||
ARM64Reg Arm64GPRCache::BindForRead(size_t index)
|
||||
|
||||
@@ -622,7 +622,7 @@ void JitArm64::GenerateQuantizedLoads()
|
||||
BitSet32 gprs_to_push = CALLER_SAVED_GPRS & ~BitSet32{0, 3};
|
||||
if (!jo.memcheck)
|
||||
gprs_to_push &= ~BitSet32{1};
|
||||
BitSet32 fprs_to_push = BitSet32(0xFFFFFFFF) & ~BitSet32{0, 1};
|
||||
BitSet32 fprs_to_push = ~BitSet32{0, 1};
|
||||
ARM64FloatEmitter float_emit(this);
|
||||
|
||||
const u8* start = GetCodePtr();
|
||||
@@ -834,7 +834,7 @@ void JitArm64::GenerateQuantizedStores()
|
||||
gprs_to_push &= ~BitSet32{2};
|
||||
if (!jo.fastmem)
|
||||
gprs_to_push &= ~BitSet32{3};
|
||||
BitSet32 fprs_to_push = BitSet32(0xFFFFFFFF) & ~BitSet32{0, 1};
|
||||
BitSet32 fprs_to_push = ~BitSet32{0, 1};
|
||||
ARM64FloatEmitter float_emit(this);
|
||||
|
||||
const u8* start = GetCodePtr();
|
||||
|
||||
@@ -608,7 +608,7 @@ void PPCAnalyzer::SetInstructionStats(CodeBlock* block, CodeOp* code,
|
||||
code->crIn = BitSet8(0);
|
||||
if (opinfo->flags & FL_READ_ALL_CR)
|
||||
{
|
||||
code->crIn = BitSet8(0xFF);
|
||||
code->crIn = BitSet8::AllTrue();
|
||||
}
|
||||
else if (opinfo->flags & FL_READ_CRn)
|
||||
{
|
||||
@@ -630,7 +630,7 @@ void PPCAnalyzer::SetInstructionStats(CodeBlock* block, CodeOp* code,
|
||||
|
||||
code->crOut = BitSet8(0);
|
||||
if (opinfo->flags & FL_SET_ALL_CR)
|
||||
code->crOut = BitSet8(0xFF);
|
||||
code->crOut = BitSet8::AllTrue();
|
||||
else if (opinfo->flags & FL_SET_CRn)
|
||||
code->crOut[code->inst.CRFD] = true;
|
||||
else if ((opinfo->flags & FL_SET_CR0) || ((opinfo->flags & FL_RC_BIT) && code->inst.Rc))
|
||||
|
||||
@@ -135,8 +135,8 @@ void UpdateVertexArrayPointers()
|
||||
void MarkAllDirty()
|
||||
{
|
||||
g_bases_dirty = true;
|
||||
g_main_vat_dirty = BitSet8::AllTrue(8);
|
||||
g_preprocess_vat_dirty = BitSet8::AllTrue(8);
|
||||
g_main_vat_dirty = BitSet8::AllTrue();
|
||||
g_preprocess_vat_dirty = BitSet8::AllTrue();
|
||||
g_needs_cp_xf_consistency_check = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,13 +18,15 @@ TEST(BitSet, Basics)
|
||||
EXPECT_EQ(bs2, bs2);
|
||||
EXPECT_NE(bs2, bs3);
|
||||
EXPECT_EQ(BitSet32(0xfff), BitSet32::AllTrue(12));
|
||||
EXPECT_EQ(BitSet64(0xffffffffffffffff), BitSet64::AllTrue(64));
|
||||
EXPECT_EQ(BitSet64(0xffffffffffffffff), BitSet64::AllTrue());
|
||||
}
|
||||
|
||||
TEST(BitSet, BitGetSet)
|
||||
{
|
||||
BitSet32 bs;
|
||||
bs[3] = bs[8] = bs[11] = true;
|
||||
bs[3] = true;
|
||||
bs[8] = true;
|
||||
bs[11] = true;
|
||||
EXPECT_TRUE(bs[3]);
|
||||
EXPECT_FALSE(bs[4]);
|
||||
EXPECT_EQ((u32)((1 << 3) | (1 << 8) | (1 << 11)), bs.m_val);
|
||||
|
||||
Reference in New Issue
Block a user