; utf8_avx2.asm: UTF-8 validation, 32 bytes per step. ; ; int utf8_valid_avx2(const uint8_t *buf, size_t len); 1 = valid, 0 = not ; ; The lookup algorithm of Keiser and Lemire: three 16-entry tables, indexed by ; nibbles of the current and previous byte, flag every illegal pair of bytes ; at once, and a saturating subtract checks the 3- and 4-byte lengths. ; ; The function is a leaf and only touches volatile registers: ymm0-5 and ; rax, rcx, r8, r9. The last partial block is padded in the caller's shadow ; space. ; ; nasm -f win64 utf8_avx2.asm default rel bits 64 %define TAIL rsp + 8 ; 32 bytes of shadow space section .rdata align=32 ; Error bits, one per kind of illegal sequence. %define TOO_SHORT 0x01 %define TOO_LONG 0x02 %define OVERLONG_3 0x04 %define TOO_LARGE 0x08 %define SURROGATE 0x10 %define OVERLONG_2 0x20 %define TOO_LARGE_1000 0x40 %define OVERLONG_4 0x40 %define TWO_CONTS 0x80 %define CARRY (TOO_SHORT | TOO_LONG | TWO_CONTS) align 16 byte_1_high: ; indexed by the high nibble of the previous byte times 8 db TOO_LONG times 4 db TWO_CONTS db TOO_SHORT | OVERLONG_2 db TOO_SHORT db TOO_SHORT | OVERLONG_3 | SURROGATE db TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4 byte_1_low: ; indexed by the low nibble of the previous byte db CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4 db CARRY | OVERLONG_2 db CARRY db CARRY db CARRY | TOO_LARGE times 8 db CARRY | TOO_LARGE | TOO_LARGE_1000 db CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE db CARRY | TOO_LARGE | TOO_LARGE_1000 db CARRY | TOO_LARGE | TOO_LARGE_1000 byte_2_high: ; indexed by the high nibble of the current byte times 8 db TOO_SHORT db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4 db TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE db TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE times 4 db TOO_SHORT align 32 nibble_mask: times 32 db 0x0F third_byte: times 32 db 0xE0 - 0x80 ; only 111_____ stays >= 0x80 fourth_byte: times 32 db 0xF0 - 0x80 ; only 1111____ stays >= 0x80 high_bit: times 32 db 0x80 spaces: times 32 db 0x20 incomplete: times 29 db 0xFF ; a block may not end inside a sequence db 0xF0 - 1, 0xE0 - 1, 0xC0 - 1 section .text global utf8_valid_avx2 ; One 32-byte block. In: ymm0 = block, ymm1 = previous block. ; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence". %macro CHECK_BLOCK 0 vpmovmskb eax, ymm0 test eax, eax jnz %%multibyte vpor ymm2, ymm2, ymm3 ; ASCII: only a dangling sequence can fail jmp %%done %%multibyte: vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo] vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor vpsrlw ymm3, ymm5, 4 vpand ymm3, ymm3, [nibble_mask] vbroadcasti128 ymm4, [byte_1_high] vpshufb ymm3, ymm4, ymm3 ; byte_1_high[prev1 >> 4] vpand ymm5, ymm5, [nibble_mask] vbroadcasti128 ymm4, [byte_1_low] vpshufb ymm5, ymm4, ymm5 ; byte_1_low[prev1 & 15] vpand ymm3, ymm3, ymm5 vpsrlw ymm5, ymm0, 4 vpand ymm5, ymm5, [nibble_mask] vbroadcasti128 ymm4, [byte_2_high] vpshufb ymm5, ymm4, ymm5 ; byte_2_high[block >> 4] vpand ymm3, ymm3, ymm5 ; special cases vperm2i128 ymm4, ymm1, ymm0, 0x21 vpalignr ymm5, ymm0, ymm4, 14 ; prev2 vpsubusb ymm5, ymm5, [third_byte] vpalignr ymm4, ymm0, ymm4, 13 ; prev3 vpsubusb ymm4, ymm4, [fourth_byte] vpor ymm4, ymm4, ymm5 vpand ymm4, ymm4, [high_bit] ; must be a 2nd or 3rd continuation vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one vpor ymm2, ymm2, ymm4 vpsubusb ymm3, ymm0, [incomplete] %%done: %endmacro utf8_valid_avx2: mov r8, rcx ; cursor mov r9, rdx ; bytes left vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros vpxor xmm2, xmm2, xmm2 vpxor xmm3, xmm3, xmm3 cmp r9, 32 jb .tail .loop: vmovdqu ymm0, [r8] CHECK_BLOCK vmovdqa ymm1, ymm0 add r8, 32 sub r9, 32 cmp r9, 32 jae .loop .tail: test r9, r9 jz .eof vmovdqa ymm0, [spaces] ; pad the last block with ASCII vmovdqu [TAIL], ymm0 xor eax, eax .copy: movzx ecx, byte [r8 + rax] mov [TAIL + rax], cl inc rax cmp rax, r9 jb .copy vmovdqu ymm0, [TAIL] CHECK_BLOCK .eof: vpor ymm2, ymm2, ymm3 ; and the input may not stop mid-sequence vptest ymm2, ymm2 setz al movzx eax, al vzeroupper ret