I am currently taking an introductory course in computer architecture. Our goal was to write a dot-product function in x86 Assembly which would use SSE and SIMD (without AVX).
I am not to that confident with my solution:
#include <time.h>
#define NUM 25600
//Example: scalarProduct using SIMD
extern float scalarProduct(float *, float *, int size);
float vsC(float * a, float * b, int size){
float sum = 0;
for(int i = 0; i < size; i++){
sum += b[i]*a[i];
}
return sum;
}
int main(int argc, char ** argv){
float * a = malloc(NUM * sizeof(double));
float * b = malloc(NUM * sizeof(double));
for(int i = 0; i < NUM; i++){
a[i] = 1;
b[i] = 1.0/(i+1);
}
clock_t start, end;
double cpu_time_used;
start = clock();
float sum = scalarProduct(a,b,NUM);
end = clock();
cpu_time_used = ((double) (end - start))/CLOCKS_PER_SEC;
printf("%.15f\n", cpu_time_used);
printf("Solution %.15f\n", (double)(sum));
start = clock();
sum = vsC(a,b,NUM);
end = clock();
cpu_time_used = ((double) (end - start))/CLOCKS_PER_SEC;
printf("%.15f\n", cpu_time_used);
printf("Solution %.15f\n", (double)(sum));
}
Assembly File
.intel_syntax noprefix
.text
.global scalarProduct
scalarProduct:
mov rax, rdx
xorps xmm0, xmm0
mov rcx, 0
start:
cmp rax, 4
jl end
movdqu xmm3, [rsi + rcx]
movdqu xmm2, [rdi + rcx]
vmulps xmm1, xmm2, xmm3
haddps xmm7, xmm1
haddps xmm7, xmm7
psrldq xmm7, 4 //Shift to pos0
addss xmm0, xmm7
xorps xmm7, xmm7
sub rax, 4
add rcx, 16
jmp start
end:
cmp rax, 0
je ret
dec rax
movss xmm1, [rsi + rcx]
movss xmm2, [rdi + rcx]
mulss xmm1, xmm2
addss xmm0, xmm1
add rcx, 4
jmp end
ret:
ret
Obviously, this Assembly - Code is far from perfect. How could I do better using basic SIMD and SSE?
The second thing which made me wonder is, I indeed outperformed GCC on a Xeon processor, which is irritating.
Compiling the code with:
gcc -o main -O7 main.c scalarProduct.S
Shows the following result:
./main
0.000015000000000
Solution 10.727574348449707
0.000026000000000
Solution 10.727569580078125
How would I have to improve my C Code so that GCC can step up?
DISCLAIMER:
My homework does not affect my grades and its editing is optional.