SDL_endian.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #define SDL_LIL_ENDIAN 1234
  59. #define SDL_BIG_ENDIAN 4321
  60. /* @} */
  61. #ifndef SDL_BYTEORDER
  62. #ifdef SDL_PLATFORM_LINUX
  63. #include <endian.h>
  64. #define SDL_BYTEORDER __BYTE_ORDER
  65. #elif defined(SDL_PLATFORM_SOLARIS)
  66. #include <sys/byteorder.h>
  67. #if defined(_LITTLE_ENDIAN)
  68. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  69. #elif defined(_BIG_ENDIAN)
  70. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  71. #else
  72. #error Unsupported endianness
  73. #endif
  74. #elif defined(SDL_PLATFORM_OPENBSD) || defined(__DragonFly__)
  75. #include <endian.h>
  76. #define SDL_BYTEORDER BYTE_ORDER
  77. #elif defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_NETBSD)
  78. #include <sys/endian.h>
  79. #define SDL_BYTEORDER BYTE_ORDER
  80. /* predefs from newer gcc and clang versions: */
  81. #elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__)
  82. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  83. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  84. #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  85. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  86. #else
  87. #error Unsupported endianness
  88. #endif /**/
  89. #else
  90. #if defined(__hppa__) || \
  91. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  92. (defined(__MIPS__) && defined(__MIPSEB__)) || \
  93. defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__) || defined(__PPC__) || \
  94. defined(__sparc__) || defined(__sparc)
  95. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  96. #else
  97. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  98. #endif
  99. #endif /* SDL_PLATFORM_LINUX */
  100. #endif /* !SDL_BYTEORDER */
  101. #ifndef SDL_FLOATWORDORDER
  102. /* predefs from newer gcc versions: */
  103. #if defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__FLOAT_WORD_ORDER__)
  104. #if (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  105. #define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
  106. #elif (__FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__)
  107. #define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
  108. #else
  109. #error Unsupported endianness
  110. #endif /**/
  111. #elif defined(__MAVERICK__)
  112. /* For Maverick, float words are always little-endian. */
  113. #define SDL_FLOATWORDORDER SDL_LIL_ENDIAN
  114. #elif (defined(__arm__) || defined(__thumb__)) && !defined(__VFP_FP__) && !defined(__ARM_EABI__)
  115. /* For FPA, float words are always big-endian. */
  116. #define SDL_FLOATWORDORDER SDL_BIG_ENDIAN
  117. #else
  118. /* By default, assume that floats words follow the memory system mode. */
  119. #define SDL_FLOATWORDORDER SDL_BYTEORDER
  120. #endif /* __FLOAT_WORD_ORDER__ */
  121. #endif /* !SDL_FLOATWORDORDER */
  122. #include <SDL3/SDL_begin_code.h>
  123. /* Set up for C function definitions, even when using C++ */
  124. #ifdef __cplusplus
  125. extern "C" {
  126. #endif
  127. /* various modern compilers may have builtin swap */
  128. #if defined(__GNUC__) || defined(__clang__)
  129. # define HAS_BUILTIN_BSWAP16 (SDL_HAS_BUILTIN(__builtin_bswap16)) || \
  130. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
  131. # define HAS_BUILTIN_BSWAP32 (SDL_HAS_BUILTIN(__builtin_bswap32)) || \
  132. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  133. # define HAS_BUILTIN_BSWAP64 (SDL_HAS_BUILTIN(__builtin_bswap64)) || \
  134. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  135. /* this one is broken */
  136. # define HAS_BROKEN_BSWAP (__GNUC__ == 2 && __GNUC_MINOR__ <= 95)
  137. #else
  138. # define HAS_BUILTIN_BSWAP16 0
  139. # define HAS_BUILTIN_BSWAP32 0
  140. # define HAS_BUILTIN_BSWAP64 0
  141. # define HAS_BROKEN_BSWAP 0
  142. #endif
  143. /* Byte swap 16-bit integer. */
  144. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  145. #if HAS_BUILTIN_BSWAP16
  146. #define SDL_Swap16(x) __builtin_bswap16(x)
  147. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  148. #pragma intrinsic(_byteswap_ushort)
  149. #define SDL_Swap16(x) _byteswap_ushort(x)
  150. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  151. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  152. {
  153. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  154. return x;
  155. }
  156. #elif defined(__x86_64__)
  157. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  158. {
  159. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  160. return x;
  161. }
  162. #elif (defined(__powerpc__) || defined(__ppc__))
  163. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  164. {
  165. int result;
  166. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  167. return (Uint16)result;
  168. }
  169. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  170. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  171. {
  172. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  173. return x;
  174. }
  175. #elif defined(__WATCOMC__) && defined(__386__)
  176. extern __inline Uint16 SDL_Swap16(Uint16);
  177. #pragma aux SDL_Swap16 = \
  178. "xchg al, ah" \
  179. parm [ax] \
  180. modify [ax];
  181. #else
  182. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
  183. {
  184. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  185. }
  186. #endif
  187. #endif
  188. /* Byte swap 32-bit integer. */
  189. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  190. #if HAS_BUILTIN_BSWAP32
  191. #define SDL_Swap32(x) __builtin_bswap32(x)
  192. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  193. #pragma intrinsic(_byteswap_ulong)
  194. #define SDL_Swap32(x) _byteswap_ulong(x)
  195. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  196. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  197. {
  198. __asm__("bswap %0": "=r"(x):"0"(x));
  199. return x;
  200. }
  201. #elif defined(__x86_64__)
  202. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  203. {
  204. __asm__("bswapl %0": "=r"(x):"0"(x));
  205. return x;
  206. }
  207. #elif (defined(__powerpc__) || defined(__ppc__))
  208. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  209. {
  210. Uint32 result;
  211. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result): "0" (x>>24), "r"(x));
  212. __asm__("rlwimi %0,%2,8,8,15" : "=&r"(result): "0" (result), "r"(x));
  213. __asm__("rlwimi %0,%2,24,0,7" : "=&r"(result): "0" (result), "r"(x));
  214. return result;
  215. }
  216. #elif (defined(__m68k__) && !defined(__mcoldfire__))
  217. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  218. {
  219. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  220. return x;
  221. }
  222. #elif defined(__WATCOMC__) && defined(__386__)
  223. extern __inline Uint32 SDL_Swap32(Uint32);
  224. #pragma aux SDL_Swap32 = \
  225. "bswap eax" \
  226. parm [eax] \
  227. modify [eax];
  228. #else
  229. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
  230. {
  231. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  232. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  233. }
  234. #endif
  235. #endif
  236. /* Byte swap 64-bit integer. */
  237. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  238. #if HAS_BUILTIN_BSWAP64
  239. #define SDL_Swap64(x) __builtin_bswap64(x)
  240. #elif (defined(_MSC_VER) && (_MSC_VER >= 1400)) && !defined(__ICL)
  241. #pragma intrinsic(_byteswap_uint64)
  242. #define SDL_Swap64(x) _byteswap_uint64(x)
  243. #elif defined(__i386__) && !HAS_BROKEN_BSWAP
  244. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  245. {
  246. union {
  247. struct {
  248. Uint32 a, b;
  249. } s;
  250. Uint64 u;
  251. } v;
  252. v.u = x;
  253. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  254. : "=r"(v.s.a), "=r"(v.s.b)
  255. : "0" (v.s.a), "1"(v.s.b));
  256. return v.u;
  257. }
  258. #elif defined(__x86_64__)
  259. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  260. {
  261. __asm__("bswapq %0": "=r"(x):"0"(x));
  262. return x;
  263. }
  264. #elif defined(__WATCOMC__) && defined(__386__)
  265. extern __inline Uint64 SDL_Swap64(Uint64);
  266. #pragma aux SDL_Swap64 = \
  267. "bswap eax" \
  268. "bswap edx" \
  269. "xchg eax,edx" \
  270. parm [eax edx] \
  271. modify [eax edx];
  272. #else
  273. SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
  274. {
  275. Uint32 hi, lo;
  276. /* Separate into high and low 32-bit values and swap them */
  277. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  278. x >>= 32;
  279. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  280. x = SDL_Swap32(lo);
  281. x <<= 32;
  282. x |= SDL_Swap32(hi);
  283. return (x);
  284. }
  285. #endif
  286. #endif
  287. /**
  288. * Byte-swap a floating point number.
  289. *
  290. * This will always byte-swap the value, whether it's currently in the native
  291. * byteorder of the system or not. You should use SDL_SwapFloatLE or
  292. * SDL_SwapFloatBE instead, in most cases.
  293. *
  294. * Note that this is a forced-inline function in a header, and not a public
  295. * API function available in the SDL library (which is to say, the code is
  296. * embedded in the calling program and the linker and dynamic loader will not
  297. * be able to find this function inside SDL itself).
  298. *
  299. * \param x the value to byte-swap.
  300. * \returns x, with its bytes in the opposite endian order.
  301. *
  302. * \threadsafety It is safe to call this function from any thread.
  303. *
  304. * \since This function is available since SDL 3.1.3.
  305. */
  306. SDL_FORCE_INLINE float SDL_SwapFloat(float x)
  307. {
  308. union {
  309. float f;
  310. Uint32 ui32;
  311. } swapper;
  312. swapper.f = x;
  313. swapper.ui32 = SDL_Swap32(swapper.ui32);
  314. return swapper.f;
  315. }
  316. /* remove extra macros */
  317. #undef HAS_BROKEN_BSWAP
  318. #undef HAS_BUILTIN_BSWAP16
  319. #undef HAS_BUILTIN_BSWAP32
  320. #undef HAS_BUILTIN_BSWAP64
  321. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  322. /**
  323. * Byte-swap an unsigned 16-bit number.
  324. *
  325. * This will always byte-swap the value, whether it's currently in the native
  326. * byteorder of the system or not. You should use SDL_Swap16LE or SDL_Swap16BE
  327. * instead, in most cases.
  328. *
  329. * Note that this is a forced-inline function in a header, and not a public
  330. * API function available in the SDL library (which is to say, the code is
  331. * embedded in the calling program and the linker and dynamic loader will not
  332. * be able to find this function inside SDL itself).
  333. *
  334. * \param x the value to byte-swap.
  335. * \returns `x`, with its bytes in the opposite endian order.
  336. *
  337. * \threadsafety It is safe to call this function from any thread.
  338. *
  339. * \since This function is available since SDL 3.1.3.
  340. */
  341. SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x) { return x_but_byteswapped; }
  342. /**
  343. * Byte-swap an unsigned 32-bit number.
  344. *
  345. * This will always byte-swap the value, whether it's currently in the native
  346. * byteorder of the system or not. You should use SDL_Swap32LE or SDL_Swap32BE
  347. * instead, in most cases.
  348. *
  349. * Note that this is a forced-inline function in a header, and not a public
  350. * API function available in the SDL library (which is to say, the code is
  351. * embedded in the calling program and the linker and dynamic loader will not
  352. * be able to find this function inside SDL itself).
  353. *
  354. * \param x the value to byte-swap.
  355. * \returns `x`, with its bytes in the opposite endian order.
  356. *
  357. * \threadsafety It is safe to call this function from any thread.
  358. *
  359. * \since This function is available since SDL 3.1.3.
  360. */
  361. SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x) { return x_but_byteswapped; }
  362. /**
  363. * Byte-swap an unsigned 64-bit number.
  364. *
  365. * This will always byte-swap the value, whether it's currently in the native
  366. * byteorder of the system or not. You should use SDL_Swap64LE or SDL_Swap64BE
  367. * instead, in most cases.
  368. *
  369. * Note that this is a forced-inline function in a header, and not a public
  370. * API function available in the SDL library (which is to say, the code is
  371. * embedded in the calling program and the linker and dynamic loader will not
  372. * be able to find this function inside SDL itself).
  373. *
  374. * \param x the value to byte-swap.
  375. * \returns `x`, with its bytes in the opposite endian order.
  376. *
  377. * \threadsafety It is safe to call this function from any thread.
  378. *
  379. * \since This function is available since SDL 3.1.3.
  380. */
  381. SDL_FORCE_INLINE Uint32 SDL_Swap64(Uint64 x) { return x_but_byteswapped; }
  382. /**
  383. * Swap a 16-bit value from littleendian to native byte order.
  384. *
  385. * If this is running on a littleendian system, `x` is returned unchanged.
  386. *
  387. * This macro never references `x` more than once, avoiding side effects.
  388. *
  389. * \param x the value to swap, in littleendian byte order.
  390. * \returns `x` in native byte order.
  391. *
  392. * \threadsafety It is safe to call this macro from any thread.
  393. *
  394. * \since This macro is available since SDL 3.1.3.
  395. */
  396. #define SDL_Swap16LE(x) SwapOnlyIfNecessary(x)
  397. /**
  398. * Swap a 32-bit value from littleendian to native byte order.
  399. *
  400. * If this is running on a littleendian system, `x` is returned unchanged.
  401. *
  402. * This macro never references `x` more than once, avoiding side effects.
  403. *
  404. * \param x the value to swap, in littleendian byte order.
  405. * \returns `x` in native byte order.
  406. *
  407. * \threadsafety It is safe to call this macro from any thread.
  408. *
  409. * \since This macro is available since SDL 3.1.3.
  410. */
  411. #define SDL_Swap32LE(x) SwapOnlyIfNecessary(x)
  412. /**
  413. * Swap a 64-bit value from littleendian to native byte order.
  414. *
  415. * If this is running on a littleendian system, `x` is returned unchanged.
  416. *
  417. * This macro never references `x` more than once, avoiding side effects.
  418. *
  419. * \param x the value to swap, in littleendian byte order.
  420. * \returns `x` in native byte order.
  421. *
  422. * \threadsafety It is safe to call this macro from any thread.
  423. *
  424. * \since This macro is available since SDL 3.1.3.
  425. */
  426. #define SDL_Swap64LE(x) SwapOnlyIfNecessary(x)
  427. /**
  428. * Swap a floating point value from littleendian to native byte order.
  429. *
  430. * If this is running on a littleendian system, `x` is returned unchanged.
  431. *
  432. * This macro never references `x` more than once, avoiding side effects.
  433. *
  434. * \param x the value to swap, in littleendian byte order.
  435. * \returns `x` in native byte order.
  436. *
  437. * \threadsafety It is safe to call this macro from any thread.
  438. *
  439. * \since This macro is available since SDL 3.1.3.
  440. */
  441. #define SDL_SwapFloatLE(x) SwapOnlyIfNecessary(x)
  442. /**
  443. * Swap a 16-bit value from bigendian to native byte order.
  444. *
  445. * If this is running on a bigendian system, `x` is returned unchanged.
  446. *
  447. * This macro never references `x` more than once, avoiding side effects.
  448. *
  449. * \param x the value to swap, in bigendian byte order.
  450. * \returns `x` in native byte order.
  451. *
  452. * \threadsafety It is safe to call this macro from any thread.
  453. *
  454. * \since This macro is available since SDL 3.1.3.
  455. */
  456. #define SDL_Swap16BE(x) SwapOnlyIfNecessary(x)
  457. /**
  458. * Swap a 32-bit value from bigendian to native byte order.
  459. *
  460. * If this is running on a bigendian system, `x` is returned unchanged.
  461. *
  462. * This macro never references `x` more than once, avoiding side effects.
  463. *
  464. * \param x the value to swap, in bigendian byte order.
  465. * \returns `x` in native byte order.
  466. *
  467. * \threadsafety It is safe to call this macro from any thread.
  468. *
  469. * \since This macro is available since SDL 3.1.3.
  470. */
  471. #define SDL_Swap32BE(x) SwapOnlyIfNecessary(x)
  472. /**
  473. * Swap a 64-bit value from bigendian to native byte order.
  474. *
  475. * If this is running on a bigendian system, `x` is returned unchanged.
  476. *
  477. * This macro never references `x` more than once, avoiding side effects.
  478. *
  479. * \param x the value to swap, in bigendian byte order.
  480. * \returns `x` in native byte order.
  481. *
  482. * \threadsafety It is safe to call this macro from any thread.
  483. *
  484. * \since This macro is available since SDL 3.1.3.
  485. */
  486. #define SDL_Swap64BE(x) SwapOnlyIfNecessary(x)
  487. /**
  488. * Swap a floating point value from bigendian to native byte order.
  489. *
  490. * If this is running on a bigendian system, `x` is returned unchanged.
  491. *
  492. * This macro never references `x` more than once, avoiding side effects.
  493. *
  494. * \param x the value to swap, in bigendian byte order.
  495. * \returns `x` in native byte order.
  496. *
  497. * \threadsafety It is safe to call this macro from any thread.
  498. *
  499. * \since This macro is available since SDL 3.1.3.
  500. */
  501. #define SDL_SwapFloatBE(x) SwapOnlyIfNecessary(x)
  502. #elif SDL_BYTEORDER == SDL_LIL_ENDIAN
  503. #define SDL_Swap16LE(x) (x)
  504. #define SDL_Swap32LE(x) (x)
  505. #define SDL_Swap64LE(x) (x)
  506. #define SDL_SwapFloatLE(x) (x)
  507. #define SDL_Swap16BE(x) SDL_Swap16(x)
  508. #define SDL_Swap32BE(x) SDL_Swap32(x)
  509. #define SDL_Swap64BE(x) SDL_Swap64(x)
  510. #define SDL_SwapFloatBE(x) SDL_SwapFloat(x)
  511. #else
  512. #define SDL_Swap16LE(x) SDL_Swap16(x)
  513. #define SDL_Swap32LE(x) SDL_Swap32(x)
  514. #define SDL_Swap64LE(x) SDL_Swap64(x)
  515. #define SDL_SwapFloatLE(x) SDL_SwapFloat(x)
  516. #define SDL_Swap16BE(x) (x)
  517. #define SDL_Swap32BE(x) (x)
  518. #define SDL_Swap64BE(x) (x)
  519. #define SDL_SwapFloatBE(x) (x)
  520. #endif
  521. /* Ends C function definitions when using C++ */
  522. #ifdef __cplusplus
  523. }
  524. #endif
  525. #include <SDL3/SDL_close_code.h>
  526. #endif /* SDL_endian_h_ */