include/SDL_endian.h
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 16 Nov 2012 09:23:18 -0300
changeset 6674 45a21e50ba5e
parent 6213 e50324ad38ee
child 6885 700f1b25f77f
permissions -rw-r--r--
Adds CMake build system by Marcus Von Appen (bug #1597)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@1895
    22
/**
slouken@3407
    23
 *  \file SDL_endian.h
slouken@3407
    24
 *  
slouken@3407
    25
 *  Functions for reading and writing endian-specific values
slouken@1895
    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@3407
    33
/**
slouken@3407
    34
 *  \name The two types of endianness
slouken@3407
    35
 */
slouken@3407
    36
/*@{*/
slouken@1354
    37
#define SDL_LIL_ENDIAN	1234
slouken@1354
    38
#define SDL_BIG_ENDIAN	4321
slouken@3407
    39
/*@}*/
slouken@0
    40
slouken@1895
    41
#ifndef SDL_BYTEORDER           /* Not defined in SDL_config.h? */
slouken@4552
    42
#ifdef __linux__
slouken@4552
    43
#include <endian.h>
slouken@4552
    44
#define SDL_BYTEORDER  __BYTE_ORDER
slouken@4552
    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@4552
    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@1895
    62
/* *INDENT-OFF* */
slouken@0
    63
extern "C" {
slouken@1895
    64
/* *INDENT-ON* */
slouken@0
    65
#endif
slouken@0
    66
slouken@3407
    67
/**
slouken@3407
    68
 *  \file SDL_endian.h
slouken@3407
    69
 *  
slouken@3407
    70
 *  Uses inline functions for compilers that support them, and static
slouken@3407
    71
 *  functions for those that do not.  Because these functions become
slouken@3407
    72
 *  static for compilers that do not support inline functions, this
slouken@3407
    73
 *  header should only be included in files that actually use them.
slouken@3407
    74
 */
slouken@1372
    75
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    76
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
slouken@1895
    77
static __inline__ Uint16
slouken@1895
    78
SDL_Swap16(Uint16 x)
slouken@848
    79
{
slouken@1895
    80
  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
slouken@1895
    81
    return x;
slouken@849
    82
}
slouken@849
    83
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
    84
static __inline__ Uint16
slouken@1895
    85
SDL_Swap16(Uint16 x)
slouken@849
    86
{
slouken@1895
    87
  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
slouken@1895
    88
    return x;
slouken@849
    89
}
slouken@859
    90
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
    91
static __inline__ Uint16
slouken@1895
    92
SDL_Swap16(Uint16 x)
slouken@849
    93
{
slouken@6213
    94
    int result;
slouken@849
    95
slouken@1895
    96
  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
slouken@6213
    97
    return (Uint16)result;
slouken@848
    98
}
icculus@3630
    99
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
slouken@1895
   100
static __inline__ Uint16
slouken@1895
   101
SDL_Swap16(Uint16 x)
patmandin@985
   102
{
slouken@1895
   103
  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   104
    return x;
patmandin@985
   105
}
slouken@848
   106
#else
slouken@1895
   107
static __inline__ Uint16
slouken@1895
   108
SDL_Swap16(Uint16 x)
slouken@1895
   109
{
slouken@4439
   110
    return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
slouken@0
   111
}
slouken@0
   112
#endif
slouken@848
   113
slouken@849
   114
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   115
static __inline__ Uint32
slouken@1895
   116
SDL_Swap32(Uint32 x)
slouken@848
   117
{
slouken@1895
   118
  __asm__("bswap %0": "=r"(x):"0"(x));
slouken@1895
   119
    return x;
slouken@849
   120
}
slouken@849
   121
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   122
static __inline__ Uint32
slouken@1895
   123
SDL_Swap32(Uint32 x)
slouken@849
   124
{
slouken@1895
   125
  __asm__("bswapl %0": "=r"(x):"0"(x));
slouken@1895
   126
    return x;
slouken@849
   127
}
slouken@859
   128
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
   129
static __inline__ Uint32
slouken@1895
   130
SDL_Swap32(Uint32 x)
slouken@849
   131
{
slouken@1895
   132
    Uint32 result;
slouken@849
   133
slouken@1895
   134
  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
slouken@1895
   135
  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   136
  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   137
    return result;
slouken@848
   138
}
icculus@3630
   139
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
slouken@1895
   140
static __inline__ Uint32
slouken@1895
   141
SDL_Swap32(Uint32 x)
patmandin@985
   142
{
slouken@1895
   143
  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   144
    return x;
patmandin@985
   145
}
slouken@848
   146
#else
slouken@1895
   147
static __inline__ Uint32
slouken@1895
   148
SDL_Swap32(Uint32 x)
slouken@1895
   149
{
slouken@4439
   150
    return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
slouken@4439
   151
                                    ((x >> 8) & 0x0000FF00) | (x >> 24)));
slouken@0
   152
}
slouken@0
   153
