Validate UTF-8 · Odin 🤝 Fable 5.2 · #2 Fastest

SHA-256 86d4ca2a384abf43230508baa9b8a013aed8466889e243eb60e6ea86c67e11b1

View source (6.7 KiB of Odin)
// validate_utf8 for calculated.world in Odin with Wasm SIMD.
// Keiser-Lemire lookup validation over 64-byte blocks with an ASCII fast path.
// The input is zero-padded to a whole block, so there is no tail code: padding
// after a cut-short sequence is rejected by the ordinary rules.
// Nothing is carried between vectors: "the bytes before" are loaded again from
// one, two and three bytes back, and the 64 bytes below the input stay zero.
package validate_utf8

import "base:intrinsics"
import "core:simd"

V :: simd.u8x16

INPUT :: 64
CAP :: 1 << 20

@(export, link_name = "input_ptr")
input_ptr :: proc "c" () -> u32 {return INPUT}

@(export, link_name = "input_bytes_cap")
input_bytes_cap :: proc "c" () -> u32 {return CAP}

@(export, link_name = "output_utf8_cap")
output_utf8_cap :: proc "c" () -> u32 {return 5}

@(rodata)
answers := [9]u8{'t', 'r', 'u', 'e', 'f', 'a', 'l', 's', 'e'}

TOO_SHORT :: 1 << 0
TOO_LONG :: 1 << 1
OVERLONG_3 :: 1 << 2
TOO_LARGE :: 1 << 3
SURROGATE :: 1 << 4
OVERLONG_2 :: 1 << 5
TOO_LARGE_1000 :: 1 << 6
OVERLONG_4 :: 1 << 6
TWO_CONTS :: 1 << 7
CARRY :: TOO_SHORT | TOO_LONG | TWO_CONTS

T1H :: V{
	TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG, TOO_LONG,
	TWO_CONTS, TWO_CONTS, TWO_CONTS, TWO_CONTS,
	TOO_SHORT | OVERLONG_2,
	TOO_SHORT,
	TOO_SHORT | OVERLONG_3 | SURROGATE,
	TOO_SHORT | TOO_LARGE | TOO_LARGE_1000 | OVERLONG_4,
}
T1L :: V{
	CARRY | OVERLONG_3 | OVERLONG_2 | OVERLONG_4,
	CARRY | OVERLONG_2,
	CARRY, CARRY,
	CARRY | TOO_LARGE,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000 | SURROGATE,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
	CARRY | TOO_LARGE | TOO_LARGE_1000,
}
T2H :: V{
	TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
	TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE_1000 | OVERLONG_4,
	TOO_LONG | OVERLONG_2 | TWO_CONTS | OVERLONG_3 | TOO_LARGE,
	TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
	TOO_LONG | OVERLONG_2 | TWO_CONTS | SURROGATE | TOO_LARGE,
	TOO_SHORT, TOO_SHORT, TOO_SHORT, TOO_SHORT,
}

// Non-zero where a vector's last three bytes leave a sequence unfinished.

load :: #force_inline proc "contextless" (p: [^]u8, i: u32) -> V {
	return intrinsics.unaligned_load((^V)(p[i:]))
}

// True when any lane of a comparison mask is set.
any :: #force_inline proc "contextless" (mask: V) -> bool {
	return simd.extract_msbs(mask) != {}
}

// True when any bit of v is set.
nonzero :: #force_inline proc "contextless" (v: V) -> bool {
	return simd.extract_msbs(simd.lanes_ne(transmute(simd.u64x2)v, simd.u64x2(0))) != {}
}

// True when v, the last vector before a boundary, ends inside a sequence.
open :: #force_inline proc "contextless" (v: V) -> bool {
	OPEN_END :: V{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0xf0 - 1, 0xe0 - 1, 0xc0 - 1}
	return nonzero(simd.saturating_sub(v, OPEN_END))
}

// Moves C0..DF to the bottom of the signed range: C0 -> -128, C1 -> -127, DF -> -97.
shift :: #force_inline proc "contextless" (v: V) -> simd.i8x16 {
	return transmute(simd.i8x16)(v + V(0xc0))
}

// Errors for one vector of 1- and 2-byte sequences; sp, sc are shift() of the vector before and of cur.
two :: #force_inline proc "contextless" (sp, sc: simd.i8x16, cur: V) -> V {
	lead := simd.lanes_lt(simd.shuffle(sp, sc, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), simd.i8x16(-96))
	cont := simd.lanes_lt(transmute(simd.i8x16)cur, simd.i8x16(-64))
	return lead ~ cont
}

