Skip to content

Lesson 8 of 9 · 24m

SIMD and Vectorization: Width Without Witchcraft

How SIMD lanes work, what compilers need in order to auto-vectorize, and when intrinsics are justified.

SIMD is the closest thing consumer and server CPUs offer to a cheat code: one instruction performs the same operation on several values. It is also one of the easiest optimizations to misuse. The trick is not knowing instruction names. The trick is shaping data and loops so the machine can apply width safely.

One instruction, many lanes

A 256-bit AVX register can hold eight 32-bit floats. Conceptually, one addition instruction produces x0+y0 through x7+y7 in independent lanes. SIMD is ideal for dense numeric loops, image processing, compression, parsing bitsets, and many analytical scans. It is poor at dependent pointer chasing and highly divergent control flow.

Auto-vectorization starts with clean loops

Compilers can vectorize ordinary C, C++, Rust, and other compiled languages when they can prove safety:

void scale(float *out, const float *in, float factor, size_t n) {
    for (size_t i = 0; i < n; i++) out[i] = in[i] * factor;
}

Potential blockers include aliasing, irregular indexing, early exits, calls with side effects, and branching inside the loop. In C, a signature like void scale(float *restrict out, const float *restrict in, float factor, size_t n) tells the compiler that pointers do not overlap. Compile with optimization and target flags appropriate to deployment:

gcc -O3 -march=x86-64-v3 -fopt-info-vec scale.c

-fopt-info-vec reports vectorized and missed loops. Clang has similar optimization remarks such as -Rpass=vector and -Rpass-missed=vector.

Alignment and layout matter

Structure-of-arrays data naturally maps lanes to adjacent elements: eight contiguous x values can become eight lanes. Array-of-structs often requires shuffling or gather/scatter operations when only x is needed. Gather instructions exist, but they are not a free substitute for locality — they still issue multiple memory transactions.

Intrinsics are a scalpel

Intrinsics expose instructions directly:

__m256 a = _mm256_loadu_ps(in + i);      // immintrin.h
__m256 b = _mm256_loadu_ps(in2 + i);
__m256 r = _mm256_add_ps(a, b);
_mm256_storeu_ps(out + i, r);

Use them when the compiler cannot express the transformation, the hot kernel is small enough to audit, and you can carry the maintenance cost. You now own lane counts, alignment assumptions, feature detection, and edge cases. Write scalar reference tests; SIMD bugs are often numerically subtle.

Width is not portable truth

-march=native is convenient on a machine you control and dangerous for binaries distributed across heterogeneous CPUs. A binary using AVX-512 can crash with an illegal instruction on older hardware. Production deployment needs either a conservative baseline, runtime dispatch, or separate builds.

Field notes

  • Vectorization reduces instruction count, but memory bandwidth can still be the ceiling.
  • Floating-point vectorization may change reduction order and therefore results. Know your numerical tolerance.
  • Masked operations can handle divergent lanes, but they are not magic for deeply branchy logic.
  • Inspect compiler output or optimization reports; assuming vectorization happened is not engineering.
  • A simple scalar loop with excellent locality often beats a clever SIMD kernel fed by terrible memory layout.

Was this lesson helpful?

Sign in to rate