MathUtil: fix IsQNAN()

The constants were one nibble too short and the lower 51 bits don't
actually have to be zero.
This commit is contained in:
Tillmann Karras 2014-03-09 19:34:58 +01:00
parent d71aef8843
commit 9ef64245fa
3 changed files with 41 additions and 24 deletions

View File

@ -20,11 +20,13 @@ inline void Clamp(T* val, const T& min, const T& max)
*val = max; *val = max;
} }
// The most significant bit of the fraction is an is-quiet bit on all architectures we care about.
static const u64 DOUBLE_SIGN = 0x8000000000000000ULL, static const u64 DOUBLE_SIGN = 0x8000000000000000ULL,
DOUBLE_EXP = 0x7FF0000000000000ULL, DOUBLE_EXP = 0x7FF0000000000000ULL,
DOUBLE_FRAC = 0x000FFFFFFFFFFFFFULL, DOUBLE_FRAC = 0x000FFFFFFFFFFFFFULL,
DOUBLE_ZERO = 0x0000000000000000ULL; DOUBLE_ZERO = 0x0000000000000000ULL,
DOUBLE_QBIT = 0x0008000000000000ULL;
static const u32 FLOAT_SIGN = 0x80000000, static const u32 FLOAT_SIGN = 0x80000000,
FLOAT_EXP = 0x7F800000, FLOAT_EXP = 0x7F800000,
@ -40,34 +42,41 @@ union IntFloat {
u32 i; u32 i;
}; };
inline bool IsINF(double d)
{
IntDouble x; x.d = d;
return (x.i & ~DOUBLE_SIGN) == DOUBLE_EXP;
}
inline bool IsNAN(double d) inline bool IsNAN(double d)
{ {
IntDouble x; x.d = d; IntDouble x; x.d = d;
return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) ); ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO);
} }
inline bool IsQNAN(double d) inline bool IsQNAN(double d)
{ {
IntDouble x; x.d = d; IntDouble x; x.d = d;
return ( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
((x.i & 0x0007fffffffffffULL) == 0x000000000000000ULL) && ((x.i & DOUBLE_QBIT) == DOUBLE_QBIT);
((x.i & 0x000800000000000ULL) == 0x000800000000000ULL) );
} }
inline bool IsSNAN(double d) inline bool IsSNAN(double d)
{ {
IntDouble x; x.d = d; IntDouble x; x.d = d;
return( ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) &&
((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) && ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
((x.i & 0x0008000000000000ULL) == DOUBLE_ZERO) ); ((x.i & DOUBLE_QBIT) == DOUBLE_ZERO);
} }
inline float FlushToZero(float f) inline float FlushToZero(float f)
{ {
IntFloat x; x.f = f; IntFloat x; x.f = f;
if ((x.i & FLOAT_EXP) == 0) if ((x.i & FLOAT_EXP) == 0)
{
x.i &= FLOAT_SIGN; // turn into signed zero x.i &= FLOAT_SIGN; // turn into signed zero
}
return x.f; return x.f;
} }
@ -75,7 +84,9 @@ inline double FlushToZero(double d)
{ {
IntDouble x; x.d = d; IntDouble x; x.d = d;
if ((x.i & DOUBLE_EXP) == 0) if ((x.i & DOUBLE_EXP) == 0)
{
x.i &= DOUBLE_SIGN; // turn into signed zero x.i &= DOUBLE_SIGN; // turn into signed zero
}
return x.d; return x.d;
} }

View File

@ -40,15 +40,12 @@ const u32 FPSCR_ANY_X = FPSCR_OX | FPSCR_UX | FPSCR_ZX | FPSCR_XX | FPSCR_V
const u64 PPC_NAN_U64 = 0x7ff8000000000000ull; const u64 PPC_NAN_U64 = 0x7ff8000000000000ull;
const double PPC_NAN = *(double* const)&PPC_NAN_U64; const double PPC_NAN = *(double* const)&PPC_NAN_U64;
inline bool IsINF(double x)
{
return ((*(u64*)&x) & ~DOUBLE_SIGN) == DOUBLE_EXP;
}
inline void SetFPException(u32 mask) inline void SetFPException(u32 mask)
{ {
if ((FPSCR.Hex & mask) != mask) if ((FPSCR.Hex & mask) != mask)
{
FPSCR.FX = 1; FPSCR.FX = 1;
}
FPSCR.Hex |= mask; FPSCR.Hex |= mask;
} }

View File

@ -2,8 +2,8 @@
// Licensed under GPLv2 // Licensed under GPLv2
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cmath>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <limits>
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
@ -27,19 +27,28 @@ TEST(MathUtil, Clamp)
EXPECT_EQ(0.0, ClampAndReturn(-1.0, 0.0, 2.0)); EXPECT_EQ(0.0, ClampAndReturn(-1.0, 0.0, 2.0));
} }
TEST(MathUtil, IsINF)
{
EXPECT_TRUE(MathUtil::IsINF( std::numeric_limits<double>::infinity()));
EXPECT_TRUE(MathUtil::IsINF(-std::numeric_limits<double>::infinity()));
}
TEST(MathUtil, IsNAN) TEST(MathUtil, IsNAN)
{ {
EXPECT_TRUE(MathUtil::IsNAN(nan(""))); EXPECT_TRUE(MathUtil::IsNAN(std::numeric_limits<double>::quiet_NaN()));
EXPECT_TRUE(MathUtil::IsNAN(std::numeric_limits<double>::signaling_NaN()));
} }
TEST(MathUtil, IsQNAN) TEST(MathUtil, IsQNAN)
{ {
// TODO EXPECT_TRUE(MathUtil::IsQNAN(std::numeric_limits<double>::quiet_NaN()));
EXPECT_FALSE(MathUtil::IsQNAN(std::numeric_limits<double>::signaling_NaN()));
} }
TEST(MathUtil, IsSNAN) TEST(MathUtil, IsSNAN)
{ {
// TODO EXPECT_FALSE(MathUtil::IsSNAN(std::numeric_limits<double>::quiet_NaN()));
EXPECT_TRUE(MathUtil::IsSNAN(std::numeric_limits<double>::signaling_NaN()));
} }
TEST(MathUtil, Log2) TEST(MathUtil, Log2)