SDL 3.0
SDL_endian.h File Reference
+ Include dependency graph for SDL_endian.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

The two types of endianness

CategoryEndian

Functions converting endian-specific values to different byte orders.

These functions either unconditionally swap byte order (SDL_Swap16, SDL_Swap32, SDL_Swap64, SDL_SwapFloat), or they swap to/from the system's native byte order (SDL_Swap16LE, SDL_Swap16BE, SDL_Swap32LE, SDL_Swap32BE, SDL_Swap32LE, SDL_Swap32BE, SDL_SwapFloatLE, SDL_SwapFloatBE). In the latter case, the functionality is provided by macros that become no-ops if a swap isn't necessary: on an x86 (littleendian) processor, SDL_Swap32LE does nothing, but SDL_Swap32BE reverses the bytes of the data. On a PowerPC processor (bigendian), the macros behavior is reversed.

The swap routines are inline functions, and attempt to use compiler intrinsics, inline assembly, and other magic to make byteswapping efficient.

#define SDL_LIL_ENDIAN   1234
 
#define SDL_BIG_ENDIAN   4321
 
#define SDL_BYTEORDER   SDL_LIL_ENDIAN
 
#define SDL_FLOATWORDORDER   SDL_BYTEORDER
 
#define HAS_BUILTIN_BSWAP16   0
 
#define HAS_BUILTIN_BSWAP32   0
 
#define HAS_BUILTIN_BSWAP64   0
 
#define HAS_BROKEN_BSWAP   0
 
#define SDL_Swap16LE(x)
 
#define SDL_Swap32LE(x)
 
#define SDL_Swap64LE(x)
 
#define SDL_SwapFloatLE(x)
 
#define SDL_Swap16BE(x)
 
#define SDL_Swap32BE(x)
 
#define SDL_Swap64BE(x)
 
#define SDL_SwapFloatBE(x)
 
SDL_FORCE_INLINE Uint16 SDL_Swap16 (Uint16 x)
 
SDL_FORCE_INLINE Uint32 SDL_Swap32 (Uint32 x)
 
SDL_FORCE_INLINE Uint64 SDL_Swap64 (Uint64 x)
 
SDL_FORCE_INLINE float SDL_SwapFloat (float x)
 

Macro Definition Documentation

◆ HAS_BROKEN_BSWAP

#define HAS_BROKEN_BSWAP   0

Definition at line 236 of file SDL_endian.h.

◆ HAS_BUILTIN_BSWAP16

#define HAS_BUILTIN_BSWAP16   0

Definition at line 233 of file SDL_endian.h.

◆ HAS_BUILTIN_BSWAP32

#define HAS_BUILTIN_BSWAP32   0

Definition at line 234 of file SDL_endian.h.

◆ HAS_BUILTIN_BSWAP64

#define HAS_BUILTIN_BSWAP64   0

Definition at line 235 of file SDL_endian.h.

◆ SDL_BIG_ENDIAN

#define SDL_BIG_ENDIAN   4321

A value to represent bigendian byteorder.

This is used with the preprocessor macro SDL_BYTEORDER, to determine a platform's byte ordering:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_Log("This system is bigendian.");
#endif
void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Since
This macro is available since SDL 3.2.0.
See also
SDL_BYTEORDER
SDL_LIL_ENDIAN

Definition at line 105 of file SDL_endian.h.

◆ SDL_BYTEORDER

#define SDL_BYTEORDER   SDL_LIL_ENDIAN

Definition at line 166 of file SDL_endian.h.

◆ SDL_FLOATWORDORDER

#define SDL_FLOATWORDORDER   SDL_BYTEORDER

Definition at line 210 of file SDL_endian.h.

◆ SDL_LIL_ENDIAN

#define SDL_LIL_ENDIAN   1234

A value to represent littleendian byteorder.

This is used with the preprocessor macro SDL_BYTEORDER, to determine a platform's byte ordering:

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
SDL_Log("This system is littleendian.");
#endif
Since
This macro is available since SDL 3.2.0.
See also
SDL_BYTEORDER
SDL_BIG_ENDIAN

Definition at line 86 of file SDL_endian.h.

◆ SDL_Swap16BE

#define SDL_Swap16BE ( x)
Value:
SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
Definition SDL_endian.h:279

Definition at line 624 of file SDL_endian.h.

◆ SDL_Swap16LE

#define SDL_Swap16LE ( x)
Value:
(x)

Definition at line 620 of file SDL_endian.h.

◆ SDL_Swap32BE

#define SDL_Swap32BE ( x)
Value:
SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
Definition SDL_endian.h:328

Definition at line 625 of file SDL_endian.h.

◆ SDL_Swap32LE

#define SDL_Swap32LE ( x)
Value:
(x)

Definition at line 621 of file SDL_endian.h.

◆ SDL_Swap64BE

#define SDL_Swap64BE ( x)
Value:
SDL_FORCE_INLINE Uint64 SDL_Swap64(Uint64 x)
Definition SDL_endian.h:373

Definition at line 626 of file SDL_endian.h.

◆ SDL_Swap64LE

#define SDL_Swap64LE ( x)
Value:
(x)

Definition at line 622 of file SDL_endian.h.

◆ SDL_SwapFloatBE

#define SDL_SwapFloatBE ( x)
Value:
SDL_FORCE_INLINE float SDL_SwapFloat(float x)
Definition SDL_endian.h:408

Definition at line 627 of file SDL_endian.h.

◆ SDL_SwapFloatLE

#define SDL_SwapFloatLE ( x)
Value:
(x)

Definition at line 623 of file SDL_endian.h.

Function Documentation

◆ SDL_Swap16()

SDL_FORCE_INLINE Uint16 SDL_Swap16 ( Uint16 x)

Definition at line 279 of file SDL_endian.h.

280{
281 return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
282}
uint16_t Uint16
Definition SDL_stdinc.h:443
#define SDL_static_cast(type, expression)
Definition SDL_stdinc.h:324

References SDL_static_cast.

◆ SDL_Swap32()

SDL_FORCE_INLINE Uint32 SDL_Swap32 ( Uint32 x)

Definition at line 328 of file SDL_endian.h.

329{
330 return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
331 ((x >> 8) & 0x0000FF00) | (x >> 24)));
332}
uint32_t Uint32
Definition SDL_stdinc.h:461

References SDL_static_cast.

Referenced by SDL_Swap64(), and SDL_SwapFloat().

◆ SDL_Swap64()

SDL_FORCE_INLINE Uint64 SDL_Swap64 ( Uint64 x)

Definition at line 373 of file SDL_endian.h.

374{
375 Uint32 hi, lo;
376
377 /* Separate into high and low 32-bit values and swap them */
378 lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
379 x >>= 32;
380 hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
381 x = SDL_Swap32(lo);
382 x <<= 32;
383 x |= SDL_Swap32(hi);
384 return (x);
385}

References SDL_static_cast, and SDL_Swap32().

◆ SDL_SwapFloat()

SDL_FORCE_INLINE float SDL_SwapFloat ( float x)

Byte-swap a floating point number.

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use SDL_SwapFloatLE or SDL_SwapFloatBE instead, in most cases.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters
xthe value to byte-swap.
Returns
x, with its bytes in the opposite endian order.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

Definition at line 408 of file SDL_endian.h.

409{
410 union {
411 float f;
412 Uint32 ui32;
413 } swapper;
414 swapper.f = x;
415 swapper.ui32 = SDL_Swap32(swapper.ui32);
416 return swapper.f;
417}

References SDL_Swap32().