UTF-8 validation in x86-64: a scalar reference and an AVX2 version that checks 32 bytes per step. Every build fuzzes one against the other.
ASCII fast path
Browse filesvpmovmskb gathers the top bit of every byte. When it is zero the block is all ASCII, and the only thing that can be wrong is that the block before it ended mid-sequence. Compare the ASCII line of this build's benchmark with the one before.
failing
6 steps in 1 ms
1 file changed
+7−0
@@ -74,6 +74,12 @@ incomplete: times 29 db 0xFF ; a block may not end inside a sequence
7474 ; One 32-byte block. In: ymm0 = block, ymm1 = previous block.
7575 ; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence".
7676 %macro CHECK_BLOCK 0
77+ vpmovmskb eax, ymm0
78+ test eax, eax
79+ jnz %%multibyte
80+ vpor ymm2, ymm2, ymm3 ; ASCII: only a dangling sequence can fail
81+ jmp %%done
82+%%multibyte:
7783 vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo]
7884 vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor
7985 vpsrlw ymm3, ymm5, 4
@@ -99,6 +105,7 @@ incomplete: times 29 db 0xFF ; a block may not end inside a sequence
99105 vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one
100106 vpor ymm2, ymm2, ymm4
101107 vpsubusb ymm3, ymm0, [incomplete]
108+%%done:
102109 %endmacro
103110
104111 utf8_valid_avx2: