Vectorization and SIMD

Scalar code touches one number at a time; hot loops touch four. Vec4f and Vec4i are 128-bit SIMD value types: four lanes in one register, with no heap allocation, no ARC traffic, and plain operators.

Constructing vectors

Four lanes from four values, or one value broadcast to all four with splat. Vectors are values: they pass as function arguments and return from functions on every backend:

.rnx
import { Vec4f } from "@std/simd";

fn dbl(v: Vec4f): Vec4f {
    return v * Vec4f.splat(2.0);
}

println!(dbl(new Vec4f(1.0, 2.0, 3.0, 4.0)).get(2));

Vec4i mirrors the shape for 32-bit integer lanes (new Vec4i(1, 2, 3, 4), Vec4i.splat(3)), with wrapping integer arithmetic.

Lane arithmetic

+ - * / combine lane-wise, and / on integer lanes traps on zero divisors like scalar division. Read lanes back with x()/y()/z()/w() or the indexed get(i) (out-of-range lanes trap):

.rnx
import { Vec4f } from "@std/simd";

let a = new Vec4f(1.0, 2.0, 3.0, 4.0);
let b = Vec4f.splat(10.0);
let c = a + b;
println!(c.get(0), c.get(3));

Reductions collapse lanes to scalars: dot sums pairwise products, min/max take per-lane extrema, and sqrt roots each lane (negative lanes yield NaN per IEEE-754):

.rnx
import { Vec4f } from "@std/simd";

let v = new Vec4f(4.0, 9.0, 16.0, 25.0);
println!(v.sqrt().get(3), v.dot(Vec4f.splat(1.0)));

Portability contract

Release artifacts target a fixed baseline ISA, so the same program runs on old and new CPUs alike: SSE2 on x86-64, NEON on aarch64. The baseline already covers every Vec4f/Vec4i operation with a single 128-bit instruction, and the object bytes do not depend on the build machine.

Functions that contain loops additionally ship tiered versions on x86-64: a baseline body plus x86-64 micro-architecture levels v2, v3, and v4. A small dispatch stub checks the CPU once per call through a cached probe (rnx cpu-tier prints this machine's tier, 0-3) and tail-jumps to the best version. Loop bodies contain no dispatch, and the steady-state cost is one cached probe plus predictable branches per function call. Every tier computes identical values: same IR, fp-contract=off, no fast-math divergence. RNX_CPU_TIER=0..3 pins the tier for testing; pins above the machine's tier are never selected implicitly.

Numeric edges are specified, not accidental: min/max return the other lane when exactly one side is NaN (both NaN yields NaN with backend-defined payload bits, per the numerics chapter); integer lanes sign-extend on readout; an out-of-range lane index traps on every backend; an integer lane division by zero traps under the interpreter and JIT and is unspecified in LLVM AOT, so guard divisors first.

Float discipline still applies

Lane values are strict Floats. Mixing a FastFloat lane source without .asFast() is the same E305 error as scalar code, and Int lane readouts convert with Int(...). The process entry point still returns Int. Vectors travel through helpers, and lanes cross the final boundary.

Vector reference

CallSignatureEdge behavior
new Vec4f(a, b, c, d)four Float lanes—
Vec4f.splat(v)one Float broadcast to all four—
new Vec4i(a, b, c, d)four Int lanes—
Vec4i.splat(v)one Int broadcast—
+ - * / (float lanes)lane-wiseIEEE-754 per lane
+ - * / (integer lanes)lane-wise, wrapping/ traps on zero divisors like scalar division
x() / y() / z() / w()lane readout—
get(i)indexed readoutout-of-range lanes trap
dot(other)pairwise products summedscalar Float
min(other) / max(other)per-lane extrema, other lane wins on single-sided NaNvector per overload
sqrt()per-lane rootnegative lanes yield NaN per IEEE-754
.rnx
import { Vec4f } from "@std/simd";

let a = new Vec4f(1.0, 2.0, 3.0, 4.0);
assert(a.x() == 1.0 && a.y() == 2.0, "named lanes");
assert(a.z() == 3.0 && a.w() == 4.0, "named lanes");
assert(a.get(0) == 1.0 && a.get(3) == 4.0, "indexed");
let b = Vec4f.splat(10.0);
let c = a + b;
assert(c.get(0) == 11.0 && c.get(3) == 14.0, "lane add");
let d = a * Vec4f.splat(2.0);
assert(d.get(2) == 6.0, "lane mul");
assert(a.dot(Vec4f.splat(1.0)) == 10.0, "dot");
assert(Int(a.get(0)) == 1, "lane to Int");

Summary

  • Vec4f/Vec4i: construct, splat, lane operators, x/y/z/w/get.
  • Reductions: dot, min, max, sqrt with IEEE-754 edge semantics.
  • First-class function values on all backends; Int at the entry point.