include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Feb 2006 09:38:05 +0000
changeset 1354 22f39393668a
parent 1353 7ba544e2888d
child 1368 533567cbb576
permissions -rw-r--r--
Fixed build problem with SDL_string.c
Officially deprecated SDL_byteorder.h, SDL_getenv.h and SDL_types.h
Moved endian-related SDL_rwops code into SDL_rwops.c
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@0
    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@0
    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@849
    60
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    61
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    62
{
slouken@849
    63
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    64
	return x;
slouken@849
    65
}
slouken@849
    66
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    67
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    68
{
slouken@1033
    69
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    70
	return x;
slouken@849
    71
}
slouken@859
    72
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    73
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    74
{
slouken@849
    75
	Uint16 result;
slouken@849
    76
slouken@849
    77
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    78
	return result;
slouken@848
    79
}
patmandin@1044
    80
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    81
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    82
{
patmandin@985
    83
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    84
	return x;
patmandin@985
    85
}
slouken@848
    86
#else
slouken@849
    87
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    88
	return((x<<8)|(x>>8));
slouken@0
    89
}
slouken@0
    90
#endif
slouken@848
    91
slouken@849
    92
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    93
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    94
{
slouken@849
    95
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    96
	return x;
slouken@849
    97
}
slouken@849
    98
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    99
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   100
{
slouken@849
   101
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   102
	return x;
slouken@849
   103
}
slouken@859
   104
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   105
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   106
{
slouken@849
   107
	Uint32 result;
slouken@849
   108
slouken@849
   109
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   110
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   111
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   112
	return result;
slouken@848
   113
}
patmandin@1044
   114
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   115
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   116
{
patmandin@1044
   117
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   118
	return x;
patmandin@985
   119
}
slouken@848
   120
#else
slouken@849
   121
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   122
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   123
}
slouken@0
   124
#endif
slouken@848
   125
slouken@0
   126
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   127
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   128
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   129
{
slouken@849
   130
	union { 
slouken@849
   131
		struct { Uint32 a,b; } s;
slouken@849
   132
		Uint64 u;
slouken@849
   133
	} v;
slouken@849
   134
	v.u = x;
slouken@849
   135
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   136
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   137
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   138
	return v.u;
slouken@849
   139
}
slouken@849
   140
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   141
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   142
{
slouken@849
   143
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   144
	return x;
slouken@849
   145
}
slouken@849
   146
#else
slouken@849
   147
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   148
{
slouken@0
   149
	Uint32 hi, lo;
slouken@0
   150
slouken@0
   151
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   152
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   153
	x >>= 32;
slouken@849
   154
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   155
	x = SDL_Swap32(lo);
slouken@849
   156
	x <<= 32;
slouken@849
   157
	x |= SDL_Swap32(hi);
slouken@849
   158
	return(x);
slouken@0
   159
}
slouken@0
   160
#endif
slouken@0
   161
#else
slouken@0
   162
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   163
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   164
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   165
*/
slouken@0
   166
#define SDL_Swap64(X)	(X)
slouken@0
   167
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   168
slouken@0
   169
slouken@0
   170
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   171
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   172
#define SDL_SwapLE16(X)	(X)
slouken@0
   173
#define SDL_SwapLE32(X)	(X)
slouken@0
   174
#define SDL_SwapLE64(X)	(X)
slouken@0
   175
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   176
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   177
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   178
#else
slouken@0
   179
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   180
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   181
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   182
#define SDL_SwapBE16(X)	(X)
slouken@0
   183
#define SDL_SwapBE32(X)	(X)
slouken@0
   184
#define SDL_SwapBE64(X)	(X)
slouken@0
   185
#endif
slouken@0
   186
slouken@0
   187
/* Ends C function definitions when using C++ */
slouken@0
   188
#ifdef __cplusplus
slouken@0
   189
}
slouken@0
   190
#endif
slouken@0
   191
#include "close_code.h"
slouken@0
   192
slouken@0
   193
#endif /* _SDL_endian_h */