TExcelTAD
Home › WebAssembly › SIMD (v128)

SIMD (v128)

SIMD ("single instruction, multiple data") lets one instruction work on several numbers at once. WebAssembly's fixed-width SIMD (standard since WebAssembly 2.0) adds a 128-bit value type, v128, which can be viewed as different lane shapes:

Shape Lanes Lane type
i8x16 16 8-bit integers
i16x8 8 16-bit integers
i32x4 4 32-bit integers
i64x2 2 64-bit integers
f32x4 4 f32
f64x2 2 f64

The v128 itself is just 128 bits; the instruction's prefix (i32x4.add, f32x4.mul …) decides the view, just as _s/_u decides how to read an i32. Engines compile these to the CPU's SSE/AVX (x86) or NEON (ARM) instructions.

Constants, lanes and splat

wat · try it yourself
(module
  (import "console" "log" (func $log4 (param i32 i32 i32 i32)))
  (func $show (param $v v128)                      ;; print the four i32 lanes
    (call $log4
      (i32x4.extract_lane 0 (local.get $v))
      (i32x4.extract_lane 1 (local.get $v))
      (i32x4.extract_lane 2 (local.get $v))
      (i32x4.extract_lane 3 (local.get $v))))
  (func (export "main")
    (call $show (v128.const i32x4 10 20 30 40))
    (call $show (i32x4.splat (i32.const 7)))                                ;; same value in every lane
    (call $show (i32x4.add (v128.const i32x4 1 2 3 4) (v128.const i32x4 100 200 300 400)))
    (call $show (i32x4.replace_lane 2 (v128.const i32x4 1 2 3 4) (i32.const -1)))))
10 20 30 40
7 7 7 7
101 202 303 404
1 2 -1 4

The lane index in extract_lane/replace_lane is an immediate constant, not a stack value.

Note: v128 values can't cross the JavaScript boundary: an exported function with a v128 parameter or result throws a TypeError when called from JS. Extract lanes (or store to memory) before returning.

Lane-wise arithmetic

Most operations apply independently to every lane:

wat · try it yourself
(module
  (import "console" "log" (func $log4 (param f64 f64 f64 f64)))
  (func $show (param $v v128)
    (call $log4
      (f64.promote_f32 (f32x4.extract_lane 0 (local.get $v)))
      (f64.promote_f32 (f32x4.extract_lane 1 (local.get $v)))
      (f64.promote_f32 (f32x4.extract_lane 2 (local.get $v)))
      (f64.promote_f32 (f32x4.extract_lane 3 (local.get $v)))))
  (func (export "main")
    (local $prices v128)
    (local.set $prices (v128.const f32x4 10 25 40 8))
    ;; 25% discount on all four prices with one multiply
    (call $show (f32x4.mul (local.get $prices) (f32x4.splat (f32.const 0.75))))
    (call $show (f32x4.sqrt (v128.const f32x4 1 4 9 16)))
    (call $show (f32x4.max (local.get $prices) (f32x4.splat (f32.const 20))))))
7.5 18.75 30 6
1 2 3 4
20 25 40 20

One f32x4.mul discounted all four prices at once. f32x4.max likewise compares lane by lane.

Saturating arithmetic for pixels

Image code works with bytes (0–255). Normal addition would wrap (250 + 10 = 4). Saturating instructions clamp instead, exactly what you want when brightening an image:

wat · try it yourself
(module
  (import "console" "log" (func $log4 (param i32 i32 i32 i32)))
  (func (export "main")
    (local $pixels v128) (local $wrapped v128) (local $clamped v128)
    (local.set $pixels (v128.const i8x16 250 100 0 128  1 2 3 4  5 6 7 8  9 10 11 12))
    (local.set $wrapped (i8x16.add (local.get $pixels) (i8x16.splat (i32.const 10))))
    (local.set $clamped (i8x16.add_sat_u (local.get $pixels) (i8x16.splat (i32.const 10))))
    (call $log4 (i8x16.extract_lane_u 0 (local.get $wrapped)) (i8x16.extract_lane_u 1 (local.get $wrapped))
                (i8x16.extract_lane_u 2 (local.get $wrapped)) (i8x16.extract_lane_u 3 (local.get $wrapped)))
    (call $log4 (i8x16.extract_lane_u 0 (local.get $clamped)) (i8x16.extract_lane_u 1 (local.get $clamped))
                (i8x16.extract_lane_u 2 (local.get $clamped)) (i8x16.extract_lane_u 3 (local.get $clamped)))))
