2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 "@(#) $Id: SDL_endian.h,v 1.5 2002/04/11 14:35:13 slouken Exp $";
28 /* Functions for reading and writing endian-specific values */
33 /* These functions read and write data of the specified endianness,
34 dynamically translating to the host machine endianness.
36 e.g.: If you want to read a 16 bit value on big-endian machine from
37 an open file containing little endian values, you would use:
38 value = SDL_ReadLE16(rp);
39 Note that the read/write functions use SDL_RWops pointers
40 instead of FILE pointers. This allows you to read and write
41 endian values from large chunks of memory as well as files
42 and other data sources.
47 #include "SDL_types.h"
48 #include "SDL_rwops.h"
49 #include "SDL_byteorder.h"
51 #include "begin_code.h"
52 /* Set up for C function definitions, even when using C++ */
57 /* The macros used to swap values */
58 /* Try to use superfast macros on systems that support them */
60 #include <asm/byteorder.h>
62 #define SDL_Swap16 __arch__swab16
65 #define SDL_Swap32 __arch__swab32
68 /* Use inline functions for compilers that support them, and static
69 functions for those that do not. Because these functions become
70 static for compilers that do not support inline functions, this
71 header should only be included in files that actually use them.
74 static __inline__ Uint16 SDL_Swap16(Uint16 D) {
75 return((D<<8)|(D>>8));
79 static __inline__ Uint32 SDL_Swap32(Uint32 D) {
80 return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
83 #ifdef SDL_HAS_64BIT_TYPE
85 static __inline__ Uint64 SDL_Swap64(Uint64 val) {
88 /* Separate into high and low 32-bit values and swap them */
89 lo = (Uint32)(val&0xFFFFFFFF);
91 hi = (Uint32)(val&0xFFFFFFFF);
94 val |= SDL_Swap32(hi);
100 /* This is mainly to keep compilers from complaining in SDL code.
101 If there is no real 64-bit datatype, then compilers will complain about
102 the fake 64-bit datatype that SDL provides when it compiles user code.
104 #define SDL_Swap64(X) (X)
106 #endif /* SDL_HAS_64BIT_TYPE */
109 /* Byteswap item from the specified endianness to the native endianness */
110 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
111 #define SDL_SwapLE16(X) (X)
112 #define SDL_SwapLE32(X) (X)
113 #define SDL_SwapLE64(X) (X)
114 #define SDL_SwapBE16(X) SDL_Swap16(X)
115 #define SDL_SwapBE32(X) SDL_Swap32(X)
116 #define SDL_SwapBE64(X) SDL_Swap64(X)
118 #define SDL_SwapLE16(X) SDL_Swap16(X)
119 #define SDL_SwapLE32(X) SDL_Swap32(X)
120 #define SDL_SwapLE64(X) SDL_Swap64(X)
121 #define SDL_SwapBE16(X) (X)
122 #define SDL_SwapBE32(X) (X)
123 #define SDL_SwapBE64(X) (X)
126 /* Read an item of the specified endianness and return in native format */
127 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
128 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
129 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
130 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
131 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
132 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
134 /* Write an item of native format to the specified endianness */
135 extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
136 extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
137 extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
138 extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
139 extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
140 extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
143 /* Ends C function definitions when using C++ */
147 #include "close_code.h"
149 #endif /* _SDL_endian_h */