include/SDL_endian.h
changeset 0 74212992fb08
child 251 b8688cfdc232
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/SDL_endian.h	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,149 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Functions for reading and writing endian-specific values */
    1.32 +
    1.33 +#ifndef _SDL_endian_h
    1.34 +#define _SDL_endian_h
    1.35 +
    1.36 +/* These functions read and write data of the specified endianness, 
    1.37 +   dynamically translating to the host machine endianness.
    1.38 +
    1.39 +   e.g.: If you want to read a 16 bit value on big-endian machine from
    1.40 +         an open file containing little endian values, you would use:
    1.41 +		value = SDL_ReadLE16(rp);
    1.42 +         Note that the read/write functions use SDL_RWops pointers
    1.43 +         instead of FILE pointers.  This allows you to read and write
    1.44 +         endian values from large chunks of memory as well as files 
    1.45 +         and other data sources.
    1.46 +*/
    1.47 +
    1.48 +#include <stdio.h>
    1.49 +
    1.50 +#include "SDL_types.h"
    1.51 +#include "SDL_rwops.h"
    1.52 +#include "SDL_byteorder.h"
    1.53 +
    1.54 +#include "begin_code.h"
    1.55 +/* Set up for C function definitions, even when using C++ */
    1.56 +#ifdef __cplusplus
    1.57 +extern "C" {
    1.58 +#endif
    1.59 +
    1.60 +/* The macros used to swap values */
    1.61 +/* Try to use superfast macros on systems that support them */
    1.62 +#ifdef linux
    1.63 +#include <asm/byteorder.h>
    1.64 +#ifdef __arch__swab16
    1.65 +#define SDL_Swap16  __arch__swab16
    1.66 +#endif
    1.67 +#ifdef __arch__swab32
    1.68 +#define SDL_Swap32  __arch__swab32
    1.69 +#endif
    1.70 +#endif /* linux */
    1.71 +/* Use inline functions for compilers that support them, and static
    1.72 +   functions for those that do not.  Because these functions become
    1.73 +   static for compilers that do not support inline functions, this
    1.74 +   header should only be included in files that actually use them.
    1.75 +*/
    1.76 +#ifndef SDL_Swap16
    1.77 +static __inline__ Uint16 SDL_Swap16(Uint16 D) {
    1.78 +	return((D<<8)|(D>>8));
    1.79 +}
    1.80 +#endif
    1.81 +#ifndef SDL_Swap32
    1.82 +static __inline__ Uint32 SDL_Swap32(Uint32 D) {
    1.83 +	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
    1.84 +}
    1.85 +#endif
    1.86 +#ifdef SDL_HAS_64BIT_TYPE
    1.87 +#ifndef SDL_Swap64
    1.88 +static __inline__ Uint64 SDL_Swap64(Uint64 val) {
    1.89 +	Uint32 hi, lo;
    1.90 +
    1.91 +	/* Separate into high and low 32-bit values and swap them */
    1.92 +	lo = (Uint32)(val&0xFFFFFFFF);
    1.93 +	val >>= 32;
    1.94 +	hi = (Uint32)(val&0xFFFFFFFF);
    1.95 +	val = SDL_Swap32(lo);
    1.96 +	val <<= 32;
    1.97 +	val |= SDL_Swap32(hi);
    1.98 +	return(val);
    1.99 +}
   1.100 +#endif
   1.101 +#else
   1.102 +#ifndef SDL_Swap64
   1.103 +/* This is mainly to keep compilers from complaining in SDL code.
   1.104 +   If there is no real 64-bit datatype, then compilers will complain about
   1.105 +   the fake 64-bit datatype that SDL provides when it compiles user code.
   1.106 +*/
   1.107 +#define SDL_Swap64(X)	(X)
   1.108 +#endif
   1.109 +#endif /* SDL_HAS_64BIT_TYPE */
   1.110 +
   1.111 +
   1.112 +/* Byteswap item from the specified endianness to the native endianness */
   1.113 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.114 +#define SDL_SwapLE16(X)	(X)
   1.115 +#define SDL_SwapLE32(X)	(X)
   1.116 +#define SDL_SwapLE64(X)	(X)
   1.117 +#define SDL_SwapBE16(X)	SDL_Swap16(X)
   1.118 +#define SDL_SwapBE32(X)	SDL_Swap32(X)
   1.119 +#define SDL_SwapBE64(X)	SDL_Swap64(X)
   1.120 +#else
   1.121 +#define SDL_SwapLE16(X)	SDL_Swap16(X)
   1.122 +#define SDL_SwapLE32(X)	SDL_Swap32(X)
   1.123 +#define SDL_SwapLE64(X)	SDL_Swap64(X)
   1.124 +#define SDL_SwapBE16(X)	(X)
   1.125 +#define SDL_SwapBE32(X)	(X)
   1.126 +#define SDL_SwapBE64(X)	(X)
   1.127 +#endif
   1.128 +
   1.129 +/* Read an item of the specified endianness and return in native format */
   1.130 +extern DECLSPEC Uint16 SDL_ReadLE16(SDL_RWops *src);
   1.131 +extern DECLSPEC Uint16 SDL_ReadBE16(SDL_RWops *src);
   1.132 +extern DECLSPEC Uint32 SDL_ReadLE32(SDL_RWops *src);
   1.133 +extern DECLSPEC Uint32 SDL_ReadBE32(SDL_RWops *src);
   1.134 +extern DECLSPEC Uint64 SDL_ReadLE64(SDL_RWops *src);
   1.135 +extern DECLSPEC Uint64 SDL_ReadBE64(SDL_RWops *src);
   1.136 +
   1.137 +/* Write an item of native format to the specified endianness */
   1.138 +extern DECLSPEC int SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
   1.139 +extern DECLSPEC int SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
   1.140 +extern DECLSPEC int SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
   1.141 +extern DECLSPEC int SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
   1.142 +extern DECLSPEC int SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
   1.143 +extern DECLSPEC int SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
   1.144 +
   1.145 +
   1.146 +/* Ends C function definitions when using C++ */
   1.147 +#ifdef __cplusplus
   1.148 +}
   1.149 +#endif
   1.150 +#include "close_code.h"
   1.151 +
   1.152 +#endif /* _SDL_endian_h */