Debian Codegraph @

mednafen/mednafen-endian.h

raw symbols SCIP
1 #ifndef __MDFN_ENDIAN_H
2 #define __MDFN_ENDIAN_H
3
4 #include <stdio.h>
5 #include <stdint.h>
6
7 #ifdef MSB_FIRST
8 #ifndef le32toh
9 #define le32toh(l) ((((l)>>24) & 0xff) | (((l)>>8) & 0xff00) \
10 | (((l)<<8) & 0xff0000) | (((l)<<24) & 0xff000000))
11 #endif
12 #ifndef le16toh
13 #define le16toh(l) ((((l)>>8) & 0xff) | (((l)<<8) & 0xff00))
14 #endif
15 #else
16 #ifndef le32toh
17 #define le32toh(l) (l)
18 #endif
19 #ifndef le16toh
20 #define le16toh(l) (l)
21 #endif
22 #endif
23
24 #ifndef htole32
25 #define htole32 le32toh
26 #endif
27
28 #ifndef htole16
29 #define htole16 le16toh
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 int write16le(uint16_t b, FILE *fp);
37 int write32le(uint32_t b, FILE *fp);
38 int read32le(uint32_t *Bufo, FILE *fp);
39
40 void Endian_A16_Swap(void *src, uint32_t nelements);
41 void Endian_A32_Swap(void *src, uint32_t nelements);
42 void Endian_A64_Swap(void *src, uint32_t nelements);
43
44 void Endian_A16_NE_to_LE(void *src, uint32_t nelements);
45 void Endian_A32_NE_to_LE(void *src, uint32_t nelements);
46 void Endian_A64_NE_to_LE(void *src, uint32_t nelements);
47
48 void Endian_A16_LE_to_NE(void *src, uint32_t nelements);
49 void Endian_A16_BE_to_NE(void *src, uint32_t nelements);
50 void Endian_A32_LE_to_NE(void *src, uint32_t nelements);
51 void Endian_A64_LE_to_NE(void *src, uint32_t nelements);
52
53 void Endian_V_LE_to_NE(void *src, uint32_t bytesize);
54 void Endian_V_NE_to_LE(void *src, uint32_t bytesize);
55
56 void FlipByteOrder(uint8_t *src, uint32_t count);
57
58 // The following functions can encode/decode to unaligned addresses.
59
60 static inline void MDFN_en16lsb(uint8_t *buf, uint16_t morp)
61 {
62 buf[0]=morp;
63 buf[1]=morp>>8;
64 }
65
66 static inline void MDFN_en24lsb(uint8_t *buf, uint32_t morp)
67 {
68 buf[0]=morp;
69 buf[1]=morp>>8;
70 buf[2]=morp>>16;
71 }
72
73
74 static inline void MDFN_en32lsb(uint8_t *buf, uint32_t morp)
75 {
76 buf[0]=morp;
77 buf[1]=morp>>8;
78 buf[2]=morp>>16;
79 buf[3]=morp>>24;
80 }
81
82 static inline void MDFN_en64lsb(uint8_t *buf, uint64_t morp)
83 {
84 buf[0]=morp >> 0;
85 buf[1]=morp >> 8;
86 buf[2]=morp >> 16;
87 buf[3]=morp >> 24;
88 buf[4]=morp >> 32;
89 buf[5]=morp >> 40;
90 buf[6]=morp >> 48;
91 buf[7]=morp >> 56;
92 }
93
94
95 static inline void MDFN_en16msb(uint8_t *buf, uint16_t morp)
96 {
97 buf[0] = morp >> 8;
98 buf[1] = morp;
99 }
100
101 static inline void MDFN_en24msb(uint8_t *buf, uint32_t morp)
102 {
103 buf[0] = morp >> 16;
104 buf[1] = morp >> 8;
105 buf[2] = morp;
106 }
107
108 static inline void MDFN_en32msb(uint8_t *buf, uint32_t morp)
109 {
110 buf[0] = morp >> 24;
111 buf[1] = morp >> 16;
112 buf[2] = morp >> 8;
113 buf[3] = morp;
114 }
115
116 static inline void MDFN_en64msb(uint8_t *buf, uint64_t morp)
117 {
118 buf[0] = morp >> 56;
119 buf[1] = morp >> 48;
120 buf[2] = morp >> 40;
121 buf[3] = morp >> 32;
122 buf[4] = morp >> 24;
123 buf[5] = morp >> 16;
124 buf[6] = morp >> 8;
125 buf[7] = morp >> 0;
126 }
127
128 static inline uint16_t MDFN_de16lsb(const uint8_t *morp)
129 {
130 return(morp[0] | (morp[1] << 8));
131 }
132
133 static inline uint32_t MDFN_de24lsb(const uint8_t *morp)
134 {
135 return(morp[0]|(morp[1]<<8)|(morp[2]<<16));
136 }
137
138 static inline uint32_t MDFN_de32lsb(const uint8_t *morp)
139 {
140 return(morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24));
141 }
142
143 static inline uint64_t MDFN_de64lsb(const uint8_t *morp)
144 {
145 uint64_t ret = 0;
146
147 ret |= (uint64_t)morp[0];
148 ret |= (uint64_t)morp[1] << 8;
149 ret |= (uint64_t)morp[2] << 16;
150 ret |= (uint64_t)morp[3] << 24;
151 ret |= (uint64_t)morp[4] << 32;
152 ret |= (uint64_t)morp[5] << 40;
153 ret |= (uint64_t)morp[6] << 48;
154 ret |= (uint64_t)morp[7] << 56;
155
156 return(ret);
157 }
158
159 static inline uint16_t MDFN_de16msb(const uint8_t *morp)
160 {
161 return(morp[1] | (morp[0] << 8));
162 }
163
164 static inline uint32_t MDFN_de24msb(const uint8_t *morp)
165 {
166 return((morp[2]<<0)|(morp[1]<<8)|(morp[0]<<16));
167 }
168
169
170 static inline uint32_t MDFN_de32msb(const uint8_t *morp)
171 {
172 return(morp[3]|(morp[2]<<8)|(morp[1]<<16)|(morp[0]<<24));
173 }
174
175 #ifdef __cplusplus
176 }
177 #endif
178
179 #endif