Skip to content

Commit 1765a4b

Browse files
committed
more precise check for negation
1 parent a1a9a2b commit 1765a4b

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/IROperator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,12 @@ Expr lossless_negate(const Expr &x) {
609609
} else if (const FloatImm *f = x.as<FloatImm>()) {
610610
return FloatImm::make(f->type, -f->value);
611611
} else if (const Cast *c = x.as<Cast>()) {
612-
// Only safe to negate through a cast when the inner type is signed.
612+
// Only safe to negate through a cast when the inner type is not unsigned.
613613
// For unsigned inner types, negation wraps modularly (e.g., -uint8(65)
614614
// = uint8(191)), so cast(outer, -inner) != -cast(outer, inner).
615-
if (c->value.type().is_int()) {
615+
// Signed integers and floats are fine: signed negation is checked for
616+
// overflow below via lossless_cast, and float negation is exact.
617+
if (!c->value.type().is_uint()) {
616618
Expr value = lossless_negate(c->value);
617619
if (value.defined()) {
618620
// This logic is only sound if we know the cast can't overflow.

0 commit comments

Comments
 (0)