include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 23 Apr 2019 07:59:31 -0700
changeset 12714 9b7633bd0aa0
parent 12503 806492103856
permissions -rw-r--r--
Use _Exit() when available
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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@7191
    24
 *
slouken@3407
    25
 *  Functions for reading and writing endian-specific values
slouken@1895
    26
 */
slouken@0
    27
slouken@10638
    28
#ifndef SDL_endian_h_
slouken@10638
    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
 */
gabomdq@7678
    36
/* @{ */
slouken@7191
    37
#define SDL_LIL_ENDIAN  1234
slouken@7191
    38
#define SDL_BIG_ENDIAN  4321
gabomdq@7678
    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
philipp@9710
    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@7191
    51
#define SDL_BYTEORDER   SDL_BIG_ENDIAN
slouken@1474
    52
#else
slouken@7191
    53
#define SDL_BYTEORDER   SDL_LIL_ENDIAN
slouken@1354
    54
#endif
philipp@9710
    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@0
    62
extern "C" {
slouken@0
    63
#endif
slouken@0
    64
slouken@3407
    65
/**
slouken@3407
    66
 *  \file SDL_endian.h
slouken@3407
    67
 */
slouken@1372
    68
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    69
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
icculus@7004
    70
SDL_FORCE_INLINE Uint16
slouken@1895
    71
SDL_Swap16(Uint16 x)
slouken@848
    72
{
slouken@1895
    73
  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
slouken@1895
    74
    return x;
slouken@849
    75
}
slouken@849
    76
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
    77
SDL_FORCE_INLINE Uint16
slouken@1895
    78
SDL_Swap16(Uint16 x)
slouken@849
    79
{
slouken@1895
    80
  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
slouken@1895
    81
    return x;
slouken@849
    82
}
slouken@859
    83
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
icculus@7004
    84
SDL_FORCE_INLINE Uint16
slouken@1895
    85
SDL_Swap16(Uint16 x)
slouken@849
    86
{
slouken@6213
    87
    int result;
slouken@849
    88
slouken@1895
    89
  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
slouken@6213
    90
    return (Uint16)result;
slouken@848
    91
}
icculus@3630
    92
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
icculus@7004
    93
SDL_FORCE_INLINE Uint16
slouken@1895
    94
SDL_Swap16(Uint16 x)
patmandin@985
    95
{
slouken@1895
    96
  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
    97
    return x;
patmandin@985
    98
}
sezeroz@11315
    99
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11315
   100
extern _inline Uint16 SDL_Swap16(Uint16);
sezeroz@11315
   101
#pragma aux SDL_Swap16 = \
sezeroz@11315
   102
  "xchg al, ah" \
sezeroz@11315
   103
  parm   [ax]   \
sezeroz@11315
   104
  modify [ax];
slouken@848
   105
#else
icculus@7004
   106
SDL_FORCE_INLINE Uint16
slouken@1895
   107
SDL_Swap16(Uint16 x)
slouken@1895
   108
{
slouken@4439
   109
    return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
slouken@0
   110
}
slouken@0
   111
#endif
slouken@848
   112
slouken@849
   113
#if defined(__GNUC__) && defined(__i386__)
icculus@7004
   114
SDL_FORCE_INLINE Uint32
slouken@1895
   115
SDL_Swap32(Uint32 x)
slouken@848
   116
{
slouken@1895
   117
  __asm__("bswap %0": "=r"(x):"0"(x));
slouken@1895
   118
    return x;
slouken@849
   119
}
slouken@849
   120
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
   121
SDL_FORCE_INLINE Uint32
slouken@1895
   122
SDL_Swap32(Uint32 x)
slouken@849
   123
{
slouken@1895
   124
  __asm__("bswapl %0": "=r"(x):"0"(x));
slouken@1895
   125
    return x;
slouken@849
   126
}
slouken@859
   127
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
icculus@7004
   128
SDL_FORCE_INLINE Uint32
slouken@1895
   129
SDL_Swap32(Uint32 x)
slouken@849
   130
{
slouken@1895
   131
    Uint32 result;
slouken@849
   132
slouken@1895
   133
  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
slouken@1895
   134
  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   135
  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   136
    return result;
slouken@848
   137
}
icculus@3630
   138
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
icculus@7004
   139
SDL_FORCE_INLINE Uint32
slouken@1895
   140
SDL_Swap32(Uint32 x)
patmandin@985
   141
{
slouken@1895
   142
  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   143
    return x;
patmandin@985
   144
}
sezeroz@11315
   145
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11315
   146
extern _inline Uint32 SDL_Swap32(Uint32);
sezeroz@11315
   147
#ifndef __SW_3 /* 486+ */
sezeroz@11315
   148
#pragma aux SDL_Swap32 = \
sezeroz@11315
   149
  "bswap eax"  \
sezeroz@11315
   150
  parm   [eax] \
sezeroz@11315
   151
  modify [eax];
sezeroz@11315
   152
#else  /* 386-only */
sezeroz@11315
   153
#pragma aux SDL_Swap32 = \
sezeroz@11315
   154
  "xchg al, ah"  \
sezeroz@11315
   155
  "ror  eax, 16" \
sezeroz@11315
   156
  "xchg al, ah"  \
sezeroz@11315
   157
  parm   [eax]   \
sezeroz@11315
   158
  modify [eax];
sezeroz@11315
   159
#endif
slouken@848
   160
#else
icculus@7004
   161
SDL_FORCE_INLINE Uint32
slouken@1895
   162
SDL_Swap32(Uint32 x)
slouken@1895
   163
{
slouken@4439
   164
    return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
slouken@4439
   165
                                    ((x >> 8) & 0x0000FF00) | (x >> 24)));
