2020-11-20 10:04:44 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
#include "src/tint/writer/float_to_string.h"
|
2020-11-20 10:04:44 +00:00
|
|
|
|
2021-05-27 20:46:46 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2020-11-20 10:04:44 +00:00
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2022-04-07 14:43:05 +00:00
|
|
|
namespace tint::writer {
|
2020-11-20 10:04:44 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-05-27 20:46:46 +00:00
|
|
|
// Makes an IEEE 754 binary32 floating point number with
|
|
|
|
// - 0 sign if sign is 0, 1 otherwise
|
|
|
|
// - 'exponent_bits' is placed in the exponent space.
|
|
|
|
// So, the exponent bias must already be included.
|
2022-06-29 14:38:15 +00:00
|
|
|
float MakeFloat(uint32_t sign, uint32_t biased_exponent, uint32_t mantissa) {
|
2022-05-01 14:40:55 +00:00
|
|
|
const uint32_t sign_bit = sign ? 0x80000000u : 0u;
|
|
|
|
// The binary32 exponent is 8 bits, just below the sign.
|
|
|
|
const uint32_t exponent_bits = (biased_exponent & 0xffu) << 23;
|
|
|
|
// The mantissa is the bottom 23 bits.
|
|
|
|
const uint32_t mantissa_bits = (mantissa & 0x7fffffu);
|
2021-05-27 20:46:46 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
uint32_t bits = sign_bit | exponent_bits | mantissa_bits;
|
|
|
|
float result = 0.0f;
|
|
|
|
static_assert(sizeof(result) == sizeof(bits),
|
|
|
|
"expected float and uint32_t to be the same size");
|
|
|
|
std::memcpy(&result, &bits, sizeof(bits));
|
|
|
|
return result;
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:04:44 +00:00
|
|
|
TEST(FloatToStringTest, Zero) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToString(0.0f), "0.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, One) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToString(1.0f), "1.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, MinusOne) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToString(-1.0f), "-1.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, Billion) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToString(1e9f), "1000000000.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, Small) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_NE(FloatToString(std::numeric_limits<float>::epsilon()), "0.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, Highest) {
|
2022-05-01 14:40:55 +00:00
|
|
|
const auto highest = std::numeric_limits<float>::max();
|
|
|
|
const auto expected_highest = 340282346638528859811704183484516925440.0f;
|
|
|
|
if (highest < expected_highest || highest > expected_highest) {
|
|
|
|
GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
|
|
|
|
"this target";
|
|
|
|
}
|
|
|
|
EXPECT_EQ(FloatToString(std::numeric_limits<float>::max()),
|
|
|
|
"340282346638528859811704183484516925440.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToStringTest, Lowest) {
|
2022-05-01 14:40:55 +00:00
|
|
|
// Some compilers complain if you test floating point numbers for equality.
|
|
|
|
// So say it via two inequalities.
|
|
|
|
const auto lowest = std::numeric_limits<float>::lowest();
|
|
|
|
const auto expected_lowest = -340282346638528859811704183484516925440.0f;
|
|
|
|
if (lowest < expected_lowest || lowest > expected_lowest) {
|
|
|
|
GTEST_SKIP() << "std::numeric_limits<float>::lowest() is not as expected for "
|
|
|
|
"this target";
|
|
|
|
}
|
|
|
|
EXPECT_EQ(FloatToString(std::numeric_limits<float>::lowest()),
|
|
|
|
"-340282346638528859811704183484516925440.0");
|
2020-11-20 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 08:55:14 +00:00
|
|
|
TEST(FloatToStringTest, Precision) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToString(1e-8f), "0.00000001");
|
|
|
|
EXPECT_EQ(FloatToString(1e-9f), "0.000000001");
|
|
|
|
EXPECT_EQ(FloatToString(1e-10f), "1.00000001e-10");
|
|
|
|
EXPECT_EQ(FloatToString(1e-20f), "9.99999968e-21");
|
2021-07-16 08:55:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 20:46:46 +00:00
|
|
|
// FloatToBitPreservingString
|
|
|
|
//
|
|
|
|
// First replicate the tests for FloatToString
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Zero) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(0.0f), "0.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, One) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(1.0f), "1.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, MinusOne) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(-1.0f), "-1.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Billion) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(1e9f), "1000000000.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Small) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_NE(FloatToBitPreservingString(std::numeric_limits<float>::epsilon()), "0.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Highest) {
|
2022-05-01 14:40:55 +00:00
|
|
|
const auto highest = std::numeric_limits<float>::max();
|
|
|
|
const auto expected_highest = 340282346638528859811704183484516925440.0f;
|
|
|
|
if (highest < expected_highest || highest > expected_highest) {
|
|
|
|
GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
|
|
|
|
"this target";
|
|
|
|
}
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(std::numeric_limits<float>::max()),
|
|
|
|
"340282346638528859811704183484516925440.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Lowest) {
|
2022-05-01 14:40:55 +00:00
|
|
|
// Some compilers complain if you test floating point numbers for equality.
|
|
|
|
// So say it via two inequalities.
|
|
|
|
const auto lowest = std::numeric_limits<float>::lowest();
|
|
|
|
const auto expected_lowest = -340282346638528859811704183484516925440.0f;
|
|
|
|
if (lowest < expected_lowest || lowest > expected_lowest) {
|
|
|
|
GTEST_SKIP() << "std::numeric_limits<float>::lowest() is not as expected for "
|
|
|
|
"this target";
|
|
|
|
}
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(std::numeric_limits<float>::lowest()),
|
|
|
|
"-340282346638528859811704183484516925440.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special cases for bit-preserving output.
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, NegativeZero) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(std::copysign(0.0f, -5.0f)), "-0.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, ZeroAsBits) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0)), "0.0");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0)), "-0.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, OneBits) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 127, 0)), "1.0");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 127, 0)), "-1.0");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, SmallestDenormal) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 1)), "0x1p-149");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 1)), "-0x1p-149");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, BiggerDenormal) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 2)), "0x1p-148");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 2)), "-0x1p-148");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, LargestDenormal) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0x7fffff)), "0x1.fffffcp-127");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Subnormal_cafebe) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0xcafebe)), "0x1.2bfaf8p-127");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0xcafebe)), "-0x1.2bfaf8p-127");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Subnormal_aaaaa) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0xaaaaa)), "0x1.55554p-130");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0xaaaaa)), "-0x1.55554p-130");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, Infinity) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0)), "0x1p+128");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0)), "-0x1p+128");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(dneto): It's unclear how Infinity and NaN should be handled.
|
|
|
|
// https://github.com/gpuweb/gpuweb/issues/1769
|
|
|
|
// Windows x86-64 sets the high mantissa bit on NaNs.
|
|
|
|
// Disable NaN tests for now.
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, DISABLED_NaN_MsbOnly) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x400000)), "0x1.8p+128");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x400000)), "-0x1.8p+128");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, DISABLED_NaN_LsbOnly) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x1)), "0x1.000002p+128");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x1)), "-0x1.000002p+128");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatToBitPreservingStringTest, DISABLED_NaN_NonMsb) {
|
2022-05-01 14:40:55 +00:00
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x20101f)), "0x1.40203ep+128");
|
|
|
|
EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x20101f)), "-0x1.40203ep+128");
|
2021-05-27 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:04:44 +00:00
|
|
|
} // namespace
|
2022-04-07 14:43:05 +00:00
|
|
|
} // namespace tint::writer
|