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.
Scalar validator with every range from table 3-7
Browse filesOne byte at a time, with each lead byte's allowed second byte spelled out: E0 needs A0..BF, ED stops at 9F before the surrogates, F0 starts at 90 and F4 ends at 8F. The tests walk the edges of the table.
failing
3 steps in 1 ms
4 files changed
+193−0
@@ -0,0 +1,9 @@
1+# utf8-check
2+
3+UTF-8 validation in x86-64 assembly. For now there is the reference: one byte at a time, with every range of Unicode table 3-7 spelled out.
4+
5+```c
6+int utf8_valid_scalar(const uint8_t *buf, size_t len); // 1 = valid, 0 = not
7+```
8+
9+`test/test.c` checks it against the edges of the table: the first and last code point of every length, both sides of the surrogates, overlongs, and bytes that never appear.
@@ -0,0 +1,23 @@
1+# Verified builds for utf8-check.
2+
3+[project]
4+isa = "x86-64"
5+assembler = "nasm"
6+target = "Windows x64 object"
7+
8+[[step]]
9+name = "assemble the scalar reference"
10+run = ["nasm", "-f", "win64", "-o", "build/utf8_scalar.obj", "src/utf8_scalar.asm"]
11+
12+[[step]]
13+name = "build the tests"
14+run = ["gcc", "-O2", "-Wall", "-o", "build/test.exe", "test/test.c", "build/utf8_scalar.obj"]
15+
16+[[step]]
17+name = "table 3-7"
18+on = "windows"
19+run = ["./build/test.exe"]
20+expect = "0 wrong"
21+
22+[artifact]
23+path = "build/utf8_scalar.obj"
@@ -0,0 +1,107 @@
1+; utf8_scalar.asm: the reference. One byte at a time, every range spelled out.
2+;
3+; int utf8_valid_scalar(const uint8_t *buf, size_t len); 1 = valid, 0 = not
4+;
5+; Unicode 15, table 3-7 (well-formed byte sequences):
6+; 00..7F
7+; C2..DF 80..BF
8+; E0 A0..BF 80..BF
9+; E1..EC 80..BF 80..BF
10+; ED 80..9F 80..BF
11+; EE..EF 80..BF 80..BF
12+; F0 90..BF 80..BF 80..BF
13+; F1..F3 80..BF 80..BF 80..BF
14+; F4 80..8F 80..BF 80..BF
15+;
16+; This is the oracle for the SIMD version to come.
17+
18+ default rel
19+ bits 64
20+
21+ section .text
22+ global utf8_valid_scalar
23+
24+utf8_valid_scalar:
25+ mov r8, rcx ; cursor
26+ mov r9, rdx
27+ add r9, r8 ; end
28+.next: cmp r8, r9
29+ jae .valid
30+ movzx eax, byte [r8]
31+ inc r8
32+ cmp al, 0x80
33+ jb .next ; ASCII
34+ cmp al, 0xC2
35+ jb .invalid ; stray continuation, or overlong C0/C1
36+ cmp al, 0xE0
37+ jb .two
38+ cmp al, 0xF0
39+ jb .three
40+ cmp al, 0xF5
41+ jb .four
42+ jmp .invalid
43+
44+.two: cmp r8, r9
45+ jae .invalid
46+ movzx ecx, byte [r8]
47+ and ecx, 0xC0
48+ cmp ecx, 0x80
49+ jne .invalid
50+ inc r8
51+ jmp .next
52+
53+.three: lea rcx, [r8 + 2]
54+ cmp rcx, r9
55+ ja .invalid
56+ mov edx, 0x80 ; second byte: low bound
57+ mov r10d, 0xBF ; high bound
58+ cmp al, 0xE0
59+ jne .t1
60+ mov edx, 0xA0 ; E0: no overlongs
61+.t1: cmp al, 0xED
62+ jne .t2
63+ mov r10d, 0x9F ; ED: no surrogates
64+.t2: movzx ecx, byte [r8]
65+ cmp ecx, edx
66+ jb .invalid
67+ cmp ecx, r10d
68+ ja .invalid
69+ movzx ecx, byte [r8 + 1]
70+ and ecx, 0xC0
71+ cmp ecx, 0x80
72+ jne .invalid
73+ add r8, 2
74+ jmp .next
75+
76+.four: lea rcx, [r8 + 3]
77+ cmp rcx, r9
78+ ja .invalid
79+ mov edx, 0x80
80+ mov r10d, 0xBF
81+ cmp al, 0xF0
82+ jne .f1
83+ mov edx, 0x90 ; F0: no overlongs
84+.f1: cmp al, 0xF4
85+ jne .f2
86+ mov r10d, 0x8F ; F4: nothing past U+10FFFF
87+.f2: movzx ecx, byte [r8]
88+ cmp ecx, edx
89+ jb .invalid
90+ cmp ecx, r10d
91+ ja .invalid
92+ movzx ecx, byte [r8 + 1]
93+ and ecx, 0xC0
94+ cmp ecx, 0x80
95+ jne .invalid
96+ movzx ecx, byte [r8 + 2]
97+ and ecx, 0xC0
98+ cmp ecx, 0x80
99+ jne .invalid
100+ add r8, 3
101+ jmp .next
102+
103+.valid: mov eax, 1
104+ ret
105+.invalid:
106+ xor eax, eax
107+ ret
@@ -0,0 +1,54 @@
1+// test.c: the boundaries of Unicode table 3-7.
2+//
3+// build/test.exe
4+
5+#include <stdint.h>
6+#include <stdio.h>
7+#include <string.h>
8+
9+int utf8_valid_scalar(const uint8_t *buf, size_t len);
10+
11+static const struct {
12+ const char *bytes;
13+ int valid;
14+ const char *what;
15+} samples[] = {
16+ { "", 1, "nothing at all" },
17+ { "plain ASCII", 1, "ASCII" },
18+ { "\xC2\x80", 1, "U+0080, the first two-byte code point" },
19+ { "\xDF\xBF", 1, "U+07FF, the last" },
20+ { "\xE0\xA0\x80", 1, "U+0800, the first three-byte code point" },
21+ { "\xED\x9F\xBF", 1, "U+D7FF, just below the surrogates" },
22+ { "\xEE\x80\x80", 1, "U+E000, just above them" },
23+ { "\xEF\xBF\xBF", 1, "U+FFFF" },
24+ { "\xF0\x90\x80\x80", 1, "U+10000, the first four-byte code point" },
25+ { "\xF4\x8F\xBF\xBF", 1, "U+10FFFF, the last code point" },
26+ { "\xC0\x80", 0, "an overlong NUL" },
27+ { "\xC1\xBF", 0, "an overlong U+007F" },
28+ { "\xE0\x9F\xBF", 0, "an overlong U+07FF" },
29+ { "\xED\xA0\x80", 0, "U+D800, a surrogate" },
30+ { "\xED\xBF\xBF", 0, "U+DFFF, a surrogate" },
31+ { "\xF0\x8F\xBF\xBF", 0, "an overlong U+FFFF" },
32+ { "\xF4\x90\x80\x80", 0, "U+110000, past the end of Unicode" },
33+ { "\xF5\x80\x80\x80", 0, "F5, which never appears" },
34+ { "\xFF", 0, "FF, which never appears" },
35+ { "\x80", 0, "a continuation with no lead" },
36+ { "\xC3\xA9\xA9", 0, "one continuation too many" },
37+ { "\xC3", 0, "a two-byte lead at the very end" },
38+ { "\xC3" "A", 0, "a two-byte lead followed by ASCII" },
39+};
40+#define SAMPLES (sizeof samples / sizeof samples[0])
41+
42+int main(void)
43+{
44+ int wrong = 0;
45+ for (size_t i = 0; i < SAMPLES; i++) {
46+ int got = utf8_valid_scalar((const uint8_t *)samples[i].bytes, strlen(samples[i].bytes));
47+ if (got == samples[i].valid)
48+ continue;
49+ wrong++;
50+ printf("wrong: %s: got %d\n", samples[i].what, got);
51+ }
52+ printf("cases: %zu samples, %d wrong\n", SAMPLES, wrong);
53+ return wrong != 0;
54+}