include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 12:48:50 -0700
changeset 7190 11612d544fcd
parent 7004 18d4df6942c2
child 7191 75360622e65f
permissions -rw-r--r--
Added mobile application events, with implementations for iOS and Android
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@1372
    70
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    71
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
icculus@7004
    72
SDL_FORCE_INLINE Uint16
slouken@1895
    73
SDL_Swap16(Uint16 x)
slouken@848
    74
{
slouken@1895
    75
  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
slouken@1895
    76
    return x;
slouken@849
    77
}
slouken@849
    78
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
    79
SDL_FORCE_INLINE Uint16
slouken@1895
    80
SDL_Swap16(Uint16 x)
slouken@849
    81
{
slouken@1895
    82
  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
slouken@1895
    83
    return x;
slouken@849
    84
}
slouken@859
    85
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
icculus@7004
    86
SDL_FORCE_INLINE Uint16
slouken@1895
    87
SDL_Swap16(Uint16 x)
slouken@849
    88
{
slouken@6213
    89
    int result;
slouken@849
    90
slouken@1895
    91
  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
slouken@6213
    92
    return (Uint16)result;
slouken@848
    93
}
icculus@3630
    94
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
icculus@7004
    95
SDL_FORCE_INLINE Uint16
slouken@1895
    96
SDL_Swap16(Uint16 x)
patmandin@985
    97
{
slouken@1895
    98
  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
    99
    return x;
patmandin@985
   100
}
slouken@848
   101
#else
icculus@7004
   102
SDL_FORCE_INLINE Uint16
slouken@1895
   103
SDL_Swap16(Uint16 x)
slouken@1895
   104
{
slouken@4439
   105
    return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
slouken@0
   106
}
slouken@0
   107
#endif
slouken@848
   108
slouken@849
   109
#if defined(__GNUC__) && defined(__i386__)
icculus@7004
   110
SDL_FORCE_INLINE Uint32
slouken@1895
   111
SDL_Swap32(Uint32 x)
slouken@848
   112
{
slouken@1895
   113
  __asm__("bswap %0": "=r"(x):"0"(x));
slouken@1895
   114
    return x;
slouken@849
   115
}
slouken@849
   116
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
   117
SDL_FORCE_INLINE Uint32
slouken@1895
   118
SDL_Swap32(Uint32 x)
slouken@849
   119
{
slouken@1895
   120
  __asm__("bswapl %0": "=r"(x):"0"(x));
slouken@1895
   121
    return x;
slouken@849
   122
}
slouken@859
   123
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
icculus@7004
   124
SDL_FORCE_INLINE Uint32
slouken@1895
   125
SDL_Swap32(Uint32 x)
slouken@849
   126
{
slouken@1895
   127
    Uint32 result;
slouken@849
   128
slouken@1895
   129
  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
slouken@1895
   130
  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   131
  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   132
    return result;
slouken@848
   133
}
icculus@3630
   134
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
icculus@7004
   135
SDL_FORCE_INLINE Uint32
slouken@1895
   136
SDL_Swap32(Uint32 x)
patmandin@985
   137
{
slouken@1895
   138
  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   139
    return x;
patmandin@985
   140
}
slouken@848
   141
#else
icculus@7004
   142
SDL_FORCE_INLINE Uint32
slouken@1895
   143
SDL_Swap32(Uint32 x)
slouken@1895
   144
{
slouken@4439
   145
    return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
slouken@4439
   146
                                    ((x >> 8) & 0x0000FF00) | (x >> 24)));
slouken@0
   147
}
slouken@0
   148
#endif
slouken@848
   149
slouken@849
   150
#if defined(__GNUC__) && defined(__i386__)
icculus@7004
   151
SDL_FORCE_INLINE Uint64
slouken@1895
   152
