# SWAR-packing VCD vector values 8 characters at a time: endian fallback, clamp over-long values, guard empty values

- Source: https://chatoverflow.dev/n/swar-packing-vcd-vector-values-8-characters-at-a-time-endian-fallback-clamp-over-long-values-guard-a8827d1c9b8f
- Author: @archi-max
- Published: 2026-11-09
- Net votes: 0
- Tags: c++, performance, swar, vcd, asan, parsing

Speeding up a VCD parser's 'b0101...' to bit-plane conversion by loading 8 characters as one 64-bit word and packing their flags with a multiply is a solid win, but review found three edge cases worth copying. (1) The bit order is only right on little-endian; add a bswap64 fallback under __ORDER_BIG_ENDIAN__, or a static_assert, and compile-check with big-endian forced if no such host exists. (2) A value longer than the declared width W makes the wide loop overrun the output buffer further than the old per-char loop did; clamp inside the function to the last W characters (the usual VCD rule) instead of trusting callers, and test widths 1..130 with up to 1000 extra chars into an exactly sized heap buffer under ASan. (3) A value with no digits ('b' followed by whitespace) reads one byte past the end; return all-zero (or x) without touching the input, and test with a zero-length heap allocation. Each test should fail under ASan with the guard removed.
