Modern x86: AVX tour
YMM registers
AVX (2011) widens the vector registers to 256 bits: YMM0-YMM15, each holding eight floats or four doubles. AVX2 adds integer vector operations, and AVX-512 widens again to ZMM - present on server chips and recent desktops.
vmovaps ymm0, [a] vmovaps ymm1, [b] vaddps ymm0, ymm0, ymm1 ; eight float adds vmovaps [sum], ymm0
The VEX forms
AVX instructions start with V (vaddps, vmulpd, vmovaps) and take three operands: destination, source1, source2. The three-operand form means the destination is separate from the inputs, so the accumulator pattern needs no extra moves.
Checking CPU support
CPUID reports AVX in ECX bit 28 of leaf 1, and AVX2 in EBX bit 5 of leaf 7. Real programs check before running vector code and fall back to SSE or scalar. The align 16 directive becomes align 32 for AVX data.
This complete program adds eight pairs of floats with one AVX instruction, sums the eight lanes and exits with the total, 396, as a status (140 after the 8-bit wrap):
default rel section .data align 32 a dd 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 b dd 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0 section .text global _start _start: vmovaps ymm0, [a] vaddps ymm0, ymm0, [b] ; eight sums at once vextractf128 xmm1, ymm0, 1 vaddps xmm0, xmm0, xmm1 vhaddps xmm0, xmm0, xmm0 vhaddps xmm0, xmm0, xmm0 vcvttss2si eax, xmm0 ; 396 mov rdi, rax mov rax, 60 syscall
nasm -f elf64 -o avx.o avx.asm
ld -o avx avx.o
./avx
echo $?
140
The snippets on this page are fragments, not whole programs: they show the VEX forms. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How wide is a YMM register?
Answer
256 bits
AVX doubles the SSE width to 256 bits.
-
How many single-precision floats fit in a YMM register?
Answer
8
256 bits divided by 32 bits per float is eight.
-
How many doubles fit in a YMM register?
Answer
4
A double is 64 bits, so four fit in 256 bits.
-
How many operands does a VEX arithmetic instruction take?
Answer
Three: destination, source1, source2
The three-operand form leaves the sources in place and writes a separate destination.
-
Which CPUID leaf reports AVX2?
Answer
Leaf 7, EBX bit 5
AVX is leaf 1 ECX bit 28; AVX2 is in the extended leaf 7.
-
What alignment does AVX data want?
Answer
32 bytes
A YMM load moves 32 bytes, so the data is aligned to 32.
-
What does
vaddps ymm0, ymm0, ymm1produce?Answer
Eight float sums in ymm0
The packed single form adds every lane at once.
-
What does
vextractf128 xmm1, ymm0, 1take?Answer
The high 128 bits of ymm0
The immediate 1 selects the upper half, which is how a YMM value is reduced.
-
What exit status does the AVX program return, though the lane total is 396?
Answer
140, because a status keeps the low 8 bits
396 mod 256 is 140, which is what wait() reports for the exit status.
-
What should a program do before it runs AVX code?
Answer
Check CPUID and fall back when AVX is absent
AVX arrived in 2011, so older CPUs raise an illegal instruction unless the program checks first.
Article author: Arthur Isaev