mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-25 07:21:14 +01:00
FloatUtils: Remove IntDouble and IntFloat
Type punning via unions in C++ invokes undefined behavior. Instead, leverage BitCast, our variant of C++2a's std::bit_cast
This commit is contained in:
parent
bde4e970f1
commit
0a3631cc76
@ -7,6 +7,7 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
#include "Common/BitUtils.h"
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
@ -55,54 +56,39 @@ enum : u32
|
|||||||
FLOAT_ZERO = 0x00000000
|
FLOAT_ZERO = 0x00000000
|
||||||
};
|
};
|
||||||
|
|
||||||
union IntDouble
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
u64 i;
|
|
||||||
|
|
||||||
explicit IntDouble(u64 _i) : i(_i) {}
|
|
||||||
explicit IntDouble(double _d) : d(_d) {}
|
|
||||||
};
|
|
||||||
union IntFloat
|
|
||||||
{
|
|
||||||
float f;
|
|
||||||
u32 i;
|
|
||||||
|
|
||||||
explicit IntFloat(u32 _i) : i(_i) {}
|
|
||||||
explicit IntFloat(float _f) : f(_f) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline bool IsQNAN(double d)
|
inline bool IsQNAN(double d)
|
||||||
{
|
{
|
||||||
IntDouble x(d);
|
const u64 i = BitCast<u64>(d);
|
||||||
return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && ((x.i & DOUBLE_QBIT) == DOUBLE_QBIT);
|
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_QBIT) == DOUBLE_QBIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool IsSNAN(double d)
|
inline bool IsSNAN(double d)
|
||||||
{
|
{
|
||||||
IntDouble x(d);
|
const u64 i = BitCast<u64>(d);
|
||||||
return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
|
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
|
||||||
((x.i & DOUBLE_QBIT) == DOUBLE_ZERO);
|
((i & DOUBLE_QBIT) == DOUBLE_ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float FlushToZero(float f)
|
inline float FlushToZero(float f)
|
||||||
{
|
{
|
||||||
IntFloat x(f);
|
u32 i = BitCast<u32>(f);
|
||||||
if ((x.i & FLOAT_EXP) == 0)
|
if ((i & FLOAT_EXP) == 0)
|
||||||
{
|
{
|
||||||
x.i &= FLOAT_SIGN; // turn into signed zero
|
// Turn into signed zero
|
||||||
|
i &= FLOAT_SIGN;
|
||||||
}
|
}
|
||||||
return x.f;
|
return BitCast<float>(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double FlushToZero(double d)
|
inline double FlushToZero(double d)
|
||||||
{
|
{
|
||||||
IntDouble x(d);
|
u64 i = BitCast<u64>(d);
|
||||||
if ((x.i & DOUBLE_EXP) == 0)
|
if ((i & DOUBLE_EXP) == 0)
|
||||||
{
|
{
|
||||||
x.i &= DOUBLE_SIGN; // turn into signed zero
|
// Turn into signed zero
|
||||||
|
i &= DOUBLE_SIGN;
|
||||||
}
|
}
|
||||||
return x.d;
|
return BitCast<double>(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PPCFpClass
|
enum PPCFpClass
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "Common/BitUtils.h"
|
||||||
#include "Common/FloatUtils.h"
|
#include "Common/FloatUtils.h"
|
||||||
|
|
||||||
TEST(FloatUtils, IsQNAN)
|
TEST(FloatUtils, IsQNAN)
|
||||||
@ -51,18 +52,16 @@ TEST(FloatUtils, FlushToZero)
|
|||||||
std::uniform_int_distribution<u32> dist(0x00800000u, 0x7fffffffu);
|
std::uniform_int_distribution<u32> dist(0x00800000u, 0x7fffffffu);
|
||||||
for (u32 i = 0; i <= 0x007fffffu; ++i)
|
for (u32 i = 0; i <= 0x007fffffu; ++i)
|
||||||
{
|
{
|
||||||
Common::IntFloat x(i);
|
u32 i_tmp = i;
|
||||||
EXPECT_EQ(+0.f, Common::FlushToZero(x.f));
|
EXPECT_EQ(+0.f, Common::FlushToZero(Common::BitCast<float>(i_tmp)));
|
||||||
|
|
||||||
x.i = i | 0x80000000u;
|
i_tmp |= 0x80000000u;
|
||||||
EXPECT_EQ(-0.f, Common::FlushToZero(x.f));
|
EXPECT_EQ(-0.f, Common::FlushToZero(Common::BitCast<float>(i_tmp)));
|
||||||
|
|
||||||
x.i = dist(engine);
|
i_tmp = dist(engine);
|
||||||
Common::IntFloat y(Common::FlushToZero(x.f));
|
EXPECT_EQ(i_tmp, Common::BitCast<u32>(Common::FlushToZero(Common::BitCast<float>(i_tmp))));
|
||||||
EXPECT_EQ(x.i, y.i);
|
|
||||||
|
|
||||||
x.i |= 0x80000000u;
|
i_tmp |= 0x80000000u;
|
||||||
y.f = Common::FlushToZero(x.f);
|
EXPECT_EQ(i_tmp, Common::BitCast<u32>(Common::FlushToZero(Common::BitCast<float>(i_tmp))));
|
||||||
EXPECT_EQ(x.i, y.i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h> // NOLINT
|
#include <gtest/gtest.h> // NOLINT
|
||||||
|
|
||||||
|
#include "Common/BitUtils.h"
|
||||||
#include "Common/Common.h"
|
#include "Common/Common.h"
|
||||||
#include "Common/FloatUtils.h"
|
|
||||||
#include "VideoCommon/CPMemory.h"
|
#include "VideoCommon/CPMemory.h"
|
||||||
#include "VideoCommon/DataReader.h"
|
#include "VideoCommon/DataReader.h"
|
||||||
#include "VideoCommon/OpcodeDecoding.h"
|
#include "VideoCommon/OpcodeDecoding.h"
|
||||||
@ -72,14 +72,15 @@ protected:
|
|||||||
m_src.Write<T, true>(val);
|
m_src.Write<T, true>(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExpectOut(float val)
|
void ExpectOut(float expected)
|
||||||
{
|
{
|
||||||
// Read unswapped.
|
// Read unswapped.
|
||||||
Common::IntFloat expected(val), actual(m_dst.Read<float, false>());
|
const float actual = m_dst.Read<float, false>();
|
||||||
if (!actual.f || actual.f != actual.f)
|
|
||||||
EXPECT_EQ(expected.i, actual.i);
|
if (!actual || actual != actual)
|
||||||
|
EXPECT_EQ(Common::BitCast<u32>(expected), Common::BitCast<u32>(actual));
|
||||||
else
|
else
|
||||||
EXPECT_EQ(expected.f, actual.f);
|
EXPECT_EQ(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunVertices(int count, int expected_count = -1)
|
void RunVertices(int count, int expected_count = -1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user