aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/alu.cpp
diff options
context:
space:
mode:
authorPavel Pisa <pisa@cmp.felk.cvut.cz>2019-01-31 09:44:11 +0100
committerPavel Pisa <pisa@cmp.felk.cvut.cz>2019-01-31 09:44:11 +0100
commit6a6bd48da1f09be7dc7c53f13393b07889eea364 (patch)
tree4d00b004c93ff4952b40efcb0ddb0af3660e0685 /qtmips_machine/alu.cpp
parent644d250ff08bbc7342b570c8684a3a15019df33c (diff)
downloadqtmips-6a6bd48da1f09be7dc7c53f13393b07889eea364.tar.gz
qtmips-6a6bd48da1f09be7dc7c53f13393b07889eea364.tar.bz2
qtmips-6a6bd48da1f09be7dc7c53f13393b07889eea364.zip
Correct signed arithmetic overflow exception.
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Diffstat (limited to 'qtmips_machine/alu.cpp')
-rw-r--r--qtmips_machine/alu.cpp8
1 files changed, 6 insertions, 2 deletions
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: