From 617a82abffacb88ff8e06988890e857826e4c88c Mon Sep 17 00:00:00 2001 From: SimoneN64 Date: Mon, 14 Oct 2024 17:01:58 +0200 Subject: [PATCH] Use bit_cast instead of memcpy/reinterpret_cast where applicable --- .../core/registers/cop/cop1instructions.cpp | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/backend/core/registers/cop/cop1instructions.cpp b/src/backend/core/registers/cop/cop1instructions.cpp index dbb4783e..756c7b29 100644 --- a/src/backend/core/registers/cop/cop1instructions.cpp +++ b/src/backend/core/registers/cop/cop1instructions.cpp @@ -120,26 +120,19 @@ auto Cop1::FGR_D(Cop0Status &status, u32 index) -> double & { return FGR_T(status, index); } -u32 floatIntegerCast(float f) { - u32 v; - memcpy(&v, &f, 4); - return v; -} - -u64 doubleIntegerCast(double f) { - u64 v; - memcpy(&v, &f, 8); - return v; +template +To floatIntegerCast(From f) { + return std::bit_cast(f); } template <> bool Cop1::isqnan(float f) { - return (floatIntegerCast(f) >> 22) & 1; + return (floatIntegerCast(f) >> 22) & 1; } template <> bool Cop1::isqnan(double f) { - return (doubleIntegerCast(f) >> 51) & 1; + return (floatIntegerCast(f) >> 51) & 1; } template <> @@ -313,8 +306,7 @@ bool Cop1::CheckResult(float &f) { return true; case FP_NAN: { - uint32_t v = 0x7fbf'ffff; - memcpy(&f, &v, 4); + f = std::bit_cast(0x7fbf'ffff); return true; } } @@ -336,8 +328,7 @@ bool Cop1::CheckResult(double &f) { return true; case FP_NAN: { - uint64_t v = 0x7ff7'ffff'ffff'ffff; - memcpy(&f, &v, 8); + f = std::bit_cast(0x7ff7'ffff'ffff'ffff); return true; } }