SDL_endian.h 8.4 KB

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