SDL_endian.h 8.5 KB

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