C8 · Lesson 42 · SIMD and vector instructions
One instruction, many data: packed lanes, vector lengths, and the limits Amdahl and memory set
Lesson 41 found parallelism by running several instructions at once. Much of the work a computer does, filtering audio, blending pixels, multiplying matrices, applies the same operation to many numbers in a row, and there's a cheaper way to speed that up: make one instruction operate on many numbers. A 256-bit register holds eight 32-bit floats, and one instruction adds eight pairs. That's SIMD, single instruction, multiple data, and it's how phones decode video and laptops run neural networks. The same idea works inside an ordinary 32-bit register with clever masking, which this lesson does on the course's own processor. Two limits stop it from scaling forever: the part of a program that isn't vectorizable, and memory bandwidth.
- Four bytes packed in each register are added lane by lane on the plain RV32I processor, with masks that stop carries from crossing between bytes: .
- 0x7F + 0x01 gives 0x80 in the top lane, while 0x01 + 0xFF and 0xFF + 0x01 wrap to 0x00 without disturbing their neighbors.
Predict
80% of a program's time is in a loop that vectorizes perfectly onto 8 lanes. The rest is serial. What's the overall speedup?
= 3.33. The vector part shrinks from 0.8 to 0.1 of the original time, but the serial 0.2 stays, and it now dominates. Even infinitely wide vectors couldn't beat = 5. This is Amdahl's law, and it's why vectorizing one hot loop gives a good but bounded win.
Name it
Packed SIMD
Packed SIMD extensions (x86's SSE and AVX, ARM's NEON) add wide registers of 128 to 512 bits and instructions that treat them as arrays of lanes: sixteen 8-bit, eight 16-bit, four 32-bit and so on. vaddps adds eight float pairs at once. Lanes don't carry into each other, saturating versions clamp instead of wrapping (useful for pixels and audio), and shuffles rearrange lanes. The width is fixed by the instruction set, so code is rewritten for each new width.
Vector instruction sets
RISC-V's V extension instead lets the program ask for a vector length. vsetvli takes the number of elements left and returns how many the hardware will process this time, up to its own maximum. A loop then strip-mines: process as many as the hardware allows, advance, repeat, with no separate code for the last partial group. The same binary runs on cores with short or long vectors.
SIMD within a register
On a plain integer processor, packed bytes can still be processed in parallel with masks:
- Lane-wise add: add the low 7 bits of each byte (no carry can escape), then fix the top bits with XOR: , with = 0x7F7F7F7F and = 0x80808080.
- Find a zero byte: is nonzero exactly when some byte of is zero. String functions such as
strlencheck four or eight bytes per step this way.
The limits
- Amdahl's law: with a fraction of the work vectorized onto lanes, the speedup is , never more than .
- Memory bandwidth: adding two float arrays moves 12 bytes per element. At 20 GB/s that's at most 1.7 billion elements per second, however many lanes there are. Wide SIMD pays off fully only when data is reused from cache or registers.
- Alignment and tails: wide loads are fastest from aligned addresses, and leftover elements need masking or a short scalar loop.
Worked example
Add the packed bytes of 0x7F01FF10 and 0x01FF0110 lane by lane with the masking trick, and check it byte by byte.
Show the solution
- Byte by byte, modulo 256. so the expected result is 0x80000020.
- Add the low 7 bits of each byte.
- Find the top bits.
- Fix the top bits with XOR. matching.
- Count the instructions. Eight instructions (plus two to build the constants, once per loop) do four additions.
Answer both ways give 0x80000020.
Worked example
A vector loop processes 1003 elements on hardware whose vectors hold 8 elements. How many iterations, and how many elements does the last one handle? What would a fixed-width SIMD loop need?
Show the solution
- The vector loop. With
vsetvli, the loop runs iterations: 125 full ones and a last one of 3 elements, using the same code. - A fixed-width SIMD loop. A fixed 8-lane SIMD loop does 125 full iterations and then needs a separate scalar tail loop, or masked instructions, for the last 3.
Answer 126 iterations, the last one handling 3 elements; a fixed-width SIMD loop needs a separate tail loop or masked instructions for those 3.
Worked example
A filter does 2 floating-point operations per 4-byte input sample it reads, and the memory system delivers 25 GB/s. A core can do 64 billion floating-point operations per second with full-width SIMD. Which limit binds?
Show the solution
- Samples from memory.
- The operations they feed. That's enough for 12.5 billion operations per second, and the core could do 64 billion.
- Which binds. The filter is memory-bound at about a fifth of the SIMD peak.
- What would help. Widening the vectors would change nothing, while reusing each sample more (a longer filter that keeps data in registers) would.
Answer memory bandwidth binds: it feeds 12.5 billion operations per second, about a fifth of the core's 64 billion.
Check
A fraction 0.8 of a program's time vectorizes perfectly onto 4 lanes. What's the overall speedup?
Check
A strip-mined vector loop processes 101 elements, at most 8 per iteration. How many iterations does it run?
Check
A loop moves 8 bytes of memory per element and does 8 floating-point operations per element. With 25 GB/s of memory bandwidth, what's the most it can compute, in billions of operations per second?
Check
a0 and a1 each hold four packed bytes. Put their lane-by-lane sums (each modulo 256) in a2, without letting carries cross between bytes.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A programmer widens a loop from 4-lane to 16-lane SIMD and sees no speedup. The loop adds two large arrays into a third. What's the most likely reason?
Check
Write two SWAR functions for 32-bit words: add_bytes(a, b), lane-wise byte addition modulo 256 using the masking trick, and has_zero_byte(x) returning True if any byte of x is zero, using . Keep results in 32 bits. They'll be checked against byte-by-byte references on random words from random.Random(seed).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A video decoder speeds up 6 times when moved from scalar code to 8-lane SIMD, but only 7 times with 16 lanes. Explain both numbers, and what you'd look at to go faster.
By Amdahl's law, if a fraction of the time vectorizes, 8 lanes give ; a speedup of 6 means is about 0.95, so about 5% of the original time stays serial. With 16 lanes the vectorized part shrinks further, but the serial 5% doesn't, and the formula gives only about 9; in practice memory bandwidth and the overhead of shuffles and tails take some of that too, so 7 is plausible. To go faster, attack what's left: vectorize or restructure the serial parts such as entropy decoding, reduce memory traffic by keeping data in cache between stages, and avoid unaligned loads and lane shuffles in the vector code.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.