SDL_endian.h 8.3 KB

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