* roottypes.cs: Rename from tree.cs.
[mono.git] / mono / metadata / mono-endian.c
1 #include <config.h>
2 #include "mono-endian.h"
3
4 #if NO_UNALIGNED_ACCESS
5
6 typedef union {
7         char c [2];
8         guint16 i;
9 } mono_rint16;
10
11 typedef union {
12         char c [4];
13         guint32 i;
14 } mono_rint32;
15
16 typedef union {
17         char c [8];
18         guint64 i;
19 } mono_rint64;
20
21 guint16 
22 mono_read16 (const unsigned char *x)
23 {
24         mono_rint16 r;
25 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
26         r.c [0] = x [0];
27         r.c [1] = x [1];
28 #else
29         r.c [1] = x [0];
30         r.c [0] = x [1];
31 #endif
32         return r.i;
33 }
34
35 guint32 
36 mono_read32 (const unsigned char *x)
37 {
38         mono_rint32 r;
39 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
40         r.c [0] = x [0];
41         r.c [1] = x [1];
42         r.c [2] = x [2];
43         r.c [3] = x [3];
44 #else
45         r.c [3] = x [0];
46         r.c [2] = x [1];
47         r.c [1] = x [2];
48         r.c [0] = x [3];
49 #endif
50         return r.i;
51 }
52
53 guint64 
54 mono_read64 (const unsigned char *x)
55 {
56         mono_rint64 r;
57 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
58         r.c [0] = x [0];
59         r.c [1] = x [1];
60         r.c [2] = x [2];
61         r.c [3] = x [3];
62         r.c [4] = x [4];
63         r.c [5] = x [5];
64         r.c [6] = x [6];
65         r.c [7] = x [7];
66 #else
67         r.c [7] = x [0];
68         r.c [6] = x [1];
69         r.c [5] = x [2];
70         r.c [4] = x [3];
71         r.c [3] = x [4];
72         r.c [2] = x [5];
73         r.c [1] = x [6];
74         r.c [0] = x [7];
75 #endif
76         return r.i;
77 }
78
79 #endif