MMU: Get rid of type punning in FP read/write functions

The previous code is actually considered undefined behavior.
This commit is contained in:
Lioncash 2016-09-30 03:49:15 -04:00
parent 74290e873a
commit e8b5e38d98

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cstring>
#include "Common/Atomic.h" #include "Common/Atomic.h"
#include "Common/BitSet.h" #include "Common/BitSet.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -470,24 +472,22 @@ u64 Read_U64(const u32 address)
double Read_F64(const u32 address) double Read_F64(const u32 address)
{ {
union { const u64 integral = Read_U64(address);
u64 i;
double d;
} cvt;
cvt.i = Read_U64(address); double real;
return cvt.d; std::memcpy(&real, &integral, sizeof(double));
return real;
} }
float Read_F32(const u32 address) float Read_F32(const u32 address)
{ {
union { const u32 integral = Read_U32(address);
u32 i;
float d;
} cvt;
cvt.i = Read_U32(address); float real;
return cvt.d; std::memcpy(&real, &integral, sizeof(float));
return real;
} }
u32 Read_U8_ZX(const u32 address) u32 Read_U8_ZX(const u32 address)
@ -541,12 +541,10 @@ void Write_U64_Swap(const u64 var, const u32 address)
void Write_F64(const double var, const u32 address) void Write_F64(const double var, const u32 address)
{ {
union { u64 integral;
u64 i; std::memcpy(&integral, &var, sizeof(u64));
double d;
} cvt; Write_U64(integral, address);
cvt.d = var;
Write_U64(cvt.i, address);
} }
u8 HostRead_U8(const u32 address) u8 HostRead_U8(const u32 address)