Question bank — Vector - SIMD instructions (SSE, AVX, NEON)
This page attacks the ideas behind SIMD, not the arithmetic. Each item below is a one-line question with a ::: reveal. Cover the answer, commit to a guess, then check. If you get one wrong, the misconception it targets is exactly the one that will bite you in real code.
Before we start, three words we will lean on the whole way — earned in plain language so nothing appears unexplained:
Keep those three pictures in mind; every trap below is one of them in disguise. The parent note, Data Paralelism, Loop Vectorization, and Flynn's Taxonomy are the background you are being tested against.
True or false — justify
SIMD gives a speedup because the CPU runs faster during vector instructions
A 256-bit AVX register always processes 8 elements
If your array has 1000 elements and , SIMD does exactly 125 vector instructions
SIMD is a form of MIMD in Flynn's Taxonomy
Unaligned loads (_mm_loadu_ps) always crash
_mm_load_ps on an unaligned pointer that faults.A dot product's w-lane multiply gives you the answer directly
Doubling register width from 128 to 256 bits always doubles real-world throughput
Saturating add and normal add differ only when values overflow
SIMD requires all lanes to hold the same value
Spot the error
float* p = malloc(16); _mm_load_ps(p); — what's unsafe?
malloc does not guarantee 16-byte alignment, so the aligned _mm_load_ps may fault; use aligned_alloc(16, ...) or the unaligned _mm_loadu_ps.A loop for (i=0; i<n; i+=8) do_avx(a+i); with n=10 — what breaks?
a[8..15], running past the array and also skipping nothing but reading garbage; you need a scalar cleanup loop for the last 2 elements, or n=10 isn't a multiple of 8.ADDPS used on an array of doubles — what's wrong?
ADDPS is packed single (32-bit floats). On 64-bit doubles you must use ADDPD (packed double), or you'll reinterpret each double's bits as two garbage floats.Vectorising if (a[i]>0) b[i]++; else b[i]--; with plain SIMD — why does it stall?
Summing 8 products with one vaddps and reading lane 0 — what's missing?
vaddps adds two vectors lane-wise, it does not collapse a single vector's 8 lanes into one. You still need horizontal adds / extracts to reduce across lanes before lane 0 holds the total.vmovaps [c], xmm0 where c is a 17-byte offset into a buffer — the flaw?
vmovups.Why questions
Why must the number of vector instructions use the ceiling ?
Why does alignment matter if unaligned loads work anyway?
Why is horizontal reduction inherently less efficient than the vertical multiply?
Why does SIMD pair naturally with Loop Vectorization and Auto-vectorization but not with recursion?
Why is memory bandwidth, not lane count, often the true speedup ceiling?
Why does packing data contiguously in memory matter for SIMD?
movaps grabs elements from consecutive addresses; if elements are scattered, you must gather them one by one (or use costly gather instructions), destroying the one-load advantage.Why isn't SIMD the same thing as multicore parallelism?
Edge cases
What speedup does SIMD give when (single element)?
What happens to when exactly (e.g. 8 elements, )?
If (one element over a full vector), what's the hidden cost?
What does a saturating 8-bit add do at the boundary ?
Is a zero-length array () safe to feed to a SIMD loop?
n>0 first; vector instructions is correct, but a naive for(i=0; i<n; i+=w) already skips the body, whereas a fixed unrolled call would read out of bounds.When all lanes hold identical values, does SIMD still help?
vdupq_n/_mm_set1) that fills lanes from one scalar is the right tool, common when adding a constant like brightness to every pixel.Recall One-line self-test
The single sentence that unlocks every trap above ::: SIMD does the same operation on different data, one instruction wide — so anything that breaks sameness (branches), width (element size), contiguity (alignment/gather), or count (remainders) is where it fails.