🛠️🐜 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.

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