Debian Codegraph @

Source/UnitTests/Common/FloatUtilsTest.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <array>
5 #include <bit>
6 #include <limits>
7 #include <random>
8
9 #include <gtest/gtest.h>
10
11 #include "Common/FloatUtils.h"
12
13 #include "../Core/PowerPC/TestValues.h"
14
15 TEST(FloatUtils, IsQNAN)
16 {
17 EXPECT_TRUE(Common::IsQNAN(std::numeric_limits<double>::quiet_NaN()));
18 EXPECT_FALSE(Common::IsQNAN(Common::SNANConstant<double>()));
19 }
20
21 TEST(FloatUtils, IsSNAN)
22 {
23 EXPECT_FALSE(Common::IsSNAN(std::numeric_limits<double>::quiet_NaN()));
24 EXPECT_TRUE(Common::IsSNAN(Common::SNANConstant<double>()));
25 }
26
27 TEST(FloatUtils, FlushToZero)
28 {
29 // To test the software implementation we need to make sure FTZ and DAZ are disabled.
30 // Using volatile here to ensure the compiler doesn't constant-fold it,
31 // we want the multiplication to occur at test runtime.
32 volatile float s = std::numeric_limits<float>::denorm_min();
33 volatile double d = std::numeric_limits<double>::denorm_min();
34 EXPECT_LT(0.f, s * 2);
35 EXPECT_LT(0.0, d * 2);
36
37 EXPECT_EQ(+0.0, Common::FlushToZero(+std::numeric_limits<double>::denorm_min()));
38 EXPECT_EQ(-0.0, Common::FlushToZero(-std::numeric_limits<double>::denorm_min()));
39 EXPECT_EQ(+0.0, Common::FlushToZero(+std::numeric_limits<double>::min() / 2));
40 EXPECT_EQ(-0.0, Common::FlushToZero(-std::numeric_limits<double>::min() / 2));
41 EXPECT_EQ(std::numeric_limits<double>::min(),
42 Common::FlushToZero(std::numeric_limits<double>::min()));
43 EXPECT_EQ(std::numeric_limits<double>::max(),
44 Common::FlushToZero(std::numeric_limits<double>::max()));
45 EXPECT_EQ(+std::numeric_limits<double>::infinity(),
46 Common::FlushToZero(+std::numeric_limits<double>::infinity()));
47 EXPECT_EQ(-std::numeric_limits<double>::infinity(),
48 Common::FlushToZero(-std::numeric_limits<double>::infinity()));
49
50 // Test all subnormals as well as an equally large set of random normal floats.
51 std::default_random_engine engine(0);
52 std::uniform_int_distribution dist(0x00800000u, 0x7fffffffu);
53 for (u32 i = 0; i <= 0x007fffffu; ++i)
54 {
55 u32 i_tmp = i;
56 EXPECT_EQ(+0.f, Common::FlushToZero(std::bit_cast<float>(i_tmp)));
57
58 i_tmp |= 0x80000000u;
59 EXPECT_EQ(-0.f, Common::FlushToZero(std::bit_cast<float>(i_tmp)));
60
61 i_tmp = dist(engine);
62 EXPECT_EQ(i_tmp, std::bit_cast<u32>(Common::FlushToZero(std::bit_cast<float>(i_tmp))));
63
64 i_tmp |= 0x80000000u;
65 EXPECT_EQ(i_tmp, std::bit_cast<u32>(Common::FlushToZero(std::bit_cast<float>(i_tmp))));
66 }
67 }
68
69 TEST(FloatUtils, ApproximateReciprocalSquareRoot)
70 {
71 constexpr std::array<u64, 57> expected_values{
72 0x7FF0'0000'0000'0000, 0x617F'FE80'0000'0000, 0x60BF'FE80'0000'0000, 0x5FE0'0008'2C00'0000,
73 0x5FDF'FE80'0000'0000, 0x5FDF'FE80'0000'0000, 0x3FEF'FE80'0000'0000, 0x1FF0'0008'2C00'0000,
74 0x0000'0000'0000'0000, 0x7FF8'0000'0000'0001, 0x7FFF'FFFF'FFFF'FFFF, 0x7FF8'0000'0000'0000,
75 0x7FFF'FFFF'FFFF'FFFF, 0xFFF0'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
76 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
77 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0xFFF8'0000'0000'0001, 0xFFFF'FFFF'FFFF'FFFF,
78 0xFFF8'0000'0000'0000, 0xFFFF'FFFF'FFFF'FFFF, 0x43E6'9FA0'0000'0000, 0x43DF'FE80'0000'0000,
79 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x43E6'9360'6000'0000, 0x43DF'ED30'7000'0000,
80 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x44A6'9FA0'0000'0000, 0x4496'9FA0'0000'0000,
81 0x448F'FE80'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000,
82 0x44A6'9360'6000'0000, 0x4496'9360'6000'0000, 0x448F'ED30'7000'0000, 0x7FF8'0000'0000'0000,
83 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x3C06'9FA0'0000'0000, 0x3BFF'FE80'0000'0000,
84 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x43EF'FE80'0000'0000, 0x43F6'9FA0'0000'0000,
85 0x7FF8'0000'0000'0000, 0x7FF8'0000'0000'0000, 0x3FEA'2040'0000'0000, 0x3FA0'3108'0000'0000,
86 0x7FF8'0000'0000'0000};
87
88 for (size_t i = 0; i < double_test_values.size(); ++i)
89 {
90 u64 ivalue = double_test_values[i];
91 double dvalue = std::bit_cast<double>(ivalue);
92
93 u64 expected = expected_values[i];
94
95 u64 actual = std::bit_cast<u64>(Common::ApproximateReciprocalSquareRoot(dvalue));
96
97 EXPECT_EQ(expected, actual);
98 }
99 }