Skip to content

Commit ee3e9ad

Browse files
committed
use more bit_cast
Signed-off-by: Rosen Penev <[email protected]>
1 parent c624554 commit ee3e9ad

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/types.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
318318
// This algorithm assumes that the internal representation of the double
319319
// type is the 8-byte IEEE 754 binary64 format, which is common but not
320320
// required by the C++ standard.
321+
#ifdef __cpp_lib_bit_cast
322+
if (byteOrder == littleEndian)
323+
return std::bit_cast<double>(static_cast<uint64_t>(buf[7]) << 56 | static_cast<uint64_t>(buf[6]) << 48 |
324+
static_cast<uint64_t>(buf[5]) << 40 | static_cast<uint64_t>(buf[4]) << 32 |
325+
static_cast<uint64_t>(buf[3]) << 24 | static_cast<uint64_t>(buf[2]) << 16 |
326+
static_cast<uint64_t>(buf[1]) << 8 | static_cast<uint64_t>(buf[0]));
327+
return std::bit_cast<double>(static_cast<uint64_t>(buf[0]) << 56 | static_cast<uint64_t>(buf[1]) << 48 |
328+
static_cast<uint64_t>(buf[2]) << 40 | static_cast<uint64_t>(buf[3]) << 32 |
329+
static_cast<uint64_t>(buf[4]) << 24 | static_cast<uint64_t>(buf[5]) << 16 |
330+
static_cast<uint64_t>(buf[6]) << 8 | static_cast<uint64_t>(buf[7]));
331+
#else
321332
union {
322333
uint64_t ull_;
323334
double d_;
@@ -335,6 +346,7 @@ double getDouble(const byte* buf, ByteOrder byteOrder) {
335346
static_cast<uint64_t>(buf[6]) << 8 | static_cast<uint64_t>(buf[7]);
336347
}
337348
return u.d_;
349+
#endif
338350
}
339351

340352
size_t us2Data(byte* buf, uint16_t s, ByteOrder byteOrder) {

0 commit comments

Comments
 (0)