8#ifndef SEN_CORE_IO_DETAIL_ENDIANNESS_H
9#define SEN_CORE_IO_DETAIL_ENDIANNESS_H
35[[nodiscard]]
constexpr bool hostIsBigEndian() noexcept
37#if defined(__BYTE_ORDER__)
38# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
40# elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
43#elif defined(__BIG_ENDIAN__)
45#elif defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
48# error "platform endianness not supported"
53[[nodiscard]]
constexpr bool hostIsLittleEndian() noexcept {
return !hostIsBigEndian(); }
55[[nodiscard]] SEN_ALWAYS_INLINE int8_t swapBytes(int8_t value)
noexcept {
return value; }
57[[nodiscard]] SEN_ALWAYS_INLINE uint8_t swapBytes(uint8_t value)
noexcept {
return value; }
59[[nodiscard]] SEN_ALWAYS_INLINE int16_t swapBytes(int16_t value)
noexcept
62 return static_cast<int16_t
>(__builtin_bswap16(value));
64 return static_cast<int16_t
>(_byteswap_ushort(value));
68[[nodiscard]] SEN_ALWAYS_INLINE uint16_t swapBytes(uint16_t value)
noexcept
71 return __builtin_bswap16(value);
73 return _byteswap_ushort(value);
77[[nodiscard]] SEN_ALWAYS_INLINE int32_t swapBytes(int32_t value)
noexcept
80 return static_cast<int32_t
>(__builtin_bswap32(value));
82 return static_cast<int32_t
>(_byteswap_ulong(value));
86[[nodiscard]] SEN_ALWAYS_INLINE uint32_t swapBytes(uint32_t value)
noexcept
89 return __builtin_bswap32(value);
91 return _byteswap_ulong(value);
95[[nodiscard]] SEN_ALWAYS_INLINE int64_t swapBytes(int64_t value)
noexcept
98 return static_cast<int64_t
>(__builtin_bswap64(value));
100 return static_cast<int64_t
>(_byteswap_uint64(value));
104[[nodiscard]] SEN_ALWAYS_INLINE uint64_t swapBytes(uint64_t value)
noexcept
107 return __builtin_bswap64(value);
109 return _byteswap_uint64(value);
113[[nodiscard]] SEN_ALWAYS_INLINE
float32_t swapBytes(
float32_t value)
noexcept {
return value; }
115[[nodiscard]] SEN_ALWAYS_INLINE
float64_t swapBytes(
float64_t value)
noexcept {
return value; }
119SEN_ALWAYS_INLINE
void swapBytesIfNeeded(T& val, LittleEndian )
noexcept
121 if constexpr (hostIsBigEndian())
123 val = swapBytes(val);
129SEN_ALWAYS_INLINE
void swapBytesIfNeeded(T& val, BigEndian )
noexcept
131 if constexpr (hostIsLittleEndian())
133 val = swapBytes(val);
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Tag type for referring to big endian encoding.
Definition endianness.h:28
Tag type for referring to little endian encoding.
Definition endianness.h:23