SDL_endian.h 7.2 KB

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