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