tint/writer: Check for inf / nan after casting to f32.

When abstract floats are supported, FloatLiteralExpression may hold values outside of the range of a f32. When we call FloatToString(), we may emit an 'inf', which is not a token for any backend.

Cast to f32 first, to ensure behavior remains consistent.

Note: Once the resolver materializes, and we constant fold, the backends shouldn't see any values outside of a f32.

Bug: tint:1504
Change-Id: I11942304a063d72302dad32e0d6d4e04aa39b5f8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91425
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2022-05-25 23:12:14 +00:00 committed by Dawn LUCI CQ
parent 3e6bf1ae73
commit 3ad927cc73
3 changed files with 14 additions and 11 deletions

View File

@ -2183,13 +2183,14 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
return true; return true;
}, },
[&](const ast::FloatLiteralExpression* l) { [&](const ast::FloatLiteralExpression* l) {
if (std::isinf(l->value)) { auto f32 = static_cast<float>(l->value);
if (std::isinf(f32)) {
out << (l->value >= 0 ? "uintBitsToFloat(0x7f800000u)" out << (l->value >= 0 ? "uintBitsToFloat(0x7f800000u)"
: "uintBitsToFloat(0xff800000u)"); : "uintBitsToFloat(0xff800000u)");
} else if (std::isnan(l->value)) { } else if (std::isnan(l->value)) {
out << "uintBitsToFloat(0x7fc00000u)"; out << "uintBitsToFloat(0x7fc00000u)";
} else { } else {
out << FloatToString(static_cast<float>(l->value)) << "f"; out << FloatToString(f32) << "f";
} }
return true; return true;
}, },

View File

@ -3139,13 +3139,14 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
out << (l->value ? "true" : "false"); out << (l->value ? "true" : "false");
return true; return true;
}, },
[&](const ast::FloatLiteralExpression* fl) { [&](const ast::FloatLiteralExpression* l) {
if (std::isinf(fl->value)) { auto f32 = static_cast<float>(l->value);
out << (fl->value >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)"); if (std::isinf(f32)) {
} else if (std::isnan(fl->value)) { out << (f32 >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)");
} else if (std::isnan(f32)) {
out << "asfloat(0x7fc00000u)"; out << "asfloat(0x7fc00000u)";
} else { } else {
out << FloatToString(static_cast<float>(fl->value)) << "f"; out << FloatToString(f32) << "f";
} }
return true; return true;
}, },

View File

@ -1544,12 +1544,13 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
return true; return true;
}, },
[&](const ast::FloatLiteralExpression* l) { [&](const ast::FloatLiteralExpression* l) {
if (std::isinf(l->value)) { auto f32 = static_cast<float>(l->value);
out << (l->value >= 0 ? "INFINITY" : "-INFINITY"); if (std::isinf(f32)) {
} else if (std::isnan(l->value)) { out << (f32 >= 0 ? "INFINITY" : "-INFINITY");
} else if (std::isnan(f32)) {
out << "NAN"; out << "NAN";
} else { } else {
out << FloatToString(static_cast<float>(l->value)) << "f"; out << FloatToString(f32) << "f";
} }
return true; return true;
}, },