include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1044 d36ea7925763
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     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 <stdio.h>
    41 
    42 #include "SDL_types.h"
    43 #include "SDL_rwops.h"
    44 #include "SDL_byteorder.h"
    45 
    46 #include "begin_code.h"
    47 /* Set up for C function definitions, even when using C++ */
    48 #ifdef __cplusplus
    49 extern "C" {
    50 #endif
    51 
    52 /* Use inline functions for compilers that support them, and static
    53    functions for those that do not.  Because these functions become
    54    static for compilers that do not support inline functions, this
    55    header should only be included in files that actually use them.
    56 */
    57 #if defined(__GNUC__) && defined(__i386__)
    58 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    59 {
    60 	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
    61 	return x;
    62 }
    63 #elif defined(__GNUC__) && defined(__x86_64__)
    64 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    65 {
    66 	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
    67 	return x;
    68 }
    69 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
    70 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    71 {
    72 	Uint16 result;
    73 
    74 	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
    75 	return result;
    76 }
    77 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
    78 static __inline__ Uint16 SDL_Swap16(Uint16 x)
    79 {
    80 	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
    81 	return x;
    82 }
    83 #else
    84 static __inline__ Uint16 SDL_Swap16(Uint16 x) {
    85 	return((x<<8)|(x>>8));
    86 }
    87 #endif
    88 
    89 #if defined(__GNUC__) && defined(__i386__)
    90 static __inline__ Uint32 SDL_Swap32(Uint32 x)
    91 {
    92 	__asm__("bswap %0" : "=r" (x) : "0" (x));
    93 	return x;
    94 }
    95 #elif defined(__GNUC__) && defined(__x86_64__)
    96 static __inline__ Uint32 SDL_Swap32(Uint32 x)
    97 {
    98 	__asm__("bswapl %0" : "=r" (x) : "0" (x));
    99 	return x;
   100 }
   101 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
   102 static __inline__ Uint32 SDL_Swap32(Uint32 x)
   103 {
   104 	Uint32 result;
   105 
   106 	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
   107 	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
   108 	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
   109 	return result;
   110 }
   111 #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
   112 static __inline__ Uint32 SDL_Swap32(Uint32 x)
   113 {
   114 	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
   115 	return x;
   116 }
   117 #else
   118 static __inline__ Uint32 SDL_Swap32(Uint32 x) {
   119 	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
   120 }
   121 #endif
   122 
   123 #ifdef SDL_HAS_64BIT_TYPE
   124 #if defined(__GNUC__) && defined(__i386__)
   125 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   126 {
   127 	union { 
   128 		struct { Uint32 a,b; } s;
   129 		Uint64 u;
   130 	} v;
   131 	v.u = x;
   132 	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
   133 	        : "=r" (v.s.a), "=r" (v.s.b) 
   134 	        : "0" (v.s.a), "1" (v.s.b)); 
   135 	return v.u;
   136 }
   137 #elif defined(__GNUC__) && defined(__x86_64__)
   138 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   139 {
   140 	__asm__("bswapq %0" : "=r" (x) : "0" (x));
   141 	return x;
   142 }
   143 #else
   144 static __inline__ Uint64 SDL_Swap64(Uint64 x)
   145 {
   146 	Uint32 hi, lo;
   147 
   148 	/* Separate into high and low 32-bit values and swap them */
   149 	lo = (Uint32)(x&0xFFFFFFFF);
   150 	x >>= 32;
   151 	hi = (Uint32)(x&0xFFFFFFFF);
   152 	x = SDL_Swap32(lo);
   153 	x <<= 32;
   154 	x |= SDL_Swap32(hi);
   155 	return(x);
   156 }
   157 #endif
   158 #else
   159 /* This is mainly to keep compilers from complaining in SDL code.
   160    If there is no real 64-bit datatype, then compilers will complain about
   161    the fake 64-bit datatype that SDL provides when it compiles user code.
   162 */
   163 #define SDL_Swap64(X)	(X)
   164 #endif /* SDL_HAS_64BIT_TYPE */
   165 
   166 
   167 /* Byteswap item from the specified endianness to the native endianness */
   168 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   169 #define SDL_SwapLE16(X)	(X)
   170 #define SDL_SwapLE32(X)	(X)
   171 #define SDL_SwapLE64(X)	(X)
   172 #define SDL_SwapBE16(X)	SDL_Swap16(X)
   173 #define SDL_SwapBE32(X)	SDL_Swap32(X)
   174 #define SDL_SwapBE64(X)	SDL_Swap64(X)
   175 #else
   176 #define SDL_SwapLE16(X)	SDL_Swap16(X)
   177 #define SDL_SwapLE32(X)	SDL_Swap32(X)
   178 #define SDL_SwapLE64(X)	SDL_Swap64(X)
   179 #define SDL_SwapBE16(X)	(X)
   180 #define SDL_SwapBE32(X)	(X)
   181 #define SDL_SwapBE64(X)	(X)
   182 #endif
   183 
   184 /* Read an item of the specified endianness and return in native format */
   185 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
   186 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
   187 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
   188 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
   189 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
   190 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
   191 
   192 /* Write an item of native format to the specified endianness */
   193 extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
   194 extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
   195 extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
   196 extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
   197 extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
   198 extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
   199 
   200 
   201 /* Ends C function definitions when using C++ */
   202 #ifdef __cplusplus
   203 }
   204 #endif
   205 #include "close_code.h"
   206 
   207 #endif /* _SDL_endian_h */