SDL_endian.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. #elif defined(__OpenBSD__)
  38. #include <endian.h>
  39. #define SDL_BYTEORDER BYTE_ORDER
  40. #else
  41. #if defined(__hppa__) || \
  42. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  43. (defined(__MIPS__) && defined(__MIPSEB__)) || \
  44. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  45. defined(__sparc__)
  46. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  47. #else
  48. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  49. #endif
  50. #endif /* __linux__ */
  51. #endif /* !SDL_BYTEORDER */
  52. #include "begin_code.h"
  53. /* Set up for C function definitions, even when using C++ */
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * \file SDL_endian.h
  59. */
  60. #if defined(__GNUC__) && defined(__i386__) && \
  61. !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
  62. SDL_FORCE_INLINE Uint16
  63. SDL_Swap16(Uint16 x)
  64. {
  65. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  66. return x;
  67. }
  68. #elif defined(__GNUC__) && defined(__x86_64__)
  69. SDL_FORCE_INLINE Uint16
  70. SDL_Swap16(Uint16 x)
  71. {
  72. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  73. return x;
  74. }
  75. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  76. SDL_FORCE_INLINE Uint16
  77. SDL_Swap16(Uint16 x)
  78. {
  79. int result;
  80. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  81. return (Uint16)result;
  82. }
  83. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  84. SDL_FORCE_INLINE Uint16
  85. SDL_Swap16(Uint16 x)
  86. {
  87. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  88. return x;
  89. }
  90. #elif defined(__WATCOMC__) && defined(__386__)
  91. extern _inline Uint16 SDL_Swap16(Uint16);
  92. #pragma aux SDL_Swap16 = \
  93. "xchg al, ah" \
  94. parm [ax] \
  95. modify [ax];
  96. #else
  97. SDL_FORCE_INLINE Uint16
  98. SDL_Swap16(Uint16 x)
  99. {
  100. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  101. }
  102. #endif
  103. #if defined(__GNUC__) && defined(__i386__)
  104. SDL_FORCE_INLINE Uint32
  105. SDL_Swap32(Uint32 x)
  106. {
  107. __asm__("bswap %0": "=r"(x):"0"(x));
  108. return x;
  109. }
  110. #elif defined(__GNUC__) && defined(__x86_64__)
  111. SDL_FORCE_INLINE Uint32
  112. SDL_Swap32(Uint32 x)
  113. {
  114. __asm__("bswapl %0": "=r"(x):"0"(x));
  115. return x;
  116. }
  117. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  118. SDL_FORCE_INLINE Uint32
  119. SDL_Swap32(Uint32 x)
  120. {
  121. Uint32 result;
  122. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
  123. __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
  124. __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
  125. return result;
  126. }
  127. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  128. SDL_FORCE_INLINE Uint32
  129. SDL_Swap32(Uint32 x)
  130. {
  131. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  132. return x;
  133. }
  134. #elif defined(__WATCOMC__) && defined(__386__)
  135. extern _inline Uint32 SDL_Swap32(Uint32);
  136. #ifndef __SW_3 /* 486+ */
  137. #pragma aux SDL_Swap32 = \
  138. "bswap eax" \
  139. parm [eax] \
  140. modify [eax];
  141. #else /* 386-only */
  142. #pragma aux SDL_Swap32 = \
  143. "xchg al, ah" \
  144. "ror eax, 16" \
  145. "xchg al, ah" \
  146. parm [eax] \
  147. modify [eax];
  148. #endif
  149. #else
  150. SDL_FORCE_INLINE Uint32
  151. SDL_Swap32(Uint32 x)
  152. {
  153. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  154. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  155. }
  156. #endif
  157. #if defined(__GNUC__) && defined(__i386__)
  158. SDL_FORCE_INLINE Uint64
  159. SDL_Swap64(Uint64 x)
  160. {
  161. union
  162. {
  163. struct
  164. {
  165. Uint32 a, b;
  166. } s;
  167. Uint64 u;
  168. } v;
  169. v.u = x;
  170. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
  171. "1"(v.s.
  172. b));
  173. return v.u;
  174. }
  175. #elif defined(__GNUC__) && defined(__x86_64__)
  176. SDL_FORCE_INLINE Uint64
  177. SDL_Swap64(Uint64 x)
  178. {
  179. __asm__("bswapq %0": "=r"(x):"0"(x));
  180. return x;
  181. }
  182. #else
  183. SDL_FORCE_INLINE Uint64
  184. SDL_Swap64(Uint64 x)
  185. {
  186. Uint32 hi, lo;
  187. /* Separate into high and low 32-bit values and swap them */
  188. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  189. x >>= 32;
  190. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  191. x = SDL_Swap32(lo);
  192. x <<= 32;
  193. x |= SDL_Swap32(hi);
  194. return (x);
  195. }
  196. #endif
  197. SDL_FORCE_INLINE float
  198. SDL_SwapFloat(float x)
  199. {
  200. union
  201. {
  202. float f;
  203. Uint32 ui32;
  204. } swapper;
  205. swapper.f = x;
  206. swapper.ui32 = SDL_Swap32(swapper.ui32);
  207. return swapper.f;
  208. }
  209. /**
  210. * \name Swap to native
  211. * Byteswap item from the specified endianness to the native endianness.
  212. */
  213. /* @{ */
  214. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  215. #define SDL_SwapLE16(X) (X)
  216. #define SDL_SwapLE32(X) (X)
  217. #define SDL_SwapLE64(X) (X)
  218. #define SDL_SwapFloatLE(X) (X)
  219. #define SDL_SwapBE16(X) SDL_Swap16(X)
  220. #define SDL_SwapBE32(X) SDL_Swap32(X)
  221. #define SDL_SwapBE64(X) SDL_Swap64(X)
  222. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  223. #else
  224. #define SDL_SwapLE16(X) SDL_Swap16(X)
  225. #define SDL_SwapLE32(X) SDL_Swap32(X)
  226. #define SDL_SwapLE64(X) SDL_Swap64(X)
  227. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  228. #define SDL_SwapBE16(X) (X)
  229. #define SDL_SwapBE32(X) (X)
  230. #define SDL_SwapBE64(X) (X)
  231. #define SDL_SwapFloatBE(X) (X)
  232. #endif
  233. /* @} *//* Swap to native */
  234. /* Ends C function definitions when using C++ */
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #include "close_code.h"
  239. #endif /* SDL_endian_h_ */
  240. /* vi: set ts=4 sw=4 expandtab: */