diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index d2e7ccbb7f453..07115c5ca7c99 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 a6f1fdc89ecd1..7af48ac1d4238 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -295,12 +295,22 @@ FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { return to; } +#ifdef _WIN32 +constexpr auto is_big_endian() -> bool { return false; } +#elif defined(__BIG_ENDIAN__) +constexpr auto is_big_endian() -> bool { return true; } +#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) +constexpr auto is_big_endian() -> bool { + return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; +} +#else inline auto is_big_endian() -> bool { 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. struct fallback_uintptr { @@ -309,7 +319,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]); }