include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 17 Jan 2011 09:09:15 -0800
changeset 5009 b21ad135d86b
parent 4552 116f05eb67c7
child 5262 b530ef003506
permissions -rw-r--r--
Minor documentation consistency cleanup
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@1895
    23
/**
slouken@3407
    24
 *  \file SDL_endian.h
slouken@3407
    25
 *  
slouken@3407
    26
 *  Functions for reading and writing endian-specific values
slouken@1895
    27
 */
slouken@0
    28
slouken@0
    29
#ifndef _SDL_endian_h
slouken@0
    30
#define _SDL_endian_h
slouken@0
    31
slouken@1354
    32
#include "SDL_stdinc.h"
slouken@0
    33
slouken@3407
    34
/**
slouken@3407
    35
 *  \name The two types of endianness
slouken@3407
    36
 */
slouken@3407
    37
/*@{*/
slouken@1354
    38
#define SDL_LIL_ENDIAN	1234
slouken@1354
    39
#define SDL_BIG_ENDIAN	4321
slouken@3407
    40
/*@}*/
slouken@0
    41
slouken@1895
    42
#ifndef SDL_BYTEORDER           /* Not defined in SDL_config.h? */
slouken@4552
    43
#ifdef __linux__
slouken@4552
    44
#include <endian.h>
slouken@4552
    45
#define SDL_BYTEORDER  __BYTE_ORDER
slouken@4552
    46
#else /* __linux __ */
slouken@1474
    47
#if defined(__hppa__) || \
slouken@1475
    48
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
slouken@1474
    49
    (defined(__MIPS__) && defined(__MISPEB__)) || \
slouken@1475
    50
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
slouken@1474
    51
    defined(__sparc__)
slouken@1474
    52
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1474
    53
#else
slouken@1354
    54
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    55
#endif
slouken@4552
    56
#endif /* __linux __ */
slouken@1354
    57
#endif /* !SDL_BYTEORDER */
slouken@1354
    58
slouken@0
    59
slouken@0
    60
#include "begin_code.h"
slouken@0
    61
/* Set up for C function definitions, even when using C++ */
slouken@0
    62
#ifdef __cplusplus
slouken@1895
    63
/* *INDENT-OFF* */
slouken@0
    64
extern "C" {
slouken@1895
    65
/* *INDENT-ON* */
slouken@0
    66
#endif
slouken@0
    67
slouken@3407
    68
/**
slouken@3407
    69
 *  \file SDL_endian.h
slouken@3407
    70
 *  
slouken@3407
    71
 *  Uses inline functions for compilers that support them, and static
slouken@3407
    72
 *  functions for those that do not.  Because these functions become
slouken@3407
    73
 *  static for compilers that do not support inline functions, this
slouken@3407
    74
 *  header should only be included in files that actually use them.
slouken@3407
    75
 */
slouken@1372
    76
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    77
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
slouken@1895
    78
static __inline__ Uint16
slouken@1895
    79
SDL_Swap16(Uint16 x)
slouken@848
    80
{
slouken@1895
    81
  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
slouken@1895
    82
    return x;
slouken@849
    83
}
slouken@849
    84
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
    85
static __inline__ Uint16
slouken@1895
    86
SDL_Swap16(Uint16 x)
slouken@849
    87
{
slouken@1895
    88
  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
slouken@1895
    89
    return x;
slouken@849
    90
}
slouken@859
    91
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
    92
static __inline__ Uint16
slouken@1895
    93
SDL_Swap16(Uint16 x)
slouken@849
    94
{
slouken@1895
    95
    Uint16 result;
slouken@849
    96
slouken@1895
    97
  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
slouken@1895
    98
    return result;
slouken@848
    99
}
icculus@3630
   100
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
slouken@1895
   101
static __inline__ Uint16
slouken@1895
   102
SDL_Swap16(Uint16 x)
patmandin@985
   103
{
slouken@1895
   104
  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   105
    return x;
patmandin@985
   106
}
slouken@848
   107
#else
slouken@1895
   108
static __inline__ Uint16
slouken@1895
   109
SDL_Swap16(Uint16 x)
slouken@1895
   110
{
slouken@4439
   111
    return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
slouken@0
   112
}
slouken@0
   113
#endif
slouken@848
   114
slouken@849
   115
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   116
static __inline__ Uint32
slouken@1895
   117
SDL_Swap32(Uint32 x)
slouken@848
   118
{
slouken@1895
   119
  __asm__("bswap %0": "=r"(x):"0"(x));
slouken@1895
   120
    return x;
slouken@849
   121
}
slouken@849
   122
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   123
static __inline__ Uint32
slouken@1895
   124
SDL_Swap32(Uint32 x)
slouken@849
   125
{
slouken@1895
   126
  __asm__("bswapl %0": "=r"(x):"0"(x));
slouken@1895
   127
    return x;
slouken@849
   128
}
slouken@859
   129
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
   130
static __inline__ Uint32
slouken@1895
   131
SDL_Swap32(Uint32 x)
slouken@849
   132
{
slouken@1895
   133
    Uint32 result;
slouken@849
   134
slouken@1895
   135
  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
slouken@1895
   136
  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   137
  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   138
    return result;
slouken@848
   139
}
icculus@3630
   140
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
slouken@1895
   141
static __inline__ Uint32
slouken@1895
   142
SDL_Swap32(Uint32 x)
patmandin@985
   143
{
slouken@1895
   144
  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   145
    return x;
patmandin@985
   146
}
slouken@848
   147
#else
slouken@1895
   148
static __inline__ Uint32
slouken@1895
   149
SDL_Swap32(Uint32 x)
slouken@1895
   150
{
slouken@4439
   151
    return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
slouken@4439
   152
                                    ((x >> 8) & 0x0000FF00) | (x >> 24)));
