Fix constant folding of division and reminder with zero divisor
Previously constant folding of zero division (e.x. 1/0) produces a compile error. This was incorrectly implemented by checking if the division result is infinite, so produces wrong results compared to the query where no constant folding is processed (e.x. 1e308/0.1). This patch delays the operation when the divisor is zero. This makes the results more consistent, but changes the exit code on zero division from 3 to 5. Also 0/0 now produces the zero division error, not NaN. This patch also fixes the modulo operation. Previously constant folding logic does not take care of the % operator, but now it folds if the both dividend and divisor are safe numbers to cast to the integer type, and the divisor is not zero. This patch also fixes some code that relies on undefined cast behaviors in C. The modulo operation produces NaN if either the dividend or divisor is NaN.
This commit is contained in:
1 parent
6716e23ae6
commit
0f80921268
7 files changed
+488
-449
No files matched your search
@@ -172,12 +172,6 @@ int block_is_const(block b) {
|
||||
return (block_is_single(b) && (b.first->op == LOADK || b.first->op == PUSHK_UNDER));
|
||||
}
|
||||
|
||||
int block_is_const_inf(block b) {
|
||||
return (block_is_single(b) && b.first->op == LOADK &&
|
||||
jv_get_kind(b.first->imm.constant) == JV_KIND_NUMBER &&
|
||||
isinf(jv_number_value(b.first->imm.constant)));
|
||||
}
|
||||
|
||||
jv_kind block_const_kind(block b) {
|
||||
assert(block_is_const(b));
|
||||
return jv_get_kind(b.first->imm.constant);
|
||||
|
||||
Reference in new issue
Block a user