From 3ad927cc73dac935f5435f8b1f2855f0ea289b31 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 25 May 2022 23:12:14 +0000 Subject: [PATCH] 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 Commit-Queue: Ben Clayton Kokoro: Kokoro --- src/tint/writer/glsl/generator_impl.cc | 5 +++-- src/tint/writer/hlsl/generator_impl.cc | 11 ++++++----- src/tint/writer/msl/generator_impl.cc | 9 +++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc index 1cf74807d1..ccce06a89a 100644 --- a/src/tint/writer/glsl/generator_impl.cc +++ b/src/tint/writer/glsl/generator_impl.cc @@ -2183,13 +2183,14 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression* return true; }, [&](const ast::FloatLiteralExpression* l) { - if (std::isinf(l->value)) { + auto f32 = static_cast(l->value); + if (std::isinf(f32)) { out << (l->value >= 0 ? "uintBitsToFloat(0x7f800000u)" : "uintBitsToFloat(0xff800000u)"); } else if (std::isnan(l->value)) { out << "uintBitsToFloat(0x7fc00000u)"; } else { - out << FloatToString(static_cast(l->value)) << "f"; + out << FloatToString(f32) << "f"; } return true; }, diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index 344d0d87c8..d6a5fa7549 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -3139,13 +3139,14 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression* out << (l->value ? "true" : "false"); return true; }, - [&](const ast::FloatLiteralExpression* fl) { - if (std::isinf(fl->value)) { - out << (fl->value >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)"); - } else if (std::isnan(fl->value)) { + [&](const ast::FloatLiteralExpression* l) { + auto f32 = static_cast(l->value); + if (std::isinf(f32)) { + out << (f32 >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)"); + } else if (std::isnan(f32)) { out << "asfloat(0x7fc00000u)"; } else { - out << FloatToString(static_cast(fl->value)) << "f"; + out << FloatToString(f32) << "f"; } return true; }, diff --git a/src/tint/writer/msl/generator_impl.cc b/src/tint/writer/msl/generator_impl.cc index be94e2c9fe..54d9164823 100644 --- a/src/tint/writer/msl/generator_impl.cc +++ b/src/tint/writer/msl/generator_impl.cc @@ -1544,12 +1544,13 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression* return true; }, [&](const ast::FloatLiteralExpression* l) { - if (std::isinf(l->value)) { - out << (l->value >= 0 ? "INFINITY" : "-INFINITY"); - } else if (std::isnan(l->value)) { + auto f32 = static_cast(l->value); + if (std::isinf(f32)) { + out << (f32 >= 0 ? "INFINITY" : "-INFINITY"); + } else if (std::isnan(f32)) { out << "NAN"; } else { - out << FloatToString(static_cast(l->value)) << "f"; + out << FloatToString(f32) << "f"; } return true; },