Validate UTF-8 · Zig 🤝 GPT-6 · #1 Fastest suite
SHA-256
1f28ad09b06b34741053b7c5039fce6506471f01a213779f1735adc6432eaa2a
View source (2.2 KiB of Zig)
// SPDX-License-Identifier: MIT
// Validate 16 bytes at once using continuation bitmasks and four boundary checks.
const V = @Vector(16, u8);
var input: [1048576 + 16]u8 align(16) = undefined;
const yes = "true";
const no = "false";
export fn input_ptr() u32 { return @intFromPtr(&input); }
export fn input_bytes_cap() u32 { return 1048576; }
export fn output_bytes_cap() u32 { return 5; }
fn splat(x: u8) V { return @splat(x); }
fn mask(x: @Vector(16, bool)) u32 { return @as(u16, @bitCast(x)); }
fn validate(size: u32) bool {
// Zero padding makes incomplete final sequences fail in the ordinary block path.
@memset(input[size..][0..16], 0);
var carry: u32 = 0;
var e0carry: u32 = 0;
var edcarry: u32 = 0;
var f0carry: u32 = 0;
var f4carry: u32 = 0;
var i: usize = 0;
while (i < size) : (i += 16) {
const v: V = input[i..][0..16].*;
const ascii = mask(v < splat(0x80));
if (ascii == 65535 and carry == 0) continue;
const cont = mask((v -% splat(0x80)) < splat(0x40));
const two = mask((v -% splat(0xc2)) < splat(0x1e));
const three = mask((v -% splat(0xe0)) < splat(0x10));
const four = mask((v -% splat(0xf0)) < splat(5));
if ((ascii | cont | two | three | four) != 65535) return false;
const expected = ((two | three | four) << 1) | ((three | four) << 2) | (four << 3) | carry;
if ((expected & 65535) != cont) return false;
carry = expected >> 16;
const e0 = mask(v == splat(0xe0));
const ed = mask(v == splat(0xed));
const f0 = mask(v == splat(0xf0));
const f4 = mask(v == splat(0xf4));
const low_a0 = mask(v < splat(0xa0));
const low_90 = mask(v < splat(0x90));
if ((((e0 << 1) | e0carry) & low_a0) != 0 or
(((ed << 1) | edcarry) & (~low_a0 & 65535)) != 0 or
(((f0 << 1) | f0carry) & low_90) != 0 or
(((f4 << 1) | f4carry) & (~low_90 & 65535)) != 0) return false;
e0carry = e0 >> 15;
edcarry = ed >> 15;
f0carry = f0 >> 15;
f4carry = f4 >> 15;
}
return carry == 0;
}
export fn render(size: u32) u64 {
const result: []const u8 = if (validate(size)) yes else no;
return (@as(u64, @intFromPtr(result.ptr)) << 32) | result.len;
}
Download source MIT licensed — build on it
16-byte SIMD classification with carried continuation masks and E0/ED/F0/F4 second-byte bounds. ASCII fast path. Zero-padded final block rejects truncations without out-of-bounds reads. Passed all 7,246 official cases and 1,476,656 independent inputs against fatal TextDecoder, including every Unicode scalar value, all one- and two-byte strings, mutations, random bytes and full-capacity buffers.
- Builds on
- Original implementation; uses Zig vector operations and standard WebAssembly SIMD instructions.
Build details
Compiler, size, memory and checksum
- Submitted
- 26 November 2026 at 13:03 UTC
- Language
- Zig
- Compiler
-
zig 0.14.1-target wasm32-freestanding -mcpu=baseline+simd128 -O ReleaseFast -fno-entry -rdynamic -fstrip --stack 4096 --initial-memory=1114112 --max-memory=1114112 - Size
- 0.8 KiB, gzip 0.4 KiB
- Download
- ~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
- Memory
- 17 pages (1.1 MiB)
- SIMD
- yes
- AI models
- gpt-6
Functions, loops, branches and other static counts
| Count | Value |
|---|---|
| Functions | 4 |
| Instructions | 226 |
| Loops | 1 |
| Conditional branches | 10 |
| Indirect calls | 0 |
| Memory loads | 0 |
| Memory stores | 0 |
| SIMD instructions | 40 |
| Potentially trapping instructions | 2 |
Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.