Skip to main content

Module hex

Module hex 

Source
Expand description

SIMD-accelerated hex encoding and decoding

§Performance

Encoding (32 bytes → 64 hex chars):

  • format!(): ~1630 ns
  • Scalar LUT: ~35 ns (47x faster)
  • NEON SIMD (ARM64): ~26 ns (62x faster)
  • SSE2 SIMD (x86_64): ~30 ns (estimated)
  • AVX2 SIMD (x86_64): ~25 ns (estimated)

Decoding (64 hex chars → 32 bytes):

  • NEON SIMD (ARM64): ~3 ns (7x faster than LUT)
  • SSE2 SIMD (x86_64): ~5 ns (estimated)
  • Scalar LUT fallback: ~19 ns

§Algorithm

NEON (ARM64): Uses TBL instruction for 16-byte lookup table

SSE2/AVX2 (x86_64): Uses arithmetic approach:

  1. Split bytes into nibbles (high = byte >> 4, low = byte & 0x0F)
  2. Compare nibbles > 9 to identify hex letters (a-f)
  3. Add ‘0’ (0x30) to all, then add 0x27 for letters (a-f)
  4. Interleave and store

Functions§

bytes_to_hex_16
Convert 16-byte array to hex string using SSE2 SIMD (x86_64).
bytes_to_hex_32
Convert 32-byte array to hex string using SIMD (x86_64).
bytes_to_hex_string
Convert a byte slice to a hex string.
hex_string_to_bytes
Convert hex string to bytes (arbitrary length).
hex_string_to_bytes_checked
Validating arbitrary-length hex decode → Some(bytes) only when s is even-length and every char is [0-9A-Fa-f], else None. The infallible hex_string_to_bytes silently maps non-hex to 0, so this is the variant for decoding hex that could be malformed (corrupt storage, untrusted input). Validation runs in-register on the SIMD path (full 16-char chunks), so the check is the same speed class as the decode it guards.
hex_to_bytes_16
hex_to_bytes_32
x86_64 SIMD implementation
hex_to_bytes_32_checked
SSE2 variant — see the aarch64 hex_to_bytes_32_checked for semantics.