include/SDL_endian.h
author Patrice Mandin <patmandin@gmail.com>
Wed, 30 Mar 2005 12:38:03 +0000
changeset 1044 d36ea7925763
parent 1033 aebfa3ce2a53
child 1312 c9b51268668f
permissions -rw-r--r--
Optimize also for 68020 and higher CPUs
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@1033
    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
}
patmandin@1044
    82
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    83
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    84
{
patmandin@985
    85
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    86
	return x;
patmandin@985
    87
}
slouken@848
    88
#else
slouken@849
    89
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    90
	return((x<<8)|(x>>8));
slouken@0
    91
}
slouken@0
    92
#endif
slouken@848
    93
slouken@849
    94
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    95
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    96
{
slouken@849
    97
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    98
	return x;
slouken@849
    99
}
slouken@849
   100
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   101
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   102
{
slouken@849
   103
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   104
	return x;
slouken@849
   105
}
slouken@859
   106
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   107
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   108
{
slouken@849
   109
	Uint32 result;
slouken@849
   110
slouken@849
   111
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   112
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   113
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   114
	return result;
slouken@848
   115
}
patmandin@1044
   116
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   117
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   118
{
patmandin@1044
   119
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   120
	return x;
patmandin@985
   121
}
slouken@848
   122
#else
slouken@849
   123
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   124
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   125
}
slouken@0
   126
#endif
slouken@848
   127
slouken@0
   128
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   129
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   130
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   131
{
slouken@849
   132
	union { 
slouken@849
   133
		struct { Uint32 a,b; } s;
slouken@849
   134
		Uint64 u;
slouken@849
   135
	} v;
slouken@849
   136
	v.u = x;
slouken@849
   137
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   138
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   139
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   140
	return v.u;
slouken@849
   141
}
slouken@849
   142
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   143
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   144
{
slouken@849
   145
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   146
	return x;
slouken@849
   147
}
slouken@849
   148
#else
slouken@849
   149
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   150
{
slouken@0
   151
	Uint32 hi, lo;
slouken@0
   152
slouken@0
   153
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   154
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   155
	x >>= 32;
slouken@849
   156
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   157
	x = SDL_Swap32(lo);
slouken@849
   158
	x <<= 32;
slouken@849
   159
	x |= SDL_Swap32(hi);
slouken@849
   160
	return(x);
slouken@0
   161
}
slouken@0
   162
#endif
slouken@0
   163
#else
slouken@0
   164
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   165
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   166
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   167
*/
slouken@0
   168
#define SDL_Swap64(X)	(X)
slouken@0
   169
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   170
slouken@0
   171
slouken@0
   172
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   173
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   174
#define SDL_SwapLE16(X)	(X)
slouken@0
   175
#define SDL_SwapLE32(X)	(X)
slouken@0
   176
#define SDL_SwapLE64(X)	(X)
slouken@0
   177
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   178
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   179
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   180
#else
slouken@0
   181
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   182
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   183
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   184
#define SDL_SwapBE16(X)	(X)
slouken@0
   185
#define SDL_SwapBE32(X)	(X)
slouken@0
   186
#define SDL_SwapBE64(X)	(X)
slouken@0
   187
#endif
slouken@0
   188
slouken@0
   189
/* Read an item of the specified endianness and return in native format */
slouken@337
   190
extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
slouken@337
   191
extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
slouken@337
   192
extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
slouken@337
   193
extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
slouken@337
   194
extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
slouken@337
   195
extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
slouken@0
   196
slouken@0
   197
/* Write an item of native format to the specified endianness */
slouken@337
   198
extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
slouken@337
   199
extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
slouken@337
   200
extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
slouken@337
   201
extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
slouken@337
   202
extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
slouken@337
   203
extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
slouken@0
   204
slouken@0
   205
slouken@0
   206
/* Ends C function definitions when using C++ */
slouken@0
   207
#ifdef __cplusplus
slouken@0
   208
}
slouken@0
   209
#endif
slouken@0
   210
#include "close_code.h"
slouken@0
   211
slouken@0
   212
#endif /* _SDL_endian_h */