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