openMSX
endian.hh
Go to the documentation of this file.
1#ifndef ENDIAN_HH
2#define ENDIAN_HH
3
4#include "inline.hh"
5#include "narrow.hh"
6
7#include <array>
8#include <bit>
9#include <cassert>
10#include <concepts>
11#include <cstdint>
12#include <cstring>
13
14namespace Endian {
15
16inline constexpr bool BIG = std::endian::native == std::endian::big;
17inline constexpr bool LITTLE = std::endian::native == std::endian::little;
18static_assert(BIG || LITTLE, "mixed endian not supported");
19
20// Reverse bytes in a 16-bit number: 0x1234 becomes 0x3412
21[[nodiscard]] static inline uint16_t byteswap16(uint16_t x)
22{
23 // This sequence generates 'optimal' code on a wide range of gcc/clang
24 // versions (a single rotate instruction on x86). The newer compiler
25 // versions also do 'the right thing' for the simpler expression below.
26 // Those newer compilers also support __builtin_bswap16() but that
27 // doesn't generate better code (and is less portable).
28 return uint16_t(((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8));
29 //return (x << 8) | (x >> 8);
30}
31
32// Reverse bytes in a 32-bit number: 0x12345678 becomes 0x78563412
33[[nodiscard]] static inline uint32_t byteswap32(uint32_t x)
34{
35#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
36 // Starting from gcc-4.3 there's a builtin function for this.
37 // E.g. on x86 this is translated to a single 'bswap' instruction.
38 return __builtin_bswap32(x);
39#else
40 return (x << 24) |
41 ((x << 8) & 0x00ff0000) |
42 ((x >> 8) & 0x0000ff00) |
43 (x >> 24);
44#endif
45}
46
47// Reverse bytes in a 64-bit value: 0x1122334455667788 becomes 0x8877665544332211
48[[nodiscard]] static inline uint64_t byteswap64(uint64_t x)
49{
50#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
51 // Starting from gcc-4.3 there's a builtin function for this.
52 // E.g. on x86 this is translated to a single 'bswap' instruction.
53 return __builtin_bswap64(x);
54#else
55 return (uint64_t(byteswap32(narrow_cast<uint32_t>(x >> 0))) << 32) |
56 (uint64_t(byteswap32(narrow_cast<uint32_t>(x >> 32))) << 0);
57#endif
58}
59
60// Use overloading to get a (statically) polymorphic byteswap() function.
61[[nodiscard]] static inline uint16_t byteswap(uint16_t x) { return byteswap16(x); }
62[[nodiscard]] static inline uint32_t byteswap(uint32_t x) { return byteswap32(x); }
63[[nodiscard]] static inline uint64_t byteswap(uint64_t x) { return byteswap64(x); }
64
65
66// Identity operator, simply returns the given value.
67struct Ident {
68 [[nodiscard]] inline auto operator()(std::integral auto t) const { return t; }
69};
70
71// Byte-swap operator, swap bytes in the given value (16 or 32 bit).
72struct ByteSwap {
73 [[nodiscard]] inline auto operator()(std::integral auto t) const { return byteswap(t); }
74};
75
76// Helper class that stores a value and allows to read/write that value. Though
77// right before it is loaded/stored the value is transformed by a configurable
78// operation.
79// TODO If needed this can be extended with stuff like operator+= ....
80template<std::integral T, std::invocable<T> Op> class EndianT {
81public:
82 EndianT() = default; // leave uninitialized
83 explicit EndianT(T t_) { Op op; t = op(t_); }
84 [[nodiscard]] inline operator T() const { Op op; return op(t); }
85 inline EndianT& operator=(T a) { Op op; t = op(a); return *this; }
86private:
87 T t;
88};
89
90// Define the types B16, B32, L16, L32.
91//
92// Typically these types are used to define the layout of external structures
93// For example:
94//
95// struct FATDirectoryEntry {
96// char filename[8];
97// char extension[3];
98// ...
99// Endian::L32 size; // 32-bit little endian value
100// };
101// ...
102// unsigned s = myDirEntry.size; // Possibly performs endianess conversion.
103// yourDirEntry.size = s; // If native endianess is already correct
104// // this has no extra overhead.
105//
106// You can assign and read values in native endianess to values of these types.
107// So basically in a single location define the structure with the correct
108// endianess and in all other places use the value as-if it were a native type.
109//
110// Note that these types should still be correctly aligned (e.g. L32 should be
111// 4-byte aligned). For unaligned access use the functions below.
112//
113template<bool> struct ConvBig;
114template<> struct ConvBig <true > : Ident {};
115template<> struct ConvBig <false> : ByteSwap {};
116template<bool> struct ConvLittle;
117template<> struct ConvLittle<true > : ByteSwap {};
118template<> struct ConvLittle<false> : Ident {};
125static_assert(sizeof(B16) == 2, "must have size 2");
126static_assert(sizeof(L16) == 2, "must have size 2");
127static_assert(sizeof(B32) == 4, "must have size 4");
128static_assert(sizeof(L32) == 4, "must have size 4");
129static_assert(sizeof(B64) == 8, "must have size 8");
130static_assert(sizeof(L64) == 8, "must have size 8");
131static_assert(alignof(B16) <= 2, "may have alignment 2");
132static_assert(alignof(L16) <= 2, "may have alignment 2");
133static_assert(alignof(B32) <= 4, "may have alignment 4");
134static_assert(alignof(L32) <= 4, "may have alignment 4");
135static_assert(alignof(B64) <= 8, "may have alignment 8");
136static_assert(alignof(L64) <= 8, "may have alignment 8");
137
138
139// Helper functions to read/write aligned 16/32 bit values.
140inline void writeB16(void* p, uint16_t x)
141{
142 *std::bit_cast<B16*>(p) = x;
143}
144inline void writeL16(void* p, uint16_t x)
145{
146 *std::bit_cast<L16*>(p) = x;
147}
148inline void writeB32(void* p, uint32_t x)
149{
150 *std::bit_cast<B32*>(p) = x;
151}
152inline void writeL32(void* p, uint32_t x)
153{
154 *std::bit_cast<L32*>(p) = x;
155}
156
157[[nodiscard]] inline uint16_t readB16(const void* p)
158{
159 return *std::bit_cast<const B16*>(p);
160}
161[[nodiscard]] inline uint16_t readL16(const void* p)
162{
163 return *std::bit_cast<const L16*>(p);
164}
165[[nodiscard]] inline uint32_t readB32(const void* p)
166{
167 return *std::bit_cast<const B32*>(p);
168}
169[[nodiscard]] inline uint32_t readL32(const void* p)
170{
171 return *std::bit_cast<const L32*>(p);
172}
173
174// Read/write big/little 16/24/32/64-bit values to/from a (possibly) unaligned
175// memory location. If the host architecture supports unaligned load/stores
176// (e.g. x86), these functions perform a single load/store (with possibly an
177// adjust operation on the value if the endianess is different from the host
178// endianess). If the architecture does not support unaligned memory operations
179// (e.g. early ARM architectures), the operation is split into byte accesses.
180
181template<bool SWAP> static ALWAYS_INLINE void write_UA(void* p, std::integral auto x)
182{
183 if constexpr (SWAP) x = byteswap(x);
184 memcpy(p, &x, sizeof(x));
185}
186ALWAYS_INLINE void write_UA_B16(void* p, uint16_t x)
187{
188 write_UA<LITTLE>(p, x);
189}
190ALWAYS_INLINE void write_UA_L16(void* p, uint16_t x)
191{
192 write_UA<BIG>(p, x);
193}
194ALWAYS_INLINE void write_UA_L24(void* p, uint32_t x)
195{
196 assert(x < 0x1000000);
197 auto* v = static_cast<uint8_t*>(p);
198 v[0] = (x >> 0) & 0xff;
199 v[1] = (x >> 8) & 0xff;
200 v[2] = (x >> 16) & 0xff;
201}
202ALWAYS_INLINE void write_UA_B32(void* p, uint32_t x)
203{
204 write_UA<LITTLE>(p, x);
205}
206ALWAYS_INLINE void write_UA_L32(void* p, uint32_t x)
207{
208 write_UA<BIG>(p, x);
209}
210ALWAYS_INLINE void write_UA_B64(void* p, uint64_t x)
211{
212 write_UA<LITTLE>(p, x);
213}
214ALWAYS_INLINE void write_UA_L64(void* p, uint64_t x)
215{
216 write_UA<BIG>(p, x);
217}
218
219template<bool SWAP, std::integral T> [[nodiscard]] static ALWAYS_INLINE T read_UA(const void* p)
220{
221 T x;
222 memcpy(&x, p, sizeof(x));
223 if constexpr (SWAP) x = byteswap(x);
224 return x;
225}
226[[nodiscard]] ALWAYS_INLINE uint16_t read_UA_B16(const void* p)
227{
228 return read_UA<LITTLE, uint16_t>(p);
229}
230[[nodiscard]] ALWAYS_INLINE uint16_t read_UA_L16(const void* p)
231{
232 return read_UA<BIG, uint16_t>(p);
233}
234[[nodiscard]] ALWAYS_INLINE uint32_t read_UA_L24(const void* p)
235{
236 const auto* v = static_cast<const uint8_t*>(p);
237 return (v[0] << 0) | (v[1] << 8) | (v[2] << 16);
238}
239[[nodiscard]] ALWAYS_INLINE uint32_t read_UA_B32(const void* p)
240{
241 return read_UA<LITTLE, uint32_t>(p);
242}
243[[nodiscard]] ALWAYS_INLINE uint32_t read_UA_L32(const void* p)
244{
245 return read_UA<BIG, uint32_t>(p);
246}
247[[nodiscard]] ALWAYS_INLINE uint64_t read_UA_B64(const void* p)
248{
249 return read_UA<LITTLE, uint64_t>(p);
250}
251[[nodiscard]] ALWAYS_INLINE uint64_t read_UA_L64(const void* p)
252{
253 return read_UA<BIG, uint64_t>(p);
254}
255
256
257// Like the types above, but these don't need to be aligned.
258
259class UA_B16 {
260public:
261 [[nodiscard]] inline operator uint16_t() const { return read_UA_B16(x.data()); }
262 inline UA_B16& operator=(uint16_t a) { write_UA_B16(x.data(), a); return *this; }
263private:
264 std::array<uint8_t, 2> x;
265};
266
267class UA_L16 {
268public:
269 [[nodiscard]] inline operator uint16_t() const { return read_UA_L16(x.data()); }
270 inline UA_L16& operator=(uint16_t a) { write_UA_L16(x.data(), a); return *this; }
271private:
272 std::array<uint8_t, 2> x;
273};
274
275class UA_L24 {
276public:
277 inline operator uint32_t() const { return read_UA_L24(x.data()); }
278 inline UA_L24& operator=(uint32_t a) { write_UA_L24(x.data(), a); return *this; }
279private:
280 std::array<uint8_t, 3> x;
281};
282
283class UA_B32 {
284public:
285 [[nodiscard]] inline operator uint32_t() const { return read_UA_B32(x.data()); }
286 inline UA_B32& operator=(uint32_t a) { write_UA_B32(x.data(), a); return *this; }
287private:
288 std::array<uint8_t, 4> x;
289};
290
291class UA_L32 {
292public:
293 [[nodiscard]] inline operator uint32_t() const { return read_UA_L32(x.data()); }
294 inline UA_L32& operator=(uint32_t a) { write_UA_L32(x.data(), a); return *this; }
295private:
296 std::array<uint8_t, 4> x;
297};
298
299static_assert(sizeof(UA_B16) == 2, "must have size 2");
300static_assert(sizeof(UA_L16) == 2, "must have size 2");
301static_assert(sizeof(UA_L24) == 3, "must have size 3");
302static_assert(sizeof(UA_B32) == 4, "must have size 4");
303static_assert(sizeof(UA_L32) == 4, "must have size 4");
304static_assert(alignof(UA_B16) == 1, "must have alignment 1");
305static_assert(alignof(UA_L16) == 1, "must have alignment 1");
306static_assert(alignof(UA_L24) == 1, "must have alignment 1");
307static_assert(alignof(UA_B32) == 1, "must have alignment 1");
308static_assert(alignof(UA_L32) == 1, "must have alignment 1");
309
310// Template meta-programming.
311// Get a type of the same size of the given type that stores the value in a
312// specific endianess. Typically used in template functions that can work on
313// either 16 or 32 bit values.
314// usage:
315// using LE_T = typename Endian::Little<T>::type;
316// The type LE_T is now a type that stores values of the same size as 'T'
317// in little endian format (independent of host endianess).
318template<typename> struct Little;
319template<> struct Little<uint8_t > { using type = uint8_t; };
320template<> struct Little<uint16_t> { using type = L16; };
321template<> struct Little<uint32_t> { using type = L32; };
322template<typename> struct Big;
323template<> struct Big<uint8_t > { using type = uint8_t; };
324template<> struct Big<uint16_t> { using type = B16; };
325template<> struct Big<uint32_t> { using type = B32; };
326
327} // namespace Endian
328
329#endif
TclObject t
EndianT(T t_)
Definition endian.hh:83
EndianT & operator=(T a)
Definition endian.hh:85
EndianT()=default
UA_B16 & operator=(uint16_t a)
Definition endian.hh:262
UA_B32 & operator=(uint32_t a)
Definition endian.hh:286
UA_L16 & operator=(uint16_t a)
Definition endian.hh:270
UA_L24 & operator=(uint32_t a)
Definition endian.hh:278
UA_L32 & operator=(uint32_t a)
Definition endian.hh:294
#define ALWAYS_INLINE
Definition inline.hh:16
uint16_t readB16(const void *p)
Definition endian.hh:157
ALWAYS_INLINE uint32_t read_UA_L24(const void *p)
Definition endian.hh:234
ALWAYS_INLINE uint16_t read_UA_B16(const void *p)
Definition endian.hh:226
void writeB16(void *p, uint16_t x)
Definition endian.hh:140
ALWAYS_INLINE void write_UA_B32(void *p, uint32_t x)
Definition endian.hh:202
ALWAYS_INLINE void write_UA_B64(void *p, uint64_t x)
Definition endian.hh:210
uint32_t readL32(const void *p)
Definition endian.hh:169
ALWAYS_INLINE void write_UA_L24(void *p, uint32_t x)
Definition endian.hh:194
ALWAYS_INLINE void write_UA_B16(void *p, uint16_t x)
Definition endian.hh:186
void writeL32(void *p, uint32_t x)
Definition endian.hh:152
ALWAYS_INLINE uint64_t read_UA_L64(const void *p)
Definition endian.hh:251
ALWAYS_INLINE uint32_t read_UA_B32(const void *p)
Definition endian.hh:239
void writeB32(void *p, uint32_t x)
Definition endian.hh:148
ALWAYS_INLINE void write_UA_L64(void *p, uint64_t x)
Definition endian.hh:214
constexpr bool LITTLE
Definition endian.hh:17
ALWAYS_INLINE uint16_t read_UA_L16(const void *p)
Definition endian.hh:230
ALWAYS_INLINE uint64_t read_UA_B64(const void *p)
Definition endian.hh:247
EndianT< uint16_t, ConvBig< BIG > > B16
Definition endian.hh:119
ALWAYS_INLINE void write_UA_L32(void *p, uint32_t x)
Definition endian.hh:206
EndianT< uint32_t, ConvBig< BIG > > B32
Definition endian.hh:121
ALWAYS_INLINE void write_UA_L16(void *p, uint16_t x)
Definition endian.hh:190
EndianT< uint32_t, ConvLittle< BIG > > L32
Definition endian.hh:122
uint16_t readL16(const void *p)
Definition endian.hh:161
uint32_t readB32(const void *p)
Definition endian.hh:165
EndianT< uint16_t, ConvLittle< BIG > > L16
Definition endian.hh:120
EndianT< uint64_t, ConvBig< BIG > > B64
Definition endian.hh:123
constexpr bool BIG
Definition endian.hh:16
ALWAYS_INLINE uint32_t read_UA_L32(const void *p)
Definition endian.hh:243
EndianT< uint64_t, ConvLittle< BIG > > L64
Definition endian.hh:124
void writeL16(void *p, uint16_t x)
Definition endian.hh:144
auto operator()(std::integral auto t) const
Definition endian.hh:73
auto operator()(std::integral auto t) const
Definition endian.hh:68