Skip to content

Commit

Permalink
registers: Add defaulted comparison operators for register classes
Browse files Browse the repository at this point in the history
Previously we only had the direct equality operations provided. We can
just provide them all and make the interface flexible.
  • Loading branch information
lioncash committed Jan 10, 2024
1 parent b4d4c12 commit 7d5f2e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
30 changes: 11 additions & 19 deletions include/biscuit/registers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <biscuit/assert.hpp>

#include <climits>
#include <compare>
#include <cstdint>
#include <type_traits>

Expand All @@ -23,6 +24,9 @@ class Register {
return m_index;
}

friend constexpr bool operator==(Register, Register) = default;
friend constexpr auto operator<=>(Register, Register) = default;

protected:
constexpr Register(uint32_t index) noexcept
: m_index{index} {}
Expand All @@ -37,12 +41,8 @@ class GPR final : public Register {
constexpr GPR() noexcept : Register{0} {}
constexpr explicit GPR(uint32_t index) noexcept : Register{index} {}

friend constexpr bool operator==(GPR lhs, GPR rhs) noexcept {
return lhs.Index() == rhs.Index();
}
friend constexpr bool operator!=(GPR lhs, GPR rhs) noexcept {
return !operator==(lhs, rhs);
}
friend constexpr bool operator==(GPR, GPR) = default;
friend constexpr auto operator<=>(GPR, GPR) = default;
};

/// Floating point register.
Expand All @@ -51,12 +51,8 @@ class FPR final : public Register {
constexpr FPR() noexcept : Register{0} {}
constexpr explicit FPR(uint32_t index) noexcept : Register{index} {}

friend constexpr bool operator==(FPR lhs, FPR rhs) noexcept {
return lhs.Index() == rhs.Index();
}
friend constexpr bool operator!=(FPR lhs, FPR rhs) noexcept {
return !operator==(lhs, rhs);
}
friend constexpr bool operator==(FPR, FPR) = default;
friend constexpr auto operator<=>(FPR, FPR) = default;
};

/// Vector register.
Expand All @@ -65,12 +61,8 @@ class Vec final : public Register {
constexpr Vec() noexcept : Register{0} {}
constexpr explicit Vec(uint32_t index) noexcept : Register{index} {}

friend constexpr bool operator==(Vec lhs, Vec rhs) noexcept {
return lhs.Index() == rhs.Index();
}
friend constexpr bool operator!=(Vec lhs, Vec rhs) noexcept {
return !operator==(lhs, rhs);
}
friend constexpr bool operator==(Vec, Vec) = default;
friend constexpr auto operator<=>(Vec, Vec) = default;
};

// General-purpose Registers
Expand Down Expand Up @@ -309,7 +301,7 @@ class PushPopList final {

// Aside from ra, it's only valid for s0-s11 to show up the register list ranges.
[[nodiscard]] static constexpr bool IsSRegister(const GPR& gpr) {
return gpr == s0 || gpr == s1 || (gpr.Index() >= s2.Index() && gpr.Index() <= s11.Index());
return gpr == s0 || gpr == s1 || (gpr >= s2 && gpr <= s11);
}

uint32_t m_bitmask = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/assembler_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void EmitCMJTType(CodeBuffer& buffer, uint32_t funct6, uint32_t index, uint32_t

void EmitCMMVType(CodeBuffer& buffer, uint32_t funct6, GPR r1s, uint32_t funct2, GPR r2s, uint32_t op) {
const auto is_valid_s_register = [](GPR reg) {
return reg == s0 || reg == s1 || (reg.Index() >= s2.Index() && reg.Index() <= s7.Index());
return reg == s0 || reg == s1 || (reg >= s2 && reg <= s7);
};

BISCUIT_ASSERT(r1s != r2s);
Expand Down

0 comments on commit 7d5f2e8

Please sign in to comment.