8#ifndef SEN_CORE_IO_DETAIL_ENDIANNESS_H
9#define SEN_CORE_IO_DETAIL_ENDIANNESS_H
36[[nodiscard]]
constexpr bool hostIsBigEndian() noexcept
38#if defined(__BYTE_ORDER__)
39# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
41# elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
44#elif defined(__BIG_ENDIAN__)
46#elif defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
49# error "platform endianness not supported"
54[[nodiscard]]
constexpr bool hostIsLittleEndian() noexcept {
return !hostIsBigEndian(); }
56[[nodiscard]] SEN_ALWAYS_INLINE int8_t swapBytes(int8_t value)
noexcept {
return value; }
58[[nodiscard]] SEN_ALWAYS_INLINE uint8_t swapBytes(uint8_t value)
noexcept {
return value; }
60[[nodiscard]] SEN_ALWAYS_INLINE int16_t swapBytes(int16_t value)
noexcept
63 return static_cast<int16_t
>(__builtin_bswap16(value));
65 return static_cast<int16_t
>(_byteswap_ushort(value));
69[[nodiscard]] SEN_ALWAYS_INLINE uint16_t swapBytes(uint16_t value)
noexcept
72 return __builtin_bswap16(value);
74 return _byteswap_ushort(value);
78[[nodiscard]] SEN_ALWAYS_INLINE int32_t swapBytes(int32_t value)
noexcept
81 return static_cast<int32_t
>(__builtin_bswap32(value));
83 return static_cast<int32_t
>(_byteswap_ulong(value));
87[[nodiscard]] SEN_ALWAYS_INLINE uint32_t swapBytes(uint32_t value)
noexcept
90 return __builtin_bswap32(value);
92 return _byteswap_ulong(value);
96[[nodiscard]] SEN_ALWAYS_INLINE int64_t swapBytes(int64_t value)
noexcept
99 return static_cast<int64_t
>(__builtin_bswap64(value));
101 return static_cast<int64_t
>(_byteswap_uint64(value));
105[[nodiscard]] SEN_ALWAYS_INLINE uint64_t swapBytes(uint64_t value)
noexcept
108 return __builtin_bswap64(value);
110 return _byteswap_uint64(value);
128SEN_ALWAYS_INLINE
void swapBytesIfNeeded(T& val, LittleEndian )
noexcept
130 if constexpr (hostIsBigEndian())
132 val = swapBytes(val);
138SEN_ALWAYS_INLINE
void swapBytesIfNeeded(T& val, BigEndian )
noexcept
140 if constexpr (hostIsLittleEndian())
142 val = swapBytes(val);
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
std::enable_if_t< sizeof(To)==sizeof(From) &&std::is_trivially_copyable_v< From > &&std::is_trivially_copyable_v< To >, To > bit_cast(const From &src) noexcept
Definition bits.h:34
Tag type for referring to big endian encoding.
Definition endianness.h:29
Tag type for referring to little endian encoding.
Definition endianness.h:24