I'm trying to implement some inline assembler (in Visual Studio 2012 C++ code) to take advantage of SSE. I want to add 7 numbers for 1e9 times so i placed them from RAM to xmm0 to xmm6 registers of CPU. when i do it with inline assembly in visual studio 2012 with this code:
the C++ code:
for(int i=0;i<count;i++)
resVal+=val1+val2+val3+val4+val5+val6+val7;
my ASM code:
int count=1000000000;
double resVal=0.0;
//placing values to register
__asm{
movsd xmm0,val1;placing var1 in xmm0 register
movsd xmm1,val2
movsd xmm2,val3
movsd xmm3,val4
movsd xmm4,val5
movsd xmm5,val6
movsd xmm6,val7
pxor xmm7,xmm7;//turns xmm7 to zero
}
for(int i=0;i<count;i++)
{
__asm
{
addsd xmm7,xmm0;//+=var1
addsd xmm7,xmm1;//+=var2
addsd xmm7,xmm2;
addsd xmm7,xmm3;
addsd xmm7,xmm4;
addsd xmm7,xmm5;
addsd xmm7,xmm6;//+=var7
}
}
__asm
{
movsd resVal,xmm7;//placing xmm7 into resVal
}
and this is the dis assembled code from C++ compiler for the code 'resVal+=val1+val2+val3+val4+val5+val6+val7':
movsd xmm0,mmword ptr [val1]
addsd xmm0,mmword ptr [val2]
addsd xmm0,mmword ptr [val3]
addsd xmm0,mmword ptr [val4]
addsd xmm0,mmword ptr [val5]
addsd xmm0,mmword ptr [val6]
addsd xmm0,mmword ptr [val7]
addsd xmm0,mmword ptr [resVal]
movsd mmword ptr [resVal],xmm0
As is visible the compiler uses just one xmm0 register and for other times it is fetching values from RAM.
Answer of both codes (my ASM code and c++ code) is same but the c++ code takes about half the time of my asm code to execute!
I was readed about CPU registers that working with them is much faster than memory. I dont think this ratio be true. Why the asm version have lower performance of C++ code?
val1+val2+val3+val4+val5+val6+val7
and replace it with a single value.for
loop with inline asm? It might be putting extra stuff around it. I see no reason for registers to ever be slower than RAM, even when the load is hot in L1 -- something fishy is going on here and I don't accept "CPU voodoo" answers!