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>
This commit is contained in:
Antonio Maiorano 2022-09-03 21:43:01 +00:00 committed by Dawn LUCI CQ
parent 1741f4443e
commit 679cf4f351
14 changed files with 36 additions and 38 deletions

View File

@ -150,25 +150,22 @@ const char* convert_texel_format_to_glsl(const ast::TexelFormat format) {
}
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 ? "uintBitsToFloat(0x7f800000u)" : "uintBitsToFloat(0xff800000u)");
out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
out << "uintBitsToFloat(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, and there is no solid way to
// generate constant/literal f16 Inf or NaN.
if (std::isinf(value) || std::isnan(value)) {
return false;
void PrintF16(std::ostream& out, float value) {
if (std::isinf(value)) {
out << "0.0hf " << (value >= 0 ? "/* inf */" : "/* -inf */");
} else if (std::isnan(value)) {
out << "0.0hf /* nan */";
} else {
out << FloatToString(value) << "hf";
return true;
}
}
@ -2185,7 +2182,10 @@ bool GeneratorImpl::EmitConstant(std::ostream& out, const sem::Constant* constan
PrintF32(out, constant->As<float>());
return true;
},
[&](const sem::F16*) { return PrintF16(out, constant->As<float>()); },
[&](const sem::F16*) {
PrintF16(out, constant->As<float>());
return true;
},
[&](const sem::I32*) {
out << constant->As<AInt>();
return true;
@ -2285,9 +2285,10 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
},
[&](const ast::FloatLiteralExpression* l) {
if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) {
return PrintF16(out, static_cast<float>(l->value));
PrintF16(out, static_cast<float>(l->value));
} else {
PrintF32(out, static_cast<float>(l->value));
}
PrintF32(out, static_cast<float>(l->value));
return true;
},
[&](const ast::IntLiteralExpression* l) {

View File

@ -112,24 +112,22 @@ const char* image_format_to_rwtexture_type(ast::TexelFormat image_format) {
}
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 @@ bool GeneratorImpl::EmitConstant(std::ostream& out, const sem::Constant* constan
[&](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 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
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;

View File

@ -14,7 +14,7 @@ float getAAtOutCoords_() {
float unaryOperation_f1_(inout float a) {
const float x_47 = a;
if ((x_47 < 0.0f)) {
return asfloat(0x7f800000u);
return 0.0f /* inf */;
}
const float x_55 = a;
return log(x_55);

View File

@ -14,7 +14,7 @@ float getAAtOutCoords_() {
float unaryOperation_f1_(inout float a) {
const float x_47 = a;
if ((x_47 < 0.0f)) {
return asfloat(0x7f800000u);
return 0.0f /* inf */;
}
const float x_55 = a;
return log(x_55);

View File

@ -26,7 +26,7 @@ float getAAtOutCoords_() {
float unaryOperation_f1_(inout float a) {
float x_47 = a;
if ((x_47 < 0.0f)) {
return uintBitsToFloat(0x7f800000u);
return 0.0f /* inf */;
}
float x_55 = a;
return log(x_55);

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = (asfloat(0xff800000u)).xxxx;
out_var_SV_TARGET = (0.0f /* -inf */).xxxx;
return;
}

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = (asfloat(0xff800000u)).xxxx;
out_var_SV_TARGET = (0.0f /* -inf */).xxxx;
return;
}

View File

@ -4,7 +4,7 @@ precision mediump float;
layout(location = 0) out vec4 out_var_SV_TARGET_1_1;
vec4 out_var_SV_TARGET = vec4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = vec4(uintBitsToFloat(0xff800000u));
out_var_SV_TARGET = vec4(0.0f /* -inf */);
return;
}

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = (asfloat(0x7f800000u)).xxxx;
out_var_SV_TARGET = (0.0f /* inf */).xxxx;
return;
}

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = (asfloat(0x7f800000u)).xxxx;
out_var_SV_TARGET = (0.0f /* inf */).xxxx;
return;
}

View File

@ -4,7 +4,7 @@ precision mediump float;
layout(location = 0) out vec4 out_var_SV_TARGET_1_1;
vec4 out_var_SV_TARGET = vec4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = vec4(uintBitsToFloat(0x7f800000u));
out_var_SV_TARGET = vec4(0.0f /* inf */);
return;
}

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = float4(asfloat(0x7fc00000u), asfloat(0x7fc00000u), asfloat(0x7fc00000u), asfloat(0x7fc00000u));
out_var_SV_TARGET = float4(0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */);
return;
}

View File

@ -1,7 +1,7 @@
static float4 out_var_SV_TARGET = float4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = float4(asfloat(0x7fc00000u), asfloat(0x7fc00000u), asfloat(0x7fc00000u), asfloat(0x7fc00000u));
out_var_SV_TARGET = float4(0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */);
return;
}

View File

@ -4,7 +4,7 @@ precision mediump float;
layout(location = 0) out vec4 out_var_SV_TARGET_1_1;
vec4 out_var_SV_TARGET = vec4(0.0f, 0.0f, 0.0f, 0.0f);
void main_1() {
out_var_SV_TARGET = vec4(uintBitsToFloat(0x7fc00000u), uintBitsToFloat(0x7fc00000u), uintBitsToFloat(0x7fc00000u), uintBitsToFloat(0x7fc00000u));
out_var_SV_TARGET = vec4(0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */, 0.0f /* nan */);
return;
}