Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix signed division overflow case #29

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,17 @@ static uint32_t op_mul(uint32_t insn, uint32_t a, uint32_t b)
case 0b011: /* MULHU */
return (uint32_t) ((((uint64_t) a) * ((uint64_t) b)) >> 32);
case 0b100: /* DIV */
return b ? (uint32_t) (((int32_t) a) / ((int32_t) b)) : 0xFFFFFFFF;
return b ? (a == 0x80000000 && (int32_t) b == -1)
? 0x80000000
: (uint32_t) (((int32_t) a) / ((int32_t) b))
: 0xFFFFFFFF;
case 0b101: /* DIVU */
return b ? (a / b) : 0xFFFFFFFF;
case 0b110: /* REM */
return b ? (uint32_t) (((int32_t) a) % ((int32_t) b)) : a;
return b ? (a == 0x80000000 && (int32_t) b == -1)
? 0
: (uint32_t) (((int32_t) a) % ((int32_t) b))
: a;
case 0b111: /* REMU */
return b ? (a % b) : a;
}
Expand Down