comparison include/SDL_endian.h @ 849:bab227101de4

Added inline byte swapping code for other architectures
author Sam Lantinga <slouken@libsdl.org>
date Mon, 23 Feb 2004 06:09:21 +0000
parents 85af65457959
children 6b28c91bf3d2
comparison
equal deleted inserted replaced
848:85af65457959 849:bab227101de4
57 /* Use inline functions for compilers that support them, and static 57 /* Use inline functions for compilers that support them, and static
58 functions for those that do not. Because these functions become 58 functions for those that do not. Because these functions become
59 static for compilers that do not support inline functions, this 59 static for compilers that do not support inline functions, this
60 header should only be included in files that actually use them. 60 header should only be included in files that actually use them.
61 */ 61 */
62 #if defined(__GNUC__) && defined(i386) 62 #if defined(__GNUC__) && defined(__i386__)
63 static __inline__ Uint16 SDL_Swap16(Uint16 D) 63 static __inline__ Uint16 SDL_Swap16(Uint16 x)
64 { 64 {
65 __asm__("xchgb %b0,%h0" : "=q" (D) : "0" (D)); 65 __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
66 return D; 66 return x;
67 } 67 }
68 #else 68 #elif defined(__GNUC__) && defined(__x86_64__)
69 static __inline__ Uint16 SDL_Swap16(Uint16 D) { 69 static __inline__ Uint16 SDL_Swap16(Uint16 x)
70 return((D<<8)|(D>>8)); 70 {
71 } 71 __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
72 #endif 72 return x;
73 73 }
74 #if defined(__GNUC__) && defined(i386) 74 #elif defined(__GNUC__) && defined(__powerpc__)
75 static __inline__ Uint32 SDL_Swap32(Uint32 D) 75 static __inline__ Uint16 SDL_Swap16(Uint16 x)
76 { 76 {
77 __asm__("bswap %0" : "=r" (D) : "0" (D)); 77 Uint16 result;
78 return D; 78
79 } 79 __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
80 #else 80 return result;
81 static __inline__ Uint32 SDL_Swap32(Uint32 D) { 81 }
82 return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24)); 82 #else
83 static __inline__ Uint16 SDL_Swap16(Uint16 x) {
84 return((x<<8)|(x>>8));
85 }
86 #endif
87
88 #if defined(__GNUC__) && defined(__i386__)
89 static __inline__ Uint32 SDL_Swap32(Uint32 x)
90 {
91 __asm__("bswap %0" : "=r" (x) : "0" (x));
92 return x;
93 }
94 #elif defined(__GNUC__) && defined(__x86_64__)
95 static __inline__ Uint32 SDL_Swap32(Uint32 x)
96 {
97 __asm__("bswapl %0" : "=r" (x) : "0" (x));
98 return x;
99 }
100 #elif defined(__GNUC__) && defined(__powerpc__)
101 static __inline__ Uint32 SDL_Swap32(Uint32 x)
102 {
103 Uint32 result;
104
105 __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
106 __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
107 __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
108 return result;
109 }
110 #else
111 static __inline__ Uint32 SDL_Swap32(Uint32 x) {
112 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
83 } 113 }
84 #endif 114 #endif
85 115
86 #ifdef SDL_HAS_64BIT_TYPE 116 #ifdef SDL_HAS_64BIT_TYPE
87 #ifndef SDL_Swap64 117 #if defined(__GNUC__) && defined(__i386__)
88 static __inline__ Uint64 SDL_Swap64(Uint64 val) { 118 static __inline__ Uint64 SDL_Swap64(Uint64 x)
119 {
120 union {
121 struct { Uint32 a,b; } s;
122 Uint64 u;
123 } v;
124 v.u = x;
125 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
126 : "=r" (v.s.a), "=r" (v.s.b)
127 : "0" (v.s.a), "1" (v.s.b));
128 return v.u;
129 }
130 #elif defined(__GNUC__) && defined(__x86_64__)
131 static __inline__ Uint64 SDL_Swap64(Uint64 x)
132 {
133 __asm__("bswapq %0" : "=r" (x) : "0" (x));
134 return x;
135 }
136 #else
137 static __inline__ Uint64 SDL_Swap64(Uint64 x)
138 {
89 Uint32 hi, lo; 139 Uint32 hi, lo;
90 140
91 /* Separate into high and low 32-bit values and swap them */ 141 /* Separate into high and low 32-bit values and swap them */
92 lo = (Uint32)(val&0xFFFFFFFF); 142 lo = (Uint32)(x&0xFFFFFFFF);
93 val >>= 32; 143 x >>= 32;
94 hi = (Uint32)(val&0xFFFFFFFF); 144 hi = (Uint32)(x&0xFFFFFFFF);
95 val = SDL_Swap32(lo); 145 x = SDL_Swap32(lo);
96 val <<= 32; 146 x <<= 32;
97 val |= SDL_Swap32(hi); 147 x |= SDL_Swap32(hi);
98 return(val); 148 return(x);
99 } 149 }
100 #endif 150 #endif
101 #else 151 #else
102 #ifndef SDL_Swap64
103 /* This is mainly to keep compilers from complaining in SDL code. 152 /* This is mainly to keep compilers from complaining in SDL code.
104 If there is no real 64-bit datatype, then compilers will complain about 153 If there is no real 64-bit datatype, then compilers will complain about
105 the fake 64-bit datatype that SDL provides when it compiles user code. 154 the fake 64-bit datatype that SDL provides when it compiles user code.
106 */ 155 */
107 #define SDL_Swap64(X) (X) 156 #define SDL_Swap64(X) (X)
108 #endif
109 #endif /* SDL_HAS_64BIT_TYPE */ 157 #endif /* SDL_HAS_64BIT_TYPE */
110 158
111 159
112 /* Byteswap item from the specified endianness to the native endianness */ 160 /* Byteswap item from the specified endianness to the native endianness */
113 #if SDL_BYTEORDER == SDL_LIL_ENDIAN 161 #if SDL_BYTEORDER == SDL_LIL_ENDIAN