Skip to content

Commit 6a7fda8

Browse files
committed
Fix incorrect edge cases with multiplication and division
1 parent b38af69 commit 6a7fda8

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main.mlog

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ MULH:
14171417

14181418
mulh_s_u:
14191419
op mul result rs1 rs2
1420-
op shr result result 32
1420+
op idiv result result 0x100000000 # shr 32 - we need to use idiv for this because large negative values cause issues when cast to long
14211421
op and result result 0xffffffff
14221422

14231423
jump end_instruction_with_result always
@@ -1442,8 +1442,10 @@ DIVU:
14421442
# R-type: arg1=rs1, arg2=rs2, arg3=rd
14431443
jump div_by_zero equal rs2 0
14441444

1445-
op idiv result rs1 rs2
1446-
op and result result 0xffffffff # only necessary for signed
1445+
# use div instead of idiv and then cast to long so that it rounds towards zero
1446+
# https://stackoverflow.com/a/8387377
1447+
op div result rs1 rs2
1448+
op and result result 0xffffffff
14471449

14481450
jump end_instruction_with_result always
14491451

0 commit comments

Comments
 (0)