From 76e1f6d9c52b44afc281e1d3ea643a34b0fb8914 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 5 Jan 2022 01:17:18 +0500 Subject: [PATCH] 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]); }