// 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. 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. OPEN :: V{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0xf0 - 1, 0xe0 - 1, 0xc0 - 1} @(require_results) load :: #force_inline proc "contextless" (p: [^]u8) -> V { return intrinsics.unaligned_load((^V)(p)) } @(require_results) non_ascii :: #force_inline proc "contextless" (v: V) -> bool { return simd.extract_msbs(v) != {} } @(require_results) any :: #force_inline proc "contextless" (mask: V) -> bool { return simd.extract_msbs(mask) != {} } // Moves C0..DF to the bottom of the signed range: C0 -> -128, C1 -> -127, DF -> -97. @(require_results) 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. @(require_results) 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. @(require_results) 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 } @(require_results) valid :: #force_inline proc "contextless" (n: u32) -> bool { input := ([^]u8)(uintptr(INPUT)) zero: V // pad to a whole block intrinsics.unaligned_store((^V)(input[n:]), zero) intrinsics.unaligned_store((^V)(input[n + 16:]), zero) intrinsics.unaligned_store((^V)(input[n + 32:]), zero) intrinsics.unaligned_store((^V)(input[n + 48:]), zero) err: V prev: V big_prev := false // the previous block had a 3- or 4-byte lead end := (n + 63) & ~u32(63) i: u32 outer: 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 !non_ascii(mx) { // ASCII block: only a sequence left open by the previous block can be wrong err |= simd.saturating_sub(prev, OPEN) prev = zero big_prev = false // then run through ASCII with nothing else to carry for { i += 64 if i >= end do break outer 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 non_ascii(mx) do break } } big := any(simd.lanes_ge(mx, V(0xe0))) if big || big_prev { err |= check(prev, a) | check(a, b) | check(b, c) | check(c, d) } 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) err |= two(sp, sa, a) | two(sa, sb, b) | two(sb, sc, c) | two(sc, sd, d) | simd.lanes_lt(simd.min(simd.min(sp, sa), simd.min(sb, simd.min(sc, sd))), simd.i8x16(-126)) } big_prev = big prev = d i += 64 } // the input may end exactly on a block edge, with no padding to catch an open sequence err |= simd.saturating_sub(prev, OPEN) return transmute(u128)err == 0 } @(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 }