include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Feb 2006 09:07:13 +0000
changeset 1353 7ba544e2888d
parent 1330 450721ad5436
child 1354 22f39393668a
permissions -rw-r--r--
Started the process of improving configure support, and merging C types
and library support into a single header.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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 /* Functions for reading and writing endian-specific values */
    24 
    25 #ifndef _SDL_endian_h
    26 #define _SDL_endian_h
    27 
    28 /* These functions read and write data of the specified endianness, 
    29    dynamically translating to the host machine endianness.
    30 
    31    e.g.: If you want to read a 16 bit value on big-endian machine from
    32          an open file containing little endian values, you would use:
    33 		value = SDL_ReadLE16(rp);
    34          Note that the read/write functions use SDL_RWops pointers
    35          instead of FILE pointers.  This allows you to read and write
    36          endian values from large chunks of memory as well as files 
    37          and other data sources.
    38 */
    39 
    40 #include "SDL_types.h"
    41 //#warning FIXME: move rwops into rwops and swapping into byteorder.h
    42 #include "SDL_rwops.h"
    43 #include "SDL_byteorder.h"
    44 
    45 #include "begin_code.h"
    46 /* Set up for C function definitions, even when using C++ */
    47 #ifdef __cplusplus
    48 extern "C" {
    49 #endif
    50 
    51 /* Use inline functions for compilers that support them, and static
    52    functions for those that do not.  Because these functions become
    53    static for compilers that do not support inline functions, this
    54    header should only be included in files that actually use them.
    55 */
    56 #if defined(__GNUC__) && defined(__i386__)
    57 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    58 {
    59 	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
    60 	return x;
    61 }
    62 #elif defined(__GNUC__) && defined(__x86_64__)
    63 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    64 {
    65 	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
    66 	return x;
    67 }
    68 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
    69 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    70 {
    71 	Uint16 result;
    72 
    73 	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
    74 	return result;
    75 }
    76 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
    77 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    78 {
    79 	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
    80 	return x;
    81 }
    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__) || defined(__ppc__))
   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 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
   111 static __inline__ Uint32 SDL_Swap32(Uint32 x)
   112 {
   113 	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
   114 	return x;
   115 }
   116 #else
   117 static __inline__ Uint32 SDL_Swap32(Uint32 x) {
   118 	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
   119 }
   120 #endif
   121 
   122 #ifdef SDL_HAS_64BIT_TYPE
   123 #if defined(__GNUC__) && defined(__i386__)
   124 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   125 {
   126 	union { 
   127 		struct { Uint32 a,b; } s;
   128 		Uint64 u;
   129 	} v;
   130 	v.u = x;
   131 	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
   132 	        : "=r" (v.s.a), "=r" (v.s.b) 
   133 	        : "0" (v.s.a), "1" (v.s.b)); 
   134 	return v.u;
   135 }
   136 #elif defined(__GNUC__) && defined(__x86_64__)
   137 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   138 {
   139 	__asm__("bswapq %0" : "=r" (x) : "0" (x));
   140 	return x;
   141 }
   142 #else
   143 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   144 {
   145 	Uint32 hi, lo;
   146 
   147 	/* Separate into high and low 32-bit values and swap them */
   148 	lo = (Uint32)(x&0xFFFFFFFF);
   149 	x >>= 32;
   150 	hi = (Uint32)(x&0xFFFFFFFF);
   151 	x = SDL_Swap32(lo);
   152 	x <<= 32;
   153 	x |= SDL_Swap32(hi);
   154 	return(x);
   155 }
   156 #endif
   157 #else
   158 /* This is mainly to keep compilers from complaining in SDL code.
   159    If there is no real 64-bit datatype, then compilers will complain about
   160    the fake 64-bit datatype that SDL provides when it compiles user code.
   161 */
   162 #define SDL_Swap64(X)	(X)
   163 #endif /* SDL_HAS_64BIT_TYPE */
   164 
   165 
   166 /* Byteswap item from the specified endianness to the native endianness */
   167 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   168 #define SDL_SwapLE16(X)	(X)
   169 #define SDL_SwapLE32(X)	(X)
   170 #define SDL_SwapLE64(X)	(X)
   171 #define SDL_SwapBE16(X)	SDL_Swap16(X)
   172 #define SDL_SwapBE32(X)	SDL_Swap32(X)
   173 #define SDL_SwapBE64(X)	SDL_Swap64(X)
   174 #else
   175 #define SDL_SwapLE16(X)	SDL_Swap16(X)
   176 #define SDL_SwapLE32(X)	SDL_Swap32(X)
   177 #define SDL_SwapLE64(X)	SDL_Swap64(X)
   178 #define SDL_SwapBE16(X)	(X)
   179 #define SDL_SwapBE32(X)	(X)
   180 #define SDL_SwapBE64(X)	(X)
   181 #endif
   182 
   183 /* Read an item of the specified endianness and return in native format */
   184 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
   185 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
   186 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
   187 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
   188 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
   189 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
   190 
   191 /* Write an item of native format to the specified endianness */
   192 extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
   193 extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
   194 extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
   195 extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
   196 extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
   197 extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
   198 
   199 
   200 /* Ends C function definitions when using C++ */
   201 #ifdef __cplusplus
   202 }
   203 #endif
   204 #include "close_code.h"
   205 
   206 #endif /* _SDL_endian_h */