SIMD
22.1 Fixed-Width 128-bit SIMD
The v128 type with lanes interpreted as i8x16, i16x8, i32x4, i64x2, f32x4, or f64x2. It's supported in all major browsers and runtimes.
v128.const, i32x4.splat, i8x16.shuffle, i8x16.swizzle
f32x4.extract_lane, i32x4.replace_lane i32x4.add, f32x4.mul, f32x4.sqrt, i16x8.add_sat_s, i8x16.avgr_u, i32x4.dot_i16x8_s
f32x4.lt, v128.and, v128.bitselect, v128.any_true, i32x4.all_true, i8x16.bitmask
f32x4.convert_i32x4_s, i32x4.trunc_sat_f32x4_s, i16x8.narrow_i32x4_s, i32x4.extend_low_i16x8_s
v128.load, v128.store, v128.load32_splat, v128.load8x8_s, v128.load32_zero, lane loads/stores
(module
(memory (export "memory") 1)
;; Add two f32 arrays of length n (n multiple of 4) → out
(func (export "add_f32x4") (param $a i32) (param $b i32) (param $out i32) (param $n i32)
(local $i i32)
(block $done
(loop $loop
(br_if $done (i32.ge_u (local.get $i) (local.get $n)))
(v128.store
(i32.add (local.get $out) (i32.shl (local.get $i) (i32.const 2)))
(f32x4.add
(v128.load (i32.add (local.get $a) (i32.shl (local.get $i) (i32.const 2))))
(v128.load (i32.add (local.get $b) (i32.shl (local.get $i) (i32.const 2))))))
(local.set $i (i32.add (local.get $i) (i32.const 4)))
(br $loop)))))22.2 SIMD from Languages
Autovectorization: LLVM vectorizes loops with -O3 -msimd128. Emscripten also translates many SSE/NEON intrinsics.
// C with wasm_simd128.h (clang/emcc -msimd128)
#include <wasm_simd128.h>
void add4(float* out, const float* a, const float* b, int n) {
for (int i = 0; i < n; i += 4) {
v128_t va = wasm_v128_load(a + i), vb = wasm_v128_load(b + i);
wasm_v128_store(out + i, wasm_f32x4_add(va, vb));
}
}// Rust: RUSTFLAGS="-C target-feature=+simd128"
use std::arch::wasm32::*;
pub unsafe fn add4(a: *const f32, b: *const f32, out: *mut f32) {
v128_store(out as *mut v128, f32x4_add(v128_load(a as *const v128), v128_load(b as *const v128)));
}22.3 Relaxed SIMD (Wasm 3.0)
Instructions like f32x4.relaxed_madd (fused multiply-add), i8x16.relaxed_swizzle, and relaxed_trunc allow hardware-specific results for more speed (ML inference, graphics). Results may differ across platforms (non-deterministic by design, with a deterministic profile available).
22.4 Feature Detection
Ship multiple builds or use WebAssembly.validate on tiny probe modules.
import { simd, threads, relaxedSimd, gc, exceptions } from "wasm-feature-detect";
const url = (await simd()) ? "app.simd.wasm" : "app.wasm";