tune the preloads a bit
[sdl_omap.git] / include / SDL_endian.h
CommitLineData
e14743d1 1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
4
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.
9
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.
14
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
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22
23/**
24 * @file SDL_endian.h
25 * Functions for reading and writing endian-specific values
26 */
27
28#ifndef _SDL_endian_h
29#define _SDL_endian_h
30
31#include "SDL_stdinc.h"
32
33/** @name SDL_ENDIANs
34 * The two types of endianness
35 */
36/*@{*/
37#define SDL_LIL_ENDIAN 1234
38#define SDL_BIG_ENDIAN 4321
39/*@}*/
40
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) || \
46 defined(__sparc__)
47#define SDL_BYTEORDER SDL_BIG_ENDIAN
48#else
49#define SDL_BYTEORDER SDL_LIL_ENDIAN
50#endif
51#endif /* !SDL_BYTEORDER */
52
53
54#include "begin_code.h"
55/* Set up for C function definitions, even when using C++ */
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/**
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.
66 */
67/*@{*/
68#if defined(__GNUC__) && defined(__i386__) && \
69 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
70static __inline__ Uint16 SDL_Swap16(Uint16 x)
71{
72 __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
73 return x;
74}
75#elif defined(__GNUC__) && defined(__x86_64__)
76static __inline__ Uint16 SDL_Swap16(Uint16 x)
77{
78 __asm__("xchgb %b0,%h0" : "=Q" (x) : "0" (x));
79 return x;
80}
81#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
82static __inline__ Uint16 SDL_Swap16(Uint16 x)
83{
84 Uint16 result;
85
86 __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
87 return result;
88}
89#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
90static __inline__ Uint16 SDL_Swap16(Uint16 x)
91{
92 __asm__("rorw #8,%0" : "=d" (x) : "0" (x) : "cc");
93 return x;
94}
95#else
96static __inline__ Uint16 SDL_Swap16(Uint16 x) {
97 return((x<<8)|(x>>8));
98}
99#endif
100
101#if defined(__GNUC__) && defined(__i386__) && \
102 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
103static __inline__ Uint32 SDL_Swap32(Uint32 x)
104{
105 __asm__("bswap %0" : "=r" (x) : "0" (x));
106 return x;
107}
108#elif defined(__GNUC__) && defined(__x86_64__)
109static __inline__ Uint32 SDL_Swap32(Uint32 x)
110{
111 __asm__("bswapl %0" : "=r" (x) : "0" (x));
112 return x;
113}
114#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
115static __inline__ Uint32 SDL_Swap32(Uint32 x)
116{
117 Uint32 result;
118
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));
122 return result;
123}
124#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
125static __inline__ Uint32 SDL_Swap32(Uint32 x)
126{
127 __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) : "0" (x) : "cc");
128 return x;
129}
130#else
131static __inline__ Uint32 SDL_Swap32(Uint32 x) {
132 return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
133}
134#endif
135
136#ifdef SDL_HAS_64BIT_TYPE
137#if defined(__GNUC__) && defined(__i386__) && \
138 !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
139static __inline__ Uint64 SDL_Swap64(Uint64 x)
140{
141 union {
142 struct { Uint32 a,b; } s;
143 Uint64 u;
144 } v;
145 v.u = x;
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));
149 return v.u;
150}
151#elif defined(__GNUC__) && defined(__x86_64__)
152static __inline__ Uint64 SDL_Swap64(Uint64 x)
153{
154 __asm__("bswapq %0" : "=r" (x) : "0" (x));
155 return x;
156}
157#else
158static __inline__ Uint64 SDL_Swap64(Uint64 x)
159{
160 Uint32 hi, lo;
161
162 /* Separate into high and low 32-bit values and swap them */
163 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
164 x >>= 32;
165 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
166 x = SDL_Swap32(lo);
167 x <<= 32;
168 x |= SDL_Swap32(hi);
169 return(x);
170}
171#endif
172#else
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.
176 */
177#define SDL_Swap64(X) (X)
178#endif /* SDL_HAS_64BIT_TYPE */
179/*@}*/
180
181/**
182 * @name SDL_SwapLE and SDL_SwapBE Functions
183 * Byteswap item from the specified endianness to the native endianness
184 */
185/*@{*/
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)
193#else
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)
200#endif
201/*@}*/
202
203/* Ends C function definitions when using C++ */
204#ifdef __cplusplus
205}
206#endif
207#include "close_code.h"
208
209#endif /* _SDL_endian_h */