From 01ae3e9c856bbfcedaedb3706eb6e9dc2295ca4e Mon Sep 17 00:00:00 2001 From: Liav A Date: Wed, 19 Feb 2020 16:58:54 +0200 Subject: [PATCH] AK: Use endianness flags to determine if conversion is necessary --- AK/NetworkOrdered.h | 14 +------------- AK/Platform.h | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/AK/NetworkOrdered.h b/AK/NetworkOrdered.h index a778bad9b8..1d328ff821 100644 --- a/AK/NetworkOrdered.h +++ b/AK/NetworkOrdered.h @@ -26,21 +26,9 @@ #pragma once +#include #include -template -[[gnu::always_inline]] inline T convert_between_host_and_network(T value) -{ - if constexpr (sizeof(T) == 8) - return __builtin_bswap64(value); - if constexpr (sizeof(T) == 4) - return __builtin_bswap32(value); - if constexpr (sizeof(T) == 2) - return __builtin_bswap16(value); - if constexpr (sizeof(T) == 1) - return value; -} - template class [[gnu::packed]] NetworkOrdered { diff --git a/AK/Platform.h b/AK/Platform.h index e023f80b9d..22f0f2e1d0 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -27,11 +27,11 @@ #pragma once #ifdef __i386__ -#define AK_ARCH_I386 1 +# define AK_ARCH_I386 1 #endif #ifdef __x86_64__ -#define AK_ARCH_X86_64 1 +# define AK_ARCH_X86_64 1 #endif #define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch) @@ -49,12 +49,12 @@ #endif #ifndef __serenity__ -#define PAGE_SIZE sysconf(_SC_PAGESIZE) +# define PAGE_SIZE sysconf(_SC_PAGESIZE) -#include -#include -#include -#include +# include +# include +# include +# include inline int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode) { auto* tmp = (char*)malloc(path_length + 1); @@ -68,3 +68,19 @@ inline int open_with_path_length(const char* path, size_t path_length, int optio } #endif +template +[[gnu::always_inline]] inline T convert_between_host_and_network(T value) +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + if constexpr (sizeof(T) == 8) + return __builtin_bswap64(value); + if constexpr (sizeof(T) == 4) + return __builtin_bswap32(value); + if constexpr (sizeof(T) == 2) + return __builtin_bswap16(value); + if constexpr (sizeof(T) == 1) + return value; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + return value; +#endif +}