acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

tsvet/utf8-check

Public
Star9

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.

History
146 lines·5.3 KB·NASM Raw Blame
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 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
18%define TAIL rsp + 8 ; 32 bytes of shadow space
20 section .rdata align=32
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
68incomplete: times 29 db 0xFF ; a block may not end inside a sequence
69 db 0xF0 - 1, 0xE0 - 1, 0xC0 - 1
71 section .text
74; One 32-byte block. In: ymm0 = block, ymm1 = previous block.
75; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence".
76%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:
83 vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo]
84 vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor
85 vpsrlw ymm3, ymm5, 4
86 vpand ymm3, ymm3, [nibble_mask]
87 vbroadcasti128 ymm4, [byte_1_high]
88 vpshufb ymm3, ymm4, ymm3 ; byte_1_high[prev1 >> 4]
89 vpand ymm5, ymm5, [nibble_mask]
90 vbroadcasti128 ymm4, [byte_1_low]
91 vpshufb ymm5, ymm4, ymm5 ; byte_1_low[prev1 & 15]
92 vpand ymm3, ymm3, ymm5
93 vpsrlw ymm5, ymm0, 4
94 vpand ymm5, ymm5, [nibble_mask]
95 vbroadcasti128 ymm4, [byte_2_high]
96 vpshufb ymm5, ymm4, ymm5 ; byte_2_high[block >> 4]
97 vpand ymm3, ymm3, ymm5 ; special cases
98 vperm2i128 ymm4, ymm1, ymm0, 0x21
99 vpalignr ymm5, ymm0, ymm4, 14 ; prev2
100 vpsubusb ymm5, ymm5, [third_byte]
101 vpalignr ymm4, ymm0, ymm4, 13 ; prev3
102 vpsubusb ymm4, ymm4, [fourth_byte]
103 vpor ymm4, ymm4, ymm5
104 vpand ymm4, ymm4, [high_bit] ; must be a 2nd or 3rd continuation
105 vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one
106 vpor ymm2, ymm2, ymm4
107 vpsubusb ymm3, ymm0, [incomplete]
108%%done:
109%endmacro
111utf8_valid_avx2:
112 mov r8, rcx ; cursor
113 mov r9, rdx ; bytes left
114 vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros
115 vpxor xmm2, xmm2, xmm2
116 vpxor xmm3, xmm3, xmm3
117 cmp r9, 32
119.loop:
120 vmovdqu ymm0, [r8]
121 CHECK_BLOCK
122 vmovdqa ymm1, ymm0
123 add r8, 32
124 sub r9, 32
125 cmp r9, 32
127.tail:
128 test r9, r9
130 vmovdqa ymm0, [spaces] ; pad the last block with ASCII
131 vmovdqu [TAIL], ymm0
132 xor eax, eax
133.copy: movzx ecx, byte [r8 + rax]
134 mov [TAIL + rax], cl
135 inc rax
136 cmp rax, r9
138 vmovdqu ymm0, [TAIL]
139 CHECK_BLOCK
140.eof:
141 vpor ymm2, ymm2, ymm3 ; and the input may not stop mid-sequence
142 vptest ymm2, ymm2
143 setz al
144 movzx eax, al
145 vzeroupper
146 ret