include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1475 d45aefcac017
child 1984 b910bcabec26
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1895
    24
 * \file SDL_endian.h
slouken@1895
    25
 *
slouken@1895
    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@1354
    34
/* The two types of endianness */
slouken@1354
    35
#define SDL_LIL_ENDIAN	1234
slouken@1354
    36
#define SDL_BIG_ENDIAN	4321
slouken@0
    37
slouken@1895
    38
#ifndef SDL_BYTEORDER           /* Not defined in SDL_config.h? */
slouken@1474
    39
#if defined(__hppa__) || \
slouken@1475
    40
    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
slouken@1474
    41
    (defined(__MIPS__) && defined(__MISPEB__)) || \
slouken@1475
    42
    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
slouken@1474
    43
    defined(__sparc__)
slouken@1474
    44
#define SDL_BYTEORDER	SDL_BIG_ENDIAN
slouken@1474
    45
#else
slouken@1354
    46
#define SDL_BYTEORDER	SDL_LIL_ENDIAN
slouken@1354
    47
#endif
slouken@1354
    48
#endif /* !SDL_BYTEORDER */
slouken@1354
    49
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@1895
    54
/* *INDENT-OFF* */
slouken@0
    55
extern "C" {
slouken@1895
    56
/* *INDENT-ON* */
slouken@0
    57
#endif
slouken@0
    58
slouken@1369
    59
/* Use inline functions for compilers that support them, and static
slouken@0
    60
   functions for those that do not.  Because these functions become
slouken@1369
    61
   static for compilers that do not support inline functions, this
slouken@0
    62
   header should only be included in files that actually use them.
slouken@0
    63
*/
slouken@1372
    64
#if defined(__GNUC__) && defined(__i386__) && \
slouken@1368
    65
   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
slouken@1895
    66
static __inline__ Uint16
slouken@1895
    67
SDL_Swap16(Uint16 x)
slouken@848
    68
{
slouken@1895
    69
  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
slouken@1895
    70
    return x;
slouken@849
    71
}
slouken@849
    72
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
    73
static __inline__ Uint16
slouken@1895
    74
SDL_Swap16(Uint16 x)
slouken@849
    75
{
slouken@1895
    76
  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
slouken@1895
    77
    return x;
slouken@849
    78
}
slouken@859
    79
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
    80
static __inline__ Uint16
slouken@1895
    81
SDL_Swap16(Uint16 x)
slouken@849
    82
{
slouken@1895
    83
    Uint16 result;
slouken@849
    84
slouken@1895
    85
  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
slouken@1895
    86
    return result;
slouken@848
    87
}
patmandin@1044
    88
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
slouken@1895
    89
static __inline__ Uint16
slouken@1895
    90
SDL_Swap16(Uint16 x)
patmandin@985
    91
{
slouken@1895
    92
  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
    93
    return x;
patmandin@985
    94
}
slouken@848
    95
#else
slouken@1895
    96
static __inline__ Uint16
slouken@1895
    97
SDL_Swap16(Uint16 x)
slouken@1895
    98
{
slouken@1895
    99
    return ((x << 8) | (x >> 8));
slouken@0
   100
}
slouken@0
   101
#endif
slouken@848
   102
slouken@849
   103
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   104
static __inline__ Uint32
slouken@1895
   105
SDL_Swap32(Uint32 x)
slouken@848
   106
{
slouken@1895
   107
  __asm__("bswap %0": "=r"(x):"0"(x));
slouken@1895
   108
    return x;
slouken@849
   109
}
slouken@849
   110
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   111
static __inline__ Uint32
slouken@1895
   112
SDL_Swap32(Uint32 x)
slouken@849
   113
{
slouken@1895
   114
  __asm__("bswapl %0": "=r"(x):"0"(x));
slouken@1895
   115
    return x;
slouken@849
   116
}
slouken@859
   117
#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@1895
   118
static __inline__ Uint32
slouken@1895
   119
SDL_Swap32(Uint32 x)
slouken@849
   120
{
slouken@1895
   121
    Uint32 result;
slouken@849
   122
slouken@1895
   123
  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
slouken@1895
   124
  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   125
  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
slouken@1895
   126
    return result;
slouken@848
   127
}
patmandin@1044
   128
#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__))
slouken@1895
   129
static __inline__ Uint32
slouken@1895
   130
SDL_Swap32(Uint32 x)
patmandin@985
   131
{
slouken@1895
   132
  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
slouken@1895
   133
    return x;
patmandin@985
   134
}
slouken@848
   135
#else
slouken@1895
   136
static __inline__ Uint32
slouken@1895
   137
SDL_Swap32(Uint32 x)
slouken@1895
   138
{
slouken@1895
   139
    return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) |
slouken@1895
   140
            (x >> 24));
