C842 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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: ((a∧𝟽𝙵𝟽𝙵𝟽𝙵𝟽𝙵)+(b∧𝟽𝙵𝟽𝙵𝟽𝙵𝟽𝙵))⊕((a⊕b)∧𝟾𝟶𝟾𝟶𝟾𝟶𝟾𝟶).
  • 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?

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: ((a∧m)+(b∧m))⊕((a⊕b)∧h), with m = 0x7F7F7F7F and h = 0x80808080.
  • Find a zero byte: (x−𝟶𝚡𝟶𝟷𝟶𝟷𝟶𝟷𝟶𝟷)∧x¯∧𝟶𝚡𝟾𝟶𝟾𝟶𝟾𝟶𝟾𝟶 is nonzero exactly when some byte of x is zero. String functions such as strlen check four or eight bytes per step this way.

The limits

  • Amdahl's law: with a fraction f of the work vectorized onto L lanes, the speedup is 1(1−f)+f/L, never more than 1/(1−f).
  • 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
  1. Byte by byte, modulo 256. 0x7F+0x01=0x800x01+0xFF=0x000xFF+0x01=0x000x10+0x10=0x20 so the expected result is 0x80000020.
  2. Add the low 7 bits of each byte. a∧m=0x7F017F10b∧m=0x017F0110sum=0x80808020
  3. Find the top bits. a⊕b=0x7EFEFE00(a⊕b)∧h=0x00808000
  4. Fix the top bits with XOR. 0x80808020⊕0x00808000=0x80000020 matching.
  5. 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
  1. The vector loop. With vsetvli, the loop runs ⌈10038⌉=126 iterations: 125 full ones and a last one of 3 elements, using the same code.
  2. 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
  1. Samples from memory. 25×1094=6.25×109/s
  2. The operations they feed. That's enough for 12.5 billion operations per second, and the core could do 64 billion.
  3. Which binds. The filter is memory-bound at about a fifth of the SIMD peak.
  4. 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 (x−𝟶𝚡𝟶𝟷𝟶𝟷𝟶𝟷𝟶𝟷)∧x¯∧𝟶𝚡𝟾𝟶𝟾𝟶𝟾𝟶𝟾𝟶. 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.

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.