include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 Feb 2006 07:05:18 +0000
changeset 1369 42eeb14770e3
parent 1368 533567cbb576
child 1372 3e171fa5f4df
permissions -rw-r--r--
Doh! Bitten by search and replace. :)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* Functions for reading and writing endian-specific values */
slouken@0
    24
slouken@0
    25
#ifndef _SDL_endian_h
slouken@0
    26
#define _SDL_endian_h
slouken@0
    27
slouken@1354
    28
#include "SDL_stdinc.h"
slouken@0
    29
slouken@1354
    30
/* The two types of endianness */
slouken@1354
    31
#define SDL_LIL_ENDIAN	1234
slouken@1354
    32
#define SDL_BIG_ENDIAN	4321
slouken@0
    33
slouken@1354
    34
#ifndef SDL_BYTEORDER	/* Not defined in SDL_config.h? */
slouken@1354
    35
#if (defined(__i386__) || defined(__i386)) || \
slouken@1354
    36
     defined(__ia64__) || defined(__x86_64__) || \
slouken@1354
    37
    (defined(__alpha__) || defined(__alpha)) || \
slouken@1354
    38
    (defined(__arm__) || defined(__thumb__)) || \
slouken@1354
    39
    (defined(__sh__) || defined(__sh64__)) || \
slouken@1354
    40
    (defined(__mips__) && defined(__MIPSEL__)) || \
slouken@1354
    41
     defined(__SYMBIAN32__) || defined(__OS2__)
slouken@1354
    42
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    43
#else
slouken@1354
    44
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1354
    45
#endif
slouken@1354
    46
#endif /* !SDL_BYTEORDER */
slouken@1354
    47
slouken@0
    48
slouken@0
    49
#include "begin_code.h"
slouken@0
    50
/* Set up for C function definitions, even when using C++ */
slouken@0
    51
#ifdef __cplusplus
slouken@0
    52
extern "C" {
slouken@0
    53
#endif
slouken@0
    54
slouken@1369
    55
/* Use inline functions for compilers that support them, and static
slouken@0
    56
   functions for those that do not.  Because these functions become
slouken@1369
    57
   static for compilers that do not support inline functions, this
slouken@0
    58
   header should only be included in files that actually use them.
slouken@0
    59
*/
slouken@1368
    60
#if defined(__GNUC__) && defined(__i386__) &&
slouken@1368
    61
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
slouken@849
    62
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    63
{
slouken@849
    64
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    65
	return x;
slouken@849
    66
}
slouken@849
    67
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    68
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    69
{
slouken@1033
    70
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    71
	return x;
slouken@849
    72
}
slouken@859
    73
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    74
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    75
{
slouken@849
    76
	Uint16 result;
slouken@849
    77
slouken@849
    78
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    79
	return result;
slouken@848
    80
}
patmandin@1044
    81
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    82
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    83
{
patmandin@985
    84
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    85
	return x;
patmandin@985
    86
}
slouken@848
    87
#else
slouken@849
    88
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    89
	return((x<<8)|(x>>8));
slouken@0
    90
}
slouken@0
    91
#endif
slouken@848
    92
slouken@849
    93
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    94
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    95
{
slouken@849
    96
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    97
	return x;
slouken@849
    98
}
slouken@849
    99
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   100
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   101
{
slouken@849
   102
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   103
	return x;
slouken@849
   104
}
slouken@859
   105
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   106
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   107
{
slouken@849
   108
	Uint32 result;
slouken@849
   109
slouken@849
   110
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   111
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   112
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   113
	return result;
slouken@848
   114
}
patmandin@1044
   115
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   116
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   117
{
patmandin@1044
   118
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   119
	return x;
patmandin@985
   120
}
slouken@848
   121
#else
slouken@849
   122
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   123
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   124
}
slouken@0
   125
#endif
slouken@848
   126
slouken@0
   127
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   128
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   129
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   130
{
slouken@849
   131
	union { 
slouken@849
   132
		struct { Uint32 a,b; } s;
slouken@849
   133
		Uint64 u;
slouken@849
   134
	} v;
slouken@849
   135
	v.u = x;
slouken@849
   136
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   137
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   138
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   139
	return v.u;
slouken@849
   140
}
slouken@849
   141
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   142
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   143
{
slouken@849
   144
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   145
	return x;
slouken@849
   146
}
slouken@849
   147
#else
slouken@849
   148
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   149
{
slouken@0
   150
	Uint32 hi, lo;
slouken@0
   151
slouken@0
   152
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   153
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   154
	x >>= 32;
slouken@849
   155
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   156
	x = SDL_Swap32(lo);
slouken@849
   157
	x <<= 32;
slouken@849
   158
	x |= SDL_Swap32(hi);
slouken@849
   159
	return(x);
slouken@0
   160
}
slouken@0
   161
#endif
slouken@0
   162
#else
slouken@0
   163
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   164
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   165
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   166
*/
slouken@0
   167
#define SDL_Swap64(X)	(X)
slouken@0
   168
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   169
slouken@0
   170
slouken@0
   171
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   172
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   173
#define SDL_SwapLE16(X)	(X)
slouken@0
   174
#define SDL_SwapLE32(X)	(X)
slouken@0
   175
#define SDL_SwapLE64(X)	(X)
slouken@0
   176
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   177
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   178
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   179
#else
slouken@0
   180
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   181
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   182
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   183
#define SDL_SwapBE16(X)	(X)
slouken@0
   184
#define SDL_SwapBE32(X)	(X)
slouken@0
   185
#define SDL_SwapBE64(X)	(X)
slouken@0
   186
#endif
slouken@0
   187
slouken@0
   188
/* Ends C function definitions when using C++ */
slouken@0
   189
#ifdef __cplusplus
slouken@0
   190
}
slouken@0
   191
#endif
slouken@0
   192
#include "close_code.h"
slouken@0
   193
slouken@0
   194
#endif /* _SDL_endian_h */