include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:37:47 +0000
changeset 251 b8688cfdc232
parent 0 74212992fb08
child 297 f6ffac90895c
permissions -rw-r--r--
Updated the headers with the correct e-mail address
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     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.
     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     Library General Public License for more details.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Functions for reading and writing endian-specific values */
    29 
    30 #ifndef _SDL_endian_h
    31 #define _SDL_endian_h
    32 
    33 /* These functions read and write data of the specified endianness, 
    34    dynamically translating to the host machine endianness.
    35 
    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.
    43 */
    44 
    45 #include <stdio.h>
    46 
    47 #include "SDL_types.h"
    48 #include "SDL_rwops.h"
    49 #include "SDL_byteorder.h"
    50 
    51 #include "begin_code.h"
    52 /* Set up for C function definitions, even when using C++ */
    53 #ifdef __cplusplus
    54 extern "C" {
    55 #endif
    56 
    57 /* The macros used to swap values */
    58 /* Try to use superfast macros on systems that support them */
    59 #ifdef linux
    60 #include <asm/byteorder.h>
    61 #ifdef __arch__swab16
    62 #define SDL_Swap16  __arch__swab16
    63 #endif
    64 #ifdef __arch__swab32
    65 #define SDL_Swap32  __arch__swab32
    66 #endif
    67 #endif /* linux */
    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.
    72 */
    73 #ifndef SDL_Swap16
    74 static __inline__ Uint16 SDL_Swap16(Uint16 D) {
    75 	return((D<<8)|(D>>8));
    76 }
    77 #endif
    78 #ifndef SDL_Swap32
    79 static __inline__ Uint32 SDL_Swap32(Uint32 D) {
    80 	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
    81 }
    82 #endif
    83 #ifdef SDL_HAS_64BIT_TYPE
    84 #ifndef SDL_Swap64
    85 static __inline__ Uint64 SDL_Swap64(Uint64 val) {
    86 	Uint32 hi, lo;
    87 
    88 	/* Separate into high and low 32-bit values and swap them */
    89 	lo = (Uint32)(val&0xFFFFFFFF);
    90 	val >>= 32;
    91 	hi = (Uint32)(val&0xFFFFFFFF);
    92 	val = SDL_Swap32(lo);
    93 	val <<= 32;
    94 	val |= SDL_Swap32(hi);
    95 	return(val);
    96 }
    97 #endif
    98 #else
    99 #ifndef SDL_Swap64
   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.
   103 */
   104 #define SDL_Swap64(X)	(X)
   105 #endif
   106 #endif /* SDL_HAS_64BIT_TYPE */
   107 
   108 
   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)
   117 #else
   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)
   124 #endif
   125 
   126 /* Read an item of the specified endianness and return in native format */
   127 extern DECLSPEC Uint16 SDL_ReadLE16(SDL_RWops *src);
   128 extern DECLSPEC Uint16 SDL_ReadBE16(SDL_RWops *src);
   129 extern DECLSPEC Uint32 SDL_ReadLE32(SDL_RWops *src);
   130 extern DECLSPEC Uint32 SDL_ReadBE32(SDL_RWops *src);
   131 extern DECLSPEC Uint64 SDL_ReadLE64(SDL_RWops *src);
   132 extern DECLSPEC Uint64 SDL_ReadBE64(SDL_RWops *src);
   133 
   134 /* Write an item of native format to the specified endianness */
   135 extern DECLSPEC int SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
   136 extern DECLSPEC int SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
   137 extern DECLSPEC int SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
   138 extern DECLSPEC int SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
   139 extern DECLSPEC int SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
   140 extern DECLSPEC int SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
   141 
   142 
   143 /* Ends C function definitions when using C++ */
   144 #ifdef __cplusplus
   145 }
   146 #endif
   147 #include "close_code.h"
   148 
   149 #endif /* _SDL_endian_h */