mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-26 14:28:34 +00:00
Common: Move floating-point utility functions to FloatUtils.h/.cpp
Keeps all of the floating-point utility functions in their own file to keep them all together. This also provides a place for other general-purpose floating-point functions to be added in the future, which will be necessary when improving the flag-setting within the interpreter.
This commit is contained in:
parent
756ef54ab6
commit
86018b503b
17 changed files with 474 additions and 434 deletions
68
Source/UnitTests/Common/FloatUtilsTest.cpp
Normal file
68
Source/UnitTests/Common/FloatUtilsTest.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
// Copyright 2018 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <limits>
|
||||
#include <random>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Common/FloatUtils.h"
|
||||
|
||||
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();
|
||||
// Casting away the volatile attribute is required in order for msvc to resolve this to the
|
||||
// correct instance of the comparison function.
|
||||
EXPECT_LT(0.f, (float)(s * 2));
|
||||
EXPECT_LT(0.0, (double)(d * 2));
|
||||
|
||||
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)
|
||||
{
|
||||
Common::IntFloat x(i);
|
||||
EXPECT_EQ(+0.f, Common::FlushToZero(x.f));
|
||||
|
||||
x.i = i | 0x80000000u;
|
||||
EXPECT_EQ(-0.f, Common::FlushToZero(x.f));
|
||||
|
||||
x.i = dist(engine);
|
||||
Common::IntFloat y(Common::FlushToZero(x.f));
|
||||
EXPECT_EQ(x.i, y.i);
|
||||
|
||||
x.i |= 0x80000000u;
|
||||
y.f = Common::FlushToZero(x.f);
|
||||
EXPECT_EQ(x.i, y.i);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue