mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-08-08 13:09:10 +00:00
Change-Id: Ie95547f065b896983b90ffd5455538fdd843b81a Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/104824 Reviewed-by: David Neto <dneto@google.com> Commit-Queue: Ben Clayton <bclayton@google.com> Kokoro: Kokoro <noreply+kokoro@google.com>
336 lines
14 KiB
C++
336 lines
14 KiB
C++
// 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.
|
|
|
|
#include "src/tint/writer/float_to_string.h"
|
|
|
|
#include <math.h>
|
|
#include <cstring>
|
|
#include <limits>
|
|
|
|
#include "gtest/gtest.h"
|
|
#include "src/tint/utils/bitcast.h"
|
|
|
|
namespace tint::writer {
|
|
namespace {
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// FloatToString //
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEST(FloatToStringTest, Zero) {
|
|
EXPECT_EQ(FloatToString(0.0f), "0.0");
|
|
}
|
|
|
|
TEST(FloatToStringTest, One) {
|
|
EXPECT_EQ(FloatToString(1.0f), "1.0");
|
|
}
|
|
|
|
TEST(FloatToStringTest, MinusOne) {
|
|
EXPECT_EQ(FloatToString(-1.0f), "-1.0");
|
|
}
|
|
|
|
TEST(FloatToStringTest, Billion) {
|
|
EXPECT_EQ(FloatToString(1e9f), "1000000000.0");
|
|
}
|
|
|
|
TEST(FloatToStringTest, Small) {
|
|
EXPECT_NE(FloatToString(std::numeric_limits<float>::epsilon()), "0.0");
|
|
}
|
|
|
|
TEST(FloatToStringTest, Highest) {
|
|
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");
|
|
}
|
|
|
|
TEST(FloatToStringTest, Lowest) {
|
|
// 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");
|
|
}
|
|
|
|
TEST(FloatToStringTest, Precision) {
|
|
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");
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// FloatToBitPreservingString //
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEST(FloatToBitPreservingStringTest, Zero) {
|
|
EXPECT_EQ(FloatToBitPreservingString(0.0f), "0.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, NegativeZero) {
|
|
EXPECT_EQ(FloatToBitPreservingString(-0.0f), "-0.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, One) {
|
|
EXPECT_EQ(FloatToBitPreservingString(1.0f), "1.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, MinusOne) {
|
|
EXPECT_EQ(FloatToBitPreservingString(-1.0f), "-1.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Billion) {
|
|
EXPECT_EQ(FloatToBitPreservingString(1e9f), "1000000000.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Small) {
|
|
EXPECT_NE(FloatToBitPreservingString(std::numeric_limits<float>::epsilon()), "0.0");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Highest) {
|
|
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");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Lowest) {
|
|
// 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");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, SmallestDenormal) {
|
|
EXPECT_EQ(FloatToBitPreservingString(0x1p-149f), "0x1p-149");
|
|
EXPECT_EQ(FloatToBitPreservingString(-0x1p-149f), "-0x1p-149");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, BiggerDenormal) {
|
|
EXPECT_EQ(FloatToBitPreservingString(0x1p-148f), "0x1p-148");
|
|
EXPECT_EQ(FloatToBitPreservingString(-0x1p-148f), "-0x1p-148");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, LargestDenormal) {
|
|
static_assert(0x0.fffffep-126f == 0x1.fffffcp-127f);
|
|
EXPECT_EQ(FloatToBitPreservingString(0x0.fffffep-126f), "0x1.fffffcp-127");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Subnormal_cafebe) {
|
|
EXPECT_EQ(FloatToBitPreservingString(0x1.2bfaf8p-127f), "0x1.2bfaf8p-127");
|
|
EXPECT_EQ(FloatToBitPreservingString(-0x1.2bfaf8p-127f), "-0x1.2bfaf8p-127");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Subnormal_aaaaa) {
|
|
EXPECT_EQ(FloatToBitPreservingString(0x1.55554p-130f), "0x1.55554p-130");
|
|
EXPECT_EQ(FloatToBitPreservingString(-0x1.55554p-130f), "-0x1.55554p-130");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, Infinity) {
|
|
EXPECT_EQ(FloatToBitPreservingString(INFINITY), "0x1p+128");
|
|
EXPECT_EQ(FloatToBitPreservingString(-INFINITY), "-0x1p+128");
|
|
}
|
|
|
|
TEST(FloatToBitPreservingStringTest, NaN) {
|
|
// TODO(crbug.com/tint/1714): On x86, this bitcast will set bit 22 (the highest mantissa bit) to
|
|
// 1, regardless of the bit value in the integer. This is likely due to IEEE 754's
|
|
// recommendation that that the highest mantissa bit differentiates quiet NaNs from signalling
|
|
// NaNs. On x86, float return values usually go via the FPU which can transform the signalling
|
|
// NaN bit (0) to quiet NaN (1). As NaN floating point numbers can be silently modified by the
|
|
// architecture, and the signalling bit is architecture defined, this test may fail on other
|
|
// architectures.
|
|
auto nan = utils::Bitcast<float>(0x7fc0beef);
|
|
EXPECT_EQ(FloatToBitPreservingString(nan), "0x1.817ddep+128");
|
|
EXPECT_EQ(FloatToBitPreservingString(-nan), "-0x1.817ddep+128");
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// DoubleToString //
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEST(DoubleToStringTest, Zero) {
|
|
EXPECT_EQ(DoubleToString(0.0), "0.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, One) {
|
|
EXPECT_EQ(DoubleToString(1.0), "1.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, MinusOne) {
|
|
EXPECT_EQ(DoubleToString(-1.0), "-1.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, Billion) {
|
|
EXPECT_EQ(DoubleToString(1e9), "1000000000.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, Small) {
|
|
EXPECT_NE(DoubleToString(std::numeric_limits<double>::epsilon()), "0.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, Highest) {
|
|
const auto highest = std::numeric_limits<double>::max();
|
|
const auto expected_highest = 1.797693134862315708e+308;
|
|
if (highest < expected_highest || highest > expected_highest) {
|
|
GTEST_SKIP() << "std::numeric_limits<double>::max() is not as expected for "
|
|
"this target";
|
|
}
|
|
EXPECT_EQ(DoubleToString(std::numeric_limits<double>::max()),
|
|
"179769313486231570814527423731704356798070567525844996598917476803157260780028538760"
|
|
"589558632766878171540458953514382464234321326889464182768467546703537516986049910576"
|
|
"551282076245490090389328944075868508455133942304583236903222948165808559332123348274"
|
|
"797826204144723168738177180919299881250404026184124858368.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, Lowest) {
|
|
// Some compilers complain if you test floating point numbers for equality.
|
|
// So say it via two inequalities.
|
|
const auto lowest = std::numeric_limits<double>::lowest();
|
|
const auto expected_lowest = -1.797693134862315708e+308;
|
|
if (lowest < expected_lowest || lowest > expected_lowest) {
|
|
GTEST_SKIP() << "std::numeric_limits<double>::lowest() is not as expected for "
|
|
"this target";
|
|
}
|
|
EXPECT_EQ(DoubleToString(std::numeric_limits<double>::lowest()),
|
|
"-17976931348623157081452742373170435679807056752584499659891747680315726078002853876"
|
|
"058955863276687817154045895351438246423432132688946418276846754670353751698604991057"
|
|
"655128207624549009038932894407586850845513394230458323690322294816580855933212334827"
|
|
"4797826204144723168738177180919299881250404026184124858368.0");
|
|
}
|
|
|
|
TEST(DoubleToStringTest, Precision) {
|
|
EXPECT_EQ(DoubleToString(1e-8), "0.00000001");
|
|
EXPECT_EQ(DoubleToString(1e-9), "0.000000001");
|
|
EXPECT_EQ(DoubleToString(1e-10), "1e-10");
|
|
EXPECT_EQ(DoubleToString(1e-15), "1.0000000000000001e-15");
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// DoubleToBitPreservingString //
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Zero) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(0.0), "0.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, NegativeZero) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0.0), "-0.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, One) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(1.0), "1.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, MinusOne) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(-1.0), "-1.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Billion) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(1e9), "1000000000.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Small) {
|
|
EXPECT_NE(DoubleToBitPreservingString(std::numeric_limits<double>::epsilon()), "0.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Highest) {
|
|
const auto highest = std::numeric_limits<double>::max();
|
|
const auto expected_highest = 1.797693134862315708e+308;
|
|
if (highest < expected_highest || highest > expected_highest) {
|
|
GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
|
|
"this target";
|
|
}
|
|
EXPECT_EQ(DoubleToBitPreservingString(std::numeric_limits<double>::max()),
|
|
"179769313486231570814527423731704356798070567525844996598917476803157260780028538760"
|
|
"589558632766878171540458953514382464234321326889464182768467546703537516986049910576"
|
|
"551282076245490090389328944075868508455133942304583236903222948165808559332123348274"
|
|
"797826204144723168738177180919299881250404026184124858368.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Lowest) {
|
|
// Some compilers complain if you test floating point numbers for equality.
|
|
// So say it via two inequalities.
|
|
const auto lowest = std::numeric_limits<double>::lowest();
|
|
const auto expected_lowest = -1.797693134862315708e+308;
|
|
if (lowest < expected_lowest || lowest > expected_lowest) {
|
|
GTEST_SKIP() << "std::numeric_limits<float>::lowest() is not as expected for "
|
|
"this target";
|
|
}
|
|
EXPECT_EQ(DoubleToBitPreservingString(std::numeric_limits<double>::lowest()),
|
|
"-17976931348623157081452742373170435679807056752584499659891747680315726078002853876"
|
|
"058955863276687817154045895351438246423432132688946418276846754670353751698604991057"
|
|
"655128207624549009038932894407586850845513394230458323690322294816580855933212334827"
|
|
"4797826204144723168738177180919299881250404026184124858368.0");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, SmallestDenormal) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(0x1p-1074), "0x1p-1074");
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0x1p-1074), "-0x1p-1074");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, BiggerDenormal) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(0x1p-1073), "0x1p-1073");
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0x1p-1073), "-0x1p-1073");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, LargestDenormal) {
|
|
static_assert(0x0.fffffffffffffp-1022 == 0x1.ffffffffffffep-1023);
|
|
EXPECT_EQ(DoubleToBitPreservingString(0x0.fffffffffffffp-1022), "0x1.ffffffffffffep-1023");
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0x0.fffffffffffffp-1022), "-0x1.ffffffffffffep-1023");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Subnormal_cafef00dbeef) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(0x1.cafef00dbeefp-1023), "0x1.cafef00dbeefp-1023");
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0x1.cafef00dbeefp-1023), "-0x1.cafef00dbeefp-1023");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Subnormal_aaaaaaaaaaaaap) {
|
|
static_assert(0x0.aaaaaaaaaaaaap-1023 == 0x1.5555555555554p-1024);
|
|
EXPECT_EQ(DoubleToBitPreservingString(0x0.aaaaaaaaaaaaap-1023), "0x1.5555555555554p-1024");
|
|
EXPECT_EQ(DoubleToBitPreservingString(-0x0.aaaaaaaaaaaaap-1023), "-0x1.5555555555554p-1024");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, Infinity) {
|
|
EXPECT_EQ(DoubleToBitPreservingString(static_cast<double>(INFINITY)), "0x1p+1024");
|
|
EXPECT_EQ(DoubleToBitPreservingString(static_cast<double>(-INFINITY)), "-0x1p+1024");
|
|
}
|
|
|
|
TEST(DoubleToBitPreservingStringTest, NaN) {
|
|
auto nan = utils::Bitcast<double>(0x7ff8cafef00dbeefull);
|
|
EXPECT_EQ(DoubleToBitPreservingString(static_cast<double>(nan)), "0x1.8cafef00dbeefp+1024");
|
|
EXPECT_EQ(DoubleToBitPreservingString(static_cast<double>(-nan)), "-0x1.8cafef00dbeefp+1024");
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace tint::writer
|