slouken@0
   166
}
slouken@0
   167
#endif
slouken@848
   168
slouken@849
   169
#if defined(__GNUC__) && defined(__i386__)
icculus@7004
   170
SDL_FORCE_INLINE Uint64
slouken@1895
   171
SDL_Swap64(Uint64 x)
slouken@849
   172
{
slouken@1895
   173
    union
slouken@1895
   174
    {
slouken@1895
   175
        struct
slouken@1895
   176
        {
slouken@1895
   177
            Uint32 a, b;
slouken@1895
   178
        } s;
slouken@1895
   179
        Uint64 u;
slouken@1895
   180
    } v;
slouken@1895
   181
    v.u = x;
slouken@1895
   182
  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
slouken@3013
   183
            "1"(v.s.
slouken@3013
   184
                b));
slouken@1895
   185
    return v.u;
slouken@849
   186
}
slouken@849
   187
#elif defined(__GNUC__) && defined(__x86_64__)
icculus@7004
   188
SDL_FORCE_INLINE Uint64
slouken@1895
   189
SDL_Swap64(Uint64 x)
slouken@849
   190
{
slouken@1895
   191
  __asm__("bswapq %0": "=r"(x):"0"(x));
slouken@1895
   192
    return x;
slouken@849
   193
}
slouken@849
   194
#else
icculus@7004
   195
SDL_FORCE_INLINE Uint64
slouken@1895
   196
SDL_Swap64(Uint64 x)
slouken@849
   197
{
slouken@1895
   198
    Uint32 hi, lo;
slouken@0
   199
slouken@1895
   200
    /* Separate into high and low 32-bit values and swap them */
slouken@3067
   201
    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   202
    x >>= 32;
slouken@3067
   203
    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
slouken@1895
   204
    x = SDL_Swap32(lo);
slouken@1895
   205
    x <<= 32;
slouken@1895
   206
    x |= SDL_Swap32(hi);
slouken@1895
   207
    return (x);
slouken@0
   208
}
slouken@0
   209
#endif
slouken@0
   210
slouken@0
   211
icculus@7004
   212
SDL_FORCE_INLINE float
icculus@1984
   213
SDL_SwapFloat(float x)
icculus@1984
   214
{
slouken@1985
   215
    union
slouken@1985
   216
    {
slouken@1985
   217
        float f;
slouken@1985
   218
        Uint32 ui32;
slouken@1985
   219
    } swapper;
icculus@1984
   220
    swapper.f = x;
icculus@1984
   221
    swapper.ui32 = SDL_Swap32(swapper.ui32);
icculus@1984
   222
    return swapper.f;
icculus@1984
   223
}
icculus@1984
   224
icculus@1984
   225
slouken@3407
   226
/**
slouken@3407
   227
 *  \name Swap to native
slouken@3407
   228
 *  Byteswap item from the specified endianness to the native endianness.
slouken@3407
   229
 */
gabomdq@7678
   230
/* @{ */
slouken@0
   231
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@7191
   232
#define SDL_SwapLE16(X) (X)
slouken@7191
   233
#define SDL_SwapLE32(X) (X)
slouken@7191
   234
#define SDL_SwapLE64(X) (X)
slouken@7191
   235
#define SDL_SwapFloatLE(X)  (X)
slouken@7191
   236
#define SDL_SwapBE16(X) SDL_Swap16(X)
slouken@7191
   237
#define SDL_SwapBE32(X) SDL_Swap32(X)
slouken@7191
   238
#define SDL_SwapBE64(X) SDL_Swap64(X)
slouken@7191
   239
#define SDL_SwapFloatBE(X)  SDL_SwapFloat(X)
slouken@0
   240
#else
slouken@7191
   241
#define SDL_SwapLE16(X) SDL_Swap16(X)
slouken@7191
   242
#define SDL_SwapLE32(X) SDL_Swap32(X)
slouken@7191
   243
#define SDL_SwapLE64(X) SDL_Swap64(X)
slouken@7191
   244
#define SDL_SwapFloatLE(X)  SDL_SwapFloat(X)
slouken@7191
   245
#define SDL_SwapBE16(X) (X)
slouken@7191
   246
#define SDL_SwapBE32(X) (X)
slouken@7191
   247
#define SDL_SwapBE64(X) (X)
slouken@7191
   248
#define SDL_SwapFloatBE(X)  (X)
slouken@0
   249
#endif
gabomdq@7678
   250
/* @} *//* Swap to native */
slouken@0
   251
slouken@0
   252
/* Ends C function definitions when using C++ */
slouken@0
   253
#ifdef __cplusplus
slouken@0
   254
}
slouken@0
   255
#endif
slouken@0
   256
#include "close_code.h"
slouken@0
   257
slouken@10638
   258
#endif /* SDL_endian_h_ */
slouken@1895
   259
slouken@1895
   260
/* vi: set ts=4 sw=4 expandtab: */