4 110 10 138
255 110 10 138

SIMD with memory: summing an array four at a time

v128.load and v128.store move 16 bytes at once. To sum an i32 array, add four lanes per step and combine the lanes at the end:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (memory 1)

  (func $sum_simd (param $ptr i32) (param $count i32) (result i32)   ;; count: multiple of 4
    (local $acc v128) (local $end i32)
    (local.set $end (i32.add (local.get $ptr) (i32.shl (local.get $count) (i32.const 2))))
    (block $done
      (loop $next
        (br_if $done (i32.ge_u (local.get $ptr) (local.get $end)))
        (local.set $acc (i32x4.add (local.get $acc) (v128.load (local.get $ptr))))
        (local.set $ptr (i32.add (local.get $ptr) (i32.const 16)))
        (br $next)))
    ;; horizontal sum of the four lanes
    (i32.add
      (i32.add (i32x4.extract_lane 0 (local.get $acc)) (i32x4.extract_lane 1 (local.get $acc)))
      (i32.add (i32x4.extract_lane 2 (local.get $acc)) (i32x4.extract_lane 3 (local.get $acc)))))

  (func (export "main")
    (local $i i32)
    ;; fill 0..99 with 1..100
    (loop $fill
      (i32.store (i32.shl (local.get $i) (i32.const 2)) (i32.add (local.get $i) (i32.const 1)))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $fill (i32.lt_u (local.get $i) (i32.const 100))))
    (call $log (call $sum_simd (i32.const 0) (i32.const 100)))))
5050

25 vector additions instead of 100 scalar ones. Real code also handles a "tail" of leftover elements when the count isn't a multiple of the lane count.

Comparisons, masks and bitselect

Lane-wise comparisons produce masks: all bits set (-1) where true, zero where false. v128.bitselect uses a mask to pick bits from two vectors, a branch-free per-lane if:

wat · try it yourself
(module
  (import "console" "log" (func $log4 (param i32 i32 i32 i32)))
  (func $show (param $v v128)
    (call $log4 (i32x4.extract_lane 0 (local.get $v)) (i32x4.extract_lane 1 (local.get $v))
                (i32x4.extract_lane 2 (local.get $v)) (i32x4.extract_lane 3 (local.get $v))))
  (func (export "main")
    (local $x v128) (local $mask v128)
    (local.set $x (v128.const i32x4 -5 3 -1 8))
    (local.set $mask (i32x4.lt_s (local.get $x) (v128.const i32x4 0 0 0 0)))
    (call $show (local.get $mask))
    ;; replace negative lanes with 0: bitselect(if_true, if_false, mask)
    (call $show (v128.bitselect (v128.const i32x4 0 0 0 0) (local.get $x) (local.get $mask)))
    (call $show (i32x4.abs (local.get $x)))))
-1 0 -1 0
0 3 0 8
5 3 1 8

Using SIMD from compilers

  • C/C++ (clang/Emscripten): compile with -msimd128; use <wasm_simd128.h> intrinsics such as wasm_i32x4_add, or let the auto-vectoriser do it.
  • Rust: #[target_feature(enable = "simd128")] and core::arch::wasm32 intrinsics, or RUSTFLAGS="-C target-feature=+simd128".

The newer relaxed SIMD feature adds faster operations (such as fused multiply-add) whose exact results may differ slightly between CPUs.

Test yourself

How many f32 lanes does a v128 have?

  1. 2
  2. 4
  3. 8
  4. 16

What does i8x16.add_sat_u do with 250 + 10 in a lane?

  1. Wraps to 4
  2. Clamps to 255
  3. Traps
  4. Produces 260 in a wider lane

Can an exported function return a v128 to JavaScript?

  1. Yes, as a BigInt
  2. Yes, as a Uint8Array
  3. No, calling it from JS throws a TypeError
  4. Only in Node.js