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