mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
Fix a violation of C++ standard rules that unions cannot be switched.
This commit is contained in:
parent
179504ccb6
commit
be94096dfb
1 changed files with 13 additions and 12 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <compat/endian.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#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)
|
||||
{
|
||||
union { double x; uint64_t y; } tmp;
|
||||
tmp.x = x;
|
||||
return tmp.y;
|
||||
uint64_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
return tmp;
|
||||
}
|
||||
inline uint32_t ser_float_to_uint32(float x)
|
||||
{
|
||||
union { float x; uint32_t y; } tmp;
|
||||
tmp.x = x;
|
||||
return tmp.y;
|
||||
uint32_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
return tmp;
|
||||
}
|
||||
inline double ser_uint64_to_double(uint64_t y)
|
||||
{
|
||||
union { double x; uint64_t y; } tmp;
|
||||
tmp.y = y;
|
||||
return tmp.x;
|
||||
double tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
return tmp;
|
||||
}
|
||||
inline float ser_uint32_to_float(uint32_t y)
|
||||
{
|
||||
union { float x; uint32_t y; } tmp;
|
||||
tmp.y = y;
|
||||
return tmp.x;
|
||||
float tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue