comparison Isolated/LGPL/SDL_endian_minimal.h @ 68:492239f6ffc9

Changed SDL_endian_minimal.h to copy from SDL 1.3 which is under the zlib license instead of LGPL. Thanks to Johnson Lin for contributing this. I made a few changes. It seems the Android stuff will hit the __linux define so I could remove the Android specific case. I also needed to copy the SDL_static_cast (and SDL_reinterpret_cast) from SDL_stdinc.h because some of the macros depend on it. Running this dumps the #defines for me: /usr/local/bin/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-cpp -dM /dev/null (Instructions found from http://annoyingtechnicaldetails.wordpress.com/2012/02/01/which-macros-does-the-android-ndk-gcc-define/) Johnson Lin < arch . jslin - at - gmail . com >
author Eric Wing <ewing . public |-at-| gmail . com>
date Wed, 20 Jun 2012 10:30:47 -0700
parents 71b465ff0622
children
comparison
equal deleted inserted replaced
67:96c61ac12446 68:492239f6ffc9
1 /* 1 /*
2 SDL - Simple DirectMedia Layer 2 Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga 3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This software is provided 'as-is', without any express or implied
6 modify it under the terms of the GNU Lesser General Public 6 warranty. In no event will the authors be held liable for any damages
7 License as published by the Free Software Foundation; either 7 arising from the use of this software.
8 version 2.1 of the License, or (at your option) any later version. 8
9 9 Permission is granted to anyone to use this software for any purpose,
10 This library is distributed in the hope that it will be useful, 10 including commercial applications, and to alter it and redistribute it
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 freely, subject to the following restrictions:
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12
13 Lesser General Public License for more details. 13 1. The origin of this software must not be misrepresented; you must not
14 14 claim that you wrote the original software. If you use this software
15 You should have received a copy of the GNU Lesser General Public 15 in a product, an acknowledgment in the product documentation would be
16 License along with this library; if not, write to the Free Software 16 appreciated but is not required.
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 2. Altered source versions must be plainly marked as such, and must not be
18 18 misrepresented as being the original software.
19 Sam Lantinga 19 3. This notice may not be removed or altered from any source distribution.
20 slouken@libsdl.org 20 */
21 */ 21
22 22 /**
23 /* 23 * \file SDL_endian.h
24 Attention: This is a stripped down file of SDL_endian for our purposes. 24 *
25 This code is licensed under the LGPL. 25 * Functions for reading and writing endian-specific values
26 This means we must not compile this code into anything that we are not willing to 26 */
27 publicly release source code. 27
28 You should compile this into a separate dynamic library that is isolated from proprietary code. 28 /*
29 Attention: This is a stripped down file of SDL_endian.h for our purposes.
30 SDL_static_cast and SDL_reinterpret_cast come from SDL_stdinc.h.
29 */ 31 */
30 32
31 33
32 #ifndef SDL_endian_minimal_h 34 #ifndef SDL_endian_minimal_h
33 #define SDL_endian_minimal_h 35 #define SDL_endian_minimal_h
34 36
35 #ifdef ANDROID_NDK 37
36 #include <endian.h> 38 /**
37 39 * \name Cast operators
38 #define SDL_BYTEORDER _BYTE_ORDER 40 *
39 #define SDL_BIG_ENDIAN _BIG_ENDIAN 41 * Use proper C++ casts when compiled as C++ to be compatible with the option
40 #define SDL_LITTLE_ENDIAN _LITTLE_ENDIAN 42 * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
41 #endif 43 */
44 /*@{*/
45 #ifdef __cplusplus
46 #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
47 #define SDL_static_cast(type, expression) static_cast<type>(expression)
48 #else
49 #define SDL_reinterpret_cast(type, expression) ((type)(expression))
50 #define SDL_static_cast(type, expression) ((type)(expression))
51 #endif
52 /*@}*//*Cast operators*/
53
54
55
56 /**
57 * \name The two types of endianness
58 */
59 /*@{*/
60 #define SDL_LIL_ENDIAN 1234
61 #define SDL_BIG_ENDIAN 4321
62 /*@}*/
63
64 #ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
65 #ifdef __linux__
66 #include <endian.h>
67 #define SDL_BYTEORDER __BYTE_ORDER
68 #else /* __linux __ */
69 #if defined(__hppa__) || \
70 defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
71 (defined(__MIPS__) && defined(__MISPEB__)) || \
72 defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
73 defined(__sparc__)
74
75 #define SDL_BYTEORDER SDL_BIG_ENDIAN
76 #else
77 #define SDL_BYTEORDER SDL_LIL_ENDIAN
78 #endif
79 #endif /* __linux __ */
80 #endif /* !SDL_BYTEORDER */
81
42 82
43 #include <stdint.h> 83 #include <stdint.h>
44 84
45 85 /**
86 * \file SDL_endian.h
87 *
88 * Uses inline functions for compilers that support them, and static
89 * functions for those that do not. Because these functions become
90 * static for compilers that do not support inline functions, this
91 * header should only be included in files that actually use them.
92 */
46 #if defined(__GNUC__) && defined(__i386__) && \ 93 #if defined(__GNUC__) && defined(__i386__) && \
47 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */) 94 !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
48 static __inline__ uint32_t SDL_Swap32(uint32_t x) 95 static __inline__ uint16_t
49 { 96 SDL_Swap16(uint16_t x)
50 __asm__("bswap %0" : "=r" (x) : "0" (x)); 97 {
51 return x; 98 __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
99 return x;
52 } 100 }
53 #elif defined(__GNUC__) && defined(__x86_64__) 101 #elif defined(__GNUC__) && defined(__x86_64__)
54 static __inline__ uint32_t SDL_Swap32(uint32_t x) 102 static __inline__ uint16_t
55 { 103 SDL_Swap16(uint16_t x)
56 __asm__("bswapl %0" : "=r" (x) : "0" (x)); 104 {
57 return x; 105 __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
106 return x;
58 } 107 }
59 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__)) 108 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
60 static __inline__ uint32_t SDL_Swap32(uint32_t x) 109 static __inline__ uint16_t
61 { 110 SDL_Swap16(uint16_t x)
62 uint32_t result; 111 {
63 112 int result;
64 __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x)); 113
65 __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x)); 114 __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
66 __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x)); 115 return (uint16_t)result;
67 return result; 116 }
68 } 117 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
69 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) 118 static __inline__ uint16_t
70 static __inline__ uint32_t SDL_Swap32(uint32_t x) 119 SDL_Swap16(uint16_t x)
71 { 120 {
72 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) : "0" (x) : "cc"); 121 __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
73 return x; 122 return x;
74 } 123 }
75 #else 124 #else
76 static __inline__ uint32_t SDL_Swap32(uint32_t x) { 125 static __inline__ uint16_t
77 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24)); 126 SDL_Swap16(uint16_t x)
78 } 127 {
79 #endif 128 return SDL_static_cast(uint16_t, ((x << 8) | (x >> 8)));
80 129 }
81 130 #endif
82 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 131
132 #if defined(__GNUC__) && defined(__i386__)
133 static __inline__ uint32_t
134 SDL_Swap32(uint32_t x)
135 {
136 __asm__("bswap %0": "=r"(x):"0"(x));
137 return x;
138 }
139 #elif defined(__GNUC__) && defined(__x86_64__)
140 static __inline__ uint32_t
141 SDL_Swap32(uint32_t x)
142 {
143 __asm__("bswapl %0": "=r"(x):"0"(x));
144 return x;
145 }
146 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
147 static __inline__ uint32_t
148 SDL_Swap32(uint32_t x)
149 {
150 uint32_t result;
151
152 __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
153 __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
154 __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
155 return result;
156 }
157 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
158 static __inline__ uint32_t
159 SDL_Swap32(uint32_t x)
160 {
161 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
162 return x;
163 }
164 #else
165 static __inline__ uint32_t
166 SDL_Swap32(uint32_t x)
167 {
168 return SDL_static_cast(uint32_t, ((x << 24) | ((x << 8) & 0x00FF0000) |
169 ((x >> 8) & 0x0000FF00) | (x >> 24)));
170 }
171 #endif
172
173 #if defined(__GNUC__) && defined(__i386__)
174 static __inline__ uint64_t
175 SDL_Swap64(uint64_t x)
176 {
177 union
178 {
179 struct
180 {
181 uint32_t a, b;
182 } s;
183 uint64_t u;
184 } v;
185 v.u = x;
186 __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
187 "1"(v.s.
188 b));
189 return v.u;
190 }
191 #elif defined(__GNUC__) && defined(__x86_64__)
192 static __inline__ uint64_t
193 SDL_Swap64(uint64_t x)
194 {
195 __asm__("bswapq %0": "=r"(x):"0"(x));
196 return x;
197 }
198 #else
199 static __inline__ uint64_t
200 SDL_Swap64(uint64_t x)
201 {
202 uint32_t hi, lo;
203
204 /* Separate into high and low 32-bit values and swap them */
205 lo = SDL_static_cast(uint32_t, x & 0xFFFFFFFF);
206 x >>= 32;
207 hi = SDL_static_cast(uint32_t, x & 0xFFFFFFFF);
208 x = SDL_Swap32(lo);
209 x <<= 32;
210 x |= SDL_Swap32(hi);
211 return (x);
212 }
213 #endif
214
215
216 static __inline__ float
217 SDL_SwapFloat(float x)
218 {
219 union
220 {
221 float f;
222 uint32_t ui32;
223 } swapper;
224 swapper.f = x;
225 swapper.ui32 = SDL_Swap32(swapper.ui32);
226 return swapper.f;
227 }
228
229
230 /**
231 * \name Swap to native
232 * Byteswap item from the specified endianness to the native endianness.
233 */
234 /*@{*/
235 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
236 #define SDL_SwapLE16(X) (X)
237 #define SDL_SwapLE32(X) (X)
238 #define SDL_SwapLE64(X) (X)
239 #define SDL_SwapFloatLE(X) (X)
240 #define SDL_SwapBE16(X) SDL_Swap16(X)
241 #define SDL_SwapBE32(X) SDL_Swap32(X)
242 #define SDL_SwapBE64(X) SDL_Swap64(X)
243 #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
244 #else
83 #define SDL_SwapLE16(X) SDL_Swap16(X) 245 #define SDL_SwapLE16(X) SDL_Swap16(X)
84 #define SDL_SwapLE32(X) SDL_Swap32(X) 246 #define SDL_SwapLE32(X) SDL_Swap32(X)
85 #define SDL_SwapLE64(X) SDL_Swap64(X) 247 #define SDL_SwapLE64(X) SDL_Swap64(X)
248 #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
86 #define SDL_SwapBE16(X) (X) 249 #define SDL_SwapBE16(X) (X)
87 #define SDL_SwapBE32(X) (X) 250 #define SDL_SwapBE32(X) (X)
88 #define SDL_SwapBE64(X) (X) 251 #define SDL_SwapBE64(X) (X)
89 #else 252 #define SDL_SwapFloatBE(X) (X)
90 #define SDL_SwapLE16(X) (X) 253 #endif
91 #define SDL_SwapLE32(X) (X) 254 /*@}*//*Swap to native*/
92 #define SDL_SwapLE64(X) (X)
93 #define SDL_SwapBE16(X) SDL_Swap16(X)
94 #define SDL_SwapBE32(X) SDL_Swap32(X)
95 #define SDL_SwapBE64(X) SDL_Swap64(X)
96 #endif
97 255
98 256
99 #endif // SDL_endian_minimal_h 257 #endif // SDL_endian_minimal_h