Merge pull request #6943 from lioncash/overflow

Interpreter/Jit64/JitArm64: Correct negative overflow handling for divw
This commit is contained in:
Markus Wick 2018-05-28 09:49:19 +02:00 committed by GitHub
commit 9e102e1584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 31 deletions

View File

@ -501,18 +501,18 @@ void Interpreter::divwx(UGeckoInstruction inst)
{ {
const s32 a = rGPR[inst.RA]; const s32 a = rGPR[inst.RA];
const s32 b = rGPR[inst.RB]; const s32 b = rGPR[inst.RB];
const bool overflow = b == 0 || ((u32)a == 0x80000000 && b == -1); const bool overflow = b == 0 || (static_cast<u32>(a) == 0x80000000 && b == -1);
if (overflow) if (overflow)
{ {
if (((u32)a & 0x80000000) && b == 0) if (a < 0)
rGPR[inst.RD] = UINT32_MAX; rGPR[inst.RD] = UINT32_MAX;
else else
rGPR[inst.RD] = 0; rGPR[inst.RD] = 0;
} }
else else
{ {
rGPR[inst.RD] = (u32)(a / b); rGPR[inst.RD] = static_cast<u32>(a / b);
} }
if (inst.OE) if (inst.OE)

View File

@ -1223,7 +1223,8 @@ void Jit64::divwx(UGeckoInstruction inst)
s32 i = gpr.R(a).SImm32(), j = gpr.R(b).SImm32(); s32 i = gpr.R(a).SImm32(), j = gpr.R(b).SImm32();
if (j == 0 || (i == (s32)0x80000000 && j == -1)) if (j == 0 || (i == (s32)0x80000000 && j == -1))
{ {
gpr.SetImmediate32(d, (i >> 31) ^ j); const u32 result = i < 0 ? 0xFFFFFFFF : 0x00000000;
gpr.SetImmediate32(d, result);
if (inst.OE) if (inst.OE)
GenerateConstantOverflow(true); GenerateConstantOverflow(true);
} }
@ -1241,38 +1242,37 @@ void Jit64::divwx(UGeckoInstruction inst)
gpr.FlushLockX(EAX, EDX); gpr.FlushLockX(EAX, EDX);
gpr.BindToRegister(d, (d == a || d == b), true); gpr.BindToRegister(d, (d == a || d == b), true);
MOV(32, R(EAX), gpr.R(a)); MOV(32, R(EAX), gpr.R(a));
CDQ();
gpr.BindToRegister(b, true, false); gpr.BindToRegister(b, true, false);
TEST(32, gpr.R(b), gpr.R(b)); TEST(32, gpr.R(b), gpr.R(b));
FixupBranch not_div_by_zero = J_CC(CC_NZ); const FixupBranch overflow = J_CC(CC_E);
MOV(32, gpr.R(d), R(EDX));
if (inst.OE) CMP(32, R(EAX), Imm32(0x80000000));
{ const FixupBranch normal_path1 = J_CC(CC_NE);
GenerateConstantOverflow(true);
} CMP(32, gpr.R(b), Imm32(0xFFFFFFFF));
FixupBranch end1 = J(); const FixupBranch normal_path2 = J_CC(CC_NE);
SetJumpTarget(not_div_by_zero);
CMP(32, gpr.R(b), R(EDX)); SetJumpTarget(overflow);
FixupBranch not_div_by_neg_one = J_CC(CC_NZ); SAR(32, R(EAX), Imm8(31));
MOV(32, gpr.R(d), R(EAX)); MOV(32, gpr.R(d), R(EAX));
NEG(32, gpr.R(d));
FixupBranch no_overflow = J_CC(CC_NO);
XOR(32, gpr.R(d), gpr.R(d));
if (inst.OE) if (inst.OE)
{ {
GenerateConstantOverflow(true); GenerateConstantOverflow(true);
} }
FixupBranch end2 = J(); const FixupBranch done = J();
SetJumpTarget(not_div_by_neg_one);
SetJumpTarget(normal_path1);
SetJumpTarget(normal_path2);
CDQ();
IDIV(32, gpr.R(b)); IDIV(32, gpr.R(b));
MOV(32, gpr.R(d), R(EAX)); MOV(32, gpr.R(d), R(EAX));
SetJumpTarget(no_overflow);
if (inst.OE) if (inst.OE)
{ {
GenerateConstantOverflow(false); GenerateConstantOverflow(false);
} }
SetJumpTarget(end1); SetJumpTarget(done);
SetJumpTarget(end2);
} }
if (inst.Rc) if (inst.Rc)
ComputeRC(gpr.R(d)); ComputeRC(gpr.R(d));

View File

@ -1164,17 +1164,17 @@ void JitArm64::divwx(UGeckoInstruction inst)
{ {
s32 imm_a = gpr.GetImm(a); s32 imm_a = gpr.GetImm(a);
s32 imm_b = gpr.GetImm(b); s32 imm_b = gpr.GetImm(b);
s32 imm_d; u32 imm_d;
if (imm_b == 0 || ((u32)imm_a == 0x80000000 && imm_b == -1)) if (imm_b == 0 || (static_cast<u32>(imm_a) == 0x80000000 && imm_b == -1))
{ {
if (((u32)imm_a & 0x80000000) && imm_b == 0) if (imm_a < 0)
imm_d = -1; imm_d = 0xFFFFFFFF;
else else
imm_d = 0; imm_d = 0;
} }
else else
{ {
imm_d = (u32)(imm_a / imm_b); imm_d = static_cast<u32>(imm_a / imm_b);
} }
gpr.SetImmediate(d, imm_d); gpr.SetImmediate(d, imm_d);
@ -1217,9 +1217,7 @@ void JitArm64::divwx(UGeckoInstruction inst)
SetJumpTarget(slow1); SetJumpTarget(slow1);
SetJumpTarget(slow2); SetJumpTarget(slow2);
CMP(RB, 0); ASR(RD, RA, 31);
CCMP(RA, 0, 0, CC_EQ);
CSETM(RD, CC_LT);
SetJumpTarget(done); SetJumpTarget(done);