Skip to content

Commit

Permalink
Fix endianness bug in write_digit2_separated
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Jan 4, 2022
1 parent fc1783f commit 0273005
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,22 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b,
auto usep = static_cast<unsigned long long>(sep);
// Add ASCII '0' to each digit byte and insert separators.
digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
#ifndef _WIN32
# if defined(__GNUC__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
digits = __builtin_bswap64(digits);
# else
if (is_big_endian()) {
digits = ((digits & 0xff00000000000000ull) >> 56) |
((digits & 0x00ff000000000000ull) >> 40) |
((digits & 0x0000ff0000000000ull) >> 24) |
((digits & 0x000000ff00000000ull) >> 8) |
((digits & 0x00000000ff000000ull) << 8) |
((digits & 0x0000000000ff0000ull) << 24) |
((digits & 0x000000000000ff00ull) << 40) |
((digits & 0x00000000000000ffull) << 56);
}
# endif
#endif
memcpy(buf, &digits, 8);
}

Expand Down

0 comments on commit 0273005

Please sign in to comment.