From f830f8f47c61035ba07be1f6b561764fef1a76c3 Mon Sep 17 00:00:00 2001 From: "Jimi He(WH-RD)" Date: Mon, 6 Nov 2023 14:37:32 +0800 Subject: [PATCH] glslang representing literal constants with double precision, so 1.0e40 and 1.0e-50 are normal values. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shader1: precision highp float; out vec4 my_FragColor; void main() { // Out-of-range floats should overflow to infinity // GLSL ES 3.00.6 section 4.1.4 Floats: // "If the value of the floating point number is too large (small) to be stored as a single precision value, it is converted to positive (negative) infinity" float correct = isinf(1.0e40) ? 1.0 : 0.0; my_FragColor = vec4(0.0, correct, 0.0, 1.0); } precision highp float; out vec4 my_FragColor; void main() { // GLSL ES 3.00.6 section 4.1.4 Floats: // "A value with a magnitude too small to be represented as a mantissa and exponent is converted to zero." // 1.0e-50 is small enough that it can't even be stored as subnormal. float correct = (1.0e-50 == 0.0) ? 1.0 : 0.0; my_FragColor = vec4(0.0, correct, 0.0, 1.0); } KHR-GLES3.number_parsing.float_out_of_range_as_infinity --- glslang/MachineIndependent/Intermediate.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/glslang/MachineIndependent/Intermediate.cpp b/glslang/MachineIndependent/Intermediate.cpp index a8e3b38bfd..a64bd58509 100644 --- a/glslang/MachineIndependent/Intermediate.cpp +++ b/glslang/MachineIndependent/Intermediate.cpp @@ -2589,7 +2589,17 @@ TIntermConstantUnion* TIntermediate::addConstantUnion(bool b, const TSourceLoc& TIntermConstantUnion* TIntermediate::addConstantUnion(double d, TBasicType baseType, const TSourceLoc& loc, bool literal) const { assert(baseType == EbtFloat || baseType == EbtDouble || baseType == EbtFloat16); - + if (baseType == EbtFloat) + d = (float)d; + else if (baseType == EbtFloat16 && !IsInfinity(d) && !IsNan(d)) { + int exponent = (((*(unsigned long long*)&d) >> 52) & 0x7ff) - 1023; + if (exponent < -14) + d = 0.0; + else if (exponent > 15){ + (*(unsigned long long*)&d) &= ~((((unsigned long long)1) << 52) - 1); + (*(unsigned long long*)&d) |= ((unsigned long long)0x7ff) << 52; + } + } TConstUnionArray unionArray(1); unionArray[0].setDConst(d);