include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1044 d36ea7925763
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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@0
    28
/* These functions read and write data of the specified endianness, 
slouken@0
    29
   dynamically translating to the host machine endianness.
slouken@0
    30
slouken@0
    31
   e.g.: If you want to read a 16 bit value on big-endian machine from
slouken@0
    32
         an open file containing little endian values, you would use:
slouken@0
    33
		value = SDL_ReadLE16(rp);
slouken@0
    34
         Note that the read/write functions use SDL_RWops pointers
slouken@0
    35
         instead of FILE pointers.  This allows you to read and write
slouken@0
    36
         endian values from large chunks of memory as well as files 
slouken@0
    37
         and other data sources.
slouken@0
    38
*/
slouken@0
    39
slouken@0
    40
#include <stdio.h>
slouken@0
    41
slouken@0
    42
#include "SDL_types.h"
slouken@0
    43
#include "SDL_rwops.h"
slouken@0
    44
#include "SDL_byteorder.h"
slouken@0
    45
slouken@0
    46
#include "begin_code.h"
slouken@0
    47
/* Set up for C function definitions, even when using C++ */
slouken@0
    48
#ifdef __cplusplus
slouken@0
    49
extern "C" {
slouken@0
    50
#endif
slouken@0
    51
slouken@0
    52
/* Use inline functions for compilers that support them, and static
slouken@0
    53
   functions for those that do not.  Because these functions become
slouken@0
    54
   static for compilers that do not support inline functions, this
slouken@0
    55
   header should only be included in files that actually use them.
slouken@0
    56
*/
slouken@849
    57
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    58
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@848
    59
{
slouken@849
    60
	__asm__("xchgb %b0,%h0" : "=q" (x) :  "0" (x));
slouken@849
    61
	return x;
slouken@849
    62
}
slouken@849
    63
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    64
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    65
{
slouken@1033
    66
	__asm__("xchgb %b0,%h0" : "=Q" (x) :  "0" (x));
slouken@849
    67
	return x;
slouken@849
    68
}
slouken@859
    69
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
    70
static __inline__ Uint16 SDL_Swap16(Uint16 x)
slouken@849
    71
{
slouken@849
    72
	Uint16 result;
slouken@849
    73
slouken@849
    74
	__asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
slouken@849
    75
	return result;
slouken@848
    76
}
patmandin@1044
    77
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@985
    78
static __inline__ Uint16 SDL_Swap16(Uint16 x)
patmandin@985
    79
{
patmandin@985
    80
	__asm__("rorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
    81
	return x;
patmandin@985
    82
}
slouken@848
    83
#else
slouken@849
    84
static __inline__ Uint16 SDL_Swap16(Uint16 x) {
slouken@849
    85
	return((x<<8)|(x>>8));
slouken@0
    86
}
slouken@0
    87
#endif
slouken@848
    88
slouken@849
    89
#if defined(__GNUC__) && defined(__i386__)
slouken@849
    90
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@848
    91
{
slouken@849
    92
	__asm__("bswap %0" : "=r" (x) : "0" (x));
slouken@849
    93
	return x;
slouken@849
    94
}
slouken@849
    95
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
    96
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
    97
{
slouken@849
    98
	__asm__("bswapl %0" : "=r" (x) : "0" (x));
slouken@849
    99
	return x;
slouken@849
   100
}
slouken@859
   101
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@849
   102
static __inline__ Uint32 SDL_Swap32(Uint32 x)
slouken@849
   103
{
slouken@849
   104
	Uint32 result;
slouken@849
   105
slouken@849
   106
	__asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
slouken@849
   107
	__asm__("rlwimi %0,%2,8,8,15"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   108
	__asm__("rlwimi %0,%2,24,0,7"   : "=&r" (result) : "0" (result),    "r" (x));
slouken@849
   109
	return result;
slouken@848
   110
}
patmandin@1044
   111
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
patmandin@994
   112
static __inline__ Uint32 SDL_Swap32(Uint32 x)
patmandin@985
   113
{
patmandin@1044
   114
	__asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) :  "0" (x) : "cc");
patmandin@985
   115
	return x;
patmandin@985
   116
}
slouken@848
   117
#else
slouken@849
   118
static __inline__ Uint32 SDL_Swap32(Uint32 x) {
slouken@849
   119
	return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
slouken@0
   120
}
slouken@0
   121
#endif
slouken@848
   122
slouken@0
   123
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   124
#if defined(__GNUC__) && defined(__i386__)
slouken@849
   125
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   126
{
slouken@849
   127
	union { 
slouken@849
   128
		struct { Uint32 a,b; } s;
slouken@849
   129
		Uint64 u;
slouken@849
   130
	} v;
slouken@849
   131
	v.u = x;
slouken@849
   132
	__asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
slouken@849
   133
	        : "=r" (v.s.a), "=r" (v.s.b) 
slouken@849
   134
	        : "0" (v.s.a), "1" (v.s.b)); 
slouken@849
   135
	return v.u;
slouken@849
   136
}
slouken@849
   137
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@849
   138
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   139
{
slouken@849
   140
	__asm__("bswapq %0" : "=r" (x) : "0" (x));
slouken@849
   141
	return x;
slouken@849
   142
}
slouken@849
   143
#else
slouken@849
   144
static __inline__ Uint64 SDL_Swap64(Uint64 x)
slouken@849
   145
{
slouken@0
   146
	Uint32 hi, lo;
slouken@0
   147
slouken@0
   148
	/* Separate into high and low 32-bit values and swap them */
slouken@849
   149
	lo = (Uint32)(x&0xFFFFFFFF);
slouken@849
   150
	x >>= 32;
slouken@849
   151
	hi = (Uint32)(x&0xFFFFFFFF);
slouken@849
   152
	x = SDL_Swap32(lo);
slouken@849
   153
	x <<= 32;
slouken@849
   154
	x |= SDL_Swap32(hi);
slouken@849
   155
	return(x);
slouken@0
   156
}
slouken@0
   157
#endif
slouken@0
   158
#else
slouken@0
   159
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   160
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   161
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   162
*/
slouken@0
   163
#define SDL_Swap64(X)	(X)
slouken@0
   164
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   165
slouken@0
   166
slouken@0
   167
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   168
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   169
#define SDL_SwapLE16(X)	(X)
slouken@0
   170
#define SDL_SwapLE32(X)	(X)
slouken@0
   171
#define SDL_SwapLE64(X)	(X)
slouken@0
   172
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   173
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   174
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   175
#else
slouken@0
   176
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   177
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   178
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   179
#define SDL_SwapBE16(X)	(X)
slouken@0
   180
#define SDL_SwapBE32(X)	(X)
slouken@0
   181
#define SDL_SwapBE64(X)	(X)
slouken@0
   182
#endif
slouken@0
   183
slouken@0
   184
/* Read an item of the specified endianness and return in native format */
slouken@337
   185
extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
slouken@337
   186
extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
slouken@337
   187
extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
slouken@337
   188
extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
slouken@337
   189
extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
slouken@337
   190
extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
slouken@0
   191
slouken@0
   192
/* Write an item of native format to the specified endianness */
slouken@337
   193
extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
slouken@337
   194
extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
slouken@337
   195
extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
slouken@337
   196
extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
slouken@337
   197
extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
slouken@337
   198
extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
slouken@0
   199
slouken@0
   200
slouken@0
   201
/* Ends C function definitions when using C++ */
slouken@0
   202
#ifdef __cplusplus
slouken@0
   203
}
slouken@0
   204
#endif
slouken@0
   205
#include "close_code.h"
slouken@0
   206
slouken@0
   207
#endif /* _SDL_endian_h */