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