1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:27:34 +00:00

AK: Use endianness flags to determine if conversion is necessary

This commit is contained in:
Liav A 2020-02-19 16:58:54 +02:00 committed by Andreas Kling
parent 940de40f28
commit 01ae3e9c85
2 changed files with 24 additions and 20 deletions

View file

@ -26,21 +26,9 @@
#pragma once #pragma once
#include <AK/Platform.h>
#include <AK/Types.h> #include <AK/Types.h>
template<typename T>
[[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<typename T> template<typename T>
class [[gnu::packed]] NetworkOrdered class [[gnu::packed]] NetworkOrdered
{ {

View file

@ -27,11 +27,11 @@
#pragma once #pragma once
#ifdef __i386__ #ifdef __i386__
#define AK_ARCH_I386 1 # define AK_ARCH_I386 1
#endif #endif
#ifdef __x86_64__ #ifdef __x86_64__
#define AK_ARCH_X86_64 1 # define AK_ARCH_X86_64 1
#endif #endif
#define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch) #define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch)
@ -49,12 +49,12 @@
#endif #endif
#ifndef __serenity__ #ifndef __serenity__
#define PAGE_SIZE sysconf(_SC_PAGESIZE) # define PAGE_SIZE sysconf(_SC_PAGESIZE)
#include <errno.h> # include <errno.h>
#include <fcntl.h> # include <fcntl.h>
#include <stdlib.h> # include <stdlib.h>
#include <string.h> # include <string.h>
inline int open_with_path_length(const char* path, size_t path_length, int options, mode_t mode) 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); 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 #endif
template<typename T>
[[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
}