#endif
slouken@848
   154
slouken@849
   155
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   156
static __inline__ Uint64
slouken@1895
   157
SDL_Swap64(Uint64 x)
slouken@849
   158
{
slouken@1895
   159
    union
slouken@1895
   160
    {
slouken@1895
   161
        struct
slouken@1895
   162
        {
slouken@1895
   163
            Uint32 a, b;
slouken@1895
   164
        } s;
slouken@1895
   165
        Uint64 u;
slouken@1895
   166
    } v;
slouken@1895
   167
    v.u = x;
slouken@1895
   168
  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
slouken@3013
   169
            "1"(v.s.
slouken@3013
   170
                b));
slouken@1895
   171
    return v.u;
slouken@849
   172
}
slouken@849
   173
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   174
static __inline__ Uint64
slouken@1895
   175
SDL_Swap64(Uint64 x)
slouken@849
   176
{
slouken@1895
   177
  __asm__("bswapq %0": "=r"(x):"0"(x));
slouken@1895
   178
    return x;
slouken@849
   179
}
slouken@849
   180
#else
slouken@1895
   181
static __inline__ Uint64
slouken@1895
   182
SDL_Swap64(Uint64 x)
slouken@849
   183
{
slouken@1895
   184
    Uint32 hi, lo;
slouken@0
   185
slouken@1895
   186
    /* Separate into high and low 32-bit values and swap them */
slouken@3067
   187
    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   188
    x >>= 32;
slouken@3067
   189
    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   190
    x = SDL_Swap32(lo);
slouken@1895
   191
    x <<= 32;
slouken@1895
   192
    x |= SDL_Swap32(hi);
slouken@1895
   193
    return (x);
slouken@0
   194
}
slouken@0
   195
#endif
slouken@0
   196
slouken@0
   197
icculus@1984
   198
static __inline__ float
icculus@1984
   199
SDL_SwapFloat(float x)
icculus@1984
   200
{
slouken@1985
   201
    union
slouken@1985
   202
    {
slouken@1985
   203
        float f;
slouken@1985
   204
        Uint32 ui32;
slouken@1985
   205
    } swapper;
icculus@1984
   206
    swapper.f = x;
icculus@1984
   207
    swapper.ui32 = SDL_Swap32(swapper.ui32);
icculus@1984
   208
    return swapper.f;
icculus@1984
   209
}
icculus@1984
   210
icculus@1984
   211
slouken@3407
   212
/**
slouken@3407
   213
 *  \name Swap to native
slouken@3407
   214
 *  Byteswap item from the specified endianness to the native endianness.
slouken@3407
   215
 */
slouken@3407
   216
/*@{*/
slouken@0
   217
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   218
#define SDL_SwapLE16(X)	(X)
slouken@0
   219
#define SDL_SwapLE32(X)	(X)
slouken@0
   220
#define SDL_SwapLE64(X)	(X)
icculus@1984
   221
#define SDL_SwapFloatLE(X)	(X)
slouken@0
   222
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   223
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   224
#define SDL_SwapBE64(X)	SDL_Swap64(X)
icculus@1984
   225
#define SDL_SwapFloatBE(X)	SDL_SwapFloat(X)
slouken@0
   226
#else
slouken@0
   227
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   228
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   229
#define SDL_SwapLE64(X)	SDL_Swap64(X)
icculus@1984
   230
#define SDL_SwapFloatLE(X)	SDL_SwapFloat(X)
slouken@0
   231
#define SDL_SwapBE16(X)	(X)
slouken@0
   232
#define SDL_SwapBE32(X)	(X)
slouken@0
   233
#define SDL_SwapBE64(X)	(X)
icculus@1984
   234
#define SDL_SwapFloatBE(X)	(X)
slouken@0
   235
#endif
slouken@3407
   236
/*@}*//*Swap to native*/
slouken@0
   237
slouken@0
   238
/* Ends C function definitions when using C++ */
slouken@0
   239
#ifdef __cplusplus
slouken@1895
   240
/* *INDENT-OFF* */
slouken@0
   241
}
slouken@1895
   242
/* *INDENT-ON* */
slouken@0
   243
#endif
slouken@0
   244
#include "close_code.h"
slouken@0
   245
slouken@0
   246
#endif /* _SDL_endian_h */
slouken@1895
   247
slouken@1895
   248
/* vi: set ts=4 sw=4 expandtab: */