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.
System V build
Browse filesUnder elf64 the arguments arrive in rdi and rsi, and the last block is padded in the red zone instead of the shadow space. Both files assemble for both ABIs on every build.
failing
8 steps in 1 ms
3 files changed
+38−13
@@ -1,9 +1,18 @@
1−# Verified builds for utf8-check.
1+# Verified builds for utf8-check. Every commit assembles both validators for
2+# both ABIs, checks them against table 3-7 and each other, then times them.
23
34 [project]
45 isa = "x86-64"
56 assembler = "nasm"
6−target = "Windows x64 objects, AVX2"
7+target = "Windows x64 and System V objects, AVX2"
8+
9+[[step]]
10+name = "assemble for System V"
11+run = ["nasm", "-f", "elf64", "-o", "build/utf8_scalar.o", "src/utf8_scalar.asm"]
12+
13+[[step]]
14+name = "assemble AVX2 for System V"
15+run = ["nasm", "-f", "elf64", "-o", "build/utf8_avx2.o", "src/utf8_avx2.asm"]
716
817 [[step]]
918 name = "assemble the scalar reference"
@@ -35,4 +44,4 @@ expect = "MiB of mixed text"
3544
3645 [artifact]
3746 path = "build/utf8_avx2.obj"
38−paths = ["build/utf8_scalar.obj"]
47+paths = ["build/utf8_scalar.obj", "build/utf8_avx2.o", "build/utf8_scalar.o"]
@@ -6,18 +6,26 @@
66 ; nibbles of the current and previous byte, flag every illegal pair of bytes
77 ; at once, and a saturating subtract checks the 3- and 4-byte lengths.
88 ;
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.
9+; The function is a leaf on both ABIs and only touches volatile registers:
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).
1212 ;
13−; nasm -f win64 utf8_avx2.asm
13+; nasm -f win64 utf8_avx2.asm or nasm -f elf64 utf8_avx2.asm
1414
1515 default rel
1616 bits 64
1717
18−%define TAIL rsp + 8 ; 32 bytes of shadow space
19−
18+%ifidn __OUTPUT_FORMAT__, win64
19+ %define ARG0 rcx
20+ %define ARG1 rdx
21+ %define TAIL rsp + 8 ; 32 bytes of shadow space
2022 section .rdata align=32
23+%else
24+ %define ARG0 rdi
25+ %define ARG1 rsi
26+ %define TAIL rsp - 32 ; red zone
27+ section .rodata align=32
28+%endif
2129
2230 ; Error bits, one per kind of illegal sequence.
2331 %define TOO_SHORT 0x01
@@ -109,8 +117,8 @@ incomplete: times 29 db 0xFF ; a block may not end inside a sequence
109117 %endmacro
110118
111119 utf8_valid_avx2:
112− mov r8, rcx ; cursor
113− mov r9, rdx ; bytes left
120+ mov r8, ARG0 ; cursor
121+ mov r9, ARG1 ; bytes left
114122 vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros
115123 vpxor xmm2, xmm2, xmm2
116124 vpxor xmm3, xmm3, xmm3
@@ -18,12 +18,20 @@
1818 default rel
1919 bits 64
2020
21+%ifidn __OUTPUT_FORMAT__, win64
22+ %define ARG0 rcx
23+ %define ARG1 rdx
24+%else
25+ %define ARG0 rdi
26+ %define ARG1 rsi
27+%endif
28+
2129 section .text
2230 global utf8_valid_scalar
2331
2432 utf8_valid_scalar:
25− mov r8, rcx ; cursor
26− mov r9, rdx
33+ mov r8, ARG0 ; cursor
34+ mov r9, ARG1
2735 add r9, r8 ; end
2836 .next: cmp r8, r9
2937 jae .valid