Skip to content

Commit

Permalink
FloatUtils: Clean up memcpy usages
Browse files Browse the repository at this point in the history
Now that we have BitCast, we can use that instead.
  • Loading branch information
lioncash committed May 10, 2018
1 parent b329229 commit bde4e97
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions Source/Core/Common/FloatUtils.cpp
Expand Up @@ -5,15 +5,14 @@
#include "Common/FloatUtils.h" #include "Common/FloatUtils.h"


#include <cmath> #include <cmath>
#include <cstring>
#include "Common/BitUtils.h"


namespace Common namespace Common
{ {
u32 ClassifyDouble(double dvalue) u32 ClassifyDouble(double dvalue)
{ {
u64 ivalue; const u64 ivalue = BitCast<u64>(dvalue);
std::memcpy(&ivalue, &dvalue, sizeof(ivalue));

const u64 sign = ivalue & DOUBLE_SIGN; const u64 sign = ivalue & DOUBLE_SIGN;
const u64 exp = ivalue & DOUBLE_EXP; const u64 exp = ivalue & DOUBLE_EXP;


Expand Down Expand Up @@ -45,9 +44,7 @@ u32 ClassifyDouble(double dvalue)


u32 ClassifyFloat(float fvalue) u32 ClassifyFloat(float fvalue)
{ {
u32 ivalue; const u32 ivalue = BitCast<u32>(fvalue);
std::memcpy(&ivalue, &fvalue, sizeof(ivalue));

const u32 sign = ivalue & FLOAT_SIGN; const u32 sign = ivalue & FLOAT_SIGN;
const u32 exp = ivalue & FLOAT_EXP; const u32 exp = ivalue & FLOAT_EXP;


Expand Down Expand Up @@ -90,9 +87,7 @@ const std::array<BaseAndDec, 32> frsqrte_expected = {{


double ApproximateReciprocalSquareRoot(double val) double ApproximateReciprocalSquareRoot(double val)
{ {
s64 integral; s64 integral = BitCast<s64>(val);
std::memcpy(&integral, &val, sizeof(integral));

s64 mantissa = integral & ((1LL << 52) - 1); s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63); const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52); s64 exponent = integral & (0x7FFLL << 52);
Expand Down Expand Up @@ -143,9 +138,7 @@ double ApproximateReciprocalSquareRoot(double val)
const auto& entry = frsqrte_expected[index]; const auto& entry = frsqrte_expected[index];
integral |= static_cast<s64>(entry.m_base - entry.m_dec * (i % 2048)) << 26; integral |= static_cast<s64>(entry.m_base - entry.m_dec * (i % 2048)) << 26;


double result; return BitCast<double>(integral);
std::memcpy(&result, &integral, sizeof(result));
return result;
} }


const std::array<BaseAndDec, 32> fres_expected = {{ const std::array<BaseAndDec, 32> fres_expected = {{
Expand All @@ -161,9 +154,7 @@ const std::array<BaseAndDec, 32> fres_expected = {{
// Used by fres and ps_res. // Used by fres and ps_res.
double ApproximateReciprocal(double val) double ApproximateReciprocal(double val)
{ {
s64 integral; s64 integral = BitCast<s64>(val);
std::memcpy(&integral, &val, sizeof(integral));

const s64 mantissa = integral & ((1LL << 52) - 1); const s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63); const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52); s64 exponent = integral & (0x7FFLL << 52);
Expand Down Expand Up @@ -195,9 +186,7 @@ double ApproximateReciprocal(double val)
integral = sign | exponent; integral = sign | exponent;
integral |= static_cast<s64>(entry.m_base - (entry.m_dec * (i % 1024) + 1) / 2) << 29; integral |= static_cast<s64>(entry.m_base - (entry.m_dec * (i % 1024) + 1) / 2) << 29;


double result; return BitCast<double>(integral);
std::memcpy(&result, &integral, sizeof(result));
return result;
} }


} // namespace Common } // namespace Common

0 comments on commit bde4e97

Please sign in to comment.