include/SDL_endian.h
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 11965 afbe1aac2dd1
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

also fix a changelog date.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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@4551
    42
#ifdef __linux__
slouken@4551
    43
#include <endian.h>
slouken@4551
    44
#define SDL_BYTEORDER  __BYTE_ORDER
slouken@4551
    45
#else /* __linux __ */
slouken@1474
    46
#if defined(__hppa__) || \
slouken@1475
    47
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
slouken@1474
    48
    (defined(__MIPS__) && defined(__MISPEB__)) || \
slouken@1475
    49
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
slouken@1474
    50
    defined(__sparc__)
slouken@1474
    51
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1474
    52
#else
slouken@1354
    53
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    54
#endif
slouken@4551
    55
#endif /* __linux __ */
slouken@1354
    56
#endif /* !SDL_BYTEORDER */
slouken@1354
    57
slouken@0
    58
slouken@0
    59
#include "begin_code.h"
slouken@0
    60
/* Set up for C function definitions, even when using C++ */
slouken@0
    61
#ifdef __cplusplus
slouken@0
    62
extern "C" {
slouken@0
    63
#endif
slouken@0
    64
slouken@4217
    65
/**
slouken@4217
    66
 *  @name SDL_Swap Functions
slouken@4217
    67
 *  Use inline functions for compilers that support them, and static
slouken@4217
    68
 *  functions for those that do not.  Because these functions become
slouken@4217
    69
 *  static for compilers that do not support inline functions, this
slouken@4217
    70
 *  header should only be included in files that actually use them.
slouken@4217
    71
 */
slouken@4217
    72
/*@{*/
slouken@1372
    73
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
    74
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
    75
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    76
{
slouken@849
    77
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    78
	return x;
slouken@849
    79
}
slouken@849
    80
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    81
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    82
{
slouken@1033
    83
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    84
	return x;
slouken@849
    85
}
slouken@859
    86
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    87
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    88
{
slouken@6234
    89
	int result;
slouken@849
    90
slouken@849
    91
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@6234
    92
	return (Uint16)result;
slouken@848
    93
}
patmandin@5657
    94
#elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
patmandin@985
    95
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    96
{
patmandin@985
    97
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    98
	return x;
patmandin@985
    99
}
sezeroz@11965
   100
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11965
   101
extern _inline Uint16 SDL_Swap16(Uint16);
sezeroz@11965
   102
#pragma aux SDL_Swap16 = \
sezeroz@11965
   103
	"xchg al, ah" \
sezeroz@11965
   104
	parm   [ax]   \
sezeroz@11965
   105
	modify [ax];
slouken@848
   106
#else
slouken@849
   107
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@4438
   108
	return SDL_static_cast(Uint16, ((x<<8)|(x>>8)));
slouken@0
   109
}
slouken@0
   110
#endif
slouken@848
   111
icculus@3909
   112
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
   113
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
   114
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
   115
{
slouken@849
   116
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
   117
	return x;
slouken@849
   118
}
slouken@849
   119
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   120
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   121
{
slouken@849
   122
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
   123
	return x;
slouken@849
   124
}
slouken@859
   125
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   126
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   127
{
slouken@849
   128
	Uint32 result;
slouken@849
   129
slouken@849
   130
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   131
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   132
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   133
	return result;
slouken@848
   134
}
patmandin@5657
   135
#elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
patmandin@994
   136
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   137
{
patmandin@1044
   138
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   139
	return x;
patmandin@985
   140
}
sezeroz@11965
   141
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11965
   142
extern _inline Uint32 SDL_Swap32(Uint32);
sezeroz@11965
   143
#ifndef __SW_3 /* 486+ */
sezeroz@11965
   144
#pragma aux SDL_Swap32 = \
sezeroz@11965
   145
	"bswap eax"  \
sezeroz@11965
   146
	parm   [eax] \
sezeroz@11965
   147
	modify [eax];
sezeroz@11965
   148
#else  /* 386-only */
sezeroz@11965
   149
#pragma aux SDL_Swap32 = \
sezeroz@11965
   150
	"xchg al, ah"  \
sezeroz@11965
   151
	"ror  eax, 16" \
sezeroz@11965
   152
	"xchg al, ah"  \
sezeroz@11965
   153
	parm   [eax]   \
sezeroz@11965
   154
	modify [eax];
sezeroz@11965
   155
#endif
slouken@848
   156
#else
slouken@849
   157
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@4438
   158
	return SDL_static_cast(Uint32, ((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24)));
slouken@0
   159
}
slouken@0
   160
#endif
slouken@848
   161
slouken@0
   162
#ifdef SDL_HAS_64BIT_TYPE
icculus@3909
   163
#if defined(__GNUC__) && defined(__i386__) && \
icculus@3909
   164
   !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
slouken@849
   165
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   166
{
slouken@849
   167
	union { 
slouken@849
   168
		struct { Uint32 a,b; } s;
slouken@849
   169
		Uint64 u;
slouken@849
   170
	} v;
slouken@849
   171
	v.u = x;
slouken@849
   172
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   173
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   174
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   175
	return v.u;
slouken@849
   176
}
slouken@849
   177
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   178
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   179
{
slouken@849
   180
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   181
	return x;
slouken@849
   182
}
slouken@849
   183
#else
slouken@849
   184
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   185
{
slouken@0
   186
	Uint32 hi, lo;
slouken@0
   187
slouken@0
   188
	/* Separate into high and low 32-bit values and swap them */
slouken@4283
   189
	lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@849
   190
	x >>= 32;
slouken@4283
   191
	hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@849
   192
	x = SDL_Swap32(lo);
slouken@849
   193
	x <<= 32;
slouken@849
   194
	x |= SDL_Swap32(hi);
slouken@4431
   195
	return (x);
slouken@0
   196
}
slouken@0
   197
#endif
slouken@0
   198
#else
slouken@0
   199
/* This is mainly to keep compilers from complaining in SDL code.
slouken@4217
   200
 * If there is no real 64-bit datatype, then compilers will complain about
slouken@4217
   201
 * the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@4217
   202
 */
slouken@0
   203
#define SDL_Swap64(X)	(X)
slouken@0
   204
#endif /* SDL_HAS_64BIT_TYPE */
slouken@4217
   205
/*@}*/
slouken@0
   206
slouken@4217
   207
/**
slouken@4217
   208
 *  @name SDL_SwapLE and SDL_SwapBE Functions
slouken@4217
   209
 *  Byteswap item from the specified endianness to the native endianness
slouken@4217
   210
 */
slouken@4217
   211
/*@{*/
slouken@0
   212
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   213
#define SDL_SwapLE16(X)	(X)
slouken@0
   214
#define SDL_SwapLE32(X)	(X)
slouken@0
   215
#define SDL_SwapLE64(X)	(X)
slouken@0
   216
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   217
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   218
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   219
#else
slouken@0
   220
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   221
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   222
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   223
#define SDL_SwapBE16(X)	(X)
slouken@0
   224
#define SDL_SwapBE32(X)	(X)
slouken@0
   225
#define SDL_SwapBE64(X)	(X)
slouken@0
   226
#endif
slouken@4217
   227
/*@}*/
slouken@0
   228
slouken@0
   229
/* Ends C function definitions when using C++ */
slouken@0
   230
#ifdef __cplusplus
slouken@0
   231
}
slouken@0
   232
#endif
slouken@0
   233
#include "close_code.h"
slouken@0
   234
slouken@0
   235
#endif /* _SDL_endian_h */