ScummVM API documentation
Endian conversions

Description

Functions and macros for endian conversions and byteswap conversions.

Classes

union  SwapFloat
 
union  SwapDouble
 

Macros

#define SWAP_CONSTANT_64(a)
 
#define SWAP_CONSTANT_32(a)
 
#define SWAP_CONSTANT_16(a)
 
#define MKTAG(a0, a1, a2, a3)   ((uint32)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))
 
#define MKTAG16(a0, a1)   ((uint16)((a1) | ((a0) << 8)))
 

Functions

uint16 SWAP_BYTES_16 (const uint16 a)
 
uint32 SWAP_BYTES_32 (uint32 a)
 
uint64 SWAP_BYTES_64 (uint64 a)
 

Functions for reading and writing native integers

Functions for reading and writing native integer values. They also transparently handle the need for alignment.

uint16 READ_UINT16 (const void *ptr)
 
uint32 READ_UINT32 (const void *ptr)
 
void WRITE_UINT16 (void *ptr, uint16 value)
 
void WRITE_UINT32 (void *ptr, uint32 value)
 
uint64 READ_UINT64 (const void *ptr)
 
void WRITE_UINT64 (void *ptr, uint64 value)
 

Map functions for reading/writing BE/LE integers depending on native endianess

#define READ_LE_UINT16(a)   READ_UINT16(a)
 
#define READ_LE_UINT32(a)   READ_UINT32(a)
 
#define WRITE_LE_UINT16(a, v)   WRITE_UINT16(a, v)
 
#define WRITE_LE_UINT32(a, v)   WRITE_UINT32(a, v)
 
#define FROM_LE_32(a)   ((uint32)(a))
 
#define FROM_LE_16(a)   ((uint16)(a))
 
#define FROM_BE_32(a)   SWAP_BYTES_32(a)
 
#define FROM_BE_16(a)   SWAP_BYTES_16(a)
 
#define TO_LE_32(a)   ((uint32)(a))
 
#define TO_LE_16(a)   ((uint16)(a))
 
#define TO_BE_32(a)   SWAP_BYTES_32(a)
 
#define TO_BE_16(a)   SWAP_BYTES_16(a)
 
#define CONSTANT_LE_32(a)   ((uint32)(a))
 
#define CONSTANT_LE_16(a)   ((uint16)(a))
 
#define CONSTANT_BE_32(a)   SWAP_CONSTANT_32(a)
 
#define CONSTANT_BE_16(a)   SWAP_CONSTANT_16(a)
 
#define READ_LE_UINT64(a)   READ_UINT64(a)
 
#define WRITE_LE_UINT64(a, v)   WRITE_UINT64(a, v)
 
#define FROM_LE_64(a)   ((uint64)(a))
 
#define FROM_BE_64(a)   SWAP_BYTES_64(a)
 
#define TO_LE_64(a)   ((uint64)(a))
 
#define TO_BE_64(a)   SWAP_BYTES_64(a)
 
#define CONSTANT_LE_64(a)   ((uint64)(a))
 
#define CONSTANT_BE_64(a)   SWAP_CONSTANT_64(a)
 

Functions for directly reading/writing and inverting

Use these in case the unaligned load and byteswap take a lot of instructions.

uint16 READ_BE_UINT16 (const void *ptr)
 
uint32 READ_BE_UINT32 (const void *ptr)
 
void WRITE_BE_UINT16 (void *ptr, uint16 value)
 
void WRITE_BE_UINT32 (void *ptr, uint32 value)
 
uint64 READ_BE_UINT64 (const void *ptr)
 
void WRITE_BE_UINT64 (void *ptr, uint64 value)
 
uint32 READ_LE_UINT24 (const void *ptr)
 
void WRITE_LE_UINT24 (void *ptr, uint32 value)
 
uint32 READ_BE_UINT24 (const void *ptr)
 
void WRITE_BE_UINT24 (void *ptr, uint32 value)
 
 STATIC_ASSERT (sizeof(float)==sizeof(uint32), Unexpected_size_of_float)
 
float READ_LE_FLOAT32 (const void *ptr)
 
void WRITE_LE_FLOAT32 (void *ptr, float value)
 
float READ_BE_FLOAT32 (const void *ptr)
 
void WRITE_BE_FLOAT32 (void *ptr, float value)
 
 STATIC_ASSERT (sizeof(double)==sizeof(uint64)||sizeof(double)==sizeof(uint32), Unexpected_size_of_double)
 
template<size_t n>
double READ_DOUBLE (const SwapDouble &sw)
 
template<size_t n>
void WRITE_DOUBLE (SwapDouble &sw, double d)
 
template<>
double READ_DOUBLE< sizeof(uint64)> (const SwapDouble &sd)
 
template<>
void WRITE_DOUBLE< sizeof(uint64)> (SwapDouble &sd, double d)
 
template<>
double READ_DOUBLE< sizeof(uint32)> (const SwapDouble &sd)
 
template<>
void WRITE_DOUBLE< sizeof(uint32)> (SwapDouble &sd, double d)
 
double READ_LE_FLOAT64 (const void *ptr)
 
void WRITE_LE_FLOAT64 (void *ptr, double value)
 
double READ_BE_FLOAT64 (const void *ptr)
 
void WRITE_BE_FLOAT64 (void *ptr, double value)
 
double READ_FPA_FLOAT64 (const void *ptr)
 
void WRITE_FPA_FLOAT64 (void *ptr, double value)
 
double READ_FLOAT64 (const void *ptr)
 
void WRITE_FLOAT64 (void *ptr, double value)
 
int16 READ_LE_INT16 (const void *ptr)
 
void WRITE_LE_INT16 (void *ptr, int16 value)
 
int16 READ_BE_INT16 (const void *ptr)
 
void WRITE_BE_INT16 (void *ptr, int16 value)
 
int32 READ_LE_INT32 (const void *ptr)
 
void WRITE_LE_INT32 (void *ptr, int32 value)
 
int32 READ_BE_INT32 (const void *ptr)
 
void WRITE_BE_INT32 (void *ptr, int32 value)
 
#define READ_UINT24(a)   READ_LE_UINT24(a)
 
#define WRITE_UINT24(a, b)   WRITE_LE_UINT24(a,b)
 
#define READ_FLOAT32(a)   READ_LE_FLOAT32(a)
 
#define WRITE_FLOAT32(a, b)   WRITE_LE_FLOAT32(a,b)
 

Macro Definition Documentation

◆ SWAP_CONSTANT_64

#define SWAP_CONSTANT_64 (   a)
Value:
((uint64)((((a) >> 56) & 0x000000FF) | \
(((a) >> 40) & 0x0000FF00) | \
(((a) >> 24) & 0x00FF0000) | \
(((a) >> 8) & 0xFF000000) | \
(((a) & 0xFF000000) << 8) | \
(((a) & 0x00FF0000) << 24) | \
(((a) & 0x0000FF00) << 40) | \
(((a) & 0x000000FF) << 56) ))

Swap the bytes in a 64-bit word in order to convert LE encoded data to BE and vice versa. Use with compile-time constants only.

◆ SWAP_CONSTANT_32

#define SWAP_CONSTANT_32 (   a)
Value:
((uint32)((((a) >> 24) & 0x00FF) | \
(((a) >> 8) & 0xFF00) | \
(((a) & 0xFF00) << 8) | \
(((a) & 0x00FF) << 24) ))

Swap the bytes in a 32-bit word in order to convert LE encoded data to BE and vice versa. Use with compile-time constants only.

◆ SWAP_CONSTANT_16

#define SWAP_CONSTANT_16 (   a)
Value:
((uint16)((((a) >> 8) & 0x00FF) | \
(((a) << 8) & 0xFF00) ))

Swap the bytes in a 16-bit word in order to convert LE encoded data to BE and vice versa. Use with compile-time constants only.

◆ MKTAG

#define MKTAG (   a0,
  a1,
  a2,
  a3 
)    ((uint32)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))

A wrapper macro used around four character constants, like 'DATA', to ensure portability. Typical usage: MKTAG('D','A','T','A').

This is required because the C/C++ standard does not define the endianess to be used for character constants. Hence, if one uses multi-byte character constants, a potential portability problem opens up.

◆ MKTAG16

#define MKTAG16 (   a0,
  a1 
)    ((uint16)((a1) | ((a0) << 8)))

A wrapper macro used around two character constants, like 'wb', to ensure portability. Typical usage: MKTAG16('w','b').

Function Documentation

◆ SWAP_BYTES_16()

uint16 SWAP_BYTES_16 ( const uint16  a)
inline

Swap the bytes in a 16-bit word in order to convert LE encoded data to BE and vice versa.

◆ SWAP_BYTES_32()

uint32 SWAP_BYTES_32 ( uint32  a)
inline

Swap the bytes in a 32-bit word in order to convert LE encoded data to BE and vice versa.

◆ SWAP_BYTES_64()

uint64 SWAP_BYTES_64 ( uint64  a)
inline

Swap the bytes in a 64-bit word in order to convert LE encoded data to BE and vice versa.