2018-05-07 05:18:41 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-05-07 05:18:41 +00:00
|
|
|
|
2023-10-29 19:57:41 +00:00
|
|
|
#include <array>
|
2022-08-06 04:10:17 +00:00
|
|
|
#include <bit>
|
2018-05-07 05:18:41 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "Common/FloatUtils.h"
|
|
|
|
|
2023-10-29 19:57:41 +00:00
|
|
|
#include "../Core/PowerPC/TestValues.h"
|
|
|
|
|
2018-05-07 05:18:41 +00:00
|
|
|
TEST(FloatUtils, IsQNAN)
|
|
|
|
{
|
|
|
|
EXPECT_TRUE(Common::IsQNAN(std::numeric_limits<double>::quiet_NaN()));
|
|
|
|
EXPECT_FALSE(Common::IsQNAN(Common::SNANConstant<double>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatUtils, IsSNAN)
|
|
|
|
{
|
|
|
|
EXPECT_FALSE(Common::IsSNAN(std::numeric_limits<double>::quiet_NaN()));
|
|
|
|
EXPECT_TRUE(Common::IsSNAN(Common::SNANConstant<double>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FloatUtils, FlushToZero)
|
|
|
|
{
|
|
|
|
// To test the software implementation we need to make sure FTZ and DAZ are disabled.
|
|
|
|
// Using volatile here to ensure the compiler doesn't constant-fold it,
|
|
|
|
// we want the multiplication to occur at test runtime.
|
|
|
|
volatile float s = std::numeric_limits<float>::denorm_min();
|
|
|
|
volatile double d = std::numeric_limits<double>::denorm_min();
|
2021-07-18 00:41:41 +00:00
|
|
|
EXPECT_LT(0.f, s * 2);
|
|
|
|
EXPECT_LT(0.0, d * 2);
|
2018-05-07 05:18:41 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(+0.0, Common::FlushToZero(+std::numeric_limits<double>::denorm_min()));
|
|
|
|
EXPECT_EQ(-0.0, Common::FlushToZero(-std::numeric_limits<double>::denorm_min()));
|
|
|
|
EXPECT_EQ(+0.0, Common::FlushToZero(+std::numeric_limits<double>::min() / 2));
|
|
|
|
EXPECT_EQ(-0.0, Common::FlushToZero(-std::numeric_limits<double>::min() / 2));
|
|
|
|
EXPECT_EQ(std::numeric_limits<double>::min(),
|
|
|
|
Common::FlushToZero(std::numeric_limits<double>::min()));
|
|
|
|
EXPECT_EQ(std::numeric_limits<double>::max(),
|
|
|
|
Common::FlushToZero(std::numeric_limits<double>::max()));
|
|
|
|
EXPECT_EQ(+std::numeric_limits<double>::infinity(),
|
|
|
|
Common::FlushToZero(+std::numeric_limits<double>::infinity()));
|
|
|
|
EXPECT_EQ(-std::numeric_limits<double>::infinity(),
|
|
|
|
Common::FlushToZero(-std::numeric_limits<double>::infinity()));
|
|
|
|
|
|
|
|
// Test all subnormals as well as an equally large set of random normal floats.
|
|
|
|
std::default_random_engine engine(0);
|
|
|
|
std::uniform_int_distribution<u32> dist(0x00800000u, 0x7fffffffu);
|
|
|
|
for (u32 i = 0; i <= 0x007fffffu; ++i)
|
|
|
|
{
|
2018-05-10 14:35:00 +00:00
|
|
|
u32 i_tmp = i;
|
2022-08-06 04:10:17 +00:00
|
|
|
EXPECT_EQ(+0.f, Common::FlushToZero(std::bit_cast<float>(i_tmp)));
|
2018-05-07 05:18:41 +00:00
|
|
|
|
2018-05-10 14:35:00 +00:00
|
|
|
i_tmp |= 0x80000000u;
|
2022-08-06 04:10:17 +00:00
|
|
|
EXPECT_EQ(-0.f, Common::FlushToZero(std::bit_cast<float>(i_tmp)));
|
2018-05-07 05:18:41 +00:00
|
|
|
|
2018-05-10 14:35:00 +00:00
|
|
|
i_tmp = dist(engine);
|
2022-08-06 04:10:17 +00:00
|
|
|
EXPECT_EQ(i_tmp, std::bit_cast<u32>(Common::FlushToZero(std::bit_cast<float>(i_tmp))));
|
2018-05-07 05:18:41 +00:00
|
|
|
|
2018-05-10 14:35:00 +00:00
|
|
|
i_tmp |= 0x80000000u;
|
2022-08-06 04:10:17 +00:00
|
|
|
EXPECT_EQ(i_tmp, std::bit_cast<u32>(Common::FlushToZero(std::bit_cast<float>(i_tmp))));
|
2018-05-07 05:18:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 19:57:41 +00:00
|
|
|
|
|
|
|
TEST(FloatUtils, ApproximateReciprocalSquareRoot)
|
|
|
|
{
|
|
|
|
constexpr std::array<u64, 57> expected_values{
|
|
|
|
0x7FF0'0000'0000'0000, 0x617F'FE80'0000'0000, 0x60BF'FE80'0000'0000, 0x5FE0'0008'2C00'0000,
|
|
|
|
0x5FDF'FE80'0000'0000, 0x5FDF'FE80'0000'0000, 0x3FEF'FE80'0000'0000, 0x1FF0'0008'2C00'0000,
|
|
|
|
0x0000'0000'0000'0000, 0x7FF8'0000'0000'0001, 0x7FFF'FFFF'FFFF'FFFF, 0x7FF8'0000'0000'0000,
|
|
|
|
0x7FFF'FFFF'FFFF'FFFF, 0xFFF0'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0xFFF8'0000'0000'0001, 0xFFFF'FFFF'FFFF'FFFF,
|
|
|
|
0xFFF8'0000'0000'0000, 0xFFFF'FFFF'FFFF'FFFF, 0x43E6'9FA0'0000'0000, 0x43DF'FE80'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x43E6'9360'6000'0000, 0x43DF'ED30'7000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x44A6'9FA0'0000'0000, 0x4496'9FA0'0000'0000,
|
|
|
|
0x448F'FE80'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
|
|
|
|
0x44A6'9360'6000'0000, 0x4496'9360'6000'0000, 0x448F'ED30'7000'0000, 0x7FF8'0000'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x3C06'9FA0'0000'0000, 0x3BFF'FE80'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x43EF'FE80'0000'0000, 0x43F6'9FA0'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x3FEA'2040'0000'0000, 0x3FA0'3108'0000'0000,
|
|
|
|
0x7FF8'0000'0000'0000};
|
|
|
|
|
2023-12-11 23:08:52 +00:00
|
|
|
for (size_t i = 0; i < double_test_values.size(); ++i)
|
2023-10-29 19:57:41 +00:00
|
|
|
{
|
|
|
|
u64 ivalue = double_test_values[i];
|
2022-08-06 04:10:17 +00:00
|
|
|
double dvalue = std::bit_cast<double>(ivalue);
|
2023-10-29 19:57:41 +00:00
|
|
|
|
|
|
|
u64 expected = expected_values[i];
|
|
|
|
|
2022-08-06 04:10:17 +00:00
|
|
|
u64 actual = std::bit_cast<u64>(Common::ApproximateReciprocalSquareRoot(dvalue));
|
2023-10-29 19:57:41 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
}
|