mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-06 16:19:23 +00:00
AK: Use endianness flags to determine if conversion is necessary
This commit is contained in:
parent
940de40f28
commit
01ae3e9c85
Notes:
sideshowbarker
2024-07-19 09:13:08 +09:00
Author: https://github.com/supercomputer7
Commit: 01ae3e9c85
Pull-request: https://github.com/SerenityOS/serenity/pull/1250
2 changed files with 24 additions and 20 deletions
|
@ -26,21 +26,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Platform.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>
|
||||
class [[gnu::packed]] NetworkOrdered
|
||||
{
|
||||
|
|
|
@ -68,3 +68,19 @@ inline int open_with_path_length(const char* path, size_t path_length, int optio
|
|||
}
|
||||
#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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue