User:Monster Iestyn/Source Code Documentation/m_swap.h

From SRB2 Wiki
Jump to navigation Jump to search
Online link GitHub entry
File type C header file
#include guard __M_SWAP__

This header file defines the macros SHORT(x) and LONG(x), which in big-endian versions of SRB2 are used to swap the endianness of 16-bit and 32-bit integers where appropriate. This is because external files such as WADs store data in little-endian format exclusively, which would make them incompatible with big-endian versions of SRB2. These macros therefore allow data loaded into the game from WADs and other such files to be converted to the correct endianness for SRB2 to use properly.

In little-endian versions of SRB2 (such as srb2win.exe, the main exe for the Windows version of SRB2), these macros act only as typecasts, and do not swap endianness.

Includes

Macros

SHORT(x)

This macro swaps the endianness of 16-bit integers (or "shorts").

Endianness SHORT(x)
Little-endian
(INT16)(x)
Big-endian
(INT16)(\
(((UINT16)(x) & (UINT16)0x00ffU) << 8) \
| \
(((UINT16)(x) & (UINT16)0xff00U) >> 8))) \

LONG(x)

This macro swaps the endianness of 32-bit integers (or "longs").

Endianness LONG(x)
Little-endian
(INT32)(x)
Big-endian
((INT32)(\
(((UINT32)(x) & (UINT32)0x000000ffUL) << 24) \
| \
(((UINT32)(x) & (UINT32)0x0000ff00UL) <<  8) \
| \
(((UINT32)(x) & (UINT32)0x00ff0000UL) >>  8) \
| \
(((UINT32)(x) & (UINT32)0xff000000UL) >> 24)))