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