include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 May 2006 08:18:28 +0000
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1475 d45aefcac017
child 1662 782fd950bd46
child 1895 c121d94672cb
child 3909 6832b00d3594
permissions -rw-r--r--
Merge trunk-1.3-3
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@1474
    35
#if defined(__hppa__) || \
slouken@1475
    36
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
slouken@1474
    37
    (defined(__MIPS__) && defined(__MISPEB__)) || \
slouken@1475
    38
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
slouken@1474
    39
    defined(__sparc__)
slouken@1474
    40
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1474
    41
#else
slouken@1354
    42
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    43
#endif
slouken@1354
    44
#endif /* !SDL_BYTEORDER */
slouken@1354
    45
slouken@0
    46
slouken@0
    47
#include "begin_code.h"
slouken@0
    48
/* Set up for C function definitions, even when using C++ */
slouken@0
    49
#ifdef __cplusplus
slouken@0
    50
extern "C" {
slouken@0
    51
#endif
slouken@0
    52
slouken@1369
    53
/* Use inline functions for compilers that support them, and static
slouken@0
    54
   functions for those that do not.  Because these functions become
slouken@1369
    55
   static for compilers that do not support inline functions, this
slouken@0
    56
   header should only be included in files that actually use them.
slouken@0
    57
*/
slouken@1372
    58
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    59
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
slouken@849
    60
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    61
{
slouken@849
    62
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    63
	return x;
slouken@849
    64
}
slouken@849
    65
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    66
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    67
{
slouken@1033
    68
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    69
	return x;
slouken@849
    70
}
slouken@859
    71
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    72
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    73
{
slouken@849
    74
	Uint16 result;
slouken@849
    75
slouken@849
    76
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    77
	return result;
slouken@848
    78
}
patmandin@1044
    79
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    80
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    81
{
patmandin@985
    82
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    83
	return x;
patmandin@985
    84
}
slouken@848
    85
#else
slouken@849
    86
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    87
	return((x<<8)|(x>>8));
slouken@0
    88
}
slouken@0
    89
#endif
slouken@848
    90
slouken@849
    91
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    92
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    93
{
slouken@849
    94
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    95
	return x;
slouken@849
    96
}
slouken@849
    97
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    98
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
    99
{
slouken@849
   100
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   101
	return x;
slouken@849
   102
}
slouken@859
   103
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   104
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   105
{
slouken@849
   106
	Uint32 result;
slouken@849
   107
slouken@849
   108
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   109
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   110
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   111
	return result;
slouken@848
   112
}
patmandin@1044
   113
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   114
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   115
{
patmandin@1044
   116
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   117
	return x;
patmandin@985
   118
}
slouken@848
   119
#else
slouken@849
   120
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   121
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   122
}
slouken@0
   123
#endif
slouken@848
   124
slouken@0
   125
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   126
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   127
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   128
{
slouken@849
   129
	union { 
slouken@849
   130
		struct { Uint32 a,b; } s;
slouken@849
   131
		Uint64 u;
slouken@849
   132
	} v;
slouken@849
   133
	v.u = x;
slouken@849
   134
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   135
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   136
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   137
	return v.u;
slouken@849
   138
}
slouken@849
   139
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   140
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   141
{
slouken@849
   142
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   143
	return x;
slouken@849
   144
}
slouken@849
   145
#else
slouken@849
   146
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   147
{
slouken@0
   148
	Uint32 hi, lo;
slouken@0
   149
slouken@0
   150
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   151
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   152
	x >>= 32;
slouken@849
   153
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   154
	x = SDL_Swap32(lo);
slouken@849
   155
	x <<= 32;
slouken@849
   156
	x |= SDL_Swap32(hi);
slouken@849
   157
	return(x);
slouken@0
   158
}
slouken@0
   159
#endif
slouken@0
   160
#else
slouken@0
   161
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   162
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   163
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   164
*/
slouken@0
   165
#define SDL_Swap64(X)	(X)
slouken@0
   166
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   167
slouken@0
   168
slouken@0
   169
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   170
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   171
#define SDL_SwapLE16(X)	(X)
slouken@0
   172
#define SDL_SwapLE32(X)	(X)
slouken@0
   173
#define SDL_SwapLE64(X)	(X)
slouken@0
   174
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   175
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   176
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   177
#else
slouken@0
   178
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   179
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   180
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   181
#define SDL_SwapBE16(X)	(X)
slouken@0
   182
#define SDL_SwapBE32(X)	(X)
slouken@0
   183
#define SDL_SwapBE64(X)	(X)
slouken@0
   184
#endif
slouken@0
   185
slouken@0
   186
/* Ends C function definitions when using C++ */
slouken@0
   187
#ifdef __cplusplus
slouken@0
   188
}
slouken@0
   189
#endif
slouken@0
   190
#include "close_code.h"
slouken@0
   191
slouken@0
   192
#endif /* _SDL_endian_h */