// Errors for one 16-byte vector, given the vector before it.
check :: #force_inline proc "contextless" (prev, cur: V) -> V {
	prev1 := simd.shuffle(prev, cur, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)
	prev2 := simd.shuffle(prev, cur, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29)
	prev3 := simd.shuffle(prev, cur, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28)
	nib :: V(0x0f)
	sc :=
		intrinsics.simd_runtime_swizzle(T1H, simd.shr(prev1, V(4))) &
		intrinsics.simd_runtime_swizzle(T1L, prev1 & nib) &
		intrinsics.simd_runtime_swizzle(T2H, simd.shr(cur, V(4)))
	// A third or fourth byte must be a continuation: lead >= E0 two back, or >= F0 three back.
	must23 := simd.saturating_sub(prev2, V(0xe0 - 0x80)) | simd.saturating_sub(prev3, V(0xf0 - 0x80))
	return (must23 & V(0x80)) ~ sc
}

valid :: #force_inline proc "contextless" (n: u32) -> bool {
	input := ([^]u8)(uintptr(INPUT))
	zero: V
	intrinsics.unaligned_store((^V)(input[n:]), zero)
	if n <= 16 {
		// one vector, zero padded: a third of the benchmark inputs are this short
		a := load(input, 0)
		if !any(a) do return true
		return !(nonzero(check(zero, a)) || open(a))
	}
	// pad to a whole block
	intrinsics.unaligned_store((^V)(input[n + 16:]), zero)
	intrinsics.unaligned_store((^V)(input[n + 32:]), zero)
	intrinsics.unaligned_store((^V)(input[n + 48:]), zero)

	prev: V
	big_prev := false // the previous block had a 3- or 4-byte lead
	end := (n + 63) & ~u32(63)
	i: u32
	for i < end {
		a := load(input, i)
		b := load(input, i + 16)
		c := load(input, i + 32)
		d := load(input, i + 48)
		mx := simd.max(simd.max(a, b), simd.max(c, d))
		if !any(mx) {
			// ASCII block: only a sequence left open by the previous block can be wrong
			if open(prev) do return false
			prev = zero
			big_prev = false
			for {
				i += 64
				if i >= end do return true
				a = load(input, i)
				b = load(input, i + 16)
				c = load(input, i + 32)
				d = load(input, i + 48)
				mx = simd.max(simd.max(a, b), simd.max(c, d))
				if any(mx) do break
			}
		}
		big := any(simd.lanes_ge(mx, V(0xe0)))
		if big || big_prev {
			if nonzero(check(prev, a) | check(a, b) | check(b, c) | check(c, d)) do return false
		} else {
			// Nothing above DF here or owed from before: a byte is a continuation
			// exactly when the byte before it is C0..DF, and C0, C1 never appear.
			// (sp is in the C0/C1 test because the lookup path reports a block's last byte in the next block.)
			sp, sa, sb, sc, sd := shift(prev), shift(a), shift(b), shift(c), shift(d)
			e :=
				two(sp, sa, a) | two(sa, sb, b) | two(sb, sc, c) | two(sc, sd, d) |
				transmute(V)simd.lanes_lt(simd.min(simd.min(sp, sa), simd.min(sb, simd.min(sc, sd))), simd.i8x16(-126))
			if any(e) do return false
		}
		big_prev = big
		prev = d
		i += 64
	}
	// the input may end exactly on a block edge, with no padding to catch an open sequence
	return !open(prev)
}

@(export, link_name = "render")
render :: proc "c" (n: u32) -> u64 {
	t := u64(uintptr(&answers[0]))
	return valid(n) ? t << 32 | 4 : (t + 4) << 32 | 5
}

Odin with Wasm SIMD over 64-byte blocks. Three paths: an ASCII run loop; a path for blocks with nothing above DF (a byte is a continuation exactly when the byte before it is C0..DF, and C0/C1 never appear), with no table lookups; and Keiser-Lemire lookup validation for the rest. Changes from the parent: returns at the first invalid block, and handles inputs of 16 bytes or fewer with one vector. Unlike bdc101d6, the previous bytes come from the carried vector rather than overlapping loads, which benchmarked slower on the server.

Builds on
Written from scratch in Odin; the lookup tables follow Keiser and Lemire, Validating UTF-8 In Less Than One Instruction Per Byte (as in simdjson). Verified locally with qipok against the compliance suite and by fuzzing against Node buffer.isUtf8.

Build details

Compiler, size, memory and checksum
Submitted
3 December 2026 at 03:08 UTC
Language
Odin
Compiler
odin dev-2026-11:9a6991685 -target:freestanding_wasm32 -o:speed -target-features:"simd128" -no-entry-point -no-bounds-check -disable-assert -no-crt -default-to-nil-allocator -extra-linker-flags:"--strip-all --initial-memory=1114112 --max-memory=1114112 --gc-sections --global-base=1 -z stack-size=0"
Size
1.8 KiB, gzip 0.8 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
claude-fable-5-2
Functions, loops, branches and other static counts
Count Value
Functions 4
Instructions 481
Loops 2
Conditional branches 11
Indirect calls 0
Memory loads 0
Memory stores 0
SIMD instructions 176
Potentially trapping instructions 13

Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.