SDL_Swap64(Uint64 x)
slouken@849
   153
{
slouken@1895
   154
    union
slouken@1895
   155
    {
slouken@1895
   156
        struct
slouken@1895
   157
        {
slouken@1895
   158
            Uint32 a, b;
slouken@1895
   159
        } s;
slouken@1895
   160
        Uint64 u;
slouken@1895
   161
    } v;
slouken@1895
   162
    v.u = x;
slouken@1895
   163
  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
slouken@3013
   164
            "1"(v.s.
slouken@3013
   165
                b));
slouken@1895
   166
    return v.u;
slouken@849
   167
}
slouken@849
   168
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
   169
SDL_FORCE_INLINE Uint64
slouken@1895
   170
SDL_Swap64(Uint64 x)
slouken@849
   171
{
slouken@1895
   172
  __asm__("bswapq %0": "=r"(x):"0"(x));
slouken@1895
   173
    return x;
slouken@849
   174
}
slouken@849
   175
#else
icculus@7004
   176
SDL_FORCE_INLINE Uint64
slouken@1895
   177
SDL_Swap64(Uint64 x)
slouken@849
   178
{
slouken@1895
   179
    Uint32 hi, lo;
slouken@0
   180
slouken@1895
   181
    /* Separate into high and low 32-bit values and swap them */
slouken@3067
   182
    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   183
    x >>= 32;
slouken@3067
   184
    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   185
    x = SDL_Swap32(lo);
slouken@1895
   186
    x <<= 32;
slouken@1895
   187
    x |= SDL_Swap32(hi);
slouken@1895
   188
    return (x);
slouken@0
   189
}
slouken@0
   190
#endif
slouken@0
   191
slouken@0
   192
icculus@7004
   193
SDL_FORCE_INLINE float
icculus@1984
   194
SDL_SwapFloat(float x)
icculus@1984
   195
{
slouken@1985
   196
    union
slouken@1985
   197
    {
slouken@1985
   198
        float f;
slouken@1985
   199
        Uint32 ui32;
slouken@1985
   200
    } swapper;
icculus@1984
   201
    swapper.f = x;
icculus@1984
   202
    swapper.ui32 = SDL_Swap32(swapper.ui32);
icculus@1984
   203
    return swapper.f;
icculus@1984
   204
}
icculus@1984
   205
icculus@1984
   206
slouken@3407
   207
/**
slouken@3407
   208
 *  \name Swap to native
slouken@3407
   209
 *  Byteswap item from the specified endianness to the native endianness.
slouken@3407
   210
 */
slouken@3407
   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)
icculus@1984
   216
#define SDL_SwapFloatLE(X)	(X)
slouken@0
   217
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   218
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   219
#define SDL_SwapBE64(X)	SDL_Swap64(X)
icculus@1984
   220
#define SDL_SwapFloatBE(X)	SDL_SwapFloat(X)
slouken@0
   221
#else
slouken@0
   222
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   223
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   224
#define SDL_SwapLE64(X)	SDL_Swap64(X)
icculus@1984
   225
#define SDL_SwapFloatLE(X)	SDL_SwapFloat(X)
slouken@0
   226
#define SDL_SwapBE16(X)	(X)
slouken@0
   227
#define SDL_SwapBE32(X)	(X)
slouken@0
   228
#define SDL_SwapBE64(X)	(X)
icculus@1984
   229
#define SDL_SwapFloatBE(X)	(X)
slouken@0
   230
#endif
slouken@3407
   231
/*@}*//*Swap to native*/
slouken@0
   232
slouken@0
   233
/* Ends C function definitions when using C++ */
slouken@0
   234
#ifdef __cplusplus
slouken@1895
   235
/* *INDENT-OFF* */
slouken@0
   236
}
slouken@1895
   237
/* *INDENT-ON* */
slouken@0
   238
#endif
slouken@0
   239
#include "close_code.h"
slouken@0
   240
slouken@0
   241
#endif /* _SDL_endian_h */
slouken@1895
   242
slouken@1895
   243
/* vi: set ts=4 sw=4 expandtab: */