You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

261 lines
6.3 KiB

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