sead
Loading...
Searching...
No Matches
seadEndian.h
Go to the documentation of this file.
1#ifndef SEAD_ENDIAN_H_
2#define SEAD_ENDIAN_H_
3
4#include <basis/seadAssert.h>
5#include <basis/seadTypes.h>
6
7namespace sead {
8
9class Endian
10{
11public:
12 enum Types
13 {
14 cBig = 0,
16 };
17
18public:
19 static u8 swapU8 (u8 x) { return cConvFuncTable.conv8 [1](x); }
20 static u16 swapU16(u16 x) { return cConvFuncTable.conv16[1](x); }
21 static u32 swapU32(u32 x) { return cConvFuncTable.conv32[1](x); }
22 static u64 swapU64(u64 x) { return cConvFuncTable.conv64[1](x); }
23
24 static s8 swapS8 (s8 x) { return cConvFuncTable.conv8 [1](x); }
25 static s16 swapS16(s16 x) { return cConvFuncTable.conv16[1](x); }
26 static s32 swapS32(s32 x) { return cConvFuncTable.conv32[1](x); }
27 static s64 swapS64(s64 x) { return cConvFuncTable.conv64[1](x); }
28
29 static f32 swapF32(f32 x)
30 {
31 union { u32* ui; f32* f; } x_ = { . f = &x };
32 *x_.ui = cConvFuncTable.conv32[1](*x_.ui);
33 return *x_. f;
34 }
35
36 static u8 convertU8 (Types from, Types to, u8 x) { return cConvFuncTable.conv8 [from ^ to](x); }
37 static u16 convertU16(Types from, Types to, u16 x) { return cConvFuncTable.conv16[from ^ to](x); }
38 static u32 convertU32(Types from, Types to, u32 x) { return cConvFuncTable.conv32[from ^ to](x); }
39 static u64 convertU64(Types from, Types to, u64 x) { return cConvFuncTable.conv64[from ^ to](x); }
40
41 static s8 convertS8 (Types from, Types to, s8 x) { return cConvFuncTable.conv8 [from ^ to](x); }
42 static s16 convertS16(Types from, Types to, s16 x) { return cConvFuncTable.conv16[from ^ to](x); }
43 static s32 convertS32(Types from, Types to, s32 x) { return cConvFuncTable.conv32[from ^ to](x); }
44 static s64 convertS64(Types from, Types to, s64 x) { return cConvFuncTable.conv64[from ^ to](x); }
45
46 static u32 convertF32(Types from, Types to, const void* x)
47 {
48 union { const u32* ui; const f32* f; } x_ = { . f = static_cast<const f32*>(x) };
49 return cConvFuncTable.conv32[from ^ to](*x_.ui);
50 }
51
52 static u8 toHostU8 (Types from, u8 x) { return convertU8 (from, cHostEndian, x); }
53 static u16 toHostU16(Types from, u16 x) { return convertU16(from, cHostEndian, x); }
54 static u32 toHostU32(Types from, u32 x) { return convertU32(from, cHostEndian, x); }
55 static u64 toHostU64(Types from, u64 x) { return convertU64(from, cHostEndian, x); }
56
57 static u8 fromHostU8 (Types to, u8 x) { return convertU8 (cHostEndian, to, x); }
58 static u16 fromHostU16(Types to, u16 x) { return convertU16(cHostEndian, to, x); }
59 static u32 fromHostU32(Types to, u32 x) { return convertU32(cHostEndian, to, x); }
60 static u64 fromHostU64(Types to, u64 x) { return convertU64(cHostEndian, to, x); }
61
62 static s8 toHostS8 (Types from, s8 x) { return convertS8 (from, cHostEndian, x); }
63 static s16 toHostS16(Types from, s16 x) { return convertS16(from, cHostEndian, x); }
64 static s32 toHostS32(Types from, s32 x) { return convertS32(from, cHostEndian, x); }
65 static s64 toHostS64(Types from, s64 x) { return convertS64(from, cHostEndian, x); }
66
67 static s8 fromHostS8 (Types to, s8 x) { return convertS8 (cHostEndian, to, x); }
68 static s16 fromHostS16(Types to, s16 x) { return convertS16(cHostEndian, to, x); }
69 static s32 fromHostS32(Types to, s32 x) { return convertS32(cHostEndian, to, x); }
70 static s64 fromHostS64(Types to, s64 x) { return convertS64(cHostEndian, to, x); }
71
72 static f32 toHostF32(Types from, const u32* x)
73 {
74 union { u32 ui; f32 f; } x_ = { .ui = convertF32(from, cHostEndian, x) };
75 return x_. f;
76 }
77
78 static u32 fromHostF32(Types to, const f32* x) { return convertF32(cHostEndian, to, x); }
79
80 static Types getHostEndian() { return cHostEndian; }
81
82 static Types markToEndian(u16 mark)
83 {
84 u8* mark8 = reinterpret_cast<u8*>(&mark);
85 if (*mark8 == 0xff && *(mark8 + 1) == 0xfe)
86 return cLittle;
87
88 else if (*mark8 == 0xfe && *(mark8 + 1) == 0xff)
89 return cBig;
90
91 else
92 {
93 SEAD_ASSERT_MSG(false, "Undefined endian mark(0x%02x 0x%02x).", *((u8*)&mark), *((u8*)&mark + 1));
94 return cLittle;
95 }
96 }
97
98 static u16 endianToMark(Types endian) { return (endian == cBig) ? 0xfeff : 0xfffe; }
99
100private:
101 typedef u8 (*CONV8FUNC)(u8);
102 typedef u16 (*CONV16FUNC)(u16);
103 typedef u32 (*CONV32FUNC)(u32);
104 typedef u64 (*CONV64FUNC)(u64);
105
113
114 static const Types cHostEndian;
116};
117
118} // namespace sead
119
120#endif // SEAD_ENDIAN_H_
Definition seadEndian.h:10
static u8 fromHostU8(Types to, u8 x)
Definition seadEndian.h:57
static u32 fromHostU32(Types to, u32 x)
Definition seadEndian.h:59
static u32 swapU32(u32 x)
Definition seadEndian.h:21
static s64 fromHostS64(Types to, s64 x)
Definition seadEndian.h:70
static u32 convertU32(Types from, Types to, u32 x)
Definition seadEndian.h:38
static s64 swapS64(s64 x)
Definition seadEndian.h:27
static u16 convertU16(Types from, Types to, u16 x)
Definition seadEndian.h:37
static s64 convertS64(Types from, Types to, s64 x)
Definition seadEndian.h:44
static Types markToEndian(u16 mark)
Definition seadEndian.h:82
static s32 fromHostS32(Types to, s32 x)
Definition seadEndian.h:69
static u32 toHostU32(Types from, u32 x)
Definition seadEndian.h:54
static u32 fromHostF32(Types to, const f32 *x)
Definition seadEndian.h:78
u8(* CONV8FUNC)(u8)
Definition seadEndian.h:101
static s32 swapS32(s32 x)
Definition seadEndian.h:26
static u8 toHostU8(Types from, u8 x)
Definition seadEndian.h:52
static s8 convertS8(Types from, Types to, s8 x)
Definition seadEndian.h:41
static u64 fromHostU64(Types to, u64 x)
Definition seadEndian.h:60
static s16 swapS16(s16 x)
Definition seadEndian.h:25
static u16 swapU16(u16 x)
Definition seadEndian.h:20
static u8 convertU8(Types from, Types to, u8 x)
Definition seadEndian.h:36
static f32 toHostF32(Types from, const u32 *x)
Definition seadEndian.h:72
static u16 toHostU16(Types from, u16 x)
Definition seadEndian.h:53
u64(* CONV64FUNC)(u64)
Definition seadEndian.h:104
static s64 toHostS64(Types from, s64 x)
Definition seadEndian.h:65
static s16 fromHostS16(Types to, s16 x)
Definition seadEndian.h:68
Types
Definition seadEndian.h:13
@ cLittle
Definition seadEndian.h:15
@ cBig
Definition seadEndian.h:14
static u32 convertF32(Types from, Types to, const void *x)
Definition seadEndian.h:46
static s32 convertS32(Types from, Types to, s32 x)
Definition seadEndian.h:43
static u64 convertU64(Types from, Types to, u64 x)
Definition seadEndian.h:39
static s8 fromHostS8(Types to, s8 x)
Definition seadEndian.h:67
static u16 endianToMark(Types endian)
Definition seadEndian.h:98
static u16 fromHostU16(Types to, u16 x)
Definition seadEndian.h:58
static u8 swapU8(u8 x)
Definition seadEndian.h:19
u16(* CONV16FUNC)(u16)
Definition seadEndian.h:102
static u64 toHostU64(Types from, u64 x)
Definition seadEndian.h:55
static const Types cHostEndian
Definition seadEndian.h:114
static const ConvFuncTable cConvFuncTable
Definition seadEndian.h:115
static s16 toHostS16(Types from, s16 x)
Definition seadEndian.h:63
static Types getHostEndian()
Definition seadEndian.h:80
static s8 swapS8(s8 x)
Definition seadEndian.h:24
static s16 convertS16(Types from, Types to, s16 x)
Definition seadEndian.h:42
static u64 swapU64(u64 x)
Definition seadEndian.h:22
u32(* CONV32FUNC)(u32)
Definition seadEndian.h:103
static f32 swapF32(f32 x)
Definition seadEndian.h:29
static s8 toHostS8(Types from, s8 x)
Definition seadEndian.h:62
static s32 toHostS32(Types from, s32 x)
Definition seadEndian.h:64
Definition seadAssert.h:44
#define SEAD_ASSERT_MSG(condition, format,...)
Definition seadAssert.h:33
Definition seadEndian.h:107
CONV64FUNC conv64[2]
Definition seadEndian.h:111
CONV8FUNC conv8[2]
Definition seadEndian.h:108
CONV32FUNC conv32[2]
Definition seadEndian.h:110
CONV16FUNC conv16[2]
Definition seadEndian.h:109