Move endianness check to compile time if it possible

This commit is contained in:
Vladislav Shchapov 2022-01-05 01:17:18 +05:00
parent 074cc781cb
commit 76e1f6d9c5
2 changed files with 10 additions and 2 deletions

View File

@ -558,7 +558,7 @@ 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);
if (is_big_endian()) {
if (const_check(is_big_endian())) {
char tmp[8];
memcpy(tmp, &digits, 8);
std::reverse_copy(tmp, tmp + 8, buf);

View File

@ -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<bytes>(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<fallback_uintptr>(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]);
}