Serenity Operating System

AK: Use endianness flags to determine if conversion is necessary

authored by

Liav A and committed by
Andreas Kling
01ae3e9c 940de40f

+24 -20
+1 -13
AK/NetworkOrdered.h
··· 26 26 27 27 #pragma once 28 28 29 + #include <AK/Platform.h> 29 30 #include <AK/Types.h> 30 - 31 - template<typename T> 32 - [[gnu::always_inline]] inline T convert_between_host_and_network(T value) 33 - { 34 - if constexpr (sizeof(T) == 8) 35 - return __builtin_bswap64(value); 36 - if constexpr (sizeof(T) == 4) 37 - return __builtin_bswap32(value); 38 - if constexpr (sizeof(T) == 2) 39 - return __builtin_bswap16(value); 40 - if constexpr (sizeof(T) == 1) 41 - return value; 42 - } 43 31 44 32 template<typename T> 45 33 class [[gnu::packed]] NetworkOrdered
+23 -7
AK/Platform.h
··· 27 27 #pragma once 28 28 29 29 #ifdef __i386__ 30 - #define AK_ARCH_I386 1 30 + # define AK_ARCH_I386 1 31 31 #endif 32 32 33 33 #ifdef __x86_64__ 34 - #define AK_ARCH_X86_64 1 34 + # define AK_ARCH_X86_64 1 35 35 #endif 36 36 37 37 #define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch) ··· 49 49 #endif 50 50 51 51 #ifndef __serenity__ 52 - #define PAGE_SIZE sysconf(_SC_PAGESIZE) 52 + # define PAGE_SIZE sysconf(_SC_PAGESIZE) 53 53 54 - #include <errno.h> 55 - #include <fcntl.h> 56 - #include <stdlib.h> 57 - #include <string.h> 54 + # include <errno.h> 55 + # include <fcntl.h> 56 + # include <stdlib.h> 57 + # include <string.h> 58 58 inline int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode) 59 59 { 60 60 auto* tmp = (char*)malloc(path_length + 1); ··· 68 68 } 69 69 #endif 70 70 71 + template<typename T> 72 + [[gnu::always_inline]] inline T convert_between_host_and_network(T value) 73 + { 74 + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 75 + if constexpr (sizeof(T) == 8) 76 + return __builtin_bswap64(value); 77 + if constexpr (sizeof(T) == 4) 78 + return __builtin_bswap32(value); 79 + if constexpr (sizeof(T) == 2) 80 + return __builtin_bswap16(value); 81 + if constexpr (sizeof(T) == 1) 82 + return value; 83 + #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 84 + return value; 85 + #endif 86 + }