7

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?

14
  • Why are you doing this? Is it strictly a learning exercise, or do you have something you want to achieve? Commented Mar 11, 2013 at 21:51
  • 1
    @Bwmat: The compiler gets the value from RAM for each time and it does it for 1e9 times, but there sould be an advantage of placing values into CPU registers and getting them for 1e9 times!
    – epsi1on
    Commented Mar 11, 2013 at 21:52
  • I'm mildly surprised that the compiler didn't optimize out the entire val1+val2+val3+val4+val5+val6+val7 and replace it with a single value.
    – Mysticial
    Commented Mar 11, 2013 at 21:53
  • @epsi1on But it's not going to ram. It's going to cache. L1 cache most likely.
    – Mysticial
    Commented Mar 11, 2013 at 21:54
  • 1
    Have you verified that the compiler is generating what you expect for that 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! Commented Mar 11, 2013 at 22:01

2 Answers 2

12
  • Once the data is in the cache (which it will be the case after the first loop, if it's not there already), it makes little difference if you use memory or register.
  • A floating point add will take a little longer than single cycle in the first place.
  • The final store to resVal "unties" the xmm0 register to allow the register to be freely "renamed", which allows more of the loops to be run in parallel.

This is a typical case of "unless you are absolutely sure, leave writing code to the compiler".

The last bullet above explains why the code is faster than code where every step of the loop depends on a previously calculated result.

In the compiler generated code, the loop can do the equivalent of:

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  

movsd       xmm1,mmword ptr [val1]  
addsd       xmm1,mmword ptr [val2]  
addsd       xmm1,mmword ptr [val3]  
addsd       xmm1,mmword ptr [val4]  
addsd       xmm1,mmword ptr [val5]  
addsd       xmm1,mmword ptr [val6]  
addsd       xmm1,mmword ptr [val7]  
addsd       xmm1,mmword ptr [resVal]  
movsd       mmword ptr [resVal],xmm1

Now, as you can see, we could "mingle" these two "threads":

movsd       xmm0,mmword ptr [val1]  
movsd       xmm1,mmword ptr [val1]  
addsd       xmm0,mmword ptr [val2]  
addsd       xmm1,mmword ptr [val2]  
addsd       xmm0,mmword ptr [val3]  
addsd       xmm1,mmword ptr [val3]  
addsd       xmm0,mmword ptr [val4]  
addsd       xmm1,mmword ptr [val4]  
addsd       xmm0,mmword ptr [val5]  
addsd       xmm1,mmword ptr [val5]  
addsd       xmm0,mmword ptr [val6]  
addsd       xmm1,mmword ptr [val6]  
addsd       xmm0,mmword ptr [val7]  
addsd       xmm1,mmword ptr [val7]  
addsd       xmm0,mmword ptr [resVal]  
movsd       mmword ptr [resVal],xmm0  
// Here we have to wait for resval to be uppdated!
addsd       xmm1,mmword ptr [resVal]  
movsd       mmword ptr [resVal],xmm1

I'm not suggesting it is quite that much out of order execution, but I can certainly see how the loop can be executed faster that your loop. You can probably achieve the same thing in your assembler code if you had a spare register [in x86_64 you do have another 8 registers, although you can't use inline assembler in x86_64...]

(Note that register renaming is different from my "threaded" loop, which is using two different registers - but the effect is roughly the same, the loop can continue after it hits the "resVal" update without having to wait for the result to be updated)

5
  • 1
    can you please explain that why the c++ code runs two times faster?
    – epsi1on
    Commented Mar 11, 2013 at 21:58
  • 1
    I broke your answer into bullets. But I'm not sure how applicable the second bullet is. The last part about the renaming and the break in dependency, I believe, is the correct answer.
    – Mysticial
    Commented Mar 11, 2013 at 21:58
  • 2
    @epsi1on Your assembly code has a data dependency chain on xmm7. The code generated by the compiler does not because all the stores to xmm0 are dead stores.
    – Mysticial
    Commented Mar 11, 2013 at 22:01
  • 2
    The register renaming means that the adding of val1 through val7 can proceed down the pipeline (using a different physical register) before the previous iteration is done updating resVal.
    – user57368
    Commented Mar 11, 2013 at 22:04
  • @epsi1on: I have edited the code to explain the "register renaming". Commented Mar 11, 2013 at 22:06
0

May be it's useful for you not use _asm, but intrinsics functions and instrinsic types like __m128i of __m128d witch are represent sse registers. See immintrin.h it's define types and many sse functions. you can find good description and spec for them here :http://software.intel.com/sites/landingpage/IntrinsicsGuide/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.