This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_endian.h
248 lines (225 loc) · 6.14 KB
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
*/
22
/**
23
24
25
* \file SDL_endian.h
*
* Functions for reading and writing endian-specific values
26
*/
27
28
29
30
#ifndef _SDL_endian_h
#define _SDL_endian_h
31
32
#include "SDL_stdinc.h"
33
34
35
36
/**
* \name The two types of endianness
*/
/*@{*/
37
38
#define SDL_LIL_ENDIAN 1234
#define SDL_BIG_ENDIAN 4321
39
/*@}*/
40
41
#ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
42
43
44
45
#ifdef __linux__
#include <endian.h>
#define SDL_BYTEORDER __BYTE_ORDER
#else /* __linux __ */
46
#if defined(__hppa__) || \
47
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
48
(defined(__MIPS__) && defined(__MISPEB__)) || \
49
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
50
defined(__sparc__)
51
#define SDL_BYTEORDER SDL_BIG_ENDIAN
52
53
#else
#define SDL_BYTEORDER SDL_LIL_ENDIAN
54
#endif
55
#endif /* __linux __ */
56
#endif /* !SDL_BYTEORDER */
57
58
59
60
61
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
62
/* *INDENT-OFF* */
63
extern "C" {
64
/* *INDENT-ON* */
65
66
#endif
67
68
69
70
71
72
73
74
/**
* \file SDL_endian.h
*
* Uses inline functions for compilers that support them, and static
* functions for those that do not. Because these functions become
* static for compilers that do not support inline functions, this
* header should only be included in files that actually use them.
*/
75
#if defined(__GNUC__) && defined(__i386__) && \
76
!(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
77
78
static __inline__ Uint16
SDL_Swap16(Uint16 x)
79
{
80
81
__asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
return x;
82
83
}
#elif defined(__GNUC__) && defined(__x86_64__)
84
85
static __inline__ Uint16
SDL_Swap16(Uint16 x)
86
{
87
88
__asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
return x;
89
}
90
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
91
92
static __inline__ Uint16
SDL_Swap16(Uint16 x)
93
{
94
Uint16 result;
95
96
97
__asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
return result;
98
}
99
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
100
101
static __inline__ Uint16
SDL_Swap16(Uint16 x)
102
{
103
104
__asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
return x;
105
}
106
#else
107
108
109
static __inline__ Uint16
SDL_Swap16(Uint16 x)
{
110
return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
111
112
}
#endif
113
114
#if defined(__GNUC__) && defined(__i386__)
115
116
static __inline__ Uint32
SDL_Swap32(Uint32 x)
117
{
118
119
__asm__("bswap %0": "=r"(x):"0"(x));
return x;
120
121
}
#elif defined(__GNUC__) && defined(__x86_64__)
122
123
static __inline__ Uint32
SDL_Swap32(Uint32 x)
124
{
125
126
__asm__("bswapl %0": "=r"(x):"0"(x));
return x;
127
}
128
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
129
130
static __inline__ Uint32
SDL_Swap32(Uint32 x)
131
{
132
Uint32 result;
133
134
135
136
137
__asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
__asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
__asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
return result;
138
}
139
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
140
141
static __inline__ Uint32
SDL_Swap32(Uint32 x)
142
{
143
144
__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
return x;
145
}
146
#else
147
148
149
static __inline__ Uint32
SDL_Swap32(Uint32 x)
{
150
151
return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) | (x >> 24)));
152
153
}
#endif
154
155
#if defined(__GNUC__) && defined(__i386__)
156
157
static __inline__ Uint64
SDL_Swap64(Uint64 x)
158
{
159
160
161
162
163
164
165
166
167
168
union
{
struct
{
Uint32 a, b;
} s;
Uint64 u;
} v;
v.u = x;
__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
169
170
"1"(v.s.
b));
171
return v.u;
172
173
}
#elif defined(__GNUC__) && defined(__x86_64__)
174
175
static __inline__ Uint64
SDL_Swap64(Uint64 x)
176
{
177
178
__asm__("bswapq %0": "=r"(x):"0"(x));
return x;
179
180
}
#else
181
182
static __inline__ Uint64
SDL_Swap64(Uint64 x)
183
{
184
185
186
Uint32 hi, lo;
/* Separate into high and low 32-bit values and swap them */
187
lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
188
x >>= 32;
189
hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
190
191
192
193
x = SDL_Swap32(lo);
x <<= 32;
x |= SDL_Swap32(hi);
return (x);
194
195
196
197
}
#endif
198
199
200
static __inline__ float
SDL_SwapFloat(float x)
{
201
202
203
204
205
union
{
float f;
Uint32 ui32;
} swapper;
206
207
208
209
210
211
swapper.f = x;
swapper.ui32 = SDL_Swap32(swapper.ui32);
return swapper.f;
}
212
213
214
215
216
/**
* \name Swap to native
* Byteswap item from the specified endianness to the native endianness.
*/
/*@{*/
217
218
219
220
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define SDL_SwapLE16(X) (X)
#define SDL_SwapLE32(X) (X)
#define SDL_SwapLE64(X) (X)
221
#define SDL_SwapFloatLE(X) (X)
222
223
224
#define SDL_SwapBE16(X) SDL_Swap16(X)
#define SDL_SwapBE32(X) SDL_Swap32(X)
#define SDL_SwapBE64(X) SDL_Swap64(X)
225
#define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
226
227
228
229
#else
#define SDL_SwapLE16(X) SDL_Swap16(X)
#define SDL_SwapLE32(X) SDL_Swap32(X)
#define SDL_SwapLE64(X) SDL_Swap64(X)
230
#define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
231
232
233
#define SDL_SwapBE16(X) (X)
#define SDL_SwapBE32(X) (X)
#define SDL_SwapBE64(X) (X)
234
#define SDL_SwapFloatBE(X) (X)
235
#endif
236
/*@}*//*Swap to native*/
237
238
239
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
240
/* *INDENT-OFF* */
241
}
242
/* *INDENT-ON* */
243
244
245
246
#endif
#include "close_code.h"
#endif /* _SDL_endian_h */
247
248
/* vi: set ts=4 sw=4 expandtab: */