@@ -314,6 +314,12 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
314
314
// This algorithm assumes that the internal representation of the double
315
315
// type is the 8-byte IEEE 754 binary64 format, which is common but not
316
316
// required by the C++ standard.
317
+ #ifdef __cpp_lib_bit_cast
318
+ uint64_t u;
319
+ std::memcpy (&u, buf, sizeof (uint64_t ));
320
+ u = Image::byteSwap (u, byteOrder == bigEndian);
321
+ return std::bit_cast<double >(u);
322
+ #else
317
323
union {
318
324
uint64_t ull_;
319
325
double d_;
@@ -331,6 +337,7 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
331
337
static_cast <uint64_t >(buf[6 ]) << 8 | static_cast <uint64_t >(buf[7 ]);
332
338
}
333
339
return u.d_ ;
340
+ #endif
334
341
}
335
342
336
343
size_t us2Data (byte* buf, uint16_t s, ByteOrder byteOrder) {
@@ -395,6 +402,11 @@ size_t d2Data(byte* buf, double d, ByteOrder byteOrder) {
395
402
// This algorithm assumes that the internal representation of the double
396
403
// type is the 8-byte IEEE 754 binary64 format, which is common but not
397
404
// required by the C++ standard.
405
+ #ifdef __cpp_lib_bit_cast
406
+ auto u = Image::byteSwap (std::bit_cast<uint64_t >(d), byteOrder == bigEndian);
407
+ std::memcpy (buf, &u, sizeof (u));
408
+ return sizeof (u);
409
+ #else
398
410
union {
399
411
uint64_t ull_;
400
412
double d_;
@@ -421,6 +433,7 @@ size_t d2Data(byte* buf, double d, ByteOrder byteOrder) {
421
433
buf[7 ] = static_cast <byte>(u.ull_ & m);
422
434
}
423
435
return 8 ;
436
+ #endif
424
437
}
425
438
426
439
void hexdump (std::ostream& os, const byte* buf, size_t len, size_t offset) {
0 commit comments