From 94f5389934aeb2060fb3d85f34269138424d834d Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 12 Jun 2023 13:28:25 +0330 Subject: [PATCH] AK: Add a 'HostIsLittleEndian' constant and use it instead of BYTE_ORDER Previously we were using the preprocessor everywhere we needed this constant, so let's move away from that and use a constexpr constant. --- AK/Endian.h | 45 ++++++++++--------- AK/FloatingPointStringConversions.cpp | 6 +-- .../Libraries/LibJS/Runtime/AtomicsObject.cpp | 2 +- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/AK/Endian.h b/AK/Endian.h index 05743cb119..e3636e6758 100644 --- a/AK/Endian.h +++ b/AK/Endian.h @@ -35,39 +35,40 @@ #endif namespace AK { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +inline constexpr static bool HostIsLittleEndian = true; +#else +inline constexpr static bool HostIsLittleEndian = false; +#endif template ALWAYS_INLINE constexpr T convert_between_host_and_little_endian(T value) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return value; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - if constexpr (sizeof(T) == 8) - return static_cast(__builtin_bswap64(static_cast(value))); - if constexpr (sizeof(T) == 4) - return static_cast(__builtin_bswap32(static_cast(value))); - if constexpr (sizeof(T) == 2) - return static_cast(__builtin_bswap16(static_cast(value))); - if constexpr (sizeof(T) == 1) + if constexpr (HostIsLittleEndian || sizeof(T) == 1) return value; -#endif + else if constexpr (sizeof(T) == 8) + return static_cast(__builtin_bswap64(static_cast(value))); + else if constexpr (sizeof(T) == 4) + return static_cast(__builtin_bswap32(static_cast(value))); + else if constexpr (sizeof(T) == 2) + return static_cast(__builtin_bswap16(static_cast(value))); + else + static_assert(DependentFalse, "Cannot byte-swap values larger than 64-bits"); } template ALWAYS_INLINE constexpr T convert_between_host_and_big_endian(T value) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - if constexpr (sizeof(T) == 8) - return static_cast(__builtin_bswap64(static_cast(value))); - if constexpr (sizeof(T) == 4) - return static_cast(__builtin_bswap32(static_cast(value))); - if constexpr (sizeof(T) == 2) - return static_cast(__builtin_bswap16(static_cast(value))); - if constexpr (sizeof(T) == 1) + if constexpr (sizeof(T) == 1 || !HostIsLittleEndian) return value; -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return value; -#endif + else if constexpr (sizeof(T) == 8) + return static_cast(__builtin_bswap64(static_cast(value))); + else if constexpr (sizeof(T) == 4) + return static_cast(__builtin_bswap32(static_cast(value))); + else if constexpr (sizeof(T) == 2) + return static_cast(__builtin_bswap16(static_cast(value))); + else + static_assert(DependentFalse, "Cannot byte-swap values larger than 64-bits"); } template diff --git a/AK/FloatingPointStringConversions.cpp b/AK/FloatingPointStringConversions.cpp index 8a26dc2c28..d63aaa5231 100644 --- a/AK/FloatingPointStringConversions.cpp +++ b/AK/FloatingPointStringConversions.cpp @@ -176,10 +176,8 @@ static constexpr auto max_representable_power_of_ten_in_u64 = 19; static_assert(1e19 <= static_cast(NumericLimits::max())); static_assert(1e20 >= static_cast(NumericLimits::max())); -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -# error Float parsing currently assumes little endian, this fact is only used in fast parsing of 8 digits at a time \ - you _should_ only need to change read eight_digits to make this big endian compatible. -#endif +static_assert(HostIsLittleEndian, "Float parsing currently assumes little endian, this fact is only used in fast parsing of 8 digits at a time" + "\nyou _should_ only need to change read eight_digits to make this big endian compatible."); constexpr u64 read_eight_digits(char const* string) { u64 val; diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index 423beec7e9..a93ce76580 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -225,7 +225,7 @@ static ThrowCompletionOr atomic_compare_exchange_impl(VM& vm, TypedArrayB // 9. Let elementSize be TypedArrayElementSize(typedArray). // 10. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record. - constexpr bool is_little_endian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; + constexpr bool is_little_endian = AK::HostIsLittleEndian; // 11. Let expectedBytes be NumericToRawBytes(elementType, expected, isLittleEndian). auto expected_bytes = MUST_OR_THROW_OOM(numeric_to_raw_bytes(vm, expected, is_little_endian));