2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 * Functions for reading and writing endian-specific values
31 #include "SDL_stdinc.h"
34 * 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? */
42 #if defined(__hppa__) || \
43 defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
44 (defined(__MIPS__) && defined(__MISPEB__)) || \
45 defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
47 #define SDL_BYTEORDER SDL_BIG_ENDIAN
49 #define SDL_BYTEORDER SDL_LIL_ENDIAN
51 #endif /* !SDL_BYTEORDER */
54 #include "begin_code.h"
55 /* Set up for C function definitions, even when using C++ */
61 * @name SDL_Swap Functions
62 * Use inline functions for compilers that support them, and static
63 * functions for those that do not. Because these functions become
64 * static for compilers that do not support inline functions, this
65 * header should only be included in files that actually use them.
68 #if defined(__GNUC__) && defined(__i386__) && \
69 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
70 static __inline__ Uint16 SDL_Swap16(Uint16 x)
72 __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
75 #elif defined(__GNUC__) && defined(__x86_64__)
76 static __inline__ Uint16 SDL_Swap16(Uint16 x)
78 __asm__("xchgb %b0,%h0" : "=Q" (x) : "0" (x));
81 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
82 static __inline__ Uint16 SDL_Swap16(Uint16 x)
86 __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
89 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
90 static __inline__ Uint16 SDL_Swap16(Uint16 x)
92 __asm__("rorw #8,%0" : "=d" (x) : "0" (x) : "cc");
96 static __inline__ Uint16 SDL_Swap16(Uint16 x) {
97 return((x<<8)|(x>>8));
101 #if defined(__GNUC__) && defined(__i386__) && \
102 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
103 static __inline__ Uint32 SDL_Swap32(Uint32 x)
105 __asm__("bswap %0" : "=r" (x) : "0" (x));
108 #elif defined(__GNUC__) && defined(__x86_64__)
109 static __inline__ Uint32 SDL_Swap32(Uint32 x)
111 __asm__("bswapl %0" : "=r" (x) : "0" (x));
114 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
115 static __inline__ Uint32 SDL_Swap32(Uint32 x)
119 __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
120 __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
121 __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
124 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
125 static __inline__ Uint32 SDL_Swap32(Uint32 x)
127 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) : "0" (x) : "cc");
131 static __inline__ Uint32 SDL_Swap32(Uint32 x) {
132 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
136 #ifdef SDL_HAS_64BIT_TYPE
137 #if defined(__GNUC__) && defined(__i386__) && \
138 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
139 static __inline__ Uint64 SDL_Swap64(Uint64 x)
142 struct { Uint32 a,b; } s;
146 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
147 : "=r" (v.s.a), "=r" (v.s.b)
148 : "0" (v.s.a), "1" (v.s.b));
151 #elif defined(__GNUC__) && defined(__x86_64__)
152 static __inline__ Uint64 SDL_Swap64(Uint64 x)
154 __asm__("bswapq %0" : "=r" (x) : "0" (x));
158 static __inline__ Uint64 SDL_Swap64(Uint64 x)
162 /* Separate into high and low 32-bit values and swap them */
163 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
165 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
173 /* This is mainly to keep compilers from complaining in SDL code.
174 * If there is no real 64-bit datatype, then compilers will complain about
175 * the fake 64-bit datatype that SDL provides when it compiles user code.
177 #define SDL_Swap64(X) (X)
178 #endif /* SDL_HAS_64BIT_TYPE */
182 * @name SDL_SwapLE and SDL_SwapBE Functions
183 * Byteswap item from the specified endianness to the native endianness
186 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
187 #define SDL_SwapLE16(X) (X)
188 #define SDL_SwapLE32(X) (X)
189 #define SDL_SwapLE64(X) (X)
190 #define SDL_SwapBE16(X) SDL_Swap16(X)
191 #define SDL_SwapBE32(X) SDL_Swap32(X)
192 #define SDL_SwapBE64(X) SDL_Swap64(X)
194 #define SDL_SwapLE16(X) SDL_Swap16(X)
195 #define SDL_SwapLE32(X) SDL_Swap32(X)
196 #define SDL_SwapLE64(X) SDL_Swap64(X)
197 #define SDL_SwapBE16(X) (X)
198 #define SDL_SwapBE32(X) (X)
199 #define SDL_SwapBE64(X) (X)
203 /* Ends C function definitions when using C++ */
207 #include "close_code.h"
209 #endif /* _SDL_endian_h */