SIMD basics

Packed versus scalar

The P suffix (addps, mulps) works on all lanes of an XMM register at once: four single-precision floats, or two doubles with the PD forms. One instruction, four results:

movaps xmm0, [a] ; load 4 aligned floats movaps xmm1, [b] addps xmm0, xmm1 ; four sums at once movaps [sum], xmm0

Alignment

MOVAPS requires 16-byte-aligned memory; the unaligned form MOVUPS works anywhere but can be slower on old CPUs. Align data with align 16 before the definition:

section .data align 16 a dd 1.0, 2.0, 3.0, 4.0

Four adds at once

A loop that adds two arrays of floats can process four elements per iteration with the P forms, then handle the remainder (length mod 4) with the scalar SD forms. That is the whole trick of hand-written SIMD.

This complete program adds four pairs of floats with one packed instruction, sums the four lanes, and exits with the total as the status (110):

default rel section .data align 16 a dd 1.0, 2.0, 3.0, 4.0 b dd 10.0, 20.0, 30.0, 40.0 section .text global _start _start: movaps xmm0, [a] addps xmm0, [b] ; four sums at once movaps xmm1, xmm0 ; sum the four lanes shufps xmm1, xmm1, 0x4e addps xmm0, xmm1 movaps xmm1, xmm0 shufps xmm1, xmm1, 0xb1 addps xmm0, xmm1 cvtss2si eax, xmm0 ; 110 mov rdi, rax mov rax, 60 syscall

nasm -f elf64 -o simd.o simd.asm
ld -o simd simd.o
./simd
echo $?

110

The snippets on this page are fragments, not whole programs: they show the packed forms and the alignment. 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.

  1. How many single-precision floats does one XMM register hold?

    Answer

    4

    An XMM register is 128 bits, and a float is 32 bits, so four fit.

  2. What does the P in ADDPS mean?

    Answer

    Packed: the operation runs on every lane

    The packed form adds all lanes at once; the S form handles one scalar.

  3. Which instruction loads 16 aligned bytes into an XMM register?

    Answer

    MOVAPS

    MOVAPS requires 16-byte alignment; MOVUPS is the unaligned form.

  4. What does align 16 do before a data definition?

    Answer

    It pads to the next 16-byte boundary

    Alignment inserts padding so the label starts at an address divisible by 16.

  5. How many doubles does a packed PD instruction add at once?

    Answer

    2

    A double is 64 bits, so one XMM register holds two of them.

  6. What is the remainder loop for?

    Answer

    The elements left when the length is not a multiple of four

    The packed loop handles groups of four, and the tail handles the rest one by one.

  7. What exit status does this program return? default rel section .data align 16 a dd 1.0, 2.0, 3.0, 4.0 b dd 10.0, 20.0, 30.0, 40.0 section .text global _start _start: movaps xmm0, [a] addps xmm0, [b] movaps xmm1, xmm0 shufps xmm1, xmm1, 0x4e addps xmm0, xmm1 movaps xmm1, xmm0 shufps xmm1, xmm1, 0xb1 addps xmm0, xmm1 cvtss2si eax, xmm0 mov rdi, rax mov rax, 60 syscall

    Answer

    110

    The lanes are 11, 22, 33 and 44, and their sum is 110, which becomes the status.

  8. Why can MOVAPS fault on a buffer that MOVUPS reads?

    Answer

    The address is not 16-byte aligned

    MOVAPS requires the aligned form; an unaligned address raises a general protection fault.

  9. Which instruction converts a scalar float in XMM0 to an integer?

    Answer

    CVTSS2SI

    CVTSS2SI converts scalar single to signed integer, with the destination a general register.

  10. Which forms handle the last elements when the length is not a multiple of four?

    Answer

    The scalar S forms

    The scalar forms process one element at a time, so the tail needs no special data.

Article author: Arthur Isaev