cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_endian.h (6516B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21
     22/**
     23 *  \file SDL_endian.h
     24 *
     25 *  Functions for reading and writing endian-specific values
     26 */
     27
     28#ifndef SDL_endian_h_
     29#define SDL_endian_h_
     30
     31#include "SDL_stdinc.h"
     32
     33/**
     34 *  \name The two types of endianness
     35 */
     36/* @{ */
     37#define SDL_LIL_ENDIAN  1234
     38#define SDL_BIG_ENDIAN  4321
     39/* @} */
     40
     41#ifndef SDL_BYTEORDER           /* Not defined in SDL_config.h? */
     42#ifdef __linux__
     43#include <endian.h>
     44#define SDL_BYTEORDER  __BYTE_ORDER
     45#elif defined(__OpenBSD__)
     46#include <endian.h>
     47#define SDL_BYTEORDER  BYTE_ORDER
     48#else
     49#if defined(__hppa__) || \
     50    defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
     51    (defined(__MIPS__) && defined(__MIPSEB__)) || \
     52    defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
     53    defined(__sparc__)
     54#define SDL_BYTEORDER   SDL_BIG_ENDIAN
     55#else
     56#define SDL_BYTEORDER   SDL_LIL_ENDIAN
     57#endif
     58#endif /* __linux__ */
     59#endif /* !SDL_BYTEORDER */
     60
     61
     62#include "begin_code.h"
     63/* Set up for C function definitions, even when using C++ */
     64#ifdef __cplusplus
     65extern "C" {
     66#endif
     67
     68/**
     69 *  \file SDL_endian.h
     70 */
     71#if defined(__GNUC__) && defined(__i386__) && \
     72   !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
     73SDL_FORCE_INLINE Uint16
     74SDL_Swap16(Uint16 x)
     75{
     76  __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
     77    return x;
     78}
     79#elif defined(__GNUC__) && defined(__x86_64__)
     80SDL_FORCE_INLINE Uint16
     81SDL_Swap16(Uint16 x)
     82{
     83  __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
     84    return x;
     85}
     86#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
     87SDL_FORCE_INLINE Uint16
     88SDL_Swap16(Uint16 x)
     89{
     90    int result;
     91
     92  __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
     93    return (Uint16)result;
     94}
     95#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
     96SDL_FORCE_INLINE Uint16
     97SDL_Swap16(Uint16 x)
     98{
     99  __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
    100    return x;
    101}
    102#elif defined(__WATCOMC__) && defined(__386__)
    103extern _inline Uint16 SDL_Swap16(Uint16);
    104#pragma aux SDL_Swap16 = \
    105  "xchg al, ah" \
    106  parm   [ax]   \
    107  modify [ax];
    108#else
    109SDL_FORCE_INLINE Uint16
    110SDL_Swap16(Uint16 x)
    111{
    112    return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
    113}
    114#endif
    115
    116#if defined(__GNUC__) && defined(__i386__)
    117SDL_FORCE_INLINE Uint32
    118SDL_Swap32(Uint32 x)
    119{
    120  __asm__("bswap %0": "=r"(x):"0"(x));
    121    return x;
    122}
    123#elif defined(__GNUC__) && defined(__x86_64__)
    124SDL_FORCE_INLINE Uint32
    125SDL_Swap32(Uint32 x)
    126{
    127  __asm__("bswapl %0": "=r"(x):"0"(x));
    128    return x;
    129}
    130#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
    131SDL_FORCE_INLINE Uint32
    132SDL_Swap32(Uint32 x)
    133{
    134    Uint32 result;
    135
    136  __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
    137  __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
    138  __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
    139    return result;
    140}
    141#elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
    142SDL_FORCE_INLINE Uint32
    143SDL_Swap32(Uint32 x)
    144{
    145  __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
    146    return x;
    147}
    148#elif defined(__WATCOMC__) && defined(__386__)
    149extern _inline Uint32 SDL_Swap32(Uint32);
    150#ifndef __SW_3 /* 486+ */
    151#pragma aux SDL_Swap32 = \
    152  "bswap eax"  \
    153  parm   [eax] \
    154  modify [eax];
    155#else  /* 386-only */
    156#pragma aux SDL_Swap32 = \
    157  "xchg al, ah"  \
    158  "ror  eax, 16" \
    159  "xchg al, ah"  \
    160  parm   [eax]   \
    161  modify [eax];
    162#endif
    163#else
    164SDL_FORCE_INLINE Uint32
    165SDL_Swap32(Uint32 x)
    166{
    167    return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
    168                                    ((x >> 8) & 0x0000FF00) | (x >> 24)));
    169}
    170#endif
    171
    172#if defined(__GNUC__) && defined(__i386__)
    173SDL_FORCE_INLINE Uint64
    174SDL_Swap64(Uint64 x)
    175{
    176    union
    177    {
    178        struct
    179        {
    180            Uint32 a, b;
    181        } s;
    182        Uint64 u;
    183    } v;
    184    v.u = x;
    185  __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
    186            "1"(v.s.
    187                b));
    188    return v.u;
    189}
    190#elif defined(__GNUC__) && defined(__x86_64__)
    191SDL_FORCE_INLINE Uint64
    192SDL_Swap64(Uint64 x)
    193{
    194  __asm__("bswapq %0": "=r"(x):"0"(x));
    195    return x;
    196}
    197#else
    198SDL_FORCE_INLINE Uint64
    199SDL_Swap64(Uint64 x)
    200{
    201    Uint32 hi, lo;
    202
    203    /* Separate into high and low 32-bit values and swap them */
    204    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
    205    x >>= 32;
    206    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
    207    x = SDL_Swap32(lo);
    208    x <<= 32;
    209    x |= SDL_Swap32(hi);
    210    return (x);
    211}
    212#endif
    213
    214
    215SDL_FORCE_INLINE float
    216SDL_SwapFloat(float x)
    217{
    218    union
    219    {
    220        float f;
    221        Uint32 ui32;
    222    } swapper;
    223    swapper.f = x;
    224    swapper.ui32 = SDL_Swap32(swapper.ui32);
    225    return swapper.f;
    226}
    227
    228
    229/**
    230 *  \name Swap to native
    231 *  Byteswap item from the specified endianness to the native endianness.
    232 */
    233/* @{ */
    234#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    235#define SDL_SwapLE16(X) (X)
    236#define SDL_SwapLE32(X) (X)
    237#define SDL_SwapLE64(X) (X)
    238#define SDL_SwapFloatLE(X)  (X)
    239#define SDL_SwapBE16(X) SDL_Swap16(X)
    240#define SDL_SwapBE32(X) SDL_Swap32(X)
    241#define SDL_SwapBE64(X) SDL_Swap64(X)
    242#define SDL_SwapFloatBE(X)  SDL_SwapFloat(X)
    243#else
    244#define SDL_SwapLE16(X) SDL_Swap16(X)
    245#define SDL_SwapLE32(X) SDL_Swap32(X)
    246#define SDL_SwapLE64(X) SDL_Swap64(X)
    247#define SDL_SwapFloatLE(X)  SDL_SwapFloat(X)
    248#define SDL_SwapBE16(X) (X)
    249#define SDL_SwapBE32(X) (X)
    250#define SDL_SwapBE64(X) (X)
    251#define SDL_SwapFloatBE(X)  (X)
    252#endif
    253/* @} *//* Swap to native */
    254
    255/* Ends C function definitions when using C++ */
    256#ifdef __cplusplus
    257}
    258#endif
    259#include "close_code.h"
    260
    261#endif /* SDL_endian_h_ */
    262
    263/* vi: set ts=4 sw=4 expandtab: */