TExcelTAD
Home › x86-64 Assembly › SIMD Intro

SIMD Intro

SIMD stands for Single Instruction, Multiple Data: one instruction performs the same operation on several values packed side by side in a wide register. A 128-bit XMM register can hold sixteen bytes, eight 16-bit words, four 32-bit integers or floats, or two doubles — and paddd adds four pairs of integers in the time add adds one pair.

text
          xmm0:  |   10   |   20   |   30   |   40   |
   paddd  xmm1:  |    1   |    2   |    3   |    4   |
          -----------------------------------------------
          xmm0:  |   11   |   22   |   33   |   44   |    one instruction, four additions

This is how video codecs, image filters, string functions like memchr, and machine-learning kernels reach their speed.

Instruction naming

Part Meaning Examples
p prefix packed integer operation paddd, psubw, pmullw, pand, pcmpeqb
last letter(s) of an integer op element size: b 8, w 16, d 32, q 64 bits paddb, paddq
ps / pd suffix packed single / packed double float addps, mulpd, sqrtpd
movdqa / movdqu move 128 bits, aligned / unaligned movdqu xmm0, [rsi]
movaps / movups the same for floats

The aligned forms (movdqa, movaps) require the memory address to be a multiple of 16 and crash otherwise; the unaligned forms work everywhere and are just as fast on modern CPUs when the data happens to be aligned. Use align 16 in the data section to align arrays.

Adding four integers at once

nasm · try it yourself
section .data
align 16
a:      dd 10, 20, 30, 40
b:      dd 1, 2, 3, 4

section .bss
alignb 16
result: resd 4

section .text
global _start
_start:
    movdqa xmm0, [a]            ; load four dwords
    paddd xmm0, [b]             ; four additions at once
    movdqa [result], xmm0       ; store four dwords

    xor ebx, ebx
.print:
    mov edi, [result + rbx*4]
    call print_uint
    inc ebx
    cmp ebx, 4
    jb .print

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
11
22
33
44

Summing an array four lanes at a time

To sum a long array, keep four partial sums in one register, add four elements per iteration, and combine the lanes at the end. Combining lanes is called a horizontal operation; pshufd (shuffle dwords) moves lanes around so that paddd can add them:

nasm · try it yourself
section .data
align 16
numbers:    dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
COUNT       equ ($ - numbers) / 4   ; a multiple of 4

section .text
global _start
_start:
    pxor xmm0, xmm0             ; four partial sums = 0
    xor ecx, ecx
.loop:
    paddd xmm0, [numbers + rcx*4]   ; add elements i .. i+3
    add ecx, 4
    cmp ecx, COUNT
    jb .loop
    ; xmm0 = [1+5+9+13, 2+6+10+14, 3+7+11+15, 4+8+12+16] = [28, 32, 36, 40]

    pshufd xmm1, xmm0, 0b01_00_11_10 ; swap the 64-bit halves
    paddd xmm0, xmm1            ; [28+36, 32+40, ...] = [64, 72, ...]
    pshufd xmm1, xmm0, 0b10_11_00_01 ; swap neighbouring lanes
    paddd xmm0, xmm1            ; lane 0 = 64 + 72 = 136
    movd edi, xmm0              ; the low dword to a normal register
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
136

The pshufd immediate holds four 2-bit fields; field i (from the right) says which source lane goes into destination lane i. 0b01_00_11_10 means lanes 2, 3, 0, 1: the two halves swapped.

Note: A real routine also handles the "tail" — the last COUNT mod 4 elements — with a normal scalar loop.

Packed doubles

The floating-point versions work the same way. mulpd multiplies two pairs of doubles in one go — here converting two prices from euros to dollars:

nasm · try it yourself
section .data
align 16
prices:     dq 19.99, 5.50          ; two prices in euros
rate:       dq 1.08, 1.08           ; the exchange rate, in both lanes
hundred:    dq 100.0, 100.0

section .bss
alignb 16
converted:  resq 2

section .text
global _start
_start:
    movapd xmm0, [prices]
    mulpd xmm0, [rate]          ; both prices * 1.08
    mulpd xmm0, [hundred]       ; both to cents
    cvtpd2dq xmm0, xmm0         ; round both to 32-bit integers (low 2 lanes)
    movq [converted], xmm0      ; store the two dwords

    mov edi, [converted]
    call print_fixed2           ; 19.99 * 1.08 = 21.59
    mov edi, [converted + 4]
    call print_fixed2           ; 5.50 * 1.08 = 5.94

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
    mov rax, rdi            ; e.g. 31415 -> "314.15"
    sub rsp, 32
    lea rsi, [rsp + 31]
    mov byte [rsi], 10
    mov ecx, 10
    xor r8d, r8d            ; digits written so far
.digit:
    cmp r8d, 2
    jne .no_point
    dec rsi
    mov byte [rsi], '.'     ; the point goes after two digits
.no_point:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc r8d
    cmp r8d, 3
    jb .digit               ; always at least three digits: "0.05"
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 32
    ret
21.59
5.94

Comparing many bytes at once

pcmpeqb compares sixteen bytes in parallel, producing 0xFF where they are equal and 0x00 where they differ. pmovmskb then collects the top bit of every byte into a 16-bit integer mask. This is the heart of fast strlen and memchr implementations:

nasm · try it yourself
section .data
align 16
text:       db "find the x in me"   ; exactly 16 bytes
pattern:    times 16 db 'x'         ; 'x' in every lane

section .text
global _start
_start:
    movdqa xmm0, [text]
    pcmpeqb xmm0, [pattern]     ; 0xFF in the lanes holding 'x'
    pmovmskb ebx, xmm0          ; bit i = 1 if byte i matched
    mov edi, ebx
    call print_uint             ; the mask: bit 9 -> 512
    bsf edi, ebx                ; index of the first match
    call print_uint             ; 9

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
512
9

Beyond SSE: AVX and AVX-512

Newer extensions widen the registers: AVX/AVX2 has 256-bit ymm0–ymm15 (eight floats or eight dwords per instruction) and a non-destructive three-operand syntax (vaddps ymm0, ymm1, ymm2); AVX-512 has 512-bit zmm registers and mask registers. Their availability depends on the CPU, so production code checks the cpuid instruction before using them. The ideas — lanes, packing, horizontal reductions, alignment — stay the same.

In practice you rarely write SIMD assembly by hand: compilers auto-vectorize simple loops at -O2/-O3, and C intrinsics like _mm_add_epi32 give you the same instructions with register allocation done for you. Knowing what the instructions do lets you read the result and nudge the compiler.

Test yourself

How many 32-bit integers does one paddd on XMM registers add?

  1. 1
  2. 2
  3. 4
  4. 8

What is the difference between movdqa and movdqu?

  1. Signed vs unsigned
  2. movdqa requires a 16-byte-aligned address; movdqu works at any address
  3. movdqu is for floats
  4. None

What is a horizontal operation?

  1. Combining the lanes of one register, e.g. summing them
  2. Moving data between registers
  3. A shift instruction
  4. Adding two registers lane by lane