I try to understand how vectorization with SSE instructions works.
Here a code snippet where vectorization is achieved :
#include <stdlib.h>
#include <stdio.h>
#define SIZE 10000
void test1(double * restrict a, double * restrict b)
{
int i;
double *x = __builtin_assume_aligned(a, 16);
double *y = __builtin_assume_aligned(b, 16);
for (i = 0; i < SIZE; i++)
{
x[i] += y[i];
}
}
and my compilation command :
gcc -std=c99 -c example1.c -O3 -S -o example1.s
Here the output for assembler code :
.file "example1.c"
.text
.p2align 4,,15
.globl test1
.type test1, @function
test1:
.LFB7:
.cfi_startproc
xorl %eax, %eax
.p2align 4,,10
.p2align 3
.L3:
movapd (%rdi,%rax), %xmm0
addpd (%rsi,%rax), %xmm0
movapd %xmm0, (%rdi,%rax)
addq $16, %rax
cmpq $80000, %rax
jne .L3
rep ret
.cfi_endproc
.LFE7:
.size test1, .-test1
.ident "GCC: (Debian 4.8.2-16) 4.8.2"
.section .note.GNU-stack,"",@progbits
I have practiced Assembler many years ago and I would like to know what represents above the registers %rdi, %rax and %rsi.
I know %xmm0 is the SIMD register where we can store 2 doubles (on 16 bytes).
But I don't understand how the simultaneous addition is performed :
I think all happens here :
movapd (%rdi,%rax), %xmm0
addpd (%rsi,%rax), %xmm0
movapd %xmm0, (%rdi,%rax)
addq $16, %rax
cmpq $80000, %rax
jne .L3
rep ret
Does %rax represents "x" array ?
What does %rsi represent in C code snippet ?
Does the final result (for example a[0]=a[0]+b[0] is stored into %rdi ?
Thanks for your help
-fverbose-asmto have gcc put comments on each instruction, giving a name for each operand. (Often they're just numbered tmp names, but sometimes you get a meaningful C variable name. Especially for array indexing.) - Peter Cordes