File tree Expand file tree Collapse file tree 1 file changed +16
-12
lines changed
Expand file tree Collapse file tree 1 file changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -126,27 +126,31 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
126126}
127127inline uint64_t ser_double_to_uint64 (double x)
128128{
129- union { double x; uint64_t y; } tmp;
130- tmp.x = x;
131- return tmp.y ;
129+ static_assert (sizeof (uint64_t ) == sizeof (double ), " Floating-point width assumption" );
130+ uint64_t y;
131+ memcpy (&y, &x, sizeof (y));
132+ return y;
132133}
133134inline uint32_t ser_float_to_uint32 (float x)
134135{
135- union { float x; uint32_t y; } tmp;
136- tmp.x = x;
137- return tmp.y ;
136+ static_assert (sizeof (uint32_t ) == sizeof (float ), " Floating-point width assumption" );
137+ uint32_t y;
138+ memcpy (&y, &x, sizeof (y));
139+ return y;
138140}
139141inline double ser_uint64_to_double (uint64_t y)
140142{
141- union { double x; uint64_t y; } tmp;
142- tmp.y = y;
143- return tmp.x ;
143+ static_assert (sizeof (double ) == sizeof (uint64_t ), " Floating-point width assumption" );
144+ double x;
145+ memcpy (&x, &y, sizeof (x));
146+ return x;
144147}
145148inline float ser_uint32_to_float (uint32_t y)
146149{
147- union { float x; uint32_t y; } tmp;
148- tmp.y = y;
149- return tmp.x ;
150+ static_assert (sizeof (float ) == sizeof (uint32_t ), " Floating-point width assumption" );
151+ float x;
152+ memcpy (&x, &y, sizeof (x));
153+ return x;
150154}
151155
152156
You can’t perform that action at this time.
0 commit comments