質問

SSE2 instruction (paddd xmm, m128) works really strange. Code tells all.

#include <iostream>
using namespace std;

int main()
{
     int * v0 = new int [80];
     for (int i=0; i<80; ++i)
          v0[i] = i;
     int * v1 = new int [80];
     for (int i=0; i<80; ++i)
          v1[i] = i;

     asm(
     ".intel_syntax noprefix;"
     "mov rcx , 20;"
     "mov rax , %0;"
     "mov rbx , %1;"

     "m_start:;"
     "cmp rcx , 0;"
     "je m_end;"

     "movdqu xmm0 , [rax];"
     "paddd xmm0 , [rbx];"
     "movdqu [rax] , xmm0;"

     "add rbx , 16;" /* WTF?? If I put there 128, it's work really bad */
     "add rax , 16;" /* but why?? I must add 128 because XMM width is 128 bits ... */
     "dec rcx;"
     "jmp m_start;"
     "m_end:;"

     ".att_syntax noprefix;"
     : //
     : "r"(v0) , "r"(v1)
     : //
     );

     for (int i=1; i<81; ++i)
     {
          cout << v0[i-1] << (char*)((i%10==0) ? "\n" : ", ");
     }

     return 0;
}
役に立ちましたか?

解決

You must add 16 because 128 bits is 16 bytes.

Additional notes: you forgot to tell the compiler that you clobber some registers and you are not supposed to switch syntax without telling the compiler either (use -masm=intel switch instead).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top