🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
8.6 KiB

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