File tree Expand file tree Collapse file tree 1 file changed +11
-22
lines changed Expand file tree Collapse file tree 1 file changed +11
-22
lines changed Original file line number Diff line number Diff line change @@ -144,22 +144,16 @@ inline namespace v1 {
144
144
}
145
145
146
146
void Serializer::writeFloat (float data) {
147
- union {
148
- float f32 ;
149
- uint32_t u32 ;
150
- };
151
-
152
- f32 = data;
147
+ static_assert (sizeof (uint32_t ) == sizeof (float ), " float should be 32 bits" );
148
+ uint32_t u32 ;
149
+ std::memcpy (&u32 , &data, sizeof (float ));
153
150
writeBigEndian32 (u32 );
154
151
}
155
152
156
153
void Serializer::writeDouble (double data) {
157
- union {
158
- double f64 ;
159
- uint64_t u64 ;
160
- };
161
-
162
- f64 = data;
154
+ static_assert (sizeof (uint64_t ) == sizeof (double ), " double should be 64 bits" );
155
+ uint64_t u64 ;
156
+ std::memcpy (&u64 , &data, sizeof (double ));
163
157
writeBigEndian64 (u64 );
164
158
}
165
159
@@ -421,31 +415,26 @@ inline namespace v1 {
421
415
}
422
416
423
417
bool Deserializer::readFloat (float & data) {
424
- union {
425
- float f32 ;
426
- uint32_t u32 ;
427
- };
418
+ uint32_t u32 ;
428
419
429
420
if (!readBigEndian32 (u32 )) {
430
421
Log::error (" Asking for float but the file is at the end.\n " );
431
422
return false ;
432
423
}
433
424
434
- data = f32 ;
425
+ std::memcpy (& data, & u32 , sizeof ( float )) ;
435
426
return true ;
436
427
}
437
428
438
429
bool Deserializer::readDouble (double & data) {
439
- union {
440
- double f64 ;
441
- uint64_t u64 ;
442
- };
430
+ uint64_t u64 ;
443
431
444
432
if (!readBigEndian64 (u64 )) {
433
+ Log::error (" Asking for double but the file is at the end.\n " );
445
434
return false ;
446
435
}
447
436
448
- data = f64 ;
437
+ std::memcpy (& data, & u64 , sizeof ( double )) ;
449
438
return true ;
450
439
}
451
440
You can’t perform that action at this time.
0 commit comments