SDL standard library

Dependents:   H261_encoder

Committer:
miruga27
Date:
Wed Sep 07 18:46:53 2016 +0000
Revision:
0:dda4f4550403
7/09/2016;

Who changed what in which revision?

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