include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 23:17:08 +0000
branchSDL-1.2
changeset 4246 8b8314cc34a6
parent 4217 4c4113c2162c
child 4283 cd511a8560b7
permissions -rw-r--r--
Fixed bug #810

Lauri Kenttä 2009-09-26 06:42:23 PDT

Support for disabling stdio redirect with environment variables.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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@4217
    23
/**
slouken@4217
    24
 *  @file SDL_endian.h
slouken@4217
    25
 *  Functions for reading and writing endian-specific values
slouken@4217
    26
 */
slouken@0
    27
slouken@0
    28
#ifndef _SDL_endian_h
slouken@0
    29
#define _SDL_endian_h
slouken@0
    30
slouken@1354
    31
#include "SDL_stdinc.h"
slouken@0
    32
slouken@4217
    33
/** @name SDL_ENDIANs
slouken@4217
    34
 *  The two types of endianness 
slouken@4217
    35
 */
slouken@4217
    36
/*@{*/
slouken@1354
    37
#define SDL_LIL_ENDIAN	1234
slouken@1354
    38
#define SDL_BIG_ENDIAN	4321
slouken@4217
    39
/*@}*/
slouken@0
    40
slouken@1354
    41
#ifndef SDL_BYTEORDER	/* Not defined in SDL_config.h? */
slouken@1474
    42
#if defined(__hppa__) || \
slouken@1475
    43
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
slouken@1474
    44
    (defined(__MIPS__) && defined(__MISPEB__)) || \
slouken@1475
    45
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
slouken@1474
    46
    defined(__sparc__)
slouken@1474
    47
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1474
    48
#else
slouken@1354
    49
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    50
#endif
slouken@1354
    51
#endif /* !SDL_BYTEORDER */
slouken@1354
    52
slouken@0
    53
slouken@0
    54
#include "begin_code.h"
slouken@0
    55
/* Set up for C function definitions, even when using C++ */
slouken@0
    56
#ifdef __cplusplus
slouken@0
    57
extern "C" {
slouken@0
    58
#endif
slouken@0
    59
slouken@4217
    60
/**
slouken@4217
    61
 *  @name SDL_Swap Functions
slouken@4217
    62
 *  Use inline functions for compilers that support them, and static
slouken@4217
    63
 *  functions for those that do not.  Because these functions become
slouken@4217
    64
 *  static for compilers that do not support inline functions, this
slouken@4217
    65
 *  header should only be included in files that actually use them.
slouken@4217
    66
 */
slouken@4217
    67
/*@{*/
slouken@1372
    68
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
    69
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
    70
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    71
{
slouken@849
    72
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    73
	return x;
slouken@849
    74
}
slouken@849
    75
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    76
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    77
{
slouken@1033
    78
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    79
	return x;
slouken@849
    80
}
slouken@859
    81
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    82
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    83
{
slouken@849
    84
	Uint16 result;
slouken@849
    85
slouken@849
    86
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    87
	return result;
slouken@848
    88
}
patmandin@1044
    89
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    90
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    91
{
patmandin@985
    92
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    93
	return x;
patmandin@985
    94
}
slouken@848
    95
#else
slouken@849
    96
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    97
	return((x<<8)|(x>>8));
slouken@0
    98
}
slouken@0
    99
#endif
slouken@848
   100
icculus@3909
   101
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
   102
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
   103
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
   104
{
slouken@849
   105
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
   106
	return x;
slouken@849
   107
}
slouken@849
   108
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   109
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   110
{
slouken@849
   111
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   112
	return x;
slouken@849
   113
}
slouken@859
   114
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   115
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   116
{
slouken@849
   117
	Uint32 result;
slouken@849
   118
slouken@849
   119
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   120
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   121
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   122
	return result;
slouken@848
   123
}
patmandin@1044
   124
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   125
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   126
{
patmandin@1044
   127
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   128
	return x;
patmandin@985
   129
}
slouken@848
   130
#else
slouken@849
   131
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   132
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   133
}
slouken@0
   134
#endif
slouken@848
   135
slouken@0
   136
#ifdef SDL_HAS_64BIT_TYPE
icculus@3909
   137
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
   138
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
   139
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   140
{
slouken@849
   141
	union { 
slouken@849
   142
		struct { Uint32 a,b; } s;
slouken@849
   143
		Uint64 u;
slouken@849
   144
	} v;
slouken@849
   145
	v.u = x;
slouken@849
   146
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   147
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   148
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   149
	return v.u;
slouken@849
   150
}
slouken@849
   151
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   152
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   153
{
slouken@849
   154
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   155
	return x;
slouken@849
   156
}
slouken@849
   157
#else
slouken@849
   158
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   159
{
slouken@0
   160
	Uint32 hi, lo;
slouken@0
   161
slouken@0
   162
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   163
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   164
	x >>= 32;
slouken@849
   165
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   166
	x = SDL_Swap32(lo);
slouken@849
   167
	x <<= 32;
slouken@849
   168
	x |= SDL_Swap32(hi);
slouken@849
   169
	return(x);
slouken@0
   170
}
slouken@0
   171
#endif
slouken@0
   172
#else
slouken@0
   173
/* This is mainly to keep compilers from complaining in SDL code.
slouken@4217
   174
 * If there is no real 64-bit datatype, then compilers will complain about
slouken@4217
   175
 * the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@4217
   176
 */
slouken@0
   177
#define SDL_Swap64(X)	(X)
slouken@0
   178
#endif /* SDL_HAS_64BIT_TYPE */
slouken@4217
   179
/*@}*/
slouken@0
   180
slouken@4217
   181
/**
slouken@4217
   182
 *  @name SDL_SwapLE and SDL_SwapBE Functions
slouken@4217
   183
 *  Byteswap item from the specified endianness to the native endianness
slouken@4217
   184
 */
slouken@4217
   185
/*@{*/
slouken@0
   186
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   187
#define SDL_SwapLE16(X)	(X)
slouken@0
   188
#define SDL_SwapLE32(X)	(X)
slouken@0
   189
#define SDL_SwapLE64(X)	(X)
slouken@0
   190
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   191
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   192
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   193
#else
slouken@0
   194
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   195
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   196
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   197
#define SDL_SwapBE16(X)	(X)
slouken@0
   198
#define SDL_SwapBE32(X)	(X)
slouken@0
   199
#define SDL_SwapBE64(X)	(X)
slouken@0
   200
#endif
slouken@4217
   201
/*@}*/
slouken@0
   202
slouken@0
   203
/* Ends C function definitions when using C++ */
slouken@0
   204
#ifdef __cplusplus
slouken@0
   205
}
slouken@0
   206
#endif
slouken@0
   207
#include "close_code.h"
slouken@0
   208
slouken@0
   209
#endif /* _SDL_endian_h */