tint: HLSL and GLSL backends now emit 0 for inf and nan
Bug: tint:1581
Change-Id: I62dcde177c3b82408cd8d737526d10d481b48a17
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/101240
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc
index fca1515..3625db6 100644
--- a/src/tint/writer/hlsl/generator_impl.cc
+++ b/src/tint/writer/hlsl/generator_impl.cc
@@ -112,24 +112,22 @@
}
void PrintF32(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, but this is implemented for the day
- // we support them.
if (std::isinf(value)) {
- out << (value >= 0 ? "asfloat(0x7f800000u)" : "asfloat(0xff800000u)");
+ out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
- out << "asfloat(0x7fc00000u)";
+ out << "0.0f /* nan */";
} else {
out << FloatToString(value) << "f";
}
}
-bool PrintF16(std::ostream& out, float value) {
- // Note: Currently inf and nan should not be constructable, don't emit them.
- if (std::isinf(value) || std::isnan(value)) {
- return false;
+void PrintF16(std::ostream& out, float value) {
+ if (std::isinf(value)) {
+ out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */");
+ } else if (std::isnan(value)) {
+ out << "0.0h /* nan */";
} else {
out << FloatToString(value) << "h";
- return true;
}
}
@@ -3127,9 +3125,9 @@
[&](const sem::F16*) {
// emit a f16 scalar with explicit float16_t type declaration.
out << "float16_t(";
- bool valid = PrintF16(out, constant->As<float>());
+ PrintF16(out, constant->As<float>());
out << ")";
- return valid;
+ return true;
},
[&](const sem::I32*) {
out << constant->As<AInt>();
@@ -3254,9 +3252,8 @@
if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) {
// Emit f16 literal with explicit float16_t type declaration.
out << "float16_t(";
- bool valid = PrintF16(out, static_cast<float>(l->value));
+ PrintF16(out, static_cast<float>(l->value));
out << ")";
- return valid;
}
PrintF32(out, static_cast<float>(l->value));
return true;