Added inline byte swapping code for other architectures
authorSam Lantinga
Mon, 23 Feb 2004 06:09:21 +0000
changeset 849bab227101de4
parent 848 85af65457959
child 850 c203b4a42701
Added inline byte swapping code for other architectures
include/SDL_endian.h
     1.1 --- a/include/SDL_endian.h	Mon Feb 23 05:32:52 2004 +0000
     1.2 +++ b/include/SDL_endian.h	Mon Feb 23 06:09:21 2004 +0000
     1.3 @@ -59,53 +59,101 @@
     1.4     static for compilers that do not support inline functions, this
     1.5     header should only be included in files that actually use them.
     1.6  */
     1.7 -#if defined(__GNUC__) && defined(i386)
     1.8 -static __inline__ Uint16 SDL_Swap16(Uint16 D)
     1.9 +#if defined(__GNUC__) && defined(__i386__)
    1.10 +static __inline__ Uint16 SDL_Swap16(Uint16 x)
    1.11 +{
    1.12 +	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
    1.13 +	return x;
    1.14 +}
    1.15 +#elif defined(__GNUC__) && defined(__x86_64__)
    1.16 +static __inline__ Uint16 SDL_Swap16(Uint16 x)
    1.17  {
    1.18 -	__asm__("xchgb %b0,%h0" : "=q" (D) :  "0" (D));
    1.19 -	return D;
    1.20 +	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
    1.21 +	return x;
    1.22 +}
    1.23 +#elif defined(__GNUC__) && defined(__powerpc__)
    1.24 +static __inline__ Uint16 SDL_Swap16(Uint16 x)
    1.25 +{
    1.26 +	Uint16 result;
    1.27 +
    1.28 +	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
    1.29 +	return result;
    1.30  }
    1.31  #else
    1.32 -static __inline__ Uint16 SDL_Swap16(Uint16 D) {
    1.33 -	return((D<<8)|(D>>8));
    1.34 +static __inline__ Uint16 SDL_Swap16(Uint16 x) {
    1.35 +	return((x<<8)|(x>>8));
    1.36  }
    1.37  #endif
    1.38  
    1.39 -#if defined(__GNUC__) && defined(i386)
    1.40 -static __inline__ Uint32 SDL_Swap32(Uint32 D)
    1.41 +#if defined(__GNUC__) && defined(__i386__)
    1.42 +static __inline__ Uint32 SDL_Swap32(Uint32 x)
    1.43 +{
    1.44 +	__asm__("bswap %0" : "=r" (x) : "0" (x));
    1.45 +	return x;
    1.46 +}
    1.47 +#elif defined(__GNUC__) && defined(__x86_64__)
    1.48 +static __inline__ Uint32 SDL_Swap32(Uint32 x)
    1.49  {
    1.50 -	__asm__("bswap %0" : "=r" (D) : "0" (D));
    1.51 -	return D;
    1.52 +	__asm__("bswapl %0" : "=r" (x) : "0" (x));
    1.53 +	return x;
    1.54 +}
    1.55 +#elif defined(__GNUC__) && defined(__powerpc__)
    1.56 +static __inline__ Uint32 SDL_Swap32(Uint32 x)
    1.57 +{
    1.58 +	Uint32 result;
    1.59 +
    1.60 +	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
    1.61 +	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
    1.62 +	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
    1.63 +	return result;
    1.64  }
    1.65  #else
    1.66 -static __inline__ Uint32 SDL_Swap32(Uint32 D) {
    1.67 -	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
    1.68 +static __inline__ Uint32 SDL_Swap32(Uint32 x) {
    1.69 +	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
    1.70  }
    1.71  #endif
    1.72  
    1.73  #ifdef SDL_HAS_64BIT_TYPE
    1.74 -#ifndef SDL_Swap64
    1.75 -static __inline__ Uint64 SDL_Swap64(Uint64 val) {
    1.76 +#if defined(__GNUC__) && defined(__i386__)
    1.77 +static __inline__ Uint64 SDL_Swap64(Uint64 x)
    1.78 +{
    1.79 +	union { 
    1.80 +		struct { Uint32 a,b; } s;
    1.81 +		Uint64 u;
    1.82 +	} v;
    1.83 +	v.u = x;
    1.84 +	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
    1.85 +	        : "=r" (v.s.a), "=r" (v.s.b) 
    1.86 +	        : "0" (v.s.a), "1" (v.s.b)); 
    1.87 +	return v.u;
    1.88 +}
    1.89 +#elif defined(__GNUC__) && defined(__x86_64__)
    1.90 +static __inline__ Uint64 SDL_Swap64(Uint64 x)
    1.91 +{
    1.92 +	__asm__("bswapq %0" : "=r" (x) : "0" (x));
    1.93 +	return x;
    1.94 +}
    1.95 +#else
    1.96 +static __inline__ Uint64 SDL_Swap64(Uint64 x)
    1.97 +{
    1.98  	Uint32 hi, lo;
    1.99  
   1.100  	/* Separate into high and low 32-bit values and swap them */
   1.101 -	lo = (Uint32)(val&0xFFFFFFFF);
   1.102 -	val >>= 32;
   1.103 -	hi = (Uint32)(val&0xFFFFFFFF);
   1.104 -	val = SDL_Swap32(lo);
   1.105 -	val <<= 32;
   1.106 -	val |= SDL_Swap32(hi);
   1.107 -	return(val);
   1.108 +	lo = (Uint32)(x&0xFFFFFFFF);
   1.109 +	x >>= 32;
   1.110 +	hi = (Uint32)(x&0xFFFFFFFF);
   1.111 +	x = SDL_Swap32(lo);
   1.112 +	x <<= 32;
   1.113 +	x |= SDL_Swap32(hi);
   1.114 +	return(x);
   1.115  }
   1.116  #endif
   1.117  #else
   1.118 -#ifndef SDL_Swap64
   1.119  /* This is mainly to keep compilers from complaining in SDL code.
   1.120     If there is no real 64-bit datatype, then compilers will complain about
   1.121     the fake 64-bit datatype that SDL provides when it compiles user code.
   1.122  */
   1.123  #define SDL_Swap64(X)	(X)
   1.124 -#endif
   1.125  #endif /* SDL_HAS_64BIT_TYPE */
   1.126  
   1.127