Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
248 lines (225 loc) · 6.14 KB

SDL_endian.h

File metadata and controls

248 lines (225 loc) · 6.14 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
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.
Apr 26, 2001
Apr 26, 2001
20
21
*/
Jul 10, 2006
Jul 10, 2006
22
/**
Oct 19, 2009
Oct 19, 2009
23
24
25
* \file SDL_endian.h
*
* Functions for reading and writing endian-specific values
Jul 10, 2006
Jul 10, 2006
26
*/
Apr 26, 2001
Apr 26, 2001
27
28
29
30
#ifndef _SDL_endian_h
#define _SDL_endian_h
Feb 9, 2006
Feb 9, 2006
31
32
#include "SDL_stdinc.h"
Oct 19, 2009
Oct 19, 2009
33
34
35
36
/**
* \name The two types of endianness
*/
/*@{*/
Feb 9, 2006
Feb 9, 2006
37
38
#define SDL_LIL_ENDIAN 1234
#define SDL_BIG_ENDIAN 4321
Oct 19, 2009
Oct 19, 2009
39
/*@}*/
Feb 9, 2006
Feb 9, 2006
40
Jul 10, 2006
Jul 10, 2006
41
#ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
Jul 18, 2010
Jul 18, 2010
42
43
44
45
#ifdef __linux__
#include <endian.h>
#define SDL_BYTEORDER __BYTE_ORDER
#else /* __linux __ */
Mar 6, 2006
Mar 6, 2006
46
#if defined(__hppa__) || \
Mar 6, 2006
Mar 6, 2006
47
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
Mar 6, 2006
Mar 6, 2006
48
(defined(__MIPS__) && defined(__MISPEB__)) || \
Mar 6, 2006
Mar 6, 2006
49
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
Mar 6, 2006
Mar 6, 2006
50
defined(__sparc__)
Feb 9, 2006
Feb 9, 2006
51
#define SDL_BYTEORDER SDL_BIG_ENDIAN
Mar 6, 2006
Mar 6, 2006
52
53
#else
#define SDL_BYTEORDER SDL_LIL_ENDIAN
Feb 9, 2006
Feb 9, 2006
54
#endif
Jul 18, 2010
Jul 18, 2010
55
#endif /* __linux __ */
Feb 9, 2006
Feb 9, 2006
56
#endif /* !SDL_BYTEORDER */
Apr 26, 2001
Apr 26, 2001
57
58
59
60
61
#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
Jul 10, 2006
Jul 10, 2006
62
/* *INDENT-OFF* */
Apr 26, 2001
Apr 26, 2001
63
extern "C" {
Jul 10, 2006
Jul 10, 2006
64
/* *INDENT-ON* */
Apr 26, 2001
Apr 26, 2001
65
66
#endif
Oct 19, 2009
Oct 19, 2009
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.
*/
Feb 19, 2006
Feb 19, 2006
75
#if defined(__GNUC__) && defined(__i386__) && \
Feb 18, 2006
Feb 18, 2006
76
!(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
Jul 10, 2006
Jul 10, 2006
77
78
static __inline__ Uint16
SDL_Swap16(Uint16 x)
Feb 23, 2004
Feb 23, 2004
79
{
Jul 10, 2006
Jul 10, 2006
80
81
__asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
return x;
Feb 23, 2004
Feb 23, 2004
82
83
}
#elif defined(__GNUC__) && defined(__x86_64__)
Jul 10, 2006
Jul 10, 2006
84
85
static __inline__ Uint16
SDL_Swap16(Uint16 x)
Feb 23, 2004
Feb 23, 2004
86
{
Jul 10, 2006
Jul 10, 2006
87
88
__asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
return x;
Feb 23, 2004
Feb 23, 2004
89
}
Feb 26, 2004
Feb 26, 2004
90
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
Jul 10, 2006
Jul 10, 2006
91
92
static __inline__ Uint16
SDL_Swap16(Uint16 x)
Feb 23, 2004
Feb 23, 2004
93
{
Jul 10, 2006
Jul 10, 2006
94
Uint16 result;
Feb 23, 2004
Feb 23, 2004
95
Jul 10, 2006
Jul 10, 2006
96
97
__asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
return result;
Feb 23, 2004
Feb 23, 2004
98
}
Jan 10, 2010
Jan 10, 2010
99
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
Jul 10, 2006
Jul 10, 2006
100
101
static __inline__ Uint16
SDL_Swap16(Uint16 x)
Nov 22, 2004
Nov 22, 2004
102
{
Jul 10, 2006
Jul 10, 2006
103
104
__asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
return x;
Nov 22, 2004
Nov 22, 2004
105
}
Feb 23, 2004
Feb 23, 2004
106
#else
Jul 10, 2006
Jul 10, 2006
107
108
109
static __inline__ Uint16
SDL_Swap16(Uint16 x)
{
Apr 23, 2010
Apr 23, 2010
110
return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
Apr 26, 2001
Apr 26, 2001
111
112
}
#endif
Feb 23, 2004
Feb 23, 2004
113
Feb 23, 2004
Feb 23, 2004
114
#if defined(__GNUC__) && defined(__i386__)
Jul 10, 2006
Jul 10, 2006
115
116
static __inline__ Uint32
SDL_Swap32(Uint32 x)
Feb 23, 2004
Feb 23, 2004
117
{
Jul 10, 2006
Jul 10, 2006
118
119
__asm__("bswap %0": "=r"(x):"0"(x));
return x;
Feb 23, 2004
Feb 23, 2004
120
121
}
#elif defined(__GNUC__) && defined(__x86_64__)
Jul 10, 2006
Jul 10, 2006
122
123
static __inline__ Uint32
SDL_Swap32(Uint32 x)
Feb 23, 2004
Feb 23, 2004
124
{
Jul 10, 2006
Jul 10, 2006
125
126
__asm__("bswapl %0": "=r"(x):"0"(x));
return x;
Feb 23, 2004
Feb 23, 2004
127
}
Feb 26, 2004
Feb 26, 2004
128
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
Jul 10, 2006
Jul 10, 2006
129
130
static __inline__ Uint32
SDL_Swap32(Uint32 x)
Feb 23, 2004
Feb 23, 2004
131
{
Jul 10, 2006
Jul 10, 2006
132
Uint32 result;
Feb 23, 2004
Feb 23, 2004
133
Jul 10, 2006
Jul 10, 2006
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;
Feb 23, 2004
Feb 23, 2004
138
}
Jan 10, 2010
Jan 10, 2010
139
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
Jul 10, 2006
Jul 10, 2006
140
141
static __inline__ Uint32
SDL_Swap32(Uint32 x)
Nov 22, 2004
Nov 22, 2004
142
{
Jul 10, 2006
Jul 10, 2006
143
144
__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
return x;
Nov 22, 2004
Nov 22, 2004
145
}
Feb 23, 2004
Feb 23, 2004
146
#else
Jul 10, 2006
Jul 10, 2006
147
148
149
static __inline__ Uint32
SDL_Swap32(Uint32 x)
{
Apr 23, 2010
Apr 23, 2010
150
151
return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) | (x >> 24)));
Apr 26, 2001
Apr 26, 2001
152
153
}
#endif
Feb 23, 2004
Feb 23, 2004
154
Feb 23, 2004
Feb 23, 2004
155
#if defined(__GNUC__) && defined(__i386__)
Jul 10, 2006
Jul 10, 2006
156
157
static __inline__ Uint64
SDL_Swap64(Uint64 x)
Feb 23, 2004
Feb 23, 2004
158
{
Jul 10, 2006
Jul 10, 2006
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),
Jan 10, 2009
Jan 10, 2009
169
170
"1"(v.s.
b));
Jul 10, 2006
Jul 10, 2006
171
return v.u;
Feb 23, 2004
Feb 23, 2004
172
173
}
#elif defined(__GNUC__) && defined(__x86_64__)
Jul 10, 2006
Jul 10, 2006
174
175
static __inline__ Uint64
SDL_Swap64(Uint64 x)
Feb 23, 2004
Feb 23, 2004
176
{
Jul 10, 2006
Jul 10, 2006
177
178
__asm__("bswapq %0": "=r"(x):"0"(x));
return x;
Feb 23, 2004
Feb 23, 2004
179
180
}
#else
Jul 10, 2006
Jul 10, 2006
181
182
static __inline__ Uint64
SDL_Swap64(Uint64 x)
Feb 23, 2004
Feb 23, 2004
183
{
Jul 10, 2006
Jul 10, 2006
184
185
186
Uint32 hi, lo;
/* Separate into high and low 32-bit values and swap them */
Feb 17, 2009
Feb 17, 2009
187
lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
Jul 10, 2006
Jul 10, 2006
188
x >>= 32;
Feb 17, 2009
Feb 17, 2009
189
hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
Jul 10, 2006
Jul 10, 2006
190
191
192
193
x = SDL_Swap32(lo);
x <<= 32;
x |= SDL_Swap32(hi);
return (x);
Apr 26, 2001
Apr 26, 2001
194
195
196
197
}
#endif
Aug 24, 2006
Aug 24, 2006
198
199
200
static __inline__ float
SDL_SwapFloat(float x)
{
Aug 28, 2006
Aug 28, 2006
201
202
203
204
205
union
{
float f;
Uint32 ui32;
} swapper;
Aug 24, 2006
Aug 24, 2006
206
207
208
209
210
211
swapper.f = x;
swapper.ui32 = SDL_Swap32(swapper.ui32);
return swapper.f;
}
Oct 19, 2009
Oct 19, 2009
212
213
214
215
216
/**
* \name Swap to native
* Byteswap item from the specified endianness to the native endianness.
*/
/*@{*/
Apr 26, 2001
Apr 26, 2001
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)
Aug 24, 2006
Aug 24, 2006
221
#define SDL_SwapFloatLE(X) (X)
Apr 26, 2001
Apr 26, 2001
222
223
224
#define SDL_SwapBE16(X) SDL_Swap16(X)
#define SDL_SwapBE32(X) SDL_Swap32(X)
#define SDL_SwapBE64(X) SDL_Swap64(X)
Aug 24, 2006
Aug 24, 2006
225
#define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
Apr 26, 2001
Apr 26, 2001
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)
Aug 24, 2006
Aug 24, 2006
230
#define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
Apr 26, 2001
Apr 26, 2001
231
232
233
#define SDL_SwapBE16(X) (X)
#define SDL_SwapBE32(X) (X)
#define SDL_SwapBE64(X) (X)
Aug 24, 2006
Aug 24, 2006
234
#define SDL_SwapFloatBE(X) (X)
Apr 26, 2001
Apr 26, 2001
235
#endif
Oct 19, 2009
Oct 19, 2009
236
/*@}*//*Swap to native*/
Apr 26, 2001
Apr 26, 2001
237
238
239
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Jul 10, 2006
Jul 10, 2006
240
/* *INDENT-OFF* */
Apr 26, 2001
Apr 26, 2001
241
}
Jul 10, 2006
Jul 10, 2006
242
/* *INDENT-ON* */
Apr 26, 2001
Apr 26, 2001
243
244
245
246
#endif
#include "close_code.h"
#endif /* _SDL_endian_h */
Jul 10, 2006
Jul 10, 2006
247
248
/* vi: set ts=4 sw=4 expandtab: */