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.
AVX2: three nibble tables and a saturating subtractAug 4
1; utf8_avx2.asm: UTF-8 validation, 32 bytes per step.
2;
3; int utf8_valid_avx2(const uint8_t *buf, size_t len); 1 = valid, 0 = not
4;
5; The lookup algorithm of Keiser and Lemire: three 16-entry tables, indexed by
6; nibbles of the current and previous byte, flag every illegal pair of bytes
7; at once, and a saturating subtract checks the 3- and 4-byte lengths.
8;
10; ymm0-5 and rax, rcx, r8, r9. The last partial block is padded in the
11; caller's shadow space (win64) or the red zone (System V).
15 default rel
16 bits 64
19 %define ARG0 rcx
20 %define ARG1 rdx
21 %define TAIL rsp + 8 ; 32 bytes of shadow space
24 %define ARG0 rdi
25 %define ARG1 rsi
26 %define TAIL rsp - 32 ; red zone
27 section .rodata align=32
28%endif
30; Error bits, one per kind of illegal sequence.
31%define TOO_SHORT 0x01
32%define TOO_LONG 0x02
33%define OVERLONG_3 0x04
34%define TOO_LARGE 0x08
35%define SURROGATE 0x10
36%define OVERLONG_2 0x20
37%define TOO_LARGE_1000 0x40
38%define OVERLONG_4 0x40
39%define TWO_CONTS 0x80
40%define CARRY (TOO_SHORT | TOO_LONG | TWO_CONTS)
42 align 16
43byte_1_high: ; indexed by the high nibble of the previous byte
44 times 8 db TOO_LONG
45 times 4 db TWO_CONTS
46 db TOO_SHORT | OVERLONG_2
47 db TOO_SHORT
48 db TOO_SHORT | OVERLONG_3 | SURROGATE
49 db TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4
51byte_1_low: ; indexed by the low nibble of the previous byte
52 db CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4
53 db CARRY | OVERLONG_2
54 db CARRY
55 db CARRY
56 db CARRY | TOO_LARGE
57 times 8 db CARRY | TOO_LARGE | TOO_LARGE_1000
58 db CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE
59 db CARRY | TOO_LARGE | TOO_LARGE_1000
60 db CARRY | TOO_LARGE | TOO_LARGE_1000
62byte_2_high: ; indexed by the high nibble of the current byte
63 times 8 db TOO_SHORT
64 db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4
65 db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE
66 db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE
67 db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE
68 times 4 db TOO_SHORT
70 align 32
71nibble_mask: times 32 db 0x0F
72third_byte: times 32 db 0xE0 - 0x80 ; only 111_____ stays >= 0x80
73fourth_byte: times 32 db 0xF0 - 0x80 ; only 1111____ stays >= 0x80
74high_bit: times 32 db 0x80
75spaces: times 32 db 0x20
A block may not end inside a sequenceAug 12
76incomplete: times 29 db 0xFF ; a block may not end inside a sequence
77 db 0xF0 - 1, 0xE0 - 1, 0xC0 - 1
79 section .text
80 global utf8_valid_avx2
82; One 32-byte block. In: ymm0 = block, ymm1 = previous block.
A block may not end inside a sequenceAug 12
83; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence".
86 test eax, eax
87 jnz %%multibyte
88 vpor ymm2, ymm2, ymm3 ; ASCII: only a dangling sequence can fail
89 jmp %%done
90%%multibyte:
AVX2: three nibble tables and a saturating subtractAug 4
91 vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo]
92 vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor
93 vpsrlw ymm3, ymm5, 4
94 vpand ymm3, ymm3, [nibble_mask]
95 vbroadcasti128 ymm4, [byte_1_high]
96 vpshufb ymm3, ymm4, ymm3 ; byte_1_high[prev1 >> 4]
97 vpand ymm5, ymm5, [nibble_mask]
98 vbroadcasti128 ymm4, [byte_1_low]
99 vpshufb ymm5, ymm4, ymm5 ; byte_1_low[prev1 & 15]
100 vpand ymm3, ymm3, ymm5
101 vpsrlw ymm5, ymm0, 4
102 vpand ymm5, ymm5, [nibble_mask]
103 vbroadcasti128 ymm4, [byte_2_high]
104 vpshufb ymm5, ymm4, ymm5 ; byte_2_high[block >> 4]
105 vpand ymm3, ymm3, ymm5 ; special cases
106 vperm2i128 ymm4, ymm1, ymm0, 0x21
107 vpalignr ymm5, ymm0, ymm4, 14 ; prev2
108 vpsubusb ymm5, ymm5, [third_byte]
109 vpalignr ymm4, ymm0, ymm4, 13 ; prev3
110 vpsubusb ymm4, ymm4, [fourth_byte]
111 vpor ymm4, ymm4, ymm5
113 vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one
114 vpor ymm2, ymm2, ymm4
119utf8_valid_avx2:
121 mov r9, ARG1 ; bytes left
AVX2: three nibble tables and a saturating subtractAug 4
122 vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros
123 vpxor xmm2, xmm2, xmm2
127.loop:
128 vmovdqu ymm0, [r8]
129 CHECK_BLOCK
130 vmovdqa ymm1, ymm0
131 add r8, 32
132 sub r9, 32
133 cmp r9, 32
135.tail:
136 test r9, r9
Stay a leaf: pad the tail in shadow space, not a stack frameAug 20
138 vmovdqa ymm0, [spaces] ; pad the last block with ASCII
139 vmovdqu [TAIL], ymm0
141.copy: movzx ecx, byte [r8 + rax]
144 cmp rax, r9
148.eof:
A block may not end inside a sequenceAug 12
149 vpor ymm2, ymm2, ymm3 ; and the input may not stop mid-sequence
151 setz al
152 movzx eax, al