slouken@0
   141
}
slouken@0
   142
#endif
slouken@848
   143
slouken@0
   144
#ifdef SDL_HAS_64BIT_TYPE
slouken@849
   145
#if defined(__GNUC__) && defined(__i386__)
slouken@1895
   146
static __inline__ Uint64
slouken@1895
   147
SDL_Swap64(Uint64 x)
slouken@849
   148
{
slouken@1895
   149
    union
slouken@1895
   150
    {
slouken@1895
   151
        struct
slouken@1895
   152
        {
slouken@1895
   153
            Uint32 a, b;
slouken@1895
   154
        } s;
slouken@1895
   155
        Uint64 u;
slouken@1895
   156
    } v;
slouken@1895
   157
    v.u = x;
slouken@1895
   158
  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
slouken@1895
   159
            "1"(v.s.
slouken@1895
   160
                b));
slouken@1895
   161
    return v.u;
slouken@849
   162
}
slouken@849
   163
#elif defined(__GNUC__) && defined(__x86_64__)
slouken@1895
   164
static __inline__ Uint64
slouken@1895
   165
SDL_Swap64(Uint64 x)
slouken@849
   166
{
slouken@1895
   167
  __asm__("bswapq %0": "=r"(x):"0"(x));
slouken@1895
   168
    return x;
slouken@849
   169
}
slouken@849
   170
#else
slouken@1895
   171
static __inline__ Uint64
slouken@1895
   172
SDL_Swap64(Uint64 x)
slouken@849
   173
{
slouken@1895
   174
    Uint32 hi, lo;
slouken@0
   175
slouken@1895
   176
    /* Separate into high and low 32-bit values and swap them */
slouken@1895
   177
    lo = (Uint32) (x & 0xFFFFFFFF);
slouken@1895
   178
    x >>= 32;
slouken@1895
   179
    hi = (Uint32) (x & 0xFFFFFFFF);
slouken@1895
   180
    x = SDL_Swap32(lo);
slouken@1895
   181
    x <<= 32;
slouken@1895
   182
    x |= SDL_Swap32(hi);
slouken@1895
   183
    return (x);
slouken@0
   184
}
slouken@0
   185
#endif
slouken@0
   186
#else
slouken@0
   187
/* This is mainly to keep compilers from complaining in SDL code.
slouken@0
   188
   If there is no real 64-bit datatype, then compilers will complain about
slouken@0
   189
   the fake 64-bit datatype that SDL provides when it compiles user code.
slouken@0
   190
*/
slouken@0
   191
#define SDL_Swap64(X)	(X)
slouken@0
   192
#endif /* SDL_HAS_64BIT_TYPE */
slouken@0
   193
slouken@0
   194
slouken@0
   195
/* Byteswap item from the specified endianness to the native endianness */
slouken@0
   196
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@0
   197
#define SDL_SwapLE16(X)	(X)
slouken@0
   198
#define SDL_SwapLE32(X)	(X)
slouken@0
   199
#define SDL_SwapLE64(X)	(X)
slouken@0
   200
#define SDL_SwapBE16(X)	SDL_Swap16(X)
slouken@0
   201
#define SDL_SwapBE32(X)	SDL_Swap32(X)
slouken@0
   202
#define SDL_SwapBE64(X)	SDL_Swap64(X)
slouken@0
   203
#else
slouken@0
   204
#define SDL_SwapLE16(X)	SDL_Swap16(X)
slouken@0
   205
#define SDL_SwapLE32(X)	SDL_Swap32(X)
slouken@0
   206
#define SDL_SwapLE64(X)	SDL_Swap64(X)
slouken@0
   207
#define SDL_SwapBE16(X)	(X)
slouken@0
   208
#define SDL_SwapBE32(X)	(X)
slouken@0
   209
#define SDL_SwapBE64(X)	(X)
slouken@0
   210
#endif
slouken@0
   211
slouken@0
   212
/* Ends C function definitions when using C++ */
slouken@0
   213
#ifdef __cplusplus
slouken@1895
   214
/* *INDENT-OFF* */
slouken@0
   215
}
slouken@1895
   216
/* *INDENT-ON* */
slouken@0
   217
#endif
slouken@0
   218
#include "close_code.h"
slouken@0
   219
slouken@0
   220
#endif /* _SDL_endian_h */
slouken@1895
   221
slouken@1895
   222
/* vi: set ts=4 sw=4 expandtab: */