Skip to content

Commit

Permalink
fix #980: UB executed with uitofp
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Nov 29, 2023
1 parent 6f95d8a commit cdf792b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,24 +446,24 @@ expr FloatType::fromFloat(State &s, const expr &fp, const Type &from_type0,

// 2) input NaN
// 3) quieted input NaN
const FloatType &from_type = *from_type0.getAsFloatType();
const FloatType *from_type = from_type0.getAsFloatType();

auto add_maybe_quiet = [&](const expr &e) {
// account for fpext/fptruc
expr fraction = e.trunc(min(from_type.fractionBits(), fraction_bits));
expr fraction = e.trunc(min(from_type->fractionBits(), fraction_bits));
if (fraction.bits() < fraction_bits)
fraction = fraction.concat_zeros(fraction_bits - fraction.bits());
assert(!fraction.isValid() || fraction.bits() == fraction_bits);

expr truncated_is_nan = true;
if (from_type.fractionBits() > fraction_bits)
if (from_type->fractionBits() > fraction_bits)
truncated_is_nan = fraction != 0;

expr qnan = fraction.extract(fraction_bits - 1, fraction_bits - 1);
expr quieted = var.extract(0, 0);
qnan = expr::mkIf(truncated_is_nan, qnan | quieted, expr::mkUInt(1, 1));
exprs.add(qnan.concat(fraction.trunc(fraction_bits - 1)),
from_type.getFloat(e).isNaN());
from_type->getFloat(e).isNaN());
};
if (nary >= 1) {
add_maybe_quiet(a);
Expand Down

0 comments on commit cdf792b

Please sign in to comment.