Hash :
3892ac14
Author :
Date :
2023-10-12T13:35:33
Do not flush normal float constants to zero. It's ok to flush denormalized constants to zero. It's not ok to flush perfectly valid normal float constants >= FLT_MIN to zero. Two problems: 1) Values when parsed as doubles with a value less than FLT_MIN are being flushed to zero. This is incorrect when the comparison is done in double, since some values below FLT_MIN in double are equal to FLT_MIN when cast to float. The fix is to perform the comparison in float. 2) Values with a decimal exponent less than FLT_MIN_10_EXP are being flushed to zero. FLT_MIN_10_EXP is -37 but FLT_MIN is 1.1754943E-38. 10^-37 may be the "minimum negative integer such that 10 raised to that power is a normalized float", but being constrained to powers of ten it's above FLT_MIN (which is 2^-126). Since this comparison is done before #1 above, it's only present (AFAIK) to ensure that the exponent will not make the pow() function overflow. Comparing against -38 (FLT_MIN_10_EXP - 1) instead will do the trick. Bug: angleproject:8373, dawn:2077 Change-Id: I1ddf410c2caa9f0d1ba3529ace693dcd326a2cb3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4936714 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Stephen White <senorblanco@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// FloatLex_test.cpp:
// Tests for parsing floats in GLSL source.
//
#include <sstream>
#include <string>
#include "common/debug.h"
#include "common/mathutil.h"
#include "compiler/translator/util.h"
#include "gtest/gtest.h"
namespace
{
class StrtofClampParser
{
public:
static float Parse(std::string str)
{
float value;
sh::strtof_clamp(str, &value);
return value;
}
};
class NumericLexFloatParser
{
public:
static float Parse(std::string str) { return sh::NumericLexFloat32OutOfRangeToInfinity(str); }
};
} // anonymous namespace
template <typename T>
class FloatLexTest : public ::testing::Test
{
public:
FloatLexTest() {}
protected:
void SetUp() override {}
void TearDown() override {}
static bool ParsedMatches(std::string str, float expected)
{
return (T::Parse(str) == expected);
}
static bool IsInfinity(std::string str)
{
float f = T::Parse(str);
return gl::isInf(f);
}
static std::string Zeros(size_t count) { return std::string(count, '0'); }
};
typedef ::testing::Types<StrtofClampParser, NumericLexFloatParser> FloatParserTypes;
TYPED_TEST_SUITE(FloatLexTest, FloatParserTypes);
TYPED_TEST(FloatLexTest, One)
{
ASSERT_TRUE(TestFixture::ParsedMatches("1.0", 1.0f));
}
TYPED_TEST(FloatLexTest, Ten)
{
ASSERT_TRUE(TestFixture::ParsedMatches("10.0", 10.0f));
}
TYPED_TEST(FloatLexTest, TenScientific)
{
ASSERT_TRUE(TestFixture::ParsedMatches("1.0e1", 10.0f));
}
TYPED_TEST(FloatLexTest, ScientificWithSmallMantissa)
{
std::stringstream ss;
ss << "0." << TestFixture::Zeros(100) << "125e102";
ASSERT_TRUE(TestFixture::ParsedMatches(ss.str(), 12.5f));
}
TYPED_TEST(FloatLexTest, ScientificWithLargeMantissa)
{
std::stringstream ss;
ss << "9" << TestFixture::Zeros(100) << ".0e-100";
ASSERT_TRUE(TestFixture::ParsedMatches(ss.str(), 9.0f));
}
TYPED_TEST(FloatLexTest, ScientificWithVerySmallMantissa)
{
std::stringstream ss;
ss << "0." << TestFixture::Zeros(5000) << "125e5002";
ASSERT_TRUE(TestFixture::ParsedMatches(ss.str(), 12.5f));
}
TYPED_TEST(FloatLexTest, ScientificWithVeryLargeMantissa)
{
std::stringstream ss;
ss << "9" << TestFixture::Zeros(5000) << ".0e-5000";
ASSERT_TRUE(TestFixture::ParsedMatches(ss.str(), 9.0f));
}
TYPED_TEST(FloatLexTest, StartWithDecimalDot)
{
ASSERT_TRUE(TestFixture::ParsedMatches(".125", 0.125f));
}
TYPED_TEST(FloatLexTest, EndWithDecimalDot)
{
ASSERT_TRUE(TestFixture::ParsedMatches("123.", 123.0f));
}
TYPED_TEST(FloatLexTest, NoDecimalDot)
{
ASSERT_TRUE(TestFixture::ParsedMatches("125e-2", 1.25f));
}
TYPED_TEST(FloatLexTest, EndStartWithDecimalDotScientific)
{
ASSERT_TRUE(TestFixture::ParsedMatches(".625e-1", 0.0625f));
}
TYPED_TEST(FloatLexTest, EndWithDecimalDotScientific)
{
ASSERT_TRUE(TestFixture::ParsedMatches("102400.e-2", 1024.0f));
}
TYPED_TEST(FloatLexTest, UppercaseE)
{
ASSERT_TRUE(TestFixture::ParsedMatches("125E-2", 1.25f));
}
TYPED_TEST(FloatLexTest, PlusInExponent)
{
ASSERT_TRUE(TestFixture::ParsedMatches("1E+2", 100.0f));
}
TYPED_TEST(FloatLexTest, SlightlyAboveMaxFloat)
{
ASSERT_TRUE(TestFixture::IsInfinity("3.4029e38"));
}
TYPED_TEST(FloatLexTest, SlightlyBelowMaxFloat)
{
ASSERT_FALSE(TestFixture::IsInfinity("3.4028e38"));
ASSERT_TRUE(TestFixture::ParsedMatches("3.4028e38", 3.4028e38f));
}
TYPED_TEST(FloatLexTest, SlightlyAboveMaxFloatLargerMantissa)
{
ASSERT_TRUE(TestFixture::IsInfinity("34.029e37"));
}
TYPED_TEST(FloatLexTest, SlightlyBelowMaxFloatLargerMantissa)
{
ASSERT_FALSE(TestFixture::IsInfinity("34.028e37"));
ASSERT_TRUE(TestFixture::ParsedMatches("34.028e37", 3.4028e38f));
}
TYPED_TEST(FloatLexTest, SlightlyAboveMaxFloatSmallerMantissa)
{
ASSERT_TRUE(TestFixture::IsInfinity("0.34029e39"));
}
TYPED_TEST(FloatLexTest, SlightlyBelowMaxFloatSmallerMantissa)
{
ASSERT_FALSE(TestFixture::IsInfinity("0.34028e39"));
ASSERT_TRUE(TestFixture::ParsedMatches("0.34028e39", 3.4028e38f));
}
TYPED_TEST(FloatLexTest, SlightlyBelowMinSubnormalFloat)
{
ASSERT_TRUE(TestFixture::ParsedMatches("1.0e-48", 0.0f));
}
TYPED_TEST(FloatLexTest, SlightlyAboveMinNormalFloat)
{
ASSERT_FALSE(TestFixture::ParsedMatches("1.1754943E-38", 0.0f));
}
TYPED_TEST(FloatLexTest, ManySignificantDigits)
{
ASSERT_TRUE(TestFixture::ParsedMatches("1.23456789", 1.23456789f));
}
TYPED_TEST(FloatLexTest, MantissaBitAboveMaxUint)
{
ASSERT_TRUE(TestFixture::ParsedMatches("4294967299.", 4294967299.0f));
}
TYPED_TEST(FloatLexTest, ExponentBitAboveMaxInt)
{
ASSERT_TRUE(TestFixture::IsInfinity("1.0e2147483649"));
}
TYPED_TEST(FloatLexTest, ExponentBitBelowMaxIntAndLargeMantissa)
{
std::stringstream ss;
ss << "1" << TestFixture::Zeros(32) << ".0e2147483640";
ASSERT_TRUE(TestFixture::IsInfinity(ss.str()));
}
TYPED_TEST(FloatLexTest, ExponentBitAboveMinIntAndSmallMantissa)
{
std::stringstream ss;
ss << "0." << TestFixture::Zeros(32) << "1e-2147483640";
ASSERT_TRUE(TestFixture::ParsedMatches(ss.str(), 0.0f));
}