SDL_endian.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. #if defined(_MSC_VER) && !defined(__clang__)
  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(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2))) || \
  67. (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  68. #define SDL_Swap16(x) __builtin_bswap16(x)
  69. #elif defined(__GNUC__) && defined(__i386__) && \
  70. !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
  71. SDL_FORCE_INLINE Uint16
  72. SDL_Swap16(Uint16 x)
  73. {
  74. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  75. return x;
  76. }
  77. #elif defined(__GNUC__) && defined(__x86_64__)
  78. SDL_FORCE_INLINE Uint16
  79. SDL_Swap16(Uint16 x)
  80. {
  81. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  82. return x;
  83. }
  84. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  85. SDL_FORCE_INLINE Uint16
  86. SDL_Swap16(Uint16 x)
  87. {
  88. int result;
  89. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  90. return (Uint16)result;
  91. }
  92. #elif defined(__GNUC__) && defined(__aarch64__)
  93. SDL_FORCE_INLINE Uint16
  94. SDL_Swap16(Uint16 x)
  95. {
  96. __asm__("rev16 %w1, %w0" : "=r"(x) : "r"(x));
  97. return x;
  98. }
  99. #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
  100. SDL_FORCE_INLINE Uint16
  101. SDL_Swap16(Uint16 x)
  102. {
  103. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  104. return x;
  105. }
  106. #elif defined(_MSC_VER)
  107. #pragma intrinsic(_byteswap_ushort)
  108. #define SDL_Swap16(x) _byteswap_ushort(x)
  109. #elif defined(__WATCOMC__) && defined(__386__)
  110. extern _inline Uint16 SDL_Swap16(Uint16);
  111. #pragma aux SDL_Swap16 = \
  112. "xchg al, ah" \
  113. parm [ax] \
  114. modify [ax];
  115. #else
  116. SDL_FORCE_INLINE Uint16
  117. SDL_Swap16(Uint16 x)
  118. {
  119. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  120. }
  121. #endif
  122. #if (defined(__clang__) && (__clang_major__ > 2 || (__clang_major__ == 2 && __clang_minor__ >= 6))) || \
  123. (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  124. #define SDL_Swap32(x) __builtin_bswap32(x)
  125. #elif defined(__GNUC__) && defined(__i386__) && \
  126. !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
  127. SDL_FORCE_INLINE Uint32
  128. SDL_Swap32(Uint32 x)
  129. {
  130. __asm__("bswap %0": "=r"(x):"0"(x));
  131. return x;
  132. }
  133. #elif defined(__GNUC__) && defined(__x86_64__)
  134. SDL_FORCE_INLINE Uint32
  135. SDL_Swap32(Uint32 x)
  136. {
  137. __asm__("bswapl %0": "=r"(x):"0"(x));
  138. return x;
  139. }
  140. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  141. SDL_FORCE_INLINE Uint32
  142. SDL_Swap32(Uint32 x)
  143. {
  144. Uint32 result;
  145. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
  146. __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
  147. __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
  148. return result;
  149. }
  150. #elif defined(__GNUC__) && defined(__aarch64__)
  151. SDL_FORCE_INLINE Uint32
  152. SDL_Swap32(Uint32 x)
  153. {
  154. __asm__("rev %w1, %w0": "=r"(x):"r"(x));
  155. return x;
  156. }
  157. #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
  158. SDL_FORCE_INLINE Uint32
  159. SDL_Swap32(Uint32 x)
  160. {
  161. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  162. return x;
  163. }
  164. #elif defined(__WATCOMC__) && defined(__386__)
  165. extern _inline Uint32 SDL_Swap32(Uint32);
  166. #pragma aux SDL_Swap32 = \
  167. "bswap eax" \
  168. parm [eax] \
  169. modify [eax];
  170. #elif defined(_MSC_VER)
  171. #pragma intrinsic(_byteswap_ulong)
  172. #define SDL_Swap32(x) _byteswap_ulong(x)
  173. #else
  174. SDL_FORCE_INLINE Uint32
  175. SDL_Swap32(Uint32 x)
  176. {
  177. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  178. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  179. }
  180. #endif
  181. #if (defined(__clang__) && (__clang_major__ > 2 || (__clang_major__ == 2 && __clang_minor__ >= 6))) || \
  182. (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)))
  183. #define SDL_Swap64(x) __builtin_bswap64(x)
  184. #elif 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. struct {
  191. Uint32 a, b;
  192. } s;
  193. Uint64 u;
  194. } v;
  195. v.u = x;
  196. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  197. : "=r"(v.s.a), "=r"(v.s.b)
  198. : "0" (v.s.a), "1"(v.s.b));
  199. return v.u;
  200. }
  201. #elif defined(__GNUC__) && defined(__x86_64__)
  202. SDL_FORCE_INLINE Uint64
  203. SDL_Swap64(Uint64 x)
  204. {
  205. __asm__("bswapq %0": "=r"(x):"0"(x));
  206. return x;
  207. }
  208. #elif defined(__WATCOMC__) && defined(__386__)
  209. extern _inline Uint64 SDL_Swap64(Uint64);
  210. #pragma aux SDL_Swap64 = \
  211. "bswap eax" \
  212. "bswap edx" \
  213. "xchg eax,edx" \
  214. parm [eax edx] \
  215. modify [eax edx];
  216. #elif defined(_MSC_VER)
  217. #pragma intrinsic(_byteswap_uint64)
  218. #define SDL_Swap64(x) _byteswap_uint64(x)
  219. #else
  220. SDL_FORCE_INLINE Uint64
  221. SDL_Swap64(Uint64 x)
  222. {
  223. Uint32 hi, lo;
  224. /* Separate into high and low 32-bit values and swap them */
  225. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  226. x >>= 32;
  227. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  228. x = SDL_Swap32(lo);
  229. x <<= 32;
  230. x |= SDL_Swap32(hi);
  231. return (x);
  232. }
  233. #endif
  234. SDL_FORCE_INLINE float
  235. SDL_SwapFloat(float x)
  236. {
  237. union {
  238. float f;
  239. Uint32 ui32;
  240. } swapper;
  241. swapper.f = x;
  242. swapper.ui32 = SDL_Swap32(swapper.ui32);
  243. return swapper.f;
  244. }
  245. /**
  246. * \name Swap to native
  247. * Byteswap item from the specified endianness to the native endianness.
  248. */
  249. /* @{ */
  250. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  251. #define SDL_SwapLE16(X) (X)
  252. #define SDL_SwapLE32(X) (X)
  253. #define SDL_SwapLE64(X) (X)
  254. #define SDL_SwapFloatLE(X) (X)
  255. #define SDL_SwapBE16(X) SDL_Swap16(X)
  256. #define SDL_SwapBE32(X) SDL_Swap32(X)
  257. #define SDL_SwapBE64(X) SDL_Swap64(X)
  258. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  259. #else
  260. #define SDL_SwapLE16(X) SDL_Swap16(X)
  261. #define SDL_SwapLE32(X) SDL_Swap32(X)
  262. #define SDL_SwapLE64(X) SDL_Swap64(X)
  263. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  264. #define SDL_SwapBE16(X) (X)
  265. #define SDL_SwapBE32(X) (X)
  266. #define SDL_SwapBE64(X) (X)
  267. #define SDL_SwapFloatBE(X) (X)
  268. #endif
  269. /* @} *//* Swap to native */
  270. /* Ends C function definitions when using C++ */
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #include "close_code.h"
  275. #endif /* SDL_endian_h_ */
  276. /* vi: set ts=4 sw=4 expandtab: */