Skip to main content

Module util

Module util 

Source
Expand description

High-performance utility functions for binary data processing.

This module provides optimized primitives for:

  • Unaligned memory reads (using byteorder for optimal codegen)
  • SIMD-accelerated byte searches (via memchr)
  • Fast pointer scanning for batch operations

§Performance Notes

All functions are aggressively inlined and designed to compile down to optimal machine code. On x86-64 and ARM64:

  • read_u64_le compiles to a single unaligned load instruction
  • read_u32_le compiles to a single unaligned load instruction
  • memchr_null uses SIMD vectorization (AVX2/NEON when available)

Constants§

ADDR_MASK_48BIT
Address mask for stripping PAC/TBI bits from pointers. Keeps lower 48 bits which is the actual address portion.
MIN_VALID_POINTER
Minimum valid pointer value (skip small values that are likely not pointers). 0x100000000 = 4GB, typical minimum for 64-bit address space.

Functions§

align_down
Aligns a value down to the given power-of-two alignment.
align_up
Aligns a value up to the given power-of-two alignment.
batch_transform_u64
Batch processes an array of u64 values, applying a transformation function.
is_aligned
Checks if a value is aligned to the given power-of-two alignment.
memchr_find
Finds the position of the first occurrence of needle in haystack.
memchr_null
Finds the position of the first null byte in a slice.
read_sleb128_fast
Reads a signed LEB128 value with fast paths for common cases.
read_u16_le
Reads a little-endian u16 from an unaligned byte slice.
read_u16_le_at
Reads a little-endian u16 from a byte slice at the given offset.
read_u32_le
Reads a little-endian u32 from an unaligned byte slice.
read_u32_le_at
Reads a little-endian u32 from a byte slice at the given offset.
read_u64_le
Reads a little-endian u64 from an unaligned byte slice.
read_u64_le_at
Reads a little-endian u64 from a byte slice at the given offset.
read_uleb128_fast
Reads an unsigned LEB128 value with fast paths for common cases.
scan_pointers_in_range
Scans a data slice for potential 64-bit pointers.