include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 20 Aug 2004 22:32:05 +0000
changeset 932 66761191fc11
parent 859 6b28c91bf3d2
child 985 cec525374267
permissions -rw-r--r--
Use the canonical glext.h on MacOS X as well (#define NO_SDL_GLEXT if you don't want this)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Functions for reading and writing endian-specific values */
slouken@0
    29
slouken@0
    30
#ifndef _SDL_endian_h
slouken@0
    31
#define _SDL_endian_h
slouken@0
    32
slouken@0
    33
/* These functions read and write data of the specified endianness, 
slouken@0
    34
   dynamically translating to the host machine endianness.
slouken@0
    35
slouken@0
    36
   e.g.: If you want to read a 16 bit value on big-endian machine from
slouken@0
    37
         an open file containing little endian values, you would use:
slouken@0
    38
		value = SDL_ReadLE16(rp);
slouken@0
    39
         Note that the read/write functions use SDL_RWops pointers
slouken@0
    40
         instead of FILE pointers.  This allows you to read and write
slouken@0
    41
         endian values from large chunks of memory as well as files 
slouken@0
    42
         and other data sources.
slouken@0
    43
*/
slouken@0
    44
slouken@0
    45
#include <stdio.h>
slouken@0
    46
slouken@0
    47
#include "SDL_types.h"
slouken@0
    48
#include "SDL_rwops.h"
slouken@0
    49
#include "SDL_byteorder.h"
slouken@0
    50
slouken@0
    51
#include "begin_code.h"
slouken@0
    52
/* Set up for C function definitions, even when using C++ */
slouken@0
    53
#ifdef __cplusplus
slouken@0
    54
extern "C" {
slouken@0
    55
#endif
slouken@0
    56
slouken@0
    57
/* Use inline functions for compilers that support them, and static
slouken@0
    58
   functions for those that do not.  Because these functions become
slouken@0
    59
   static for compilers that do not support inline functions, this
slouken@0
    60
   header should only be included in files that actually use them.
slouken@0
    61
*/
slouken@849
    62
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    63
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    64
{
slouken@849
    65
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    66
	return x;
slouken@849
    67
}
slouken@849
    68
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    69
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    70
{
slouken@849
    71
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    72
	return x;
slouken@849
    73
}
slouken@859
    74
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    75
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    76
{
slouken@849
    77
	Uint16 result;
slouken@849
    78
slouken@849
    79
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    80
	return result;
slouken@848
    81
}
slouken@848
    82
#else
slouken@849
    83
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    84
	return((x<<8)|(x>>8));
slouken@0
    85
}
slouken@0
    86
#endif
slouken@848
    87
slouken@849
    88
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    89
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    90
{
slouken@849
    91
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    92
	return x;
slouken@849
    93
}
slouken@849
    94
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    95
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
    96
{
slouken@849
    97
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
    98
	return x;
slouken@849
    99
}
slouken@859
   100
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   101
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   102
{
slouken@849
   103
	Uint32 result;
slouken@849
   104
slouken@849
   105
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   106
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   107
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   108
	return result;
slouken@848
   109
}
slouken@848
   110
#else
slouken@849
   111
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   112
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   113
}
slouken@0
   114
#endif
slouken@848
   115
slouken@0
   116
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   117
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   118
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   119
{
slouken@849
   120
	union { 
slouken@849
   121
		struct { Uint32 a,b; } s;
slouken@849
   122
		Uint64 u;
slouken@849
   123
	} v;
slouken@849
   124
	v.u = x;
slouken@849
   125
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   126
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   127
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   128
	return v.u;
slouken@849
   129
}
slouken@849
   130
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   131
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   132
{
slouken@849
   133
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   134
	return x;
slouken@849
   135
}
slouken@849
   136
#else
slouken@849
   137
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   138
{
slouken@0
   139
	Uint32 hi, lo;
slouken@0
   140
slouken@0
   141
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   142
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   143
	x >>= 32;
slouken@849
   144
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   145
	x = SDL_Swap32(lo);
slouken@849
   146
	x <<= 32;
slouken@849
   147
	x |= SDL_Swap32(hi);
slouken@849
   148
	return(x);
slouken@0
   149
}
slouken@0
   150
#endif
slouken@0
   151
#else
slouken@0
   152
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   153
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   154
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   155
*/
slouken@0
   156
#define SDL_Swap64(X)	(X)
slouken@0
   157
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   158
slouken@0
   159
slouken@0
   160
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   161
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   162
#define SDL_SwapLE16(X)	(X)
slouken@0
   163
#define SDL_SwapLE32(X)	(X)
slouken@0
   164
#define SDL_SwapLE64(X)	(X)
slouken@0
   165
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   166
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   167
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   168
#else
slouken@0
   169
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   170
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   171
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   172
#define SDL_SwapBE16(X)	(X)
slouken@0
   173
#define SDL_SwapBE32(X)	(X)
slouken@0
   174
#define SDL_SwapBE64(X)	(X)
slouken@0
   175
#endif
slouken@0
   176
slouken@0
   177
/* Read an item of the specified endianness and return in native format */
slouken@337
   178
extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
slouken@337
   179
extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
slouken@337
   180
extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
slouken@337
   181
extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
slouken@337
   182
extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
slouken@337
   183
extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
slouken@0
   184
slouken@0
   185
/* Write an item of native format to the specified endianness */
slouken@337
   186
extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
slouken@337
   187
extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
slouken@337
   188
extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
slouken@337
   189
extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
slouken@337
   190
extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
slouken@337
   191
extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
slouken@0
   192
slouken@0
   193
slouken@0
   194
/* Ends C function definitions when using C++ */
slouken@0
   195
#ifdef __cplusplus
slouken@0
   196
}
slouken@0
   197
#endif
slouken@0
   198
#include "close_code.h"
slouken@0
   199
slouken@0
   200
#endif /* _SDL_endian_h */