SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Macros | Functions
Byte Order and Byte Swapping

Functions converting endian-specific values to different byte orders. More...

Macros

#define SDL_LIL_ENDIAN   1234
 A value to represent littleendian byteorder.
 
#define SDL_BIG_ENDIAN   4321
 A value to represent bigendian byteorder.
 
#define SDL_BYTEORDER   SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN
 A macro that reports the target system's byte order.
 
#define SDL_FLOATWORDORDER   SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN
 A macro that reports the target system's floating point word order.
 

Functions

constexpr float SDL::SwapFloat (float x)
 Byte-swap a floating point number.
 
constexpr Uint16 SDL::Swap16 (Uint16 x)
 Byte-swap an unsigned 16-bit number.
 
constexpr Uint32 SDL::Swap32 (Uint32 x)
 Byte-swap an unsigned 32-bit number.
 
constexpr Uint32 SDL::Swap64 (Uint64 x)
 Byte-swap an unsigned 64-bit number.
 
constexpr Uint16 SDL::Swap16LE (Uint16 x)
 Swap a 16-bit value from littleendian to native byte order.
 
constexpr Uint32 SDL::Swap32LE (Uint32 x)
 Swap a 32-bit value from littleendian to native byte order.
 
constexpr Uint64 SDL::Swap64LE (Uint64 x)
 Swap a 64-bit value from littleendian to native byte order.
 
constexpr float SDL::SwapFloatLE (float x)
 Swap a floating point value from littleendian to native byte order.
 
constexpr Uint16 SDL::Swap16BE (Uint16 x)
 Swap a 16-bit value from bigendian to native byte order.
 
constexpr Uint32 SDL::Swap32BE (Uint32 x)
 Swap a 32-bit value from bigendian to native byte order.
 
constexpr Uint64 SDL::Swap64BE (Uint64 x)
 Swap a 64-bit value from bigendian to native byte order.
 
constexpr float SDL::SwapFloatBE (float x)
 Swap a floating point value from bigendian to native byte order.
 

Detailed Description

These functions either unconditionally swap byte order (Swap16, Swap32, Swap64, SwapFloat), or they swap to/from the system's native byte order (Swap16LE, Swap16BE, Swap32LE, Swap32BE, Swap32LE, Swap32BE, SwapFloatLE, 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, Swap32LE does nothing, but 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.

Macro Definition Documentation

◆ SDL_BIG_ENDIAN

#define SDL_BIG_ENDIAN   4321

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
Since
This macro is available since SDL 3.2.0.
See also
SDL_BYTEORDER
SDL_LIL_ENDIAN

◆ SDL_BYTEORDER

#define SDL_BYTEORDER   SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN

This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other values in the future, if something else becomes popular). This can be tested with the preprocessor, so decisions can be made at compile time.

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

◆ SDL_FLOATWORDORDER

#define SDL_FLOATWORDORDER   SDL_LIL_ENDIAN___or_maybe___SDL_BIG_ENDIAN

This is set to either SDL_LIL_ENDIAN or SDL_BIG_ENDIAN (and maybe other values in the future, if something else becomes popular). This can be tested with the preprocessor, so decisions can be made at compile time.

#if SDL_FLOATWORDORDER == SDL_BIG_ENDIAN
SDL_Log("This system's floats are bigendian.");
#endif
Since
This macro is available since SDL 3.2.0.
See also
SDL_LIL_ENDIAN
SDL_BIG_ENDIAN

◆ SDL_LIL_ENDIAN

#define SDL_LIL_ENDIAN   1234

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

Function Documentation

◆ Swap16()

constexpr Uint16 SDL::Swap16 ( Uint16  x)
constexpr

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use SDL_Swap16LE or SDL_Swap16BE 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.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap16BE()

constexpr Uint16 SDL::Swap16BE ( Uint16  x)
constexpr

If this is running on a bigendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in bigendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ Swap16LE()

constexpr Uint16 SDL::Swap16LE ( Uint16  x)
constexpr

If this is running on a littleendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in littleendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ Swap32()

constexpr Uint32 SDL::Swap32 ( Uint32  x)
constexpr

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use SDL_Swap32LE or SDL_Swap32BE 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.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap32BE()

constexpr Uint32 SDL::Swap32BE ( Uint32  x)
constexpr

If this is running on a bigendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in bigendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ Swap32LE()

constexpr Uint32 SDL::Swap32LE ( Uint32  x)
constexpr

If this is running on a littleendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in littleendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ Swap64()

constexpr Uint32 SDL::Swap64 ( Uint64  x)
constexpr

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use SDL_Swap64LE or SDL_Swap64BE 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.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap64BE()

constexpr Uint64 SDL::Swap64BE ( Uint64  x)
constexpr

If this is running on a bigendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in bigendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ Swap64LE()

constexpr Uint64 SDL::Swap64LE ( Uint64  x)
constexpr

If this is running on a littleendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in littleendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ SwapFloat()

constexpr float SDL::SwapFloat ( float  x)
constexpr

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.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.

◆ SwapFloatBE()

constexpr float SDL::SwapFloatBE ( float  x)
constexpr

If this is running on a bigendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in bigendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.

◆ SwapFloatLE()

constexpr float SDL::SwapFloatLE ( float  x)
constexpr

If this is running on a littleendian system, x is returned unchanged.

This macro never references x more than once, avoiding side effects.

Parameters
xthe value to swap, in littleendian byte order.
Returns
x in native byte order.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.