Use bit_cast instead of memcpy/reinterpret_cast where applicable

This commit is contained in:
SimoneN64
2024-10-14 17:01:58 +02:00
parent b6f795a4df
commit 617a82abff

View File

@@ -120,26 +120,19 @@ auto Cop1::FGR_D<double>(Cop0Status &status, u32 index) -> double & {
return FGR_T<double>(status, index); return FGR_T<double>(status, index);
} }
u32 floatIntegerCast(float f) { template <typename To, typename From>
u32 v; To floatIntegerCast(From f) {
memcpy(&v, &f, 4); return std::bit_cast<To, From>(f);
return v;
}
u64 doubleIntegerCast(double f) {
u64 v;
memcpy(&v, &f, 8);
return v;
} }
template <> template <>
bool Cop1::isqnan<float>(float f) { bool Cop1::isqnan<float>(float f) {
return (floatIntegerCast(f) >> 22) & 1; return (floatIntegerCast<u32>(f) >> 22) & 1;
} }
template <> template <>
bool Cop1::isqnan<double>(double f) { bool Cop1::isqnan<double>(double f) {
return (doubleIntegerCast(f) >> 51) & 1; return (floatIntegerCast<u64>(f) >> 51) & 1;
} }
template <> template <>
@@ -313,8 +306,7 @@ bool Cop1::CheckResult<float>(float &f) {
return true; return true;
case FP_NAN: case FP_NAN:
{ {
uint32_t v = 0x7fbf'ffff; f = std::bit_cast<float, u32>(0x7fbf'ffff);
memcpy(&f, &v, 4);
return true; return true;
} }
} }
@@ -336,8 +328,7 @@ bool Cop1::CheckResult<double>(double &f) {
return true; return true;
case FP_NAN: case FP_NAN:
{ {
uint64_t v = 0x7ff7'ffff'ffff'ffff; f = std::bit_cast<double, u64>(0x7ff7'ffff'ffff'ffff);
memcpy(&f, &v, 8);
return true; return true;
} }
} }