mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 09:20:28 +03:00
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.
This commit is contained in:
parent
b3a295a5bd
commit
94f5389934
Notes:
sideshowbarker
2024-07-17 02:39:10 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/94f5389934 Pull-request: https://github.com/SerenityOS/serenity/pull/19623 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/DanShaders
45
AK/Endian.h
45
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<typename T>
|
||||
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<T>(__builtin_bswap64(static_cast<u64>(value)));
|
||||
if constexpr (sizeof(T) == 4)
|
||||
return static_cast<T>(__builtin_bswap32(static_cast<u32>(value)));
|
||||
if constexpr (sizeof(T) == 2)
|
||||
return static_cast<T>(__builtin_bswap16(static_cast<u16>(value)));
|
||||
if constexpr (sizeof(T) == 1)
|
||||
if constexpr (HostIsLittleEndian || sizeof(T) == 1)
|
||||
return value;
|
||||
#endif
|
||||
else if constexpr (sizeof(T) == 8)
|
||||
return static_cast<T>(__builtin_bswap64(static_cast<u64>(value)));
|
||||
else if constexpr (sizeof(T) == 4)
|
||||
return static_cast<T>(__builtin_bswap32(static_cast<u32>(value)));
|
||||
else if constexpr (sizeof(T) == 2)
|
||||
return static_cast<T>(__builtin_bswap16(static_cast<u16>(value)));
|
||||
else
|
||||
static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<T>(__builtin_bswap64(static_cast<u64>(value)));
|
||||
if constexpr (sizeof(T) == 4)
|
||||
return static_cast<T>(__builtin_bswap32(static_cast<u32>(value)));
|
||||
if constexpr (sizeof(T) == 2)
|
||||
return static_cast<T>(__builtin_bswap16(static_cast<u16>(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<T>(__builtin_bswap64(static_cast<u64>(value)));
|
||||
else if constexpr (sizeof(T) == 4)
|
||||
return static_cast<T>(__builtin_bswap32(static_cast<u32>(value)));
|
||||
else if constexpr (sizeof(T) == 2)
|
||||
return static_cast<T>(__builtin_bswap16(static_cast<u16>(value)));
|
||||
else
|
||||
static_assert(DependentFalse<T>, "Cannot byte-swap values larger than 64-bits");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -176,10 +176,8 @@ static constexpr auto max_representable_power_of_ten_in_u64 = 19;
|
||||
static_assert(1e19 <= static_cast<double>(NumericLimits<u64>::max()));
|
||||
static_assert(1e20 >= static_cast<double>(NumericLimits<u64>::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;
|
||||
|
@ -225,7 +225,7 @@ static ThrowCompletionOr<Value> 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<T>(vm, expected, is_little_endian));
|
||||
|
Loading…
Reference in New Issue
Block a user