Validate UTF-8 · WAT 🤝 Claude Sonnet 5.5 (Anthropic)
SHA-256
2edb342603730df6175357c5eea2b105217a04d50f184bbad794b6e75dbc5ea4
View source (12 KiB of WAT)
;; validate_utf8 in hand-written WebAssembly text, SIMD (Keiser-Lemire lookup validation), 17 pages of memory, input near the start.
;; Written with Claude Sonnet 5.5 (Anthropic); generated by a small script (flags pad,pt,sb,rx,or1,burst,n8) and tuned by local timing.
;; Builds on patrickgwsmith's entries (8b1c547b WAT; the Odin and Zig ones for the tiers): Keiser and Lemire, "Validating UTF-8 In Less Than One Instruction Per Byte".
;; - Input sits at 256, with zero bytes written after it so the last block is whole and a cut-short character meets a zero.
;; - Blocks of 128 bytes. All-ASCII blocks only check the 16 bytes before them for an unfinished character.
;; - Blocks with no byte above DF and nothing owed from before are checked with one range test per byte (2-byte characters only).
;; - Anything else uses the three-table lookup with i8x16.relaxed_swizzle (indexes are always 0..15, so the result is defined).
;; - Errors are collected and tested every 512 bytes; one vector test per block picks the path.
;; Assembled with binaryen wasm-as version_132 --all-features.
(module
(memory (export "memory") 17 17)
(data (i32.const 0) "truefalse")
(data (i32.const 64) "\02\02\02\02\02\02\02\02\80\80\80\80\21\01\15\49\e7\a3\83\83\8b\cb\cb\cb\cb\cb\cb\cb\cb\db\cb\cb\01\01\01\01\01\01\01\01\e6\ae\ba\ba\01\01\01\01\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ef\df\bf\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ff\ef\df\df")
(func (export "input_ptr") (result i32) i32.const 256)
(func (export "input_utf8_cap") (export "output_utf8_cap") (result i32) i32.const 1048576)
(func (export "render") (param $n i32) (result i64)
(local $q i32) (local $end i32) (local $t i32)
(local $t1 v128) (local $t2 v128) (local $t3 v128) (local $inc v128) (local $owed v128) (local $mc0 v128) (local $ma0 v128) (local $m82 v128) (local $mx v128) (local $mn v128) (local $acc v128) (local $pv v128) (local $s0 v128) (local $s1 v128) (local $s2 v128) (local $s3 v128) (local $s4 v128) (local $s5 v128) (local $s6 v128) (local $s7 v128) (local $m0f v128) (local $d2 v128) (local $d3 v128) (local $e2 v128) (local $e3 v128) (local $pt v128) (local $hi v128) (local $lo v128) (local $t12 v128) (local $m80 v128)
(local $c0 v128) (local $c1 v128) (local $c2 v128) (local $c3 v128) (local $c4 v128) (local $c5 v128) (local $c6 v128) (local $c7 v128)
(local.set $t1 (v128.load (i32.const 64)))
(local.set $t2 (v128.load (i32.const 80)))
(local.set $t3 (v128.load (i32.const 96)))
(local.set $inc (v128.load (i32.const 112)))
(local.set $owed (v128.load (i32.const 128)))
(local.set $mc0 (i8x16.splat (i32.const 192)))
(local.set $ma0 (i8x16.splat (i32.const 160)))
(local.set $m82 (i8x16.splat (i32.const 130)))
(local.set $m0f (i8x16.splat (i32.const 15)))
(local.set $d2 (i8x16.splat (i32.const 223)))
(local.set $d3 (i8x16.splat (i32.const 239)))
(local.set $m80 (i8x16.splat (i32.const 128)))
(local.set $e2 (i8x16.splat (i32.const 96)))
(local.set $e3 (i8x16.splat (i32.const 112)))
(v128.store offset=256 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=272 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=288 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=304 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=320 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=336 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=352 (local.get $n) (v128.const i32x4 0 0 0 0))
(v128.store offset=368 (local.get $n) (v128.const i32x4 0 0 0 0))
(local.set $q (i32.const 240))
(local.set $end (i32.add (i32.const 240) (i32.and (i32.add (local.get $n) (i32.const 128)) (i32.const -128))))
block $false
block $tail
loop $blk
(br_if $tail (i32.eq (local.get $q) (local.get $end)))
(local.set $c0 (v128.load offset=16 (local.get $q)))
(local.set $c1 (v128.load offset=32 (local.get $q)))
(local.set $c2 (v128.load offset=48 (local.get $q)))
(local.set $c3 (v128.load offset=64 (local.get $q)))
(local.set $c4 (v128.load offset=80 (local.get $q)))
(local.set $c5 (v128.load offset=96 (local.get $q)))
(local.set $c6 (v128.load offset=112 (local.get $q)))
(local.set $c7 (v128.load offset=128 (local.get $q)))
(local.set $mx (i8x16.max_u (i8x16.max_u (i8x16.max_u (local.get $c0) (local.get $c1)) (i8x16.max_u (local.get $c2) (local.get $c3))) (i8x16.max_u (i8x16.max_u (local.get $c4) (local.get $c5)) (i8x16.max_u (local.get $c6) (local.get $c7)))))
(if (i32.eqz (i8x16.bitmask (local.get $mx)))
(then
(local.set $acc (v128.or (local.get $acc) (i8x16.sub_sat_u (v128.load (local.get $q)) (local.get $inc))))
(local.set $q (i32.add (local.get $q) (i32.const 128)))
br $blk))
(if (v128.any_true (v128.or (i8x16.sub_sat_u (local.get $mx) (local.get $d2)) (i8x16.sub_sat_u (v128.load (local.get $q)) (local.get $owed))))
(then (local.set $acc (v128.or (local.get $acc) (v128.or (v128.or (v128.or (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=15 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=15 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c0) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=14 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=13 (local.get $q)) (local.get $e3))))) (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=31 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=31 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c1) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=30 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=29 (local.get $q)) (local.get $e3)))))) (v128.or (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=47 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=47 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c2) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=46 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=45 (local.get $q)) (local.get $e3))))) (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=63 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=63 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c3) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=62 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=61 (local.get $q)) (local.get $e3))))))) (v128.or (v128.or (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=79 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=79 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c4) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=78 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=77 (local.get $q)) (local.get $e3))))) (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=95 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=95 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c5) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=94 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=93 (local.get $q)) (local.get $e3)))))) (v128.or (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=111 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=111 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c6) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=110 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=109 (local.get $q)) (local.get $e3))))) (v128.xor (v128.and (v128.and (i8x16.relaxed_swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=127 (local.get $q)) (i32.const 4))) (i8x16.relaxed_swizzle (local.get $t2) (v128.and (v128.load offset=127 (local.get $q)) (local.get $m0f)))) (i8x16.relaxed_swizzle (local.get $t3) (i8x16.shr_u (local.get $c7) (i32.const 4)))) (v128.and (local.get $m80) (v128.or (i8x16.sub_sat_u (v128.load offset=126 (local.get $q)) (local.get $e2)) (i8x16.sub_sat_u (v128.load offset=125 (local.get $q)) (local.get $e3)))))))))))
(else
(local.set $s0 (i8x16.add (local.get $c0) (local.get $mc0)))
(local.set $s1 (i8x16.add (local.get $c1) (local.get $mc0)))
(local.set $s2 (i8x16.add (local.get $c2) (local.get $mc0)))
(local.set $s3 (i8x16.add (local.get $c3) (local.get $mc0)))
(local.set $s4 (i8x16.add (local.get $c4) (local.get $mc0)))
(local.set $s5 (i8x16.add (local.get $c5) (local.get $mc0)))
(local.set $s6 (i8x16.add (local.get $c6) (local.get $mc0)))
(local.set $s7 (i8x16.add (local.get $c7) (local.get $mc0)))
(local.set $mn (i8x16.min_s (i8x16.min_s (i8x16.min_s (local.get $s0) (local.get $s1)) (i8x16.min_s (local.get $s2) (local.get $s3))) (i8x16.min_s (i8x16.min_s (local.get $s4) (local.get $s5)) (i8x16.min_s (local.get $s6) (local.get $s7)))))
(local.set $acc (v128.or (local.get $acc) (v128.or (v128.or (v128.or (v128.or (i8x16.lt_s (local.get $mn) (local.get $m82)) (v128.xor (i8x16.lt_s (i8x16.add (v128.load offset=15 (local.get $q)) (local.get $mc0)) (local.get $ma0)) (i8x16.lt_s (local.get $c0) (local.get $mc0)))) (v128.or (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s0) (local.get $s1)) (local.get $ma0)) (i8x16.lt_s (local.get $c1) (local.get $mc0))) (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s1) (local.get $s2)) (local.get $ma0)) (i8x16.lt_s (local.get $c2) (local.get $mc0))))) (v128.or (v128.or (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s2) (local.get $s3)) (local.get $ma0)) (i8x16.lt_s (local.get $c3) (local.get $mc0))) (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s3) (local.get $s4)) (local.get $ma0)) (i8x16.lt_s (local.get $c4) (local.get $mc0)))) (v128.or (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s4) (local.get $s5)) (local.get $ma0)) (i8x16.lt_s (local.get $c5) (local.get $mc0))) (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s5) (local.get $s6)) (local.get $ma0)) (i8x16.lt_s (local.get $c6) (local.get $mc0)))))) (v128.xor (i8x16.lt_s (i8x16.shuffle 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (local.get $s6) (local.get $s7)) (local.get $ma0)) (i8x16.lt_s (local.get $c7) (local.get $mc0))))))))
(local.set $q (i32.add (local.get $q) (i32.const 128)))
(if (i32.eqz (i32.and (i32.add (local.get $q) (i32.const 16)) (i32.const 511))) (then (br_if $false (v128.any_true (local.get $acc)))))
br $blk
end
end
(br_if $false (v128.any_true (local.get $acc)))
(return (i64.const 4))
end
(i64.const 0x400000005)))
Download source MIT licensed — build on it
Hand-written SIMD WAT, 17 pages of memory with the input near the start, to see how memory layout affects the official timing (compare 6ec12f3d). Keiser-Lemire lookup validation in 128-byte blocks with ASCII and 2-byte tiers, i8x16.relaxed_swizzle (indexes always 0..15), errors collected and tested every 512 bytes.
- Builds on
- Builds on patrickgwsmith 8b1c547b (WAT), and the tiering ideas seen in patrickgwsmith Odin 0d424f67 and the kksat Zig entries. Method: Keiser and Lemire, Validating UTF-8 In Less Than One Instruction Per Byte.
Build details
Compiler, size, memory and checksum
- Submitted
- 2 December 2026 at 20:26 UTC
- Language
- WAT
- Compiler
-
binaryen wasm-as version_132 (hand-written WAT, generated by a script)--all-features - Size
- 1.7 KiB, gzip 0.7 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 Sonnet 5.5 (Anthropic)
Functions, loops, branches and other static counts
| Count | Value |
|---|---|
| Functions | 3 |
| Instructions | 576 |
| Loops | 1 |
| Conditional branches | 6 |
| Indirect calls | 0 |
| Memory loads | 0 |
| Memory stores | 0 |
| SIMD instructions | 258 |
| Potentially trapping instructions | 57 |
Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.