include/SDL_endian.h
author Manuel Alfayate Corchete
Sat, 23 Jan 2021 22:47:06 +0100
changeset 14788 592afde63bb7
parent 14690 6ec80d45949c
permissions -rw-r--r--
[KMS/DRM] Bugfix for #5489: Non-FULLSCREEN windows incorrecty use videomode changing to look fullscreen.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2021 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 #ifdef _MSC_VER
    34 #include <intrin.h>
    35 #endif
    36 
    37 /**
    38  *  \name The two types of endianness
    39  */
    40 /* @{ */
    41 #define SDL_LIL_ENDIAN  1234
    42 #define SDL_BIG_ENDIAN  4321
    43 /* @} */
    44 
    45 #ifndef SDL_BYTEORDER           /* Not defined in SDL_config.h? */
    46 #ifdef __linux__
    47 #include <endian.h>
    48 #define SDL_BYTEORDER  __BYTE_ORDER
    49 #elif defined(__OpenBSD__)
    50 #include <endian.h>
    51 #define SDL_BYTEORDER  BYTE_ORDER
    52 #elif defined(__FreeBSD__)
    53 #include <sys/endian.h>
    54 #define SDL_BYTEORDER  BYTE_ORDER
    55 #else
    56 #if defined(__hppa__) || \
    57     defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
    58     (defined(__MIPS__) && defined(__MIPSEB__)) || \
    59     defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
    60     defined(__sparc__)
    61 #define SDL_BYTEORDER   SDL_BIG_ENDIAN
    62 #else
    63 #define SDL_BYTEORDER   SDL_LIL_ENDIAN
    64 #endif
    65 #endif /* __linux__ */
    66 #endif /* !SDL_BYTEORDER */
    67 
    68 
    69 #include "begin_code.h"
    70 /* Set up for C function definitions, even when using C++ */
    71 #ifdef __cplusplus
    72 extern "C" {
    73 #endif
    74 
    75 /**
    76  *  \file SDL_endian.h
    77  */
    78 #if (defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2))) || \
    79     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
    80 #define SDL_Swap16(x) __builtin_bswap16(x)
    81 #elif defined(__GNUC__) && defined(__i386__) && \
    82    !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
    83 SDL_FORCE_INLINE Uint16
    84 SDL_Swap16(Uint16 x)
    85 {
    86   __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
    87     return x;
    88 }
    89 #elif defined(__GNUC__) && defined(__x86_64__)
    90 SDL_FORCE_INLINE Uint16
    91 SDL_Swap16(Uint16 x)
    92 {
    93   __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
    94     return x;
    95 }
    96 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
    97 SDL_FORCE_INLINE Uint16
    98 SDL_Swap16(Uint16 x)
    99 {
   100     int result;
   101 
   102   __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
   103     return (Uint16)result;
   104 }
   105 #elif defined(__GNUC__) && defined(__aarch64__)
   106 SDL_FORCE_INLINE Uint16
   107 SDL_Swap16(Uint16 x)
   108 {
   109   __asm__("rev16 %w1, %w0" : "=r"(x) : "r"(x));
   110   return x;
   111 }
   112 #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
   113 SDL_FORCE_INLINE Uint16
   114 SDL_Swap16(Uint16 x)
   115 {
   116   __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
   117     return x;
   118 }
   119 #elif defined(_MSC_VER)
   120 #pragma intrinsic(_byteswap_ushort)
   121 #define SDL_Swap16(x) _byteswap_ushort(x)
   122 #elif defined(__WATCOMC__) && defined(__386__)
   123 extern _inline Uint16 SDL_Swap16(Uint16);
   124 #pragma aux SDL_Swap16 = \
   125   "xchg al, ah" \
   126   parm   [ax]   \
   127   modify [ax];
   128 #else
   129 SDL_FORCE_INLINE Uint16
   130 SDL_Swap16(Uint16 x)
   131 {
   132     return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
   133 }
   134 #endif
   135 
   136 #if (defined(__clang__) && (__clang_major__ > 2 || (__clang_major__ == 2 && __clang_minor__ >= 6))) || \
   137     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
   138 #define SDL_Swap32(x) __builtin_bswap32(x)
   139 #elif defined(__GNUC__) && defined(__i386__) && \
   140    !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
   141 SDL_FORCE_INLINE Uint32
   142 SDL_Swap32(Uint32 x)
   143 {
   144   __asm__("bswap %0": "=r"(x):"0"(x));
   145     return x;
   146 }
   147 #elif defined(__GNUC__) && defined(__x86_64__)
   148 SDL_FORCE_INLINE Uint32
   149 SDL_Swap32(Uint32 x)
   150 {
   151   __asm__("bswapl %0": "=r"(x):"0"(x));
   152     return x;
   153 }
   154 #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
   155 SDL_FORCE_INLINE Uint32
   156 SDL_Swap32(Uint32 x)
   157 {
   158     Uint32 result;
   159 
   160   __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
   161   __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
   162   __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
   163     return result;
   164 }
   165 #elif defined(__GNUC__) && defined(__aarch64__)
   166 SDL_FORCE_INLINE Uint32
   167 SDL_Swap32(Uint32 x)
   168 {
   169   __asm__("rev %w1, %w0": "=r"(x):"r"(x));
   170   return x;
   171 }
   172 #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
   173 SDL_FORCE_INLINE Uint32
   174 SDL_Swap32(Uint32 x)
   175 {
   176   __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
   177     return x;
   178 }
   179 #elif defined(__WATCOMC__) && defined(__386__)
   180 extern _inline Uint32 SDL_Swap32(Uint32);
   181 #ifndef __SW_3 /* 486+ */
   182 #pragma aux SDL_Swap32 = \
   183   "bswap eax"  \
   184   parm   [eax] \
   185   modify [eax];
   186 #else  /* 386-only */
   187 #pragma aux SDL_Swap32 = \
   188   "xchg al, ah"  \
   189   "ror  eax, 16" \
   190   "xchg al, ah"  \
   191   parm   [eax]   \
   192   modify [eax];
   193 #endif
   194 #elif defined(_MSC_VER)
   195 #pragma intrinsic(_byteswap_ulong)
   196 #define SDL_Swap32(x) _byteswap_ulong(x)
   197 #else
   198 SDL_FORCE_INLINE Uint32
   199 SDL_Swap32(Uint32 x)
   200 {
   201     return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
   202                                     ((x >> 8) & 0x0000FF00) | (x >> 24)));
   203 }
   204 #endif
   205 
   206 #if (defined(__clang__) && (__clang_major__ > 2 || (__clang_major__ == 2 && __clang_minor__ >= 6))) || \
   207     (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
   208 #define SDL_Swap64(x) __builtin_bswap64(x)
   209 #elif defined(__GNUC__) && defined(__i386__) && \
   210    !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
   211 SDL_FORCE_INLINE Uint64
   212 SDL_Swap64(Uint64 x)
   213 {
   214     union
   215     {
   216         struct
   217         {
   218             Uint32 a, b;
   219         } s;
   220         Uint64 u;
   221     } v;
   222     v.u = x;
   223   __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
   224             "1"(v.s.
   225                 b));
   226     return v.u;
   227 }
   228 #elif defined(__GNUC__) && defined(__x86_64__)
   229 SDL_FORCE_INLINE Uint64
   230 SDL_Swap64(Uint64 x)
   231 {
   232   __asm__("bswapq %0": "=r"(x):"0"(x));
   233     return x;
   234 }
   235 #elif defined(_MSC_VER)
   236 #pragma intrinsic(_byteswap_uint64)
   237 #define SDL_Swap64(x) _byteswap_uint64(x)
   238 #else
   239 SDL_FORCE_INLINE Uint64
   240 SDL_Swap64(Uint64 x)
   241 {
   242     Uint32 hi, lo;
   243 
   244     /* Separate into high and low 32-bit values and swap them */
   245     lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
   246     x >>= 32;
   247     hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
   248     x = SDL_Swap32(lo);
   249     x <<= 32;
   250     x |= SDL_Swap32(hi);
   251     return (x);
   252 }
   253 #endif
   254 
   255 
   256 SDL_FORCE_INLINE float
   257 SDL_SwapFloat(float x)
   258 {
   259     union
   260     {
   261         float f;
   262         Uint32 ui32;
   263     } swapper;
   264     swapper.f = x;
   265     swapper.ui32 = SDL_Swap32(swapper.ui32);
   266     return swapper.f;
   267 }
   268 
   269 
   270 /**
   271  *  \name Swap to native
   272  *  Byteswap item from the specified endianness to the native endianness.
   273  */
   274 /* @{ */
   275 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   276 #define SDL_SwapLE16(X) (X)
   277 #define SDL_SwapLE32(X) (X)
   278 #define SDL_SwapLE64(X) (X)
   279 #define SDL_SwapFloatLE(X)  (X)
   280 #define SDL_SwapBE16(X) SDL_Swap16(X)
   281 #define SDL_SwapBE32(X) SDL_Swap32(X)
   282 #define SDL_SwapBE64(X) SDL_Swap64(X)
   283 #define SDL_SwapFloatBE(X)  SDL_SwapFloat(X)
   284 #else
   285 #define SDL_SwapLE16(X) SDL_Swap16(X)
   286 #define SDL_SwapLE32(X) SDL_Swap32(X)
   287 #define SDL_SwapLE64(X) SDL_Swap64(X)
   288 #define SDL_SwapFloatLE(X)  SDL_SwapFloat(X)
   289 #define SDL_SwapBE16(X) (X)
   290 #define SDL_SwapBE32(X) (X)
   291 #define SDL_SwapBE64(X) (X)
   292 #define SDL_SwapFloatBE(X)  (X)
   293 #endif
   294 /* @} *//* Swap to native */
   295 
   296 /* Ends C function definitions when using C++ */
   297 #ifdef __cplusplus
   298 }
   299 #endif
   300 #include "close_code.h"
   301 
   302 #endif /* SDL_endian_h_ */
   303 
   304 /* vi: set ts=4 sw=4 expandtab: */