在C ++中执行SSE时编译错误

时间:2011-08-26 06:23:55

标签: x86 sse simd

我的代码很容易理解SSE。我的代码是:

#include <iostream>
#include <iomanip>
#include <xmmintrin.h>
using namespace std;
struct cVector  {
float x,y,z; };
int main()
{
cVector vec1;
vec1.x=0.5;
vec1.y=1.5;
vec1.z=-3.141;
__asm 
{
movups xmm1, vec1
mulps xmm1, xmm1
movups vec1, xmm1
}
cout << vec1.x << " " << vec1.y << " " << vec1.z << '\n';
return 0;
} 

我正在使用Ubuntu 10.04。对于编译,我使用以下命令:

$ gcc -o program -msse -mmmx -msse2 sse.cpp

我发现以下错误:

sse.cpp: In function ‘int main()’:
sse.cpp:20: error: expected ‘(’ before ‘{’ token
sse.cpp:21: error: ‘movups’ was not declared in this scope
sse.cpp:21: error: expected ‘;’ before ‘xmm1’

任何人都可以帮助我如何删除此错误?

1 个答案:

答案 0 :(得分:3)

不要尝试使用内联asm执行此操作 - 使用提供的内在函数,例如

// ...

#include <xmmintrin.h>

// ...

int main()
{
    cVector vec1;

    vec1.x = 0.5f;
    vec1.y = 1.5f;
    vec1.z = -3.141f;

    __m128 v = _mm_loadu_ps(&vec1.x); // load unaligned floats to SSE vector
    v = _mm_mul_ps(v, v);             // square
    _mm_storeu_ps(&vec1.x, v);        // store SSE vector back to unaligned floats

    cout << vec1.x << " " << vec1.y << " " << vec1.z << endl;

    return 0;
}