mirror of
https://github.com/gbdev/rgbds.git
synced 2026-08-22 14:54:33 +00:00
Fix $8000_0000 % -1 to warn with -Wdiv like $8000_0000 / -1 does (#2012)
This commit is contained in:
+3
-4
@@ -375,8 +375,7 @@ void Expression::makeBinaryOp(RPNCommand op, Expression &&src1, Expression const
|
||||
case RPN_DIV:
|
||||
if (rval == 0) {
|
||||
fatal("Division by zero");
|
||||
}
|
||||
if (lval == INT32_MIN && rval == -1) {
|
||||
} else if (lval == INT32_MIN && rval == -1) {
|
||||
warning(
|
||||
WARNING_DIV,
|
||||
"Division of %" PRId32 " by -1 yields %" PRId32,
|
||||
@@ -391,8 +390,8 @@ void Expression::makeBinaryOp(RPNCommand op, Expression &&src1, Expression const
|
||||
case RPN_MOD:
|
||||
if (rval == 0) {
|
||||
fatal("Modulo by zero");
|
||||
}
|
||||
if (lval == INT32_MIN && rval == -1) {
|
||||
} else if (lval == INT32_MIN && rval == -1) {
|
||||
warning(WARNING_DIV, "Modulo of %" PRId32 " by -1 yields 0", INT32_MIN);
|
||||
data = 0;
|
||||
} else {
|
||||
data = op_modulo(lval, rval);
|
||||
|
||||
+4
-1
@@ -143,8 +143,11 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
|
||||
firstErrorAt(patch, "Modulo by 0");
|
||||
popRPN(patch);
|
||||
value = 0;
|
||||
} else if (int32_t lval = popRPN(patch); lval == INT32_MIN && value == -1) {
|
||||
diagnosticAt(patch, WARNING_DIV, "Modulo of %" PRId32 " by -1 yields 0", INT32_MIN);
|
||||
value = 0;
|
||||
} else {
|
||||
value = op_modulo(popRPN(patch), value);
|
||||
value = op_modulo(lval, value);
|
||||
}
|
||||
break;
|
||||
case RPN_NEG:
|
||||
|
||||
+5
-1
@@ -6,9 +6,11 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "helpers.hpp" // clz, ctz
|
||||
#include "helpers.hpp" // assume, clz, ctz
|
||||
|
||||
int32_t op_divide(int32_t dividend, int32_t divisor) {
|
||||
assume(divisor != 0); // Division by 0 is UB
|
||||
assume(dividend != INT32_MIN || divisor != -1); // INT32_MIN / -1 is UB
|
||||
// Adjust division to floor toward negative infinity,
|
||||
// not truncate toward zero
|
||||
int32_t remainder = dividend % divisor;
|
||||
@@ -16,6 +18,8 @@ int32_t op_divide(int32_t dividend, int32_t divisor) {
|
||||
}
|
||||
|
||||
int32_t op_modulo(int32_t dividend, int32_t divisor) {
|
||||
assume(divisor != 0); // Modulo by 0 is UB
|
||||
assume(dividend != INT32_MIN || divisor != -1); // INT32_MIN % -1 is UB
|
||||
// Adjust modulo to have the sign of the divisor,
|
||||
// not the sign of the dividend
|
||||
return static_cast<int32_t>(
|
||||
|
||||
Reference in New Issue
Block a user