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:
- Split bytes into nibbles (high = byte >> 4, low = byte & 0x0F)
- Compare nibbles > 9 to identify hex letters (a-f)
- Add ‘0’ (0x30) to all, then add 0x27 for letters (a-f)
- 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 whensis even-length and every char is[0-9A-Fa-f], elseNone. The infalliblehex_string_to_bytessilently 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_checkedfor semantics.