SDL_endian.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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 <SDL3/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
  49. #ifdef SDL_PLATFORM_LINUX
  50. #include <endian.h>
  51. #define SDL_BYTEORDER __BYTE_ORDER
  52. #elif defined(SDL_PLATFORM_OPENBSD) || defined(__DragonFly__)
  53. #include <endian.h>
  54. #define SDL_BYTEORDER BYTE_ORDER
  55. #elif defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_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 /* SDL_PLATFORM_LINUX */
  78. #endif /* !SDL_BYTEORDER */
  79. #ifndef SDL_FLOATWORDORDER
  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 <SDL3/SDL_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. /**
  125. * Byte swap 16-bit integer.
  126. */
  127. #if HAS_BUILTIN_BSWAP16
  128. #define SDL_Swap16(x) __builtin_bswap16(x)
  129. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  130. #pragma intrinsic(_byteswap_ushort)
  131. #define SDL_Swap16(x) _byteswap_ushort(x)
  132. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  133. SDL_FORCE_INLINE Uint16
  134. SDL_Swap16(Uint16 x)
  135. {
  136. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  137. return x;
  138. }
  139. #elif defined(__x86_64__)
  140. SDL_FORCE_INLINE Uint16
  141. SDL_Swap16(Uint16 x)
  142. {
  143. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  144. return x;
  145. }
  146. #elif (defined(__powerpc__) || defined(__ppc__))
  147. SDL_FORCE_INLINE Uint16
  148. SDL_Swap16(Uint16 x)
  149. {
  150. int result;
  151. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  152. return (Uint16)result;
  153. }
  154. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  155. SDL_FORCE_INLINE Uint16
  156. SDL_Swap16(Uint16 x)
  157. {
  158. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  159. return x;
  160. }
  161. #elif defined(__WATCOMC__) && defined(__386__)
  162. extern __inline Uint16 SDL_Swap16(Uint16);
  163. #pragma aux SDL_Swap16 = \
  164. "xchg al, ah" \
  165. parm [ax] \
  166. modify [ax];
  167. #else
  168. SDL_FORCE_INLINE Uint16
  169. SDL_Swap16(Uint16 x)
  170. {
  171. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  172. }
  173. #endif
  174. /**
  175. * Byte swap 32-bit integer.
  176. */
  177. #if HAS_BUILTIN_BSWAP32
  178. #define SDL_Swap32(x) __builtin_bswap32(x)
  179. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  180. #pragma intrinsic(_byteswap_ulong)
  181. #define SDL_Swap32(x) _byteswap_ulong(x)
  182. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  183. SDL_FORCE_INLINE Uint32
  184. SDL_Swap32(Uint32 x)
  185. {
  186. __asm__("bswap %0": "=r"(x):"0"(x));
  187. return x;
  188. }
  189. #elif defined(__x86_64__)
  190. SDL_FORCE_INLINE Uint32
  191. SDL_Swap32(Uint32 x)
  192. {
  193. __asm__("bswapl %0": "=r"(x):"0"(x));
  194. return x;
  195. }
  196. #elif (defined(__powerpc__) || defined(__ppc__))
  197. SDL_FORCE_INLINE Uint32
  198. SDL_Swap32(Uint32 x)
  199. {
  200. Uint32 result;
  201. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
  202. __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
  203. __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
  204. return result;
  205. }
  206. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  207. SDL_FORCE_INLINE Uint32
  208. SDL_Swap32(Uint32 x)
  209. {
  210. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  211. return x;
  212. }
  213. #elif defined(__WATCOMC__) && defined(__386__)
  214. extern __inline Uint32 SDL_Swap32(Uint32);
  215. #pragma aux SDL_Swap32 = \
  216. "bswap eax" \
  217. parm [eax] \
  218. modify [eax];
  219. #else
  220. SDL_FORCE_INLINE Uint32
  221. SDL_Swap32(Uint32 x)
  222. {
  223. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  224. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  225. }
  226. #endif
  227. /**
  228. * Byte swap 64-bit integer.
  229. */
  230. #if HAS_BUILTIN_BSWAP64
  231. #define SDL_Swap64(x) __builtin_bswap64(x)
  232. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  233. #pragma intrinsic(_byteswap_uint64)
  234. #define SDL_Swap64(x) _byteswap_uint64(x)
  235. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  236. SDL_FORCE_INLINE Uint64
  237. SDL_Swap64(Uint64 x)
  238. {
  239. union {
  240. struct {
  241. Uint32 a, b;
  242. } s;
  243. Uint64 u;
  244. } v;
  245. v.u = x;
  246. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  247. : "=r"(v.s.a), "=r"(v.s.b)
  248. : "0" (v.s.a), "1"(v.s.b));
  249. return v.u;
  250. }
  251. #elif defined(__x86_64__)
  252. SDL_FORCE_INLINE Uint64
  253. SDL_Swap64(Uint64 x)
  254. {
  255. __asm__("bswapq %0": "=r"(x):"0"(x));
  256. return x;
  257. }
  258. #elif defined(__WATCOMC__) && defined(__386__)
  259. extern __inline Uint64 SDL_Swap64(Uint64);
  260. #pragma aux SDL_Swap64 = \
  261. "bswap eax" \
  262. "bswap edx" \
  263. "xchg eax,edx" \
  264. parm [eax edx] \
  265. modify [eax edx];
  266. #else
  267. SDL_FORCE_INLINE Uint64
  268. SDL_Swap64(Uint64 x)
  269. {
  270. Uint32 hi, lo;
  271. /* Separate into high and low 32-bit values and swap them */
  272. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  273. x >>= 32;
  274. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  275. x = SDL_Swap32(lo);
  276. x <<= 32;
  277. x |= SDL_Swap32(hi);
  278. return (x);
  279. }
  280. #endif
  281. /**
  282. * Byte swap floating point number.
  283. */
  284. SDL_FORCE_INLINE float
  285. SDL_SwapFloat(float x)
  286. {
  287. union {
  288. float f;
  289. Uint32 ui32;
  290. } swapper;
  291. swapper.f = x;
  292. swapper.ui32 = SDL_Swap32(swapper.ui32);
  293. return swapper.f;
  294. }
  295. /* remove extra macros */
  296. #undef HAS_BROKEN_BSWAP
  297. #undef HAS_BUILTIN_BSWAP16
  298. #undef HAS_BUILTIN_BSWAP32
  299. #undef HAS_BUILTIN_BSWAP64
  300. /**
  301. * \name Swap to native
  302. * Byteswap item from the specified endianness to the native endianness.
  303. */
  304. /**
  305. * \def SDL_SwapLE16
  306. * Swap 16-bit little endian integer to 16-bit native endian integer.
  307. */
  308. /**
  309. * \def SDL_SwapLE32
  310. * Swap 32-bit little endian integer to 32-bit native endian integer.
  311. */
  312. /**
  313. * \def SDL_SwapLE64
  314. * Swap 64-bit little endian integer to 64-bit native endian integer.
  315. */
  316. /**
  317. * \def SDL_SwapFloatLE
  318. * Swap little endian float to native endian float.
  319. */
  320. /**
  321. * \def SDL_SwapBE16
  322. * Swap 16-bit big endian integer to 16-bit native endian integer.
  323. */
  324. /**
  325. * \def SDL_SwapBE32
  326. * Swap 32-bit big endian integer to 32-bit native endian integer.
  327. */
  328. /**
  329. * \def SDL_SwapBE64
  330. * Swap 64-bit big endian integer to 64-bit native endian integer.
  331. */
  332. /**
  333. * \def SDL_SwapFloatBE
  334. * Swap endian float to native endian float.
  335. */
  336. /* @{ */
  337. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  338. #define SDL_SwapLE16(X) (X)
  339. #define SDL_SwapLE32(X) (X)
  340. #define SDL_SwapLE64(X) (X)
  341. #define SDL_SwapFloatLE(X) (X)
  342. #define SDL_SwapBE16(X) SDL_Swap16(X)
  343. #define SDL_SwapBE32(X) SDL_Swap32(X)
  344. #define SDL_SwapBE64(X) SDL_Swap64(X)
  345. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  346. #else
  347. #define SDL_SwapLE16(X) SDL_Swap16(X)
  348. #define SDL_SwapLE32(X) SDL_Swap32(X)
  349. #define SDL_SwapLE64(X) SDL_Swap64(X)
  350. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  351. #define SDL_SwapBE16(X) (X)
  352. #define SDL_SwapBE32(X) (X)
  353. #define SDL_SwapBE64(X) (X)
  354. #define SDL_SwapFloatBE(X) (X)
  355. #endif
  356. /* @} *//* Swap to native */
  357. /* Ends C function definitions when using C++ */
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #include <SDL3/SDL_close_code.h>
  362. #endif /* SDL_endian_h_ */