00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef _SDL_endian_h
00029 #define _SDL_endian_h
00030
00031 #include "SDL_stdinc.h"
00032
00037 #define SDL_LIL_ENDIAN 1234
00038 #define SDL_BIG_ENDIAN 4321
00039
00041 #ifndef SDL_BYTEORDER
00042 #if defined(__hppa__) || \
00043 defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
00044 (defined(__MIPS__) && defined(__MISPEB__)) || \
00045 defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
00046 defined(__sparc__)
00047 #define SDL_BYTEORDER SDL_BIG_ENDIAN
00048 #else
00049 #define SDL_BYTEORDER SDL_LIL_ENDIAN
00050 #endif
00051 #endif
00052
00053
00054 #include "begin_code.h"
00055
00056 #ifdef __cplusplus
00057 extern "C" {
00058 #endif
00059
00068 #if defined(__GNUC__) && defined(__i386__) && \
00069 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 )
00070 static __inline__ Uint16 SDL_Swap16(Uint16 x)
00071 {
00072 __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
00073 return x;
00074 }
00075 #elif defined(__GNUC__) && defined(__x86_64__)
00076 static __inline__ Uint16 SDL_Swap16(Uint16 x)
00077 {
00078 __asm__("xchgb %b0,%h0" : "=Q" (x) : "0" (x));
00079 return x;
00080 }
00081 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
00082 static __inline__ Uint16 SDL_Swap16(Uint16 x)
00083 {
00084 Uint16 result;
00085
00086 __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
00087 return result;
00088 }
00089 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
00090 static __inline__ Uint16 SDL_Swap16(Uint16 x)
00091 {
00092 __asm__("rorw #8,%0" : "=d" (x) : "0" (x) : "cc");
00093 return x;
00094 }
00095 #else
00096 static __inline__ Uint16 SDL_Swap16(Uint16 x) {
00097 return((x<<8)|(x>>8));
00098 }
00099 #endif
00100
00101 #if defined(__GNUC__) && defined(__i386__) && \
00102 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 )
00103 static __inline__ Uint32 SDL_Swap32(Uint32 x)
00104 {
00105 __asm__("bswap %0" : "=r" (x) : "0" (x));
00106 return x;
00107 }
00108 #elif defined(__GNUC__) && defined(__x86_64__)
00109 static __inline__ Uint32 SDL_Swap32(Uint32 x)
00110 {
00111 __asm__("bswapl %0" : "=r" (x) : "0" (x));
00112 return x;
00113 }
00114 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
00115 static __inline__ Uint32 SDL_Swap32(Uint32 x)
00116 {
00117 Uint32 result;
00118
00119 __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
00120 __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
00121 __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
00122 return result;
00123 }
00124 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
00125 static __inline__ Uint32 SDL_Swap32(Uint32 x)
00126 {
00127 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) : "0" (x) : "cc");
00128 return x;
00129 }
00130 #else
00131 static __inline__ Uint32 SDL_Swap32(Uint32 x) {
00132 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
00133 }
00134 #endif
00135
00136 #ifdef SDL_HAS_64BIT_TYPE
00137 #if defined(__GNUC__) && defined(__i386__) && \
00138 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 )
00139 static __inline__ Uint64 SDL_Swap64(Uint64 x)
00140 {
00141 union {
00142 struct { Uint32 a,b; } s;
00143 Uint64 u;
00144 } v;
00145 v.u = x;
00146 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
00147 : "=r" (v.s.a), "=r" (v.s.b)
00148 : "0" (v.s.a), "1" (v.s.b));
00149 return v.u;
00150 }
00151 #elif defined(__GNUC__) && defined(__x86_64__)
00152 static __inline__ Uint64 SDL_Swap64(Uint64 x)
00153 {
00154 __asm__("bswapq %0" : "=r" (x) : "0" (x));
00155 return x;
00156 }
00157 #else
00158 static __inline__ Uint64 SDL_Swap64(Uint64 x)
00159 {
00160 Uint32 hi, lo;
00161
00162
00163 lo = (Uint32)(x&0xFFFFFFFF);
00164 x >>= 32;
00165 hi = (Uint32)(x&0xFFFFFFFF);
00166 x = SDL_Swap32(lo);
00167 x <<= 32;
00168 x |= SDL_Swap32(hi);
00169 return(x);
00170 }
00171 #endif
00172 #else
00173
00174
00175
00176
00177 #define SDL_Swap64(X) (X)
00178 #endif
00179
00186 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
00187 #define SDL_SwapLE16(X) (X)
00188 #define SDL_SwapLE32(X) (X)
00189 #define SDL_SwapLE64(X) (X)
00190 #define SDL_SwapBE16(X) SDL_Swap16(X)
00191 #define SDL_SwapBE32(X) SDL_Swap32(X)
00192 #define SDL_SwapBE64(X) SDL_Swap64(X)
00193 #else
00194 #define SDL_SwapLE16(X) SDL_Swap16(X)
00195 #define SDL_SwapLE32(X) SDL_Swap32(X)
00196 #define SDL_SwapLE64(X) SDL_Swap64(X)
00197 #define SDL_SwapBE16(X) (X)
00198 #define SDL_SwapBE32(X) (X)
00199 #define SDL_SwapBE64(X) (X)
00200 #endif
00201
00203
00204 #ifdef __cplusplus
00205 }
00206 #endif
00207 #include "close_code.h"
00208
00209 #endif