include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 11 Apr 2002 14:35:16 +0000
changeset 337 9154ec9ca3d2
parent 297 f6ffac90895c
child 769 b8d311d90021
permissions -rw-r--r--
Explicitly specify the SDL API calling convention (C by default)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@251
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Functions for reading and writing endian-specific values */
slouken@0
    29
slouken@0
    30
#ifndef _SDL_endian_h
slouken@0
    31
#define _SDL_endian_h
slouken@0
    32
slouken@0
    33
/* These functions read and write data of the specified endianness, 
slouken@0
    34
   dynamically translating to the host machine endianness.
slouken@0
    35
slouken@0
    36
   e.g.: If you want to read a 16 bit value on big-endian machine from
slouken@0
    37
         an open file containing little endian values, you would use:
slouken@0
    38
		value = SDL_ReadLE16(rp);
slouken@0
    39
         Note that the read/write functions use SDL_RWops pointers
slouken@0
    40
         instead of FILE pointers.  This allows you to read and write
slouken@0
    41
         endian values from large chunks of memory as well as files 
slouken@0
    42
         and other data sources.
slouken@0
    43
*/
slouken@0
    44
slouken@0
    45
#include <stdio.h>
slouken@0
    46
slouken@0
    47
#include "SDL_types.h"
slouken@0
    48
#include "SDL_rwops.h"
slouken@0
    49
#include "SDL_byteorder.h"
slouken@0
    50
slouken@0
    51
#include "begin_code.h"
slouken@0
    52
/* Set up for C function definitions, even when using C++ */
slouken@0
    53
#ifdef __cplusplus
slouken@0
    54
extern "C" {
slouken@0
    55
#endif
slouken@0
    56
slouken@0
    57
/* The macros used to swap values */
slouken@0
    58
/* Try to use superfast macros on systems that support them */
slouken@0
    59
#ifdef linux
slouken@0
    60
#include <asm/byteorder.h>
slouken@0
    61
#ifdef __arch__swab16
slouken@0
    62
#define SDL_Swap16  __arch__swab16
slouken@0
    63
#endif
slouken@0
    64
#ifdef __arch__swab32
slouken@0
    65
#define SDL_Swap32  __arch__swab32
slouken@0
    66
#endif
slouken@0
    67
#endif /* linux */
slouken@0
    68
/* Use inline functions for compilers that support them, and static
slouken@0
    69
   functions for those that do not.  Because these functions become
slouken@0
    70
   static for compilers that do not support inline functions, this
slouken@0
    71
   header should only be included in files that actually use them.
slouken@0
    72
*/
slouken@0
    73
#ifndef SDL_Swap16
slouken@0
    74
static __inline__ Uint16 SDL_Swap16(Uint16 D) {
slouken@0
    75
	return((D<<8)|(D>>8));
slouken@0
    76
}
slouken@0
    77
#endif
slouken@0
    78
#ifndef SDL_Swap32
slouken@0
    79
static __inline__ Uint32 SDL_Swap32(Uint32 D) {
slouken@0
    80
	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
slouken@0
    81
}
slouken@0
    82
#endif
slouken@0
    83
#ifdef SDL_HAS_64BIT_TYPE
slouken@0
    84
#ifndef SDL_Swap64
slouken@0
    85
static __inline__ Uint64 SDL_Swap64(Uint64 val) {
slouken@0
    86
	Uint32 hi, lo;
slouken@0
    87
slouken@0
    88
	/* Separate into high and low 32-bit values and swap them */
slouken@0
    89
	lo = (Uint32)(val&0xFFFFFFFF);
slouken@0
    90
	val >>= 32;
slouken@0
    91
	hi = (Uint32)(val&0xFFFFFFFF);
slouken@0
    92
	val = SDL_Swap32(lo);
slouken@0
    93
	val <<= 32;
slouken@0
    94
	val |= SDL_Swap32(hi);
slouken@0
    95
	return(val);
slouken@0
    96
}
slouken@0
    97
#endif
slouken@0
    98
#else
slouken@0
    99
#ifndef SDL_Swap64
slouken@0
   100
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   101
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   102
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   103
*/
slouken@0
   104
#define SDL_Swap64(X)	(X)
slouken@0
   105
#endif
slouken@0
   106
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   107
slouken@0
   108
slouken@0
   109
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   110
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   111
#define SDL_SwapLE16(X)	(X)
slouken@0
   112
#define SDL_SwapLE32(X)	(X)
slouken@0
   113
#define SDL_SwapLE64(X)	(X)
slouken@0
   114
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   115
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   116
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   117
#else
slouken@0
   118
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   119
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   120
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   121
#define SDL_SwapBE16(X)	(X)
slouken@0
   122
#define SDL_SwapBE32(X)	(X)
slouken@0
   123
#define SDL_SwapBE64(X)	(X)
slouken@0
   124
#endif
slouken@0
   125
slouken@0
   126
/* Read an item of the specified endianness and return in native format */
slouken@337
   127
extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops *src);
slouken@337
   128
extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops *src);
slouken@337
   129
extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops *src);
slouken@337
   130
extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops *src);
slouken@337
   131
extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops *src);
slouken@337
   132
extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops *src);
slouken@0
   133
slouken@0
   134
/* Write an item of native format to the specified endianness */
slouken@337
   135
extern DECLSPEC int SDLCALL SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
slouken@337
   136
extern DECLSPEC int SDLCALL SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
slouken@337
   137
extern DECLSPEC int SDLCALL SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
slouken@337
   138
extern DECLSPEC int SDLCALL SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
slouken@337
   139
extern DECLSPEC int SDLCALL SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
slouken@337
   140
extern DECLSPEC int SDLCALL SDL_WriteBE64(SDL_RWops *dst, Uint64 value);
slouken@0
   141
slouken@0
   142
slouken@0
   143
/* Ends C function definitions when using C++ */
slouken@0
   144
#ifdef __cplusplus
slouken@0
   145
}
slouken@0
   146
#endif
slouken@0
   147
#include "close_code.h"
slouken@0
   148
slouken@0
   149
#endif /* _SDL_endian_h */