diff --git a/pcsx2/R5900OpcodeImpl.cpp b/pcsx2/R5900OpcodeImpl.cpp index 8141d7b806..b5c5d94cd8 100644 --- a/pcsx2/R5900OpcodeImpl.cpp +++ b/pcsx2/R5900OpcodeImpl.cpp @@ -60,6 +60,31 @@ static __fi bool _add32_Overflow( s32 x, s32 y, s64 &ret ) return false; } +static __fi bool _sub64_Overflow(s64 x, s64 y, s64& ret) +{ + const s64 result = static_cast(static_cast(x) - static_cast(y)); + // Overflow only happens if the sign differs on the x and y. + if (((x ^ y) & (x ^ result)) < 0) + { + cpuException(0x30, cpuRegs.branch); + return true; + } + ret = result; + return false; +} + +static __fi bool _sub32_Overflow(s32 x, s32 y, s64& ret) +{ + const s64 result = static_cast(x) - y; + + if (result != static_cast(result)) + { + cpuException(0x30, cpuRegs.branch); + return true; + } + ret = result; + return false; +} const R5900::OPCODE& R5900::GetCurrentInstruction() { @@ -347,7 +372,7 @@ void DADD() void SUB() { s64 result; - bool overflow = _add32_Overflow( cpuRegs.GPR.r[_Rs_].SD[0], -cpuRegs.GPR.r[_Rt_].SD[0], result ); + bool overflow = _sub32_Overflow(cpuRegs.GPR.r[_Rs_].SD[0], cpuRegs.GPR.r[_Rt_].SD[0], result); if (overflow || !_Rd_) return; cpuRegs.GPR.r[_Rd_].SD[0] = result; } @@ -356,7 +381,7 @@ void SUB() void DSUB() { s64 result; - bool overflow = _add64_Overflow( cpuRegs.GPR.r[_Rs_].SD[0], -cpuRegs.GPR.r[_Rt_].SD[0], result ); + bool overflow = _sub64_Overflow(cpuRegs.GPR.r[_Rs_].SD[0], cpuRegs.GPR.r[_Rt_].SD[0], result); if (overflow || !_Rd_) return; cpuRegs.GPR.r[_Rd_].SD[0] = result; }