mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Fix division and modulo for very large negative numbers (#1790)
This commit is contained in:
@@ -11,15 +11,16 @@
|
||||
int32_t op_divide(int32_t dividend, int32_t divisor) {
|
||||
// Adjust division to floor toward negative infinity,
|
||||
// not truncate toward zero
|
||||
return dividend / divisor - ((dividend % divisor < 0) != (divisor < 0));
|
||||
int32_t remainder = dividend % divisor;
|
||||
return dividend / divisor - (remainder != 0 && (remainder < 0) != (divisor < 0));
|
||||
}
|
||||
|
||||
int32_t op_modulo(int32_t dividend, int32_t divisor) {
|
||||
int32_t remainder = dividend % divisor;
|
||||
|
||||
// Adjust modulo to have the sign of the divisor,
|
||||
// not the sign of the dividend
|
||||
return remainder + divisor * ((remainder < 0) != (divisor < 0));
|
||||
return static_cast<int32_t>(
|
||||
dividend - static_cast<int64_t>(op_divide(dividend, divisor)) * divisor
|
||||
);
|
||||
}
|
||||
|
||||
int32_t op_exponent(int32_t base, uint32_t power) {
|
||||
|
||||
Reference in New Issue
Block a user