Validate UTF-8 · WAT 🤝 Claude Sonnet 5.5 (Anthropic)

SHA-256 cce5e6ebf2f72976dda01f741fcfc3a9b254186e5a4d0f9a9a2df14814e83739

View source (7.7 KiB of WAT)
;; validate_utf8 in hand-written WebAssembly text, SIMD (Keiser-Lemire lookup validation), no relaxed SIMD.
;; Written with Claude Sonnet 5.5 (Anthropic); generated by a small script (flags pad,pt,sb) 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 1 MiB, with zero bytes written after it so the last block is whole and a cut-short character meets a zero.
;;  - Blocks of 64 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 (i8x16.swizzle).
;;  - sub_sat_u replaces gt_u for the "must continue twice" test (one instruction instead of three in V8).
;; Assembled with binaryen wasm-as version_132 --all-features.
(module
  (memory (export "memory") 32 32)
  (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") (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=1048576 (local.get $n) (v128.const i32x4 0 0 0 0))
    (v128.store offset=1048592 (local.get $n) (v128.const i32x4 0 0 0 0))
    (v128.store offset=1048608 (local.get $n) (v128.const i32x4 0 0 0 0))
    (v128.store offset=1048624 (local.get $n) (v128.const i32x4 0 0 0 0))
    (local.set $q (i32.const 1048560))
    (local.set $end (i32.add (i32.const 1048560) (i32.and (i32.add (local.get $n) (i32.const 64)) (i32.const -64))))
    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 $mx (i8x16.max_u (i8x16.max_u (local.get $c0) (local.get $c1)) (i8x16.max_u (local.get $c2) (local.get $c3))))
          (if (i32.eqz (i8x16.bitmask (local.get $mx)))
            (then
              (br_if $false (v128.any_true (i8x16.sub_sat_u (v128.load (local.get $q)) (local.get $inc))))
              
              (local.set $q (i32.add (local.get $q) (i32.const 64)))
              br $blk))

          (if (i32.or (v128.any_true (i8x16.sub_sat_u (local.get $mx) (local.get $d2))) (v128.any_true (i8x16.sub_sat_u (v128.load (local.get $q)) (local.get $owed))))
            (then (br_if $false (v128.any_true (v128.or (v128.or (v128.xor (v128.and (v128.and (i8x16.swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=15 (local.get $q)) (i32.const 4))) (i8x16.swizzle (local.get $t2) (v128.and (v128.load offset=15 (local.get $q)) (local.get $m0f)))) (i8x16.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.swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=31 (local.get $q)) (i32.const 4))) (i8x16.swizzle (local.get $t2) (v128.and (v128.load offset=31 (local.get $q)) (local.get $m0f)))) (i8x16.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.swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=47 (local.get $q)) (i32.const 4))) (i8x16.swizzle (local.get $t2) (v128.and (v128.load offset=47 (local.get $q)) (local.get $m0f)))) (i8x16.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.swizzle (local.get $t1) (i8x16.shr_u (v128.load offset=63 (local.get $q)) (i32.const 4))) (i8x16.swizzle (local.get $t2) (v128.and (v128.load offset=63 (local.get $q)) (local.get $m0f)))) (i8x16.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))))))))))
            (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 $mn (i8x16.min_s (i8x16.min_s (local.get $s0) (local.get $s1)) (i8x16.min_s (local.get $s2) (local.get $s3))))
                  (br_if $false (v128.any_true (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.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))))))))
          
          (local.set $q (i32.add (local.get $q) (i32.const 64)))
          br $blk
        end
      end
      (return (i64.const 4))
    end
    (i64.const 0x400000005)))

Hand-written SIMD WAT without relaxed SIMD, about 1 KB. Keiser-Lemire lookup validation in 64-byte blocks with three tiers: all-ASCII blocks, blocks of 2-byte characters only (range tests, no tables), and the three-table lookup for the rest. Input is padded with zeros in place; sub_sat_u replaces gt_u for the continuation test. Companion to 6ec12f3d, which uses relaxed swizzle.

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 19:55 UTC
Language
WAT
Compiler
binaryen wasm-as version_132 (hand-written WAT, generated by a script) --all-features
Size
1.1 KiB, gzip 0.6 KiB
Download
~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
Memory
32 pages (2 MiB)
SIMD
yes
AI models
Claude Sonnet 5.5 (Anthropic)
Functions, loops, branches and other static counts
Count Value
Functions 2
Instructions 334
Loops 1
Conditional branches 6
Indirect calls 0
Memory loads 0
Memory stores 0
SIMD instructions 140
Potentially trapping instructions 33

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