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
14
15// std
16#include <cstdlib>
17
18namespace sen
19{
20
23{
24};
25
28{
29};
30
31namespace impl
32{
33
35[[nodiscard]] constexpr bool hostIsBigEndian() noexcept
36{
37#if defined(__BYTE_ORDER__)
38# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
39 return true;
40# elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
41 return false;
42# endif
43#elif defined(__BIG_ENDIAN__)
44 return true;
45#elif defined(__LITTLE_ENDIAN__) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
46 return false;
47#else
48# error "platform endianness not supported"
49#endif
50}
51
53[[nodiscard]] constexpr bool hostIsLittleEndian() noexcept { return !hostIsBigEndian(); }
54
55[[nodiscard]] SEN_ALWAYS_INLINE int8_t swapBytes(int8_t value) noexcept { return value; }
56
57[[nodiscard]] SEN_ALWAYS_INLINE uint8_t swapBytes(uint8_t value) noexcept { return value; }
58
59[[nodiscard]] SEN_ALWAYS_INLINE int16_t swapBytes(int16_t value) noexcept
60{
61#if defined(__GNUC__)
62 return static_cast<int16_t>(__builtin_bswap16(value));
63#else
64 return static_cast<int16_t>(_byteswap_ushort(value));
65#endif
66}
67
68[[nodiscard]] SEN_ALWAYS_INLINE uint16_t swapBytes(uint16_t value) noexcept
69{
70#if defined(__GNUC__)
71 return __builtin_bswap16(value);
72#else
73 return _byteswap_ushort(value);
74#endif
75}
76
77[[nodiscard]] SEN_ALWAYS_INLINE int32_t swapBytes(int32_t value) noexcept
78{
79#if defined(__GNUC__)
80 return static_cast<int32_t>(__builtin_bswap32(value));
81#else
82 return static_cast<int32_t>(_byteswap_ulong(value));
83#endif
84}
85
86[[nodiscard]] SEN_ALWAYS_INLINE uint32_t swapBytes(uint32_t value) noexcept
87{
88#if defined(__GNUC__)
89 return __builtin_bswap32(value);
90#else
91 return _byteswap_ulong(value);
92#endif
93}
94
95[[nodiscard]] SEN_ALWAYS_INLINE int64_t swapBytes(int64_t value) noexcept
96{
97#if defined(__GNUC__)
98 return static_cast<int64_t>(__builtin_bswap64(value));
99#else
100 return static_cast<int64_t>(_byteswap_uint64(value));
101#endif
102}
103
104[[nodiscard]] SEN_ALWAYS_INLINE uint64_t swapBytes(uint64_t value) noexcept
105{
106#if defined(__GNUC__)
107 return __builtin_bswap64(value);
108#else
109 return _byteswap_uint64(value);
110#endif
111}
112
113[[nodiscard]] SEN_ALWAYS_INLINE float32_t swapBytes(float32_t value) noexcept { return value; }
114
115[[nodiscard]] SEN_ALWAYS_INLINE float64_t swapBytes(float64_t value) noexcept { return value; }
116
118template <typename T>
119SEN_ALWAYS_INLINE void swapBytesIfNeeded(T& val, LittleEndian /* target */) noexcept
120{
121 if constexpr (hostIsBigEndian())
122 {
123 val = swapBytes(val);
124 }
125}
126
128template <typename T>
129SEN_ALWAYS_INLINE void swapBytesIfNeeded(T& val, BigEndian /* target */) noexcept
130{
131 if constexpr (hostIsLittleEndian())
132 {
133 val = swapBytes(val);
134 }
135}
136
137} // namespace impl
138
139} // namespace sen
140
141#endif // SEN_CORE_IO_DETAIL_ENDIANNESS_H
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Definition assert.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