Fix a violation of C++ standard rules that unions cannot be switched.

This commit is contained in:
Samer Afach 2020-02-17 20:53:50 +01:00
parent 179504ccb6
commit be94096dfb

View File

@ -9,6 +9,7 @@
#include <compat/endian.h> #include <compat/endian.h>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <ios> #include <ios>
#include <limits> #include <limits>
#include <map> #include <map>
@ -139,27 +140,27 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
} }
inline uint64_t ser_double_to_uint64(double x) inline uint64_t ser_double_to_uint64(double x)
{ {
union { double x; uint64_t y; } tmp; uint64_t tmp;
tmp.x = x; std::memcpy(&tmp, &x, sizeof(x));
return tmp.y; return tmp;
} }
inline uint32_t ser_float_to_uint32(float x) inline uint32_t ser_float_to_uint32(float x)
{ {
union { float x; uint32_t y; } tmp; uint32_t tmp;
tmp.x = x; std::memcpy(&tmp, &x, sizeof(x));
return tmp.y; return tmp;
} }
inline double ser_uint64_to_double(uint64_t y) inline double ser_uint64_to_double(uint64_t y)
{ {
union { double x; uint64_t y; } tmp; double tmp;
tmp.y = y; std::memcpy(&tmp, &y, sizeof(y));
return tmp.x; return tmp;
} }
inline float ser_uint32_to_float(uint32_t y) inline float ser_uint32_to_float(uint32_t y)
{ {
union { float x; uint32_t y; } tmp; float tmp;
tmp.y = y; std::memcpy(&tmp, &y, sizeof(y));
return tmp.x; return tmp;
} }