Sen API
Sen Libraries
Loading...
Searching...
No Matches
endianness.h
Go to the documentation of this file.
1// === endianness.h ====================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_IO_DETAIL_ENDIANNESS_H
9#define SEN_CORE_IO_DETAIL_ENDIANNESS_H
10
11// sen
12#include "sen/core/base/bits.h"
15
16// std
17#include <cstdlib>
18
19namespace sen
20{
21
24{
25};
26
29{
30};
31
32namespace impl
33{
34
36[[nodiscard]] constexpr bool hostIsBigEndian() noexcept
37{
38#if defined(__BYTE_ORDER__)
39# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
40 return true;
41# elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
42 return false;
43# endif
44#elif defined(__BIG_ENDIAN__)
45 return true;
46#elif defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
47 return false;
48#else
49# error "platform endianness not supported"
50#endif
51}
52
54[[nodiscard]] constexpr bool hostIsLittleEndian() noexcept { return !hostIsBigEndian(); }
55
56[[nodiscard]] SEN_ALWAYS_INLINE int8_t swapBytes(int8_t value) noexcept { return value; }
57
58[[nodiscard]] SEN_ALWAYS_INLINE uint8_t swapBytes(uint8_t value) noexcept { return value; }
59
60[[nodiscard]] SEN_ALWAYS_INLINE int16_t swapBytes(int16_t value) noexcept
61{
62#if defined(__GNUC__)
63 return static_cast<int16_t>(__builtin_bswap16(value));
64#else
65 return static_cast<int16_t>(_byteswap_ushort(value));
66#endif
67}
68
69[[nodiscard]] SEN_ALWAYS_INLINE uint16_t swapBytes(uint16_t value) noexcept
70{
71#if defined(__GNUC__)
72 return __builtin_bswap16(value);
73#else
74 return _byteswap_ushort(value);
75#endif
76}
77
78[[nodiscard]] SEN_ALWAYS_INLINE int32_t swapBytes(int32_t value) noexcept
79{
80#if defined(__GNUC__)
81 return static_cast<int32_t>(__builtin_bswap32(value));
82#else
83 return static_cast<int32_t>(_byteswap_ulong(value));
84#endif
85}
86
87[[nodiscard]] SEN_ALWAYS_INLINE uint32_t swapBytes(uint32_t value) noexcept
88{
89#if defined(__GNUC__)
90 return __builtin_bswap32(value);
91#else
92 return _byteswap_ulong(value);
93#endif
94}
95
96[[nodiscard]] SEN_ALWAYS_INLINE int64_t swapBytes(int64_t value) noexcept
97{
98#if defined(__GNUC__)
99 return static_cast<int64_t>(__builtin_bswap64(value));
100#else
101 return static_cast<int64_t>(_byteswap_uint64(value));
102#endif
103}
104
105[[nodiscard]] SEN_ALWAYS_INLINE uint64_t swapBytes(uint64_t value) noexcept
106{
107#if defined(__GNUC__)
108 return __builtin_bswap64(value);
109#else
110 return _byteswap_uint64(value);
111#endif
112}
113
114// Through the same-width integer, because IEEE 754 fixes the bit pattern and not the order those
115// bytes sit in. Returning the value unchanged made the stream little-endian for integers only.
116[[nodiscard]] SEN_ALWAYS_INLINE float32_t swapBytes(float32_t value) noexcept
117{
119}
120
121[[nodiscard]] SEN_ALWAYS_INLINE float64_t swapBytes(float64_t value) noexcept
122{
124}
125
127template <typename T>
128SEN_ALWAYS_INLINE void swapBytesIfNeeded(T& val, LittleEndian /* target */) noexcept
129{
130 if constexpr (hostIsBigEndian())
131 {
132 val = swapBytes(val);
133 }
134}
135
137template <typename T>
138SEN_ALWAYS_INLINE void swapBytesIfNeeded(T& val, BigEndian /* target */) noexcept
139{
140 if constexpr (hostIsLittleEndian())
141 {
142 val = swapBytes(val);
143 }
144}
145
146} // namespace impl
147
148} // namespace sen
149
150#endif // SEN_CORE_IO_DETAIL_ENDIANNESS_H
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
Definition assert.h:17
Tag type for referring to big endian encoding.
Definition endianness.h:29
Tag type for referring to little endian encoding.
Definition endianness.h:24