diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2026-11-15 16:56:08 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-15 16:56:08 -0600 |
| commit | 545bf5d5cc3bb94463ed0f4519594f5075397786 (patch) | |
| tree | 48618c02ca13e4f919ae2008f3058d72f033ca77 /Source/Core | |
| parent | 4f9d60d80e33ad03d5bec6c18e03f3bd1b40a801 (diff) | |
| parent | 95d7468be5a9d9407310df48df7a6711573a9a7e (diff) | |
Merge pull request #15004 from dokslade/interp-addme-carry
Interpreter: Fix addme/subfme carry and share add/subtract code
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Core/PowerPC/Interpreter/Interpreter.h | 9 | ||||
| -rw-r--r-- | Source/Core/Core/PowerPC/Interpreter/Interpreter_Integer.cpp | 213 |
2 files changed, 73 insertions, 149 deletions
diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h index 52dd0d6cc9..d051ae960c 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter.h +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter.h @@ -304,6 +304,15 @@ private: template <typename T> static void Helper_IntCompare(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, T a, T b); + template <bool update_ca, bool xo_form> + static void Helper_Add(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, u32 a, u32 b, + u32 carry_in); + static void Helper_AddXO(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, u32 a, u32 b, + u32 carry_in); + static void Helper_AddXOCarrying(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, u32 a, + u32 b, u32 carry_in); + static void Helper_AddImmediateCarrying(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, + u32 a, u32 b, u32 carry_in); static void Helper_FloatCompareOrdered(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, double a, double b); static void Helper_FloatCompareUnordered(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter_Integer.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter_Integer.cpp index 975f408068..d85a54169c 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter_Integer.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter_Integer.cpp @@ -35,6 +35,55 @@ u32 Interpreter::Helper_Carry(u32 value1, u32 value2) return value2 > (~value1); } +// All PowerPC add/subtract instructions compute a + b + carry_in (subtractions use ~rA as a). +// Writes rD and, if update_ca, CA. OV and CR0 are only written for the XO-form instructions, as +// requested by their OE and Rc bits. +template <bool update_ca, bool xo_form> +inline void Interpreter::Helper_Add(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, u32 a, + u32 b, u32 carry_in) +{ + const u64 sum = u64{a} + u64{b} + u64{carry_in}; + const u32 result = static_cast<u32>(sum); + + ppc_state.gpr[inst.RD] = result; + + if constexpr (update_ca) + ppc_state.SetCarry(static_cast<u32>(sum >> 32)); + + if constexpr (xo_form) + { + // Signed overflow: a and b have the same sign and the result has the other one. + if (inst.OE) + ppc_state.SetXER_OV(((a ^ result) & (b ^ result)) >> 31); + + if (inst.Rc) + Helper_UpdateCR0(ppc_state, result); + } +} + +// XO-form instructions that leave CA unchanged (add, subf, neg). +inline void Interpreter::Helper_AddXO(PowerPC::PowerPCState& ppc_state, UGeckoInstruction inst, + u32 a, u32 b, u32 carry_in) +{ + Helper_Add<false, true>(ppc_state, inst, a, b, carry_in); +} + +// XO-form instructions that update CA (addc, adde, addme, addze and their subf counterparts). +inline void Interpreter::Helper_AddXOCarrying(PowerPC::PowerPCState& ppc_state, + UGeckoInstruction inst, u32 a, u32 b, u32 carry_in) +{ + Helper_Add<true, true>(ppc_state, inst, a, b, carry_in); +} + +// D-form instructions (addic, subfic). They never update OV or CR0: the OE and Rc bits of +// UGeckoInstruction overlap their immediate. +inline void Interpreter::Helper_AddImmediateCarrying(PowerPC::PowerPCState& ppc_state, + UGeckoInstruction inst, u32 a, u32 b, + u32 carry_in) +{ + Helper_Add<true, false>(ppc_state, inst, a, b, carry_in); +} + void Interpreter::addi(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; @@ -47,11 +96,7 @@ void Interpreter::addi(Interpreter& interpreter, UGeckoInstruction inst) void Interpreter::addic(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ppc_state.gpr[inst.RA]; - const u32 imm = u32(s32{inst.SIMM_16}); - - ppc_state.gpr[inst.RD] = a + imm; - ppc_state.SetCarry(Helper_Carry(a, imm)); + Helper_AddImmediateCarrying(ppc_state, inst, ppc_state.gpr[inst.RA], u32(s32{inst.SIMM_16}), 0); } void Interpreter::addic_rc(Interpreter& interpreter, UGeckoInstruction inst) @@ -140,10 +185,7 @@ void Interpreter::oris(Interpreter& interpreter, UGeckoInstruction inst) void Interpreter::subfic(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const s32 a = s32(ppc_state.gpr[inst.RA]); - const s32 immediate = inst.SIMM_16; - ppc_state.gpr[inst.RD] = u32(immediate - a); - ppc_state.SetCarry((a == 0) || (Helper_Carry(0 - u32(a), u32(immediate)))); + Helper_AddImmediateCarrying(ppc_state, inst, ~ppc_state.gpr[inst.RA], u32(s32{inst.SIMM_16}), 1); } void Interpreter::twi(Interpreter& interpreter, UGeckoInstruction inst) @@ -404,97 +446,35 @@ void Interpreter::xorx(Interpreter& interpreter, UGeckoInstruction inst) Helper_UpdateCR0(ppc_state, ppc_state.gpr[inst.RA]); } -static bool HasAddOverflowed(u32 x, u32 y, u32 result) -{ - // If x and y have the same sign, but the result is different - // then an overflow has occurred. - return (((x ^ result) & (y ^ result)) >> 31) != 0; -} - void Interpreter::addx(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 result = a + b; - - ppc_state.gpr[inst.RD] = result; - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXO(ppc_state, inst, ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], 0); } void Interpreter::addcx(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 result = a + b; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, b)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], 0); } void Interpreter::addex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 carry = ppc_state.GetCarry(); - const u32 a = ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 result = a + b + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, b) || (carry != 0 && Helper_Carry(a + b, carry))); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], + ppc_state.GetCarry()); } void Interpreter::addmex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 carry = ppc_state.GetCarry(); - const u32 a = ppc_state.gpr[inst.RA]; - const u32 b = 0xFFFFFFFF; - const u32 result = a + b + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, carry - 1)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ppc_state.gpr[inst.RA], 0xFFFFFFFF, ppc_state.GetCarry()); } void Interpreter::addzex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 carry = ppc_state.GetCarry(); - const u32 a = ppc_state.gpr[inst.RA]; - const u32 result = a + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, carry)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, 0, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ppc_state.gpr[inst.RA], 0, ppc_state.GetCarry()); } void Interpreter::divwx(Interpreter& interpreter, UGeckoInstruction inst) @@ -591,101 +571,36 @@ void Interpreter::mullwx(Interpreter& interpreter, UGeckoInstruction inst) void Interpreter::negx(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ppc_state.gpr[inst.RA]; - - ppc_state.gpr[inst.RD] = (~a) + 1; - - if (inst.OE) - ppc_state.SetXER_OV(a == 0x80000000); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, ppc_state.gpr[inst.RD]); + Helper_AddXO(ppc_state, inst, ~ppc_state.gpr[inst.RA], 0, 1); } void Interpreter::subfx(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ~ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 result = a + b + 1; - - ppc_state.gpr[inst.RD] = result; - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXO(ppc_state, inst, ~ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], 1); } void Interpreter::subfcx(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ~ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 result = a + b + 1; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(a == 0xFFFFFFFF || Helper_Carry(b, a + 1)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ~ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], 1); } void Interpreter::subfex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ~ppc_state.gpr[inst.RA]; - const u32 b = ppc_state.gpr[inst.RB]; - const u32 carry = ppc_state.GetCarry(); - const u32 result = a + b + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, b) || Helper_Carry(a + b, carry)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ~ppc_state.gpr[inst.RA], ppc_state.gpr[inst.RB], + ppc_state.GetCarry()); } -// sub from minus one void Interpreter::subfmex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ~ppc_state.gpr[inst.RA]; - const u32 b = 0xFFFFFFFF; - const u32 carry = ppc_state.GetCarry(); - const u32 result = a + b + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, carry - 1)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, b, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ~ppc_state.gpr[inst.RA], 0xFFFFFFFF, ppc_state.GetCarry()); } -// sub from zero void Interpreter::subfzex(Interpreter& interpreter, UGeckoInstruction inst) { auto& ppc_state = interpreter.m_ppc_state; - const u32 a = ~ppc_state.gpr[inst.RA]; - const u32 carry = ppc_state.GetCarry(); - const u32 result = a + carry; - - ppc_state.gpr[inst.RD] = result; - ppc_state.SetCarry(Helper_Carry(a, carry)); - - if (inst.OE) - ppc_state.SetXER_OV(HasAddOverflowed(a, 0, result)); - - if (inst.Rc) - Helper_UpdateCR0(ppc_state, result); + Helper_AddXOCarrying(ppc_state, inst, ~ppc_state.gpr[inst.RA], 0, ppc_state.GetCarry()); } |
