PDA

View Full Version : MMX array addition


LUBOBYA
11-Jul-2010, 12:06
#include "stdafx.h"
#include <iostream>
#include "emmintrin.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

short x0[4]={1,1,1,1};//array xo
short x1[4]={2,2,2,2};//array x1
short c[4]; // sum of x0 and x1
__asm{

movq mm4,x0 // load x0 into mm4
movq mm7,x1 // load x1 into mm7
paddw mm4,mm7// add array x0 to array x1
movq mm5, mm1
movq c,mm1 // move result into xmm1
emms
}
for (int i = 0; i < 4; i++)
{
cout << x0[i] << " ";
cout << x1[1]<< " ";
cout << endl;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << c[i] << " ";
cout << endl;
}


return 0;
}

Rys
12-Jul-2010, 18:14
Are you asking why it doesn't work?

Betanumerical
12-Jul-2010, 18:46
#include "stdafx.h"
#include <iostream>
#include "emmintrin.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

short x0[4]={1,1,1,1};//array xo
short x1[4]={2,2,2,2};//array x1
short c[4]; // sum of x0 and x1
__asm{

movq mm4,x0 // load x0 into mm4
movq mm7,x1 // load x1 into mm7
paddw mm4,mm7// add array x0 to array x1
movq mm5, mm1
movq c,mm1 // move result into xmm1
emms
}
for (int i = 0; i < 4; i++)
{
cout << x0[i] << " ";
cout << x1[1]<< " ";
cout << endl;
}
cout << endl;
for (int i = 0; i < 4; i++)
{
cout << c[i] << " ";
cout << endl;
}


return 0;
}

Assuming this isnt spam.

You nearly got it right.


movq mm5, mm1
movq c,mm1 // move result into xmm1
emms
should be


movq mm1, mm4
movq c,mm1 // move result into xmm1
emms
[ninja edit]because in your code your moving the results of a register you havent worked on and contains zero into c which is whats causing your problem

assuming of course that you want to move the result of


paddw mm4,mm7// add array x0 to array x1
into mm4 before you move it into c. If not you can just do.


paddw mm4,mm7// add array x0 to array x1
movq c,mm4 // move result into xmm1
emms
and save a mov.

hope that helps.

Andrew Lauritzen
13-Jul-2010, 18:24
Also it must be said: use SSE instead of MMX!