Compilation error when perfoming SSE in C++
My code is very simple for understanding SSE. My code is:
#include <iostream>
#include <iomanip&开发者_高级运维gt;
#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;
}
I am using Ubuntu 10.04. for compilation i used the following command:
$ gcc -o program -msse -mmmx -msse2 sse.cpp
The following error I found:
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’
Can anyone please help me how can I remove this error?
Don't try to do this with inline asm - use the provided intrinsics, e.g.
// ...
#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;
}
精彩评论