2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
25 * Functions for reading and writing endian-specific values
31 #include "SDL_stdinc.h"
34 * \name The two types of endianness
37 #define SDL_LIL_ENDIAN 1234
38 #define SDL_BIG_ENDIAN 4321
41 #ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
44 #define SDL_BYTEORDER __BYTE_ORDER
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) || \
51 #define SDL_BYTEORDER SDL_BIG_ENDIAN
53 #define SDL_BYTEORDER SDL_LIL_ENDIAN
55 #endif /* __linux__ */
56 #endif /* !SDL_BYTEORDER */
59 #include "begin_code.h"
60 /* Set up for C function definitions, even when using C++ */
68 #if defined(__GNUC__) && defined(__i386__) && \
69 !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
70 SDL_FORCE_INLINE Uint16
73 __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
76 #elif defined(__GNUC__) && defined(__x86_64__)
77 SDL_FORCE_INLINE Uint16
80 __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
83 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
84 SDL_FORCE_INLINE Uint16
89 __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
90 return (Uint16)result;
92 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
93 SDL_FORCE_INLINE Uint16
96 __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
100 SDL_FORCE_INLINE Uint16
103 return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
107 #if defined(__GNUC__) && defined(__i386__)
108 SDL_FORCE_INLINE Uint32
111 __asm__("bswap %0": "=r"(x):"0"(x));
114 #elif defined(__GNUC__) && defined(__x86_64__)
115 SDL_FORCE_INLINE Uint32
118 __asm__("bswapl %0": "=r"(x):"0"(x));
121 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
122 SDL_FORCE_INLINE Uint32
127 __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
128 __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
129 __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
132 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
133 SDL_FORCE_INLINE Uint32
136 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
140 SDL_FORCE_INLINE Uint32
143 return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
144 ((x >> 8) & 0x0000FF00) | (x >> 24)));
148 #if defined(__GNUC__) && defined(__i386__)
149 SDL_FORCE_INLINE Uint64
161 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
166 #elif defined(__GNUC__) && defined(__x86_64__)
167 SDL_FORCE_INLINE Uint64
170 __asm__("bswapq %0": "=r"(x):"0"(x));
174 SDL_FORCE_INLINE Uint64
179 /* Separate into high and low 32-bit values and swap them */
180 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
182 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
191 SDL_FORCE_INLINE float
192 SDL_SwapFloat(float x)
200 swapper.ui32 = SDL_Swap32(swapper.ui32);
206 * \name Swap to native
207 * Byteswap item from the specified endianness to the native endianness.
210 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
211 #define SDL_SwapLE16(X) (X)
212 #define SDL_SwapLE32(X) (X)
213 #define SDL_SwapLE64(X) (X)
214 #define SDL_SwapFloatLE(X) (X)
215 #define SDL_SwapBE16(X) SDL_Swap16(X)
216 #define SDL_SwapBE32(X) SDL_Swap32(X)
217 #define SDL_SwapBE64(X) SDL_Swap64(X)
218 #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
220 #define SDL_SwapLE16(X) SDL_Swap16(X)
221 #define SDL_SwapLE32(X) SDL_Swap32(X)
222 #define SDL_SwapLE64(X) SDL_Swap64(X)
223 #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
224 #define SDL_SwapBE16(X) (X)
225 #define SDL_SwapBE32(X) (X)
226 #define SDL_SwapBE64(X) (X)
227 #define SDL_SwapFloatBE(X) (X)
229 /* @} *//* Swap to native */
231 /* Ends C function definitions when using C++ */
235 #include "close_code.h"
237 #endif /* _SDL_endian_h */
239 /* vi: set ts=4 sw=4 expandtab: */