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