SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
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

Uint16 SDL::Swap16 (Uint16 x)
 Byte-swap an unsigned 16-bit number.
Uint32 SDL::Swap32 (Uint32 x)
 Byte-swap an unsigned 32-bit number.
Uint64 SDL::Swap64 (Uint64 x)
 Byte-swap an unsigned 64-bit number.
float SDL::SwapFloat (float x)
 Byte-swap a floating point 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.
Uint16 SDL::Swap16BE (Uint16 x)
 Swap a 16-bit value from bigendian to native byte order.
Uint32 SDL::Swap32BE (Uint32 x)
 Swap a 32-bit value from bigendian to native byte order.
Uint64 SDL::Swap64BE (Uint64 x)
 Swap a 64-bit value from bigendian to native byte order.
float SDL::SwapFloatBE (float x)
 Swap a floating point value from bigendian to native byte order.

Detailed Description

Functions converting endian-specific values to different byte orders.

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

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
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

A macro that reports the target system's byte order.

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
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

A macro that reports the target system's floating point word order.

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
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

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
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()

Uint16 SDL::Swap16 ( Uint16 x)
inline

Byte-swap an unsigned 16-bit number.

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use Swap16LE or 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()

Uint16 SDL::Swap16BE ( Uint16 x)
inline

Swap a 16-bit value from bigendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap16LE()

Uint16 SDL::Swap16LE ( Uint16 x)
constexpr

Swap a 16-bit value from littleendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap32()

Uint32 SDL::Swap32 ( Uint32 x)
inline

Byte-swap an unsigned 32-bit number.

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use Swap32LE or 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()

Uint32 SDL::Swap32BE ( Uint32 x)
inline

Swap a 32-bit value from bigendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap32LE()

Uint32 SDL::Swap32LE ( Uint32 x)
constexpr

Swap a 32-bit value from littleendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap64()

Uint64 SDL::Swap64 ( Uint64 x)
inline

Byte-swap an unsigned 64-bit number.

This will always byte-swap the value, whether it's currently in the native byteorder of the system or not. You should use Swap64LE or 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()

Uint64 SDL::Swap64BE ( Uint64 x)
inline

Swap a 64-bit value from bigendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ Swap64LE()

Uint64 SDL::Swap64LE ( Uint64 x)
constexpr

Swap a 64-bit value from littleendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ SwapFloat()

float SDL::SwapFloat ( float x)
inline

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 SwapFloatLE or 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()

float SDL::SwapFloatBE ( float x)
inline

Swap a floating point value from bigendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.

◆ SwapFloatLE()

float SDL::SwapFloatLE ( float x)
constexpr

Swap a floating point value from littleendian to native byte order.

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 function from any thread.
Since
This function is available since SDL 3.2.0.