Skip to content

Commit

Permalink
Define cast<i32>(u32) overflow behavior (halide#7769)
Browse files Browse the repository at this point in the history
uint32 -> int32 casting should not produce SIO
  • Loading branch information
rootjalex authored and ardier committed Mar 3, 2024
1 parent c43160d commit 560fe47
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Simplify_Cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ Expr Simplify::visit(const Cast *op, ExprInfo *bounds) {
} else if (op->type.is_int() &&
const_uint(value, &u) &&
op->type.bits() < value.type().bits()) {
// uint -> int
// uint -> int narrowing
// Recursively call mutate just to set the bounds
return mutate(make_const(op->type, safe_numeric_cast<int64_t>(u)), bounds);
} else if (op->type.is_int() &&
const_uint(value, &u) &&
op->type.bits() >= value.type().bits()) {
// uint -> int with less than or equal to the number of bits
op->type.bits() == value.type().bits()) {
// uint -> int reinterpret
// Recursively call mutate just to set the bounds
return mutate(make_const(op->type, safe_numeric_cast<int64_t>(u)), bounds);
} else if (op->type.is_int() &&
const_uint(value, &u) &&
op->type.bits() > value.type().bits()) {
// uint -> int widening
if (op->type.can_represent(u) || op->type.bits() < 32) {
// If the type can represent the value or overflow is well-defined.
// Recursively call mutate just to set the bounds
return mutate(make_const(op->type, safe_numeric_cast<int64_t>(u)), bounds);
} else {
Expand Down

0 comments on commit 560fe47

Please sign in to comment.