SDL_endian.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <[email protected]>
  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(__aarch64__)
  84. SDL_FORCE_INLINE Uint16
  85. SDL_Swap16(Uint16 x)
  86. {
  87. __asm__("rev16 %1, %0" : "=r"(x) : "r"(x));
  88. return x;
  89. }
  90. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  91. SDL_FORCE_INLINE Uint16
  92. SDL_Swap16(Uint16 x)
  93. {
  94. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  95. return x;
  96. }
  97. #elif defined(__WATCOMC__) && defined(__386__)
  98. extern _inline Uint16 SDL_Swap16(Uint16);
  99. #pragma aux SDL_Swap16 = \
  100. "xchg al, ah" \
  101. parm [ax] \
  102. modify [ax];
  103. #else
  104. SDL_FORCE_INLINE Uint16
  105. SDL_Swap16(Uint16 x)
  106. {
  107. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  108. }
  109. #endif
  110. #if defined(__GNUC__) && defined(__i386__)
  111. SDL_FORCE_INLINE Uint32
  112. SDL_Swap32(Uint32 x)
  113. {
  114. __asm__("bswap %0": "=r"(x):"0"(x));
  115. return x;
  116. }
  117. #elif defined(__GNUC__) && defined(__x86_64__)
  118. SDL_FORCE_INLINE Uint32
  119. SDL_Swap32(Uint32 x)
  120. {
  121. __asm__("bswapl %0": "=r"(x):"0"(x));
  122. return x;
  123. }
  124. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  125. SDL_FORCE_INLINE Uint32
  126. SDL_Swap32(Uint32 x)
  127. {
  128. Uint32 result;
  129. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
  130. __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
  131. __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
  132. return result;
  133. }
  134. #elif defined(__GNUC__) && defined(__aarch64__)
  135. SDL_FORCE_INLINE Uint32
  136. SDL_Swap32(Uint32 x)
  137. {
  138. __asm__("rev %1, %0": "=r"(x):"r"(x));
  139. return x;
  140. }
  141. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  142. SDL_FORCE_INLINE Uint32
  143. SDL_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__)
  149. extern _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
  164. SDL_FORCE_INLINE Uint32
  165. SDL_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. #if defined(__GNUC__) && defined(__i386__)
  172. SDL_FORCE_INLINE Uint64
  173. SDL_Swap64(Uint64 x)
  174. {
  175. union
  176. {
  177. struct
  178. {
  179. Uint32 a, b;
  180. } s;
  181. Uint64 u;
  182. } v;
  183. v.u = x;
  184. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
  185. "1"(v.s.
  186. b));
  187. return v.u;
  188. }
  189. #elif defined(__GNUC__) && defined(__x86_64__)
  190. SDL_FORCE_INLINE Uint64
  191. SDL_Swap64(Uint64 x)
  192. {
  193. __asm__("bswapq %0": "=r"(x):"0"(x));
  194. return x;
  195. }
  196. #else
  197. SDL_FORCE_INLINE Uint64
  198. SDL_Swap64(Uint64 x)
  199. {
  200. Uint32 hi, lo;
  201. /* Separate into high and low 32-bit values and swap them */
  202. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  203. x >>= 32;
  204. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  205. x = SDL_Swap32(lo);
  206. x <<= 32;
  207. x |= SDL_Swap32(hi);
  208. return (x);
  209. }
  210. #endif
  211. SDL_FORCE_INLINE float
  212. SDL_SwapFloat(float x)
  213. {
  214. union
  215. {
  216. float f;
  217. Uint32 ui32;
  218. } swapper;
  219. swapper.f = x;
  220. swapper.ui32 = SDL_Swap32(swapper.ui32);
  221. return swapper.f;
  222. }
  223. /**
  224. * \name Swap to native
  225. * Byteswap item from the specified endianness to the native endianness.
  226. */
  227. /* @{ */
  228. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  229. #define SDL_SwapLE16(X) (X)
  230. #define SDL_SwapLE32(X) (X)
  231. #define SDL_SwapLE64(X) (X)
  232. #define SDL_SwapFloatLE(X) (X)
  233. #define SDL_SwapBE16(X) SDL_Swap16(X)
  234. #define SDL_SwapBE32(X) SDL_Swap32(X)
  235. #define SDL_SwapBE64(X) SDL_Swap64(X)
  236. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  237. #else
  238. #define SDL_SwapLE16(X) SDL_Swap16(X)
  239. #define SDL_SwapLE32(X) SDL_Swap32(X)
  240. #define SDL_SwapLE64(X) SDL_Swap64(X)
  241. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  242. #define SDL_SwapBE16(X) (X)
  243. #define SDL_SwapBE32(X) (X)
  244. #define SDL_SwapBE64(X) (X)
  245. #define SDL_SwapFloatBE(X) (X)
  246. #endif
  247. /* @} *//* Swap to native */
  248. /* Ends C function definitions when using C++ */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #include "close_code.h"
  253. #endif /* SDL_endian_h_ */
  254. /* vi: set ts=4 sw=4 expandtab: */