From 074cc781cb9b04d4d4767dfab4ac9ed36b42175a Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Tue, 4 Jan 2022 15:04:30 +0500 Subject: [PATCH 1/3] Fix endianness bug in write_digit2_separated --- include/fmt/chrono.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 908999ab5f5d..d2e7ccbb7f45 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -558,7 +558,13 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b, auto usep = static_cast(sep); // Add ASCII '0' to each digit byte and insert separators. digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); - memcpy(buf, &digits, 8); + if (is_big_endian()) { + char tmp[8]; + memcpy(tmp, &digits, 8); + std::reverse_copy(tmp, tmp + 8, buf); + } else { + memcpy(buf, &digits, 8); + } } template FMT_CONSTEXPR inline const char* get_units() { From 76e1f6d9c52b44afc281e1d3ea643a34b0fb8914 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 5 Jan 2022 01:17:18 +0500 Subject: [PATCH 2/3] Move endianness check to compile time if it possible --- include/fmt/chrono.h | 2 +- include/fmt/format.h | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index d2e7ccbb7f45..07115c5ca7c9 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -558,7 +558,7 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b, auto usep = static_cast(sep); // Add ASCII '0' to each digit byte and insert separators. digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); - if (is_big_endian()) { + if (const_check(is_big_endian())) { char tmp[8]; memcpy(tmp, &digits, 8); std::reverse_copy(tmp, tmp + 8, buf); diff --git a/include/fmt/format.h b/include/fmt/format.h index a6f1fdc89ecd..8b789574ec5e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -296,10 +296,18 @@ FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { } inline auto is_big_endian() -> bool { +#ifdef _WIN32 + return false; +#elif defined(__BIG_ENDIAN__) + return true; +#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) + return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; +#else struct bytes { char data[sizeof(int)]; }; return bit_cast(1).data[0] == 0; +#endif } // A fallback implementation of uintptr_t for systems that lack it. @@ -309,7 +317,7 @@ struct fallback_uintptr { fallback_uintptr() = default; explicit fallback_uintptr(const void* p) { *this = bit_cast(p); - if (is_big_endian()) { + if (const_check(is_big_endian())) { for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j) std::swap(value[i], value[j]); } From 7460d4432600b8d7e70254c11c28b71d19e30c82 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 5 Jan 2022 03:13:41 +0500 Subject: [PATCH 3/3] Turn 8 into a constant --- include/fmt/chrono.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 07115c5ca7c9..682efd8d21b1 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -558,12 +558,14 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b, auto usep = static_cast(sep); // Add ASCII '0' to each digit byte and insert separators. digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); + + constexpr const size_t len = 8; if (const_check(is_big_endian())) { - char tmp[8]; - memcpy(tmp, &digits, 8); - std::reverse_copy(tmp, tmp + 8, buf); + char tmp[len]; + memcpy(tmp, &digits, len); + std::reverse_copy(tmp, tmp + len, buf); } else { - memcpy(buf, &digits, 8); + memcpy(buf, &digits, len); } }