Skip to content

Commit 1dc332c

Browse files
committed
Fix M extension overflow case
1 parent bf75987 commit 1dc332c

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

riscv.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,17 @@ static uint32_t op_mul(uint32_t insn, uint32_t a, uint32_t b)
609609
case 0b011: /* MULHU */
610610
return (uint32_t) ((((uint64_t) a) * ((uint64_t) b)) >> 32);
611611
case 0b100: /* DIV */
612-
return b ? (uint32_t) (((int32_t) a) / ((int32_t) b)) : 0xFFFFFFFF;
612+
return b ? (a == 0x80000000 && (int32_t) b == -1)
613+
? 0x80000000
614+
: (uint32_t) (((int32_t) a) / ((int32_t) b))
615+
: 0xFFFFFFFF;
613616
case 0b101: /* DIVU */
614617
return b ? (a / b) : 0xFFFFFFFF;
615618
case 0b110: /* REM */
616-
return b ? (uint32_t) (((int32_t) a) % ((int32_t) b)) : a;
619+
return b ? (a == 0x80000000 && (int32_t) b == -1)
620+
? 0
621+
: (uint32_t) (((int32_t) a) % ((int32_t) b))
622+
: a;
617623
case 0b111: /* REMU */
618624
return b ? (a % b) : a;
619625
}

0 commit comments

Comments
 (0)