1#pragma once
2
3#ifdef _MSC_VER
4#include <stdlib.h>
5#endif
6
7#include <cstring>
8#include <ore/Types.h>
9
10namespace ore {
11
12[[nodiscard]] inline u8 SwapEndian(u8 x) {
13 return x;
14}
15
16[[nodiscard]] inline u16 SwapEndian(u16 x) {
17#ifdef _MSC_VER
18 return _byteswap_ushort(x);
19#else
20 return __builtin_bswap16(x);
21#endif
22}
23
24[[nodiscard]] inline u32 SwapEndian(u32 x) {
25#ifdef _MSC_VER
26 return _byteswap_ulong(x);
27#else
28 return __builtin_bswap32(x);
29#endif
30}
31
32[[nodiscard]] inline u64 SwapEndian(u64 x) {
33#ifdef _MSC_VER
34 return _byteswap_uint64(x);
35#else
36 return __builtin_bswap64(x);
37#endif
38}
39
40[[nodiscard]] inline s8 SwapEndian(s8 x) {
41 return SwapEndian(u8(x));
42}
43
44[[nodiscard]] inline s16 SwapEndian(s16 x) {
45 return SwapEndian(u16(x));
46}
47
48[[nodiscard]] inline s32 SwapEndian(s32 x) {
49 return SwapEndian(u32(x));
50}
51
52[[nodiscard]] inline s64 SwapEndian(s64 x) {
53 return SwapEndian(u64(x));
54}
55
56[[nodiscard]] inline f32 SwapEndian(f32 x) {
57 static_assert(sizeof(u32) == sizeof(f32));
58 u32 i;
59 std::memcpy(&i, &x, sizeof(i));
60 i = SwapEndian(i);
61 std::memcpy(&x, &i, sizeof(i));
62 return x;
63}
64
65template <typename T>
66inline void SwapEndian(T* value) {
67 *value = SwapEndian(*value);
68}
69
70struct ResEndian {
71 char* base;
72 bool is_serializing;
73};
74
75} // namespace ore
76