slouken@0
   153
}
slouken@0
   154
#endif
slouken@848
   155
slouken@0
   156
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   157
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   158
static __inline__ Uint64
slouken@1895
   159
SDL_Swap64(Uint64 x)
slouken@849
   160
{
slouken@1895
   161
    union
slouken@1895
   162
    {
slouken@1895
   163
        struct
slouken@1895
   164
        {
slouken@1895
   165
            Uint32 a, b;
slouken@1895
   166
        } s;
slouken@1895
   167
        Uint64 u;
slouken@1895
   168
    } v;
slouken@1895
   169
    v.u = x;
slouken@1895
   170
  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
slouken@3013
   171
            "1"(v.s.
slouken@3013
   172
                b));
slouken@1895
   173
    return v.u;
slouken@849
   174
}
slouken@849
   175
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   176
static __inline__ Uint64
slouken@1895
   177
SDL_Swap64(Uint64 x)
slouken@849
   178
{
slouken@1895
   179
  __asm__("bswapq %0": "=r"(x):"0"(x));
slouken@1895
   180
    return x;
slouken@849
   181
}
slouken@849
   182
#else
slouken@1895
   183
static __inline__ Uint64
slouken@1895
   184
SDL_Swap64(Uint64 x)
slouken@849
   185
{
slouken@1895
   186
    Uint32 hi, lo;
slouken@0
   187
slouken@1895
   188
    /* Separate into high and low 32-bit values and swap them */
slouken@3067
   189
    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   190
    x >>= 32;
slouken@3067
   191
    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   192
    x = SDL_Swap32(lo);
slouken@1895
   193
    x <<= 32;
slouken@1895
   194
    x |= SDL_Swap32(hi);
slouken@1895
   195
    return (x);
slouken@0
   196
}
slouken@0
   197
#endif
slouken@0
   198
#else
slouken@3407
   199
/**
slouken@3407
   200
 *  This is mainly to keep compilers from complaining in SDL code.
slouken@3407
   201
 *  If there is no real 64-bit datatype, then compilers will complain about
slouken@3407
   202
 *  the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@3407
   203
 */
slouken@0
   204
#define SDL_Swap64(X)	(X)
slouken@0
   205
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   206
slouken@0
   207
icculus@1984
   208
static __inline__ float
icculus@1984
   209
SDL_SwapFloat(float x)
icculus@1984
   210
{
slouken@1985
   211
    union
slouken@1985
   212
    {
slouken@1985
   213
        float f;
slouken@1985
   214
        Uint32 ui32;
slouken@1985
   215
    } swapper;
icculus@1984
   216
    swapper.f = x;
icculus@1984
   217
    swapper.ui32 = SDL_Swap32(swapper.ui32);
icculus@1984
   218
    return swapper.f;
icculus@1984
   219
}
icculus@1984
   220
icculus@1984
   221
slouken@3407
   222
/**
slouken@3407
   223
 *  \name Swap to native
slouken@3407
   224
 *  Byteswap item from the specified endianness to the native endianness.
slouken@3407
   225
 */
slouken@3407
   226
/*@{*/
slouken@0
   227
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   228
#define SDL_SwapLE16(X)	(X)
slouken@0
   229
#define SDL_SwapLE32(X)	(X)
slouken@0
   230
#define SDL_SwapLE64(X)	(X)
icculus@1984
   231
#define SDL_SwapFloatLE(X)	(X)
slouken@0
   232
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   233
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   234
#define SDL_SwapBE64(X)	SDL_Swap64(X)
icculus@1984
   235
#define SDL_SwapFloatBE(X)	SDL_SwapFloat(X)
slouken@0
   236
#else
slouken@0
   237
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   238
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   239
#define SDL_SwapLE64(X)	SDL_Swap64(X)
icculus@1984
   240
#define SDL_SwapFloatLE(X)	SDL_SwapFloat(X)
slouken@0
   241
#define SDL_SwapBE16(X)	(X)
slouken@0
   242
#define SDL_SwapBE32(X)	(X)
slouken@0
   243
#define SDL_SwapBE64(X)	(X)
icculus@1984
   244
#define SDL_SwapFloatBE(X)	(X)
slouken@0
   245
#endif
slouken@3407
   246
/*@}*//*Swap to native*/
slouken@0
   247
slouken@0
   248
/* Ends C function definitions when using C++ */
slouken@0
   249
#ifdef __cplusplus
slouken@1895
   250
/* *INDENT-OFF* */
slouken@0
   251
}
slouken@1895
   252
/* *INDENT-ON* */
slouken@0
   253
#endif
slouken@0
   254
#include "close_code.h"
slouken@0
   255
slouken@0
   256
#endif /* _SDL_endian_h */
slouken@1895
   257
slouken@1895
   258
/* vi: set ts=4 sw=4 expandtab: */