tuklib_integer.h File Reference

Various integer and bit operations. More...

`#include "tuklib_common.h"`
`#include <string.h>`

## Macros

#define | bswap16(n)
#define | bswap32(n)
#define | bswap64(n)
#define | conv16be(num) bswap16(num)
#define | conv32be(num) bswap32(num)
#define | conv64be(num) bswap64(num)
#define | conv16le(num) ((uint16_t)(num))
#define | conv32le(num) ((uint32_t)(num))
#define | conv64le(num) ((uint64_t)(num))
#define | write16le(buf, num) write16ne(buf, conv16le(num))
#define | write32le(buf, num) write32ne(buf, conv32le(num))
#define | tuklib_memcpy_aligned(dest, src, size) memcpy(dest, src, size)
#define | TUKLIB_USE_UNSAFE_ALIGNED_READS 1
#define | aligned_write16be(buf, num) aligned_write16ne((buf), conv16be(num))
#define | aligned_write16le(buf, num) aligned_write16ne((buf), conv16le(num))
#define | aligned_write32be(buf, num) aligned_write32ne((buf), conv32be(num))
#define | aligned_write32le(buf, num) aligned_write32ne((buf), conv32le(num))
#define | aligned_write64be(buf, num) aligned_write64ne((buf), conv64be(num))
#define | aligned_write64le(buf, num) aligned_write64ne((buf), conv64le(num))
#define | bsf32 ctz32

## Functions

static uint16_t | read16ne (const uint8_t *buf)
static uint32_t | read32ne (const uint8_t *buf)
static uint64_t | read64ne (const uint8_t *buf)
static void | write16ne (uint8_t *buf, uint16_t num)
static void | write32ne (uint8_t *buf, uint32_t num)
static void | write64ne (uint8_t *buf, uint64_t num)
static uint16_t | read16be (const uint8_t *buf)
static uint16_t | read16le (const uint8_t *buf)
static uint32_t | read32be (const uint8_t *buf)
static uint32_t | read32le (const uint8_t *buf)
static void | write16be (uint8_t *buf, uint16_t num)
static void | write32be (uint8_t *buf, uint32_t num)
static uint16_t | aligned_read16ne (const uint8_t *buf)
static uint32_t | aligned_read32ne (const uint8_t *buf)
static uint64_t | aligned_read64ne (const uint8_t *buf)
static void | aligned_write16ne (uint8_t *buf, uint16_t num)
static void | aligned_write32ne (uint8_t *buf, uint32_t num)
static void | aligned_write64ne (uint8_t *buf, uint64_t num)
static uint16_t | aligned_read16be (const uint8_t *buf)
static uint16_t | aligned_read16le (const uint8_t *buf)
static uint32_t | aligned_read32be (const uint8_t *buf)
static uint32_t | aligned_read32le (const uint8_t *buf)
static uint64_t | aligned_read64be (const uint8_t *buf)
static uint64_t | aligned_read64le (const uint8_t *buf)
static uint32_t | bsr32 (uint32_t n)
static uint32_t | clz32 (uint32_t n)
static uint32_t | ctz32 (uint32_t n)

## Detailed Description

Various integer and bit operations.

This file provides macros or functions to do some basic integer and bit operations.

Native endian inline functions (XX = 16, 32, or 64):

- Unaligned native endian reads: readXXne(ptr)
- Unaligned native endian writes: writeXXne(ptr, num)
- Aligned native endian reads: aligned_readXXne(ptr)
- Aligned native endian writes: aligned_writeXXne(ptr, num)

Endianness-converting integer operations (these can be macros!) (XX = 16, 32, or 64; Y = b or l):

- Byte swapping: bswapXX(num)
- Byte order conversions to/from native (byteswaps if Y isn't the native endianness): convXXYe(num)
- Unaligned reads (16/32-bit only): readXXYe(ptr)
- Unaligned writes (16/32-bit only): writeXXYe(ptr, num)
- Aligned reads: aligned_readXXYe(ptr)
- Aligned writes: aligned_writeXXYe(ptr, num)

Since the above can macros, the arguments should have no side effects because they may be evaluated more than once.

Bit scan operations for non-zero 32-bit integers (inline functions):

- Bit scan reverse (find highest non-zero bit): bsr32(num)
- Count leading zeros: clz32(num)
- Count trailing zeros: ctz32(num)
- Bit scan forward (simply an alias for ctz32()): bsf32(num)

The above bit scan operations return 0-31. If num is zero, the result is undefined.

## Macro Definition Documentation

## bswap16

#define bswap16 | ( |  | n | ) |

Value:
(uint16_t)( \
(((n) & 0x00FFU) << 8) \
| (((n) & 0xFF00U) >> 8) \
)

## bswap32

#define bswap32 | ( |  | n | ) |

Value:
(uint32_t)( \
(((n) & UINT32_C(0x000000FF)) << 24) \
| (((n) & UINT32_C(0x0000FF00)) << 8) \
| (((n) & UINT32_C(0x00FF0000)) >> 8) \
| (((n) & UINT32_C(0xFF000000)) >> 24) \
)

## bswap64

#define bswap64 | ( |  | n | ) |

Value:
(uint64_t)( \
(((n) & UINT64_C(0x00000000000000FF)) << 56) \
| (((n) & UINT64_C(0x000000000000FF00)) << 40) \
| (((n) & UINT64_C(0x0000000000FF0000)) << 24) \
| (((n) & UINT64_C(0x00000000FF000000)) << 8) \
| (((n) & UINT64_C(0x000000FF00000000)) >> 8) \
| (((n) & UINT64_C(0x0000FF0000000000)) >> 24) \
| (((n) & UINT64_C(0x00FF000000000000)) >> 40) \
| (((n) & UINT64_C(0xFF00000000000000)) >> 56) \
)