When you write a plain for loop to add two []float32 slices, it feels like the CPU is working at full tilt. Under the hood, the reality is slower. A core grabs one pair of numbers, runs them through a scalar register, and writes the sum back—one step at a time. Meanwhile, wide vector registers sit idle, capable of adding eight floats in a single instruction. In Go 1.27, you can tap into them directly from Go without writing assembly.
How One Wide Register Outruns a Scalar Loop
SIMD (Single Instruction, Multiple Data) runs one operation across a batch of values at once. Picture a supermarket checkout with eight lanes instead of one: the cashier presses a single button and all eight items ring up together. Inside a CPU, that's a 128-, 256-, or 512-bit vector register (AVX2 on amd64, Neon on arm64). It splits into lanes—a 256-bit register holds eight float32 values, four float64 values, or thirty-two int8 values.
The workflow boils down to three steps: Load a slice chunk into a register, Operate across all lanes, and Store the result back in memory. The catch is that every lane executes the same instruction, so if branches have to be replaced with bitmasks.
From Assembly to simd and simd/archsimd
Go's compiler barely auto-vectorizes loops, so using SIMD used to mean writing Go Assembly for each CPU architecture. In Go 1.26, GOEXPERIMENT=simd introduced the low-level simd/archsimd package (Float32x8 on amd64). Go 1.27 added 128-bit Neon for arm64 and SIMD for wasm, plus a portable simd package on top (simd.Float32s, simd.Int8s). The compiler picks the right width for the hardware and falls back to scalar math when vector instructions aren't available.
In a deep dive on Go SIMD, Jesús Espino compared a regular loop against the portable package when adding two []float32 slices:
//go:build goexperiment.simd
import "simd"
// Scalar loop: one pair per step
func addScalar(a, b, out []float32) {
for i := 0; i < len(a); i++ {
out[i] = a[i] + b[i]
}
}
// Portable SIMD (Go 1.27+): width adapts to the CPU
func addPortable(a, b, out []float32) {
var v simd.Float32s
n := v.Len() // 4, 8, or 16 elements
i := 0
for ; i+n <= len(a); i += n {
x := simd.LoadFloat32s(a[i:])
y := simd.LoadFloat32s(b[i:])
x.Add(y).Store(out[i:])
}
// Handle remaining tail elements with a scalar loop
for ; i < len(a); i++ {
out[i] = a[i] + b[i]
}
}
There are no ugly C intrinsics here: Add is a compiler intrinsic that lowers directly to a machine instruction. When you need maximum throughput on a specific CPU, drop down to simd/archsimd. In the same post, Espino computed a dot product on Go 1.27.0 using Float32x8 and MulAdd (FMA—fused multiply-add) and measured roughly a 9x speedup:
//go:build goexperiment.simd
import "simd/archsimd"
func dotSIMD(a, b []float32) float32 {
// Without this check, older CPUs crash with SIGILL
if !archsimd.X86.AVX2() || !archsimd.X86.FMA() {
return dotScalar(a, b)
}
var acc archsimd.Float32x8
i := 0
for ; i+8 <= len(a); i += 8 {
x := archsimd.LoadFloat32x8(a[i:])
y := archsimd.LoadFloat32x8(b[i:])
// acc = x*y + acc across all eight lanes
acc = x.MulAdd(y, acc)
}
// Tail < 8 elements: unused lanes are zero-padded
x, _ := archsimd.LoadFloat32x8Part(a[i:])
y, _ := archsimd.LoadFloat32x8Part(b[i:])
acc = x.MulAdd(y, acc)
// Horizontally reduce 8 sums into one scalar
s := acc.GetLo().Add(acc.GetHi())
s = s.ConcatAddPairs(s)
s = s.ConcatAddPairs(s)
return s.GetElem(0)
}
Run it with GOEXPERIMENT=simd go run .. The tail loads via LoadFloat32x8Part, which pads empty lanes with zeros so they don't skew the sum. Checking archsimd.X86.AVX2() and archsimd.X86.FMA() with a scalar fallback is mandatory—otherwise your binary dies with SIGILL on CPUs lacking those instructions.
When SIMD Actually Pays Off—and When It Gets in the Way
A 9x speedup is tempting, yet rewriting random loops makes no sense. SIMD shines when thousands of uniform numbers run through simple branch-free math: image and audio processing, compression, cryptography, byte scanning, vector search, and ML data prep.
On tiny slices, branch-heavy if/else logic, or memory-bandwidth-bound workloads, overhead easily wipes out the gain. Remember too that APIs under GOEXPERIMENT=simd are experimental and exempt from Go 1's compatibility promise, so method names can still change. For typical backend services, start with PGO and keep vector code behind //go:build goexperiment.simd only in hot loops flagged by your profiler.


