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;
Stay a leaf: pad the tail in shadow space, not a stack frameAug 20
9; The function is a leaf and only touches volatile registers: ymm0-5 and
10; rax, rcx, r8, r9. The last partial block is padded in the caller's shadow
11; space.
13; nasm -f win64 utf8_avx2.asm
15 default rel
16 bits 64
Stay a leaf: pad the tail in shadow space, not a stack frameAug 20
18%define TAIL rsp + 8 ; 32 bytes of shadow space
22; Error bits, one per kind of illegal sequence.
23%define TOO_SHORT 0x01
24%define TOO_LONG 0x02
25%define OVERLONG_3 0x04
26%define TOO_LARGE 0x08
27%define SURROGATE 0x10
28%define OVERLONG_2 0x20
29%define TOO_LARGE_1000 0x40
30%define OVERLONG_4 0x40
31%define TWO_CONTS 0x80
32%define CARRY (TOO_SHORT | TOO_LONG | TWO_CONTS)
34 align 16
35byte_1_high: ; indexed by the high nibble of the previous byte
36 times 8 db TOO_LONG
37 times 4 db TWO_CONTS
38 db TOO_SHORT | OVERLONG_2
39 db TOO_SHORT
40 db TOO_SHORT | OVERLONG_3 | SURROGATE
41 db TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4
43byte_1_low: ; indexed by the low nibble of the previous byte
44 db CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4
45 db CARRY | OVERLONG_2
46 db CARRY
47 db CARRY
48 db CARRY | TOO_LARGE
49 times 8 db CARRY | TOO_LARGE | TOO_LARGE_1000
50 db CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE
51 db CARRY | TOO_LARGE | TOO_LARGE_1000
52 db CARRY | TOO_LARGE | TOO_LARGE_1000
54byte_2_high: ; indexed by the high nibble of the current byte
55 times 8 db TOO_SHORT
56 db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4
57 db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE
58 db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE
59 db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE
60 times 4 db TOO_SHORT
62 align 32
63nibble_mask: times 32 db 0x0F
64third_byte: times 32 db 0xE0 - 0x80 ; only 111_____ stays >= 0x80
65fourth_byte: times 32 db 0xF0 - 0x80 ; only 1111____ stays >= 0x80
66high_bit: times 32 db 0x80
67spaces: times 32 db 0x20
A block may not end inside a sequenceAug 12
68incomplete: times 29 db 0xFF ; a block may not end inside a sequence
69 db 0xF0 - 1, 0xE0 - 1, 0xC0 - 1
71 section .text
72 global utf8_valid_avx2
74; One 32-byte block. In: ymm0 = block, ymm1 = previous block.
A block may not end inside a sequenceAug 12
75; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence".
77 vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo]
78 vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor
79 vpsrlw ymm3, ymm5, 4
80 vpand ymm3, ymm3, [nibble_mask]
81 vbroadcasti128 ymm4, [byte_1_high]
82 vpshufb ymm3, ymm4, ymm3 ; byte_1_high[prev1 >> 4]
83 vpand ymm5, ymm5, [nibble_mask]
84 vbroadcasti128 ymm4, [byte_1_low]
85 vpshufb ymm5, ymm4, ymm5 ; byte_1_low[prev1 & 15]
86 vpand ymm3, ymm3, ymm5
87 vpsrlw ymm5, ymm0, 4
88 vpand ymm5, ymm5, [nibble_mask]
89 vbroadcasti128 ymm4, [byte_2_high]
90 vpshufb ymm5, ymm4, ymm5 ; byte_2_high[block >> 4]
91 vpand ymm3, ymm3, ymm5 ; special cases
92 vperm2i128 ymm4, ymm1, ymm0, 0x21
93 vpalignr ymm5, ymm0, ymm4, 14 ; prev2
94 vpsubusb ymm5, ymm5, [third_byte]
95 vpalignr ymm4, ymm0, ymm4, 13 ; prev3
96 vpsubusb ymm4, ymm4, [fourth_byte]
97 vpor ymm4, ymm4, ymm5
99 vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one
100 vpor ymm2, ymm2, ymm4
104utf8_valid_avx2:
105 mov r8, rcx ; cursor
106 mov r9, rdx ; bytes left
107 vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros
108 vpxor xmm2, xmm2, xmm2
112.loop:
113 vmovdqu ymm0, [r8]
114 CHECK_BLOCK
115 vmovdqa ymm1, ymm0
116 add r8, 32
117 sub r9, 32
118 cmp r9, 32
120.tail:
121 test r9, r9
Stay a leaf: pad the tail in shadow space, not a stack frameAug 20
123 vmovdqa ymm0, [spaces] ; pad the last block with ASCII
124 vmovdqu [TAIL], ymm0
126.copy: movzx ecx, byte [r8 + rax]
129 cmp rax, r9
133.eof:
A block may not end inside a sequenceAug 12
134 vpor ymm2, ymm2, ymm3 ; and the input may not stop mid-sequence
136 setz al
137 movzx eax, al