From 6a6bd48da1f09be7dc7c53f13393b07889eea364 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Thu, 31 Jan 2019 09:44:11 +0100 Subject: Correct signed arithmetic overflow exception. Signed-off-by: Pavel Pisa --- qtmips_machine/alu.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'qtmips_machine') diff --git a/qtmips_machine/alu.cpp b/qtmips_machine/alu.cpp index 3822955..2e36d4c 100644 --- a/qtmips_machine/alu.cpp +++ b/qtmips_machine/alu.cpp @@ -41,13 +41,17 @@ std::uint32_t machine::alu_operate(enum AluOp operation, std::uint32_t s, std::u regs->write_hi_lo(false, s); return 0x0; case ALU_OP_ADD: - if (s > (0xFFFFFFFF - t)) + /* s(31) ^ ~t(31) ... same signs on input */ + /* (s + t)(31) ^ s(31) ... different sign on output */ + if (((s ^ ~t) & ((s + t) ^ s)) & 0x80000000) throw QTMIPS_EXCEPTION(Overflow, "ADD operation overflow/underflow", QString::number(s) + QString(" + ") + QString::number(t)); FALLTROUGH case ALU_OP_ADDU: return s + t; case ALU_OP_SUB: - if (s < t) + /* s(31) ^ t(31) ... differnt signd on input */ + /* (s - t)(31) ^ ~s(31) <> 0 ... otput sign differs from s */ + if (((s ^ t) & ((s - t) ^ s)) & 0x80000000) throw QTMIPS_EXCEPTION(Overflow, "SUB operation overflow/underflow", QString::number(s) + QString(" - ") + QString::number(t)); FALLTROUGH case ALU_OP_SUBU: -- cgit v1.2.3