include/SDL_endian.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Oct 2009 13:31:58 +0000
changeset 3407 d3baf5ac4e37
parent 3067 bcd41b269091
child 3630 efb79807afe1
permissions -rw-r--r--
Partial fix for bug #859

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