Merge pull request #6803 from lioncash/tidy

FloatUtils: Remove IntDouble and IntFloat
This commit is contained in:
Léo Lam
2018-05-10 19:04:49 +02:00
committed by GitHub
6 changed files with 87 additions and 65 deletions

View File

@ -6,6 +6,7 @@
#include <climits>
#include <cstddef>
#include <cstring>
#include <type_traits>
namespace Common
@ -165,4 +166,37 @@ constexpr bool IsValidLowMask(const T mask) noexcept
// and doesn't require special casing either edge case.
return (mask & (mask + 1)) == 0;
}
///
/// Reinterpret objects of one type as another by bit-casting between object representations.
///
/// @remark This is the example implementation of std::bit_cast which is to be included
/// in C++2a. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0476r2.html
/// for more details. The only difference is this variant is not constexpr,
/// as the mechanism for bit_cast requires a compiler built-in to have that quality.
///
/// @param source The source object to convert to another representation.
///
/// @tparam To The type to reinterpret source as.
/// @tparam From The initial type representation of source.
///
/// @return The representation of type From as type To.
///
/// @pre Both To and From types must be the same size
/// @pre Both To and From types must satisfy the TriviallyCopyable concept.
///
template <typename To, typename From>
inline To BitCast(const From& source) noexcept
{
static_assert(sizeof(From) == sizeof(To),
"BitCast source and destination types must be equal in size.");
static_assert(std::is_trivially_copyable<From>(),
"BitCast source type must be trivially copyable.");
static_assert(std::is_trivially_copyable<To>(),
"BitCast destination type must be trivially copyable.");
std::aligned_storage_t<sizeof(To), alignof(To)> storage;
std::memcpy(&storage, &source, sizeof(storage));
return reinterpret_cast<To&>(storage);
}
} // namespace Common

View File

@ -5,15 +5,14 @@
#include "Common/FloatUtils.h"
#include <cmath>
#include <cstring>
#include "Common/BitUtils.h"
namespace Common
{
u32 ClassifyDouble(double dvalue)
{
u64 ivalue;
std::memcpy(&ivalue, &dvalue, sizeof(ivalue));
const u64 ivalue = BitCast<u64>(dvalue);
const u64 sign = ivalue & DOUBLE_SIGN;
const u64 exp = ivalue & DOUBLE_EXP;
@ -45,9 +44,7 @@ u32 ClassifyDouble(double dvalue)
u32 ClassifyFloat(float fvalue)
{
u32 ivalue;
std::memcpy(&ivalue, &fvalue, sizeof(ivalue));
const u32 ivalue = BitCast<u32>(fvalue);
const u32 sign = ivalue & FLOAT_SIGN;
const u32 exp = ivalue & FLOAT_EXP;
@ -90,9 +87,7 @@ const std::array<BaseAndDec, 32> frsqrte_expected = {{
double ApproximateReciprocalSquareRoot(double val)
{
s64 integral;
std::memcpy(&integral, &val, sizeof(integral));
s64 integral = BitCast<s64>(val);
s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52);
@ -143,9 +138,7 @@ double ApproximateReciprocalSquareRoot(double val)
const auto& entry = frsqrte_expected[index];
integral |= static_cast<s64>(entry.m_base - entry.m_dec * (i % 2048)) << 26;
double result;
std::memcpy(&result, &integral, sizeof(result));
return result;
return BitCast<double>(integral);
}
const std::array<BaseAndDec, 32> fres_expected = {{
@ -161,9 +154,7 @@ const std::array<BaseAndDec, 32> fres_expected = {{
// Used by fres and ps_res.
double ApproximateReciprocal(double val)
{
s64 integral;
std::memcpy(&integral, &val, sizeof(integral));
s64 integral = BitCast<s64>(val);
const s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52);
@ -195,9 +186,7 @@ double ApproximateReciprocal(double val)
integral = sign | exponent;
integral |= static_cast<s64>(entry.m_base - (entry.m_dec * (i % 1024) + 1) / 2) << 29;
double result;
std::memcpy(&result, &integral, sizeof(result));
return result;
return BitCast<double>(integral);
}
} // namespace Common

View File

@ -7,6 +7,7 @@
#include <array>
#include <limits>
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
namespace Common
@ -55,54 +56,39 @@ enum : u32
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)
{
IntDouble x(d);
return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && ((x.i & DOUBLE_QBIT) == DOUBLE_QBIT);
const u64 i = BitCast<u64>(d);
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_QBIT) == DOUBLE_QBIT);
}
inline bool IsSNAN(double d)
{
IntDouble x(d);
return ((x.i & DOUBLE_EXP) == DOUBLE_EXP) && ((x.i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
((x.i & DOUBLE_QBIT) == DOUBLE_ZERO);
const u64 i = BitCast<u64>(d);
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
((i & DOUBLE_QBIT) == DOUBLE_ZERO);
}
inline float FlushToZero(float f)
{
IntFloat x(f);
if ((x.i & FLOAT_EXP) == 0)
u32 i = BitCast<u32>(f);
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)
{
IntDouble x(d);
if ((x.i & DOUBLE_EXP) == 0)
u64 i = BitCast<u64>(d);
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