SDL_endian.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. * # CategoryEndian
  20. *
  21. * Functions converting endian-specific values to different byte orders.
  22. *
  23. * These functions either unconditionally swap byte order (SDL_Swap16,
  24. * SDL_Swap32, SDL_Swap64, SDL_SwapFloat), or they swap to/from the system's
  25. * native byte order (SDL_Swap16LE, SDL_Swap16BE, SDL_Swap32LE, SDL_Swap32BE,
  26. * SDL_Swap32LE, SDL_Swap32BE, SDL_SwapFloatLE, SDL_SwapFloatBE). In the
  27. * latter case, the functionality is provided by macros that become no-ops if
  28. * a swap isn't necessary: on an x86 (littleendian) processor, SDL_Swap32LE
  29. * does nothing, but SDL_Swap32BE reverses the bytes of the data. On a PowerPC
  30. * processor (bigendian), the macros behavior is reversed.
  31. *
  32. * The swap routines are inline functions, and attempt to use compiler
  33. * intrinsics, inline assembly, and other magic to make byteswapping
  34. * efficient.
  35. */
  36. #ifndef SDL_endian_h_
  37. #define SDL_endian_h_
  38. #include <SDL3/SDL_stdinc.h>
  39. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  40. /* As of Clang 11, '_m_prefetchw' is conflicting with the winnt.h's version,
  41. so we define the needed '_m_prefetch' here as a pseudo-header, until the issue is fixed. */
  42. #ifdef __clang__
  43. #ifndef __PRFCHWINTRIN_H
  44. #define __PRFCHWINTRIN_H
  45. static __inline__ void __attribute__((__always_inline__, __nodebug__))
  46. _m_prefetch(void *__P)
  47. {
  48. __builtin_prefetch(__P, 0, 3 /* _MM_HINT_T0 */);
  49. }
  50. #endif /* __PRFCHWINTRIN_H */
  51. #endif /* __clang__ */
  52. #include <intrin.h>
  53. #endif
  54. /**
  55. * \name The two types of endianness
  56. */
  57. /* @{ */
  58. /**
  59. * A value to represent littleendian byteorder.
  60. *
  61. * This is used with the preprocessor macro SDL_BYTEORDER, to determine a
  62. * platform's byte ordering:
  63. *
  64. * ```c
  65. * #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  66. * SDL_Log("This system is littleendian.");
  67. * #endif
  68. * ```
  69. *
  70. * \since This macro is available since SDL 3.1.3.
  71. *
  72. * \sa SDL_BYTEORDER
  73. * \sa SDL_BIG_ENDIAN
  74. */
  75. #define SDL_LIL_ENDIAN 1234
  76. /**
  77. * A value to represent bigendian byteorder.
  78. *
  79. * This is used with the preprocessor macro SDL_BYTEORDER, to determine a
  80. * platform's byte ordering:
  81. *
  82. * ```c
  83. * #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  84. * SDL_Log("This system is bigendian.");
  85. * #endif
  86. * ```
  87. *
  88. * \since This macro is available since SDL 3.1.3.
  89. *
  90. * \sa SDL_BYTEORDER
  91. * \sa SDL_LIL_ENDIAN
  92. */
  93. #define SDL_BIG_ENDIAN 4321
  94. /* @} */
  95. #ifndef SDL_BYTEORDER
  96. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  97. /**
  98. * A macro that reports the target system's byte order.
  99. *
  100. * This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other
  101. * values in the future, if something else becomes popular). This can be
  102. * tested with the preprocessor, so decisions can be made at compile time.
  103. *
  104. * ```c
  105. * #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  106. * SDL_Log("This system is bigendian.");
  107. * #endif
  108. * ```
  109. *
  110. * \since This macro is available since SDL 3.1.3.
  111. *
  112. * \sa SDL_LIL_ENDIAN
  113. * \sa SDL_BIG_ENDIAN
  114. */
  115. #define SDL_BYTEORDER SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN
  116. #elif defined(SDL_PLATFORM_LINUX)
  117. #include <endian.h>
  118. #define SDL_BYTEORDER __BYTE_ORDER
  119. #elif defined(SDL_PLATFORM_SOLARIS)
  120. #include <sys/byteorder.h>
  121. #if defined(_LITTLE_ENDIAN)
  122. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  123. #elif defined(_BIG_ENDIAN)
  124. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  125. #else
  126. #error Unsupported endianness
  127. #endif
  128. #elif defined(SDL_PLATFORM_OPENBSD) || defined(__DragonFly__)
  129. #include <endian.h>
  130. #define SDL_BYTEORDER BYTE_ORDER
  131. #elif defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_NETBSD)
  132. #include <sys/endian.h>
  133. #define SDL_BYTEORDER BYTE_ORDER
  134. /* predefs from newer gcc and clang versions: */
  135. #elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)
  136. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  137. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  138. #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  139. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  140. #else
  141. #error Unsupported endianness
  142. #endif /**/
  143. #else
  144. #if defined(__hppa__) || \
  145. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  146. (defined(__MIPS__) && defined(__MIPSEB__)) || \
  147. defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__) || defined(__PPC__) || \
  148. defined(__sparc__) || defined(__sparc)
  149. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  150. #else
  151. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  152. #endif
  153. #endif /* SDL_PLATFORM_LINUX */
  154. #endif /* !SDL_BYTEORDER */
  155. #ifndef SDL_FLOATWORDORDER
  156. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  157. /**
  158. * A macro that reports the target system's floating point word order.
  159. *
  160. * This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other
  161. * values in the future, if something else becomes popular). This can be
  162. * tested with the preprocessor, so decisions can be made at compile time.
  163. *
  164. * ```c
  165. * #if SDL_FLOATWORDORDER == SDL_BIG_ENDIAN
  166. * SDL_Log("This system's floats are bigendian.");
  167. * #endif
  168. * ```
  169. *
  170. * \since This macro is available since SDL 3.1.3.
  171. *
  172. * \sa SDL_LIL_ENDIAN
  173. * \sa SDL_BIG_ENDIAN
  174. */
  175. #define SDL_FLOATWORDORDER SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN
  176. /* predefs from newer gcc versions: */
  177. #elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__FLOAT_WORD_ORDER__)
  178. #if (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  179. #define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
  180. #elif (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
  181. #define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
  182. #else
  183. #error Unsupported endianness
  184. #endif /**/
  185. #elif defined(__MAVERICK__)
  186. /* For Maverick, float words are always little-endian. */
  187. #define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
  188. #elif (defined(__arm__) || defined(__thumb__)) && !defined(__VFP_FP__) && !defined(__ARM_EABI__)
  189. /* For FPA, float words are always big-endian. */
  190. #define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
  191. #else
  192. /* By default, assume that floats words follow the memory system mode. */
  193. #define SDL_FLOATWORDORDER SDL_BYTEORDER
  194. #endif /* __FLOAT_WORD_ORDER__ */
  195. #endif /* !SDL_FLOATWORDORDER */
  196. #include <SDL3/SDL_begin_code.h>
  197. /* Set up for C function definitions, even when using C++ */
  198. #ifdef __cplusplus
  199. extern "C" {
  200. #endif
  201. /* various modern compilers may have builtin swap */
  202. #if defined(__GNUC__) || defined(__clang__)
  203. # define HAS_BUILTIN_BSWAP16 (SDL_HAS_BUILTIN(__builtin_bswap16)) || \
  204. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  205. # define HAS_BUILTIN_BSWAP32 (SDL_HAS_BUILTIN(__builtin_bswap32)) || \
  206. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  207. # define HAS_BUILTIN_BSWAP64 (SDL_HAS_BUILTIN(__builtin_bswap64)) || \
  208. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  209. /* this one is broken */
  210. # define HAS_BROKEN_BSWAP (__GNUC__ == 2 && __GNUC_MINOR__ <= 95)
  211. #else
  212. # define HAS_BUILTIN_BSWAP16 0
  213. # define HAS_BUILTIN_BSWAP32 0
  214. # define HAS_BUILTIN_BSWAP64 0
  215. # define HAS_BROKEN_BSWAP 0
  216. #endif
  217. /* Byte swap 16-bit integer. */
  218. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  219. #if HAS_BUILTIN_BSWAP16
  220. #define SDL_Swap16(x) __builtin_bswap16(x)
  221. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  222. #pragma intrinsic(_byteswap_ushort)
  223. #define SDL_Swap16(x) _byteswap_ushort(x)
  224. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  225. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  226. {
  227. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  228. return x;
  229. }
  230. #elif defined(__x86_64__)
  231. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  232. {
  233. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  234. return x;
  235. }
  236. #elif (defined(__powerpc__) || defined(__ppc__))
  237. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  238. {
  239. int result;
  240. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  241. return (Uint16)result;
  242. }
  243. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  244. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  245. {
  246. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  247. return x;
  248. }
  249. #elif defined(__WATCOMC__) && defined(__386__)
  250. extern __inline Uint16 SDL_Swap16(Uint16);
  251. #pragma aux SDL_Swap16 = \
  252. "xchg al, ah" \
  253. parm [ax] \
  254. modify [ax];
  255. #else
  256. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  257. {
  258. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  259. }
  260. #endif
  261. #endif
  262. /* Byte swap 32-bit integer. */
  263. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  264. #if HAS_BUILTIN_BSWAP32
  265. #define SDL_Swap32(x) __builtin_bswap32(x)
  266. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  267. #pragma intrinsic(_byteswap_ulong)
  268. #define SDL_Swap32(x) _byteswap_ulong(x)
  269. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  270. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  271. {
  272. __asm__("bswap %0": "=r"(x):"0"(x));
  273. return x;
  274. }
  275. #elif defined(__x86_64__)
  276. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  277. {
  278. __asm__("bswapl %0": "=r"(x):"0"(x));
  279. return x;
  280. }
  281. #elif (defined(__powerpc__) || defined(__ppc__))
  282. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  283. {
  284. Uint32 result;
  285. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
  286. __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
  287. __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
  288. return result;
  289. }
  290. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  291. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  292. {
  293. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  294. return x;
  295. }
  296. #elif defined(__WATCOMC__) && defined(__386__)
  297. extern __inline Uint32 SDL_Swap32(Uint32);
  298. #pragma aux SDL_Swap32 = \
  299. "bswap eax" \
  300. parm [eax] \
  301. modify [eax];
  302. #else
  303. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  304. {
  305. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  306. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  307. }
  308. #endif
  309. #endif
  310. /* Byte swap 64-bit integer. */
  311. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  312. #if HAS_BUILTIN_BSWAP64
  313. #define SDL_Swap64(x) __builtin_bswap64(x)
  314. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  315. #pragma intrinsic(_byteswap_uint64)
  316. #define SDL_Swap64(x) _byteswap_uint64(x)
  317. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  318. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  319. {
  320. union {
  321. struct {
  322. Uint32 a, b;
  323. } s;
  324. Uint64 u;
  325. } v;
  326. v.u = x;
  327. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  328. : "=r"(v.s.a), "=r"(v.s.b)
  329. : "0" (v.s.a), "1"(v.s.b));
  330. return v.u;
  331. }
  332. #elif defined(__x86_64__)
  333. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  334. {
  335. __asm__("bswapq %0": "=r"(x):"0"(x));
  336. return x;
  337. }
  338. #elif defined(__WATCOMC__) && defined(__386__)
  339. extern __inline Uint64 SDL_Swap64(Uint64);
  340. #pragma aux SDL_Swap64 = \
  341. "bswap eax" \
  342. "bswap edx" \
  343. "xchg eax,edx" \
  344. parm [eax edx] \
  345. modify [eax edx];
  346. #else
  347. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  348. {
  349. Uint32 hi, lo;
  350. /* Separate into high and low 32-bit values and swap them */
  351. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  352. x >>= 32;
  353. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  354. x = SDL_Swap32(lo);
  355. x <<= 32;
  356. x |= SDL_Swap32(hi);
  357. return (x);
  358. }
  359. #endif
  360. #endif
  361. /**
  362. * Byte-swap a floating point number.
  363. *
  364. * This will always byte-swap the value, whether it's currently in the native
  365. * byteorder of the system or not. You should use SDL_SwapFloatLE or
  366. * SDL_SwapFloatBE instead, in most cases.
  367. *
  368. * Note that this is a forced-inline function in a header, and not a public
  369. * API function available in the SDL library (which is to say, the code is
  370. * embedded in the calling program and the linker and dynamic loader will not
  371. * be able to find this function inside SDL itself).
  372. *
  373. * \param x the value to byte-swap.
  374. * \returns x, with its bytes in the opposite endian order.
  375. *
  376. * \threadsafety It is safe to call this function from any thread.
  377. *
  378. * \since This function is available since SDL 3.1.3.
  379. */
  380. SDL_FORCE_INLINE float SDL_SwapFloat(float x)
  381. {
  382. union {
  383. float f;
  384. Uint32 ui32;
  385. } swapper;
  386. swapper.f = x;
  387. swapper.ui32 = SDL_Swap32(swapper.ui32);
  388. return swapper.f;
  389. }
  390. /* remove extra macros */
  391. #undef HAS_BROKEN_BSWAP
  392. #undef HAS_BUILTIN_BSWAP16
  393. #undef HAS_BUILTIN_BSWAP32
  394. #undef HAS_BUILTIN_BSWAP64
  395. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  396. /**
  397. * Byte-swap an unsigned 16-bit number.
  398. *
  399. * This will always byte-swap the value, whether it's currently in the native
  400. * byteorder of the system or not. You should use SDL_Swap16LE or SDL_Swap16BE
  401. * instead, in most cases.
  402. *
  403. * Note that this is a forced-inline function in a header, and not a public
  404. * API function available in the SDL library (which is to say, the code is
  405. * embedded in the calling program and the linker and dynamic loader will not
  406. * be able to find this function inside SDL itself).
  407. *
  408. * \param x the value to byte-swap.
  409. * \returns `x`, with its bytes in the opposite endian order.
  410. *
  411. * \threadsafety It is safe to call this function from any thread.
  412. *
  413. * \since This function is available since SDL 3.1.3.
  414. */
  415. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x) { return x_but_byteswapped; }
  416. /**
  417. * Byte-swap an unsigned 32-bit number.
  418. *
  419. * This will always byte-swap the value, whether it's currently in the native
  420. * byteorder of the system or not. You should use SDL_Swap32LE or SDL_Swap32BE
  421. * instead, in most cases.
  422. *
  423. * Note that this is a forced-inline function in a header, and not a public
  424. * API function available in the SDL library (which is to say, the code is
  425. * embedded in the calling program and the linker and dynamic loader will not
  426. * be able to find this function inside SDL itself).
  427. *
  428. * \param x the value to byte-swap.
  429. * \returns `x`, with its bytes in the opposite endian order.
  430. *
  431. * \threadsafety It is safe to call this function from any thread.
  432. *
  433. * \since This function is available since SDL 3.1.3.
  434. */
  435. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x) { return x_but_byteswapped; }
  436. /**
  437. * Byte-swap an unsigned 64-bit number.
  438. *
  439. * This will always byte-swap the value, whether it's currently in the native
  440. * byteorder of the system or not. You should use SDL_Swap64LE or SDL_Swap64BE
  441. * instead, in most cases.
  442. *
  443. * Note that this is a forced-inline function in a header, and not a public
  444. * API function available in the SDL library (which is to say, the code is
  445. * embedded in the calling program and the linker and dynamic loader will not
  446. * be able to find this function inside SDL itself).
  447. *
  448. * \param x the value to byte-swap.
  449. * \returns `x`, with its bytes in the opposite endian order.
  450. *
  451. * \threadsafety It is safe to call this function from any thread.
  452. *
  453. * \since This function is available since SDL 3.1.3.
  454. */
  455. SDL_FORCE_INLINE Uint32 SDL_Swap64(Uint64 x) { return x_but_byteswapped; }
  456. /**
  457. * Swap a 16-bit value from littleendian to native byte order.
  458. *
  459. * If this is running on a littleendian system, `x` is returned unchanged.
  460. *
  461. * This macro never references `x` more than once, avoiding side effects.
  462. *
  463. * \param x the value to swap, in littleendian byte order.
  464. * \returns `x` in native byte order.
  465. *
  466. * \threadsafety It is safe to call this macro from any thread.
  467. *
  468. * \since This macro is available since SDL 3.1.3.
  469. */
  470. #define SDL_Swap16LE(x) SwapOnlyIfNecessary(x)
  471. /**
  472. * Swap a 32-bit value from littleendian to native byte order.
  473. *
  474. * If this is running on a littleendian system, `x` is returned unchanged.
  475. *
  476. * This macro never references `x` more than once, avoiding side effects.
  477. *
  478. * \param x the value to swap, in littleendian byte order.
  479. * \returns `x` in native byte order.
  480. *
  481. * \threadsafety It is safe to call this macro from any thread.
  482. *
  483. * \since This macro is available since SDL 3.1.3.
  484. */
  485. #define SDL_Swap32LE(x) SwapOnlyIfNecessary(x)
  486. /**
  487. * Swap a 64-bit value from littleendian to native byte order.
  488. *
  489. * If this is running on a littleendian system, `x` is returned unchanged.
  490. *
  491. * This macro never references `x` more than once, avoiding side effects.
  492. *
  493. * \param x the value to swap, in littleendian byte order.
  494. * \returns `x` in native byte order.
  495. *
  496. * \threadsafety It is safe to call this macro from any thread.
  497. *
  498. * \since This macro is available since SDL 3.1.3.
  499. */
  500. #define SDL_Swap64LE(x) SwapOnlyIfNecessary(x)
  501. /**
  502. * Swap a floating point value from littleendian to native byte order.
  503. *
  504. * If this is running on a littleendian system, `x` is returned unchanged.
  505. *
  506. * This macro never references `x` more than once, avoiding side effects.
  507. *
  508. * \param x the value to swap, in littleendian byte order.
  509. * \returns `x` in native byte order.
  510. *
  511. * \threadsafety It is safe to call this macro from any thread.
  512. *
  513. * \since This macro is available since SDL 3.1.3.
  514. */
  515. #define SDL_SwapFloatLE(x) SwapOnlyIfNecessary(x)
  516. /**
  517. * Swap a 16-bit value from bigendian to native byte order.
  518. *
  519. * If this is running on a bigendian system, `x` is returned unchanged.
  520. *
  521. * This macro never references `x` more than once, avoiding side effects.
  522. *
  523. * \param x the value to swap, in bigendian byte order.
  524. * \returns `x` in native byte order.
  525. *
  526. * \threadsafety It is safe to call this macro from any thread.
  527. *
  528. * \since This macro is available since SDL 3.1.3.
  529. */
  530. #define SDL_Swap16BE(x) SwapOnlyIfNecessary(x)
  531. /**
  532. * Swap a 32-bit value from bigendian to native byte order.
  533. *
  534. * If this is running on a bigendian system, `x` is returned unchanged.
  535. *
  536. * This macro never references `x` more than once, avoiding side effects.
  537. *
  538. * \param x the value to swap, in bigendian byte order.
  539. * \returns `x` in native byte order.
  540. *
  541. * \threadsafety It is safe to call this macro from any thread.
  542. *
  543. * \since This macro is available since SDL 3.1.3.
  544. */
  545. #define SDL_Swap32BE(x) SwapOnlyIfNecessary(x)
  546. /**
  547. * Swap a 64-bit value from bigendian to native byte order.
  548. *
  549. * If this is running on a bigendian system, `x` is returned unchanged.
  550. *
  551. * This macro never references `x` more than once, avoiding side effects.
  552. *
  553. * \param x the value to swap, in bigendian byte order.
  554. * \returns `x` in native byte order.
  555. *
  556. * \threadsafety It is safe to call this macro from any thread.
  557. *
  558. * \since This macro is available since SDL 3.1.3.
  559. */
  560. #define SDL_Swap64BE(x) SwapOnlyIfNecessary(x)
  561. /**
  562. * Swap a floating point value from bigendian to native byte order.
  563. *
  564. * If this is running on a bigendian system, `x` is returned unchanged.
  565. *
  566. * This macro never references `x` more than once, avoiding side effects.
  567. *
  568. * \param x the value to swap, in bigendian byte order.
  569. * \returns `x` in native byte order.
  570. *
  571. * \threadsafety It is safe to call this macro from any thread.
  572. *
  573. * \since This macro is available since SDL 3.1.3.
  574. */
  575. #define SDL_SwapFloatBE(x) SwapOnlyIfNecessary(x)
  576. #elif SDL_BYTEORDER == SDL_LIL_ENDIAN
  577. #define SDL_Swap16LE(x) (x)
  578. #define SDL_Swap32LE(x) (x)
  579. #define SDL_Swap64LE(x) (x)
  580. #define SDL_SwapFloatLE(x) (x)
  581. #define SDL_Swap16BE(x) SDL_Swap16(x)
  582. #define SDL_Swap32BE(x) SDL_Swap32(x)
  583. #define SDL_Swap64BE(x) SDL_Swap64(x)
  584. #define SDL_SwapFloatBE(x) SDL_SwapFloat(x)
  585. #else
  586. #define SDL_Swap16LE(x) SDL_Swap16(x)
  587. #define SDL_Swap32LE(x) SDL_Swap32(x)
  588. #define SDL_Swap64LE(x) SDL_Swap64(x)
  589. #define SDL_SwapFloatLE(x) SDL_SwapFloat(x)
  590. #define SDL_Swap16BE(x) (x)
  591. #define SDL_Swap32BE(x) (x)
  592. #define SDL_Swap64BE(x) (x)
  593. #define SDL_SwapFloatBE(x) (x)
  594. #endif
  595. /* Ends C function definitions when using C++ */
  596. #ifdef __cplusplus
  597. }
  598. #endif
  599. #include <SDL3/SDL_close_code.h>
  600. #endif /* SDL_endian_h_ */