Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I add a constant to each array elements?
See code snippet below:
C++
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "xmmintrin.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    int x[4][4]={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4}; //source or input
    int y[4][4];//destination or output
    int b[4] ={5,5,5,5};// constant
    int f0,f1,f2,f3;
    __asm
    {
    mov eax,x
    mov ecx,b
    mov edi,y
        //stage one
    movq mm0,[eax]
    movq mm7,[ecx]
    paddw mm0,mm7
    movq [f0],mm0
    movq mm1,[eax+8]
    paddw mm1,mm7
    movq [f1],mm1
    movq mm2,[eax+16]
    paddw mm2,mm7
    movq [f2],mm2
    movq mm3,[eax+24]
    paddw mm3,mm7
    movq [f3],mm3
    //stage two

    movq mm0,[f0]
    psraw mm0,6
    movq [edi],mm0
    movq mm1,[f1]
    psraw mm1,6
    movq [edi+8],mm1
    movq mm2,[f2]
    psraw mm2,6
    movq [edi+16],mm2
    movq mm3,[f3]
    psraw mm3,6
    movq [edi+24],mm3

    }
for (int i = 0; i < 4; i++)
       {
          for (int j = 0; j < 4; j++)
             cout << y[i][j] << " ";
          cout << endl;
       }
    return 0;
}
Posted
Updated 20-Feb-11 0:34am
v4

1 solution

Do you mean something like that?
C++
int _tmain(int argc, _TCHAR* argv[])
{
    int x[4][4]={1,2,3,4,2,2,2,2,3,3,3,3,4,4,4,4}; //source or input
    int y[4][4];//destination or output
    int b[4] ={5,5,5,5}; // constant
    //int f0,f1,f2,f3;

    __asm
    {
        push    esi
        push    edi
        push    ebx

        lea     esi, x
        lea     edi, y
        lea     edx, b
        //stage one

        mov     eax, 4
        mov     ebx, eax        ; mov   ebx, 1st dim size
_loop_00:
        mov     ecx, eax        ; mov   ecx, 2nd dim size
_loop_01:
        movd    mm0, [edx]
        movd    mm1, [esi]
        paddd   mm0, mm1
        ; psraw mm0, 6           ; // stage two
        movd    [edi], mm0
        add     esi, eax
        add     edi, eax
        loop    _loop_01
        add     edx, eax
        dec     ebx
        jnz     _loop_00

        //stage two
        // ???

        pop     ebx
        pop     edi
        pop     esi
    }

    _mm_empty();

    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
            cout << y[i][j] << " ";
        cout << endl;
    }
    return 0;
}


I didn't understand stage two. I commented it out because it yields all 0 with such small values. :-) If you need to do arithmetic shift, you can un-comment line "; psraw mm0, 6".

You can also change array sizes by ebx and ecx. I've used eax register as all constant values equal 4 for optimization (in this particular case).

BTW, I've used MMX on behalf of your question's title. There would be some more optimized (using only x86) way of doing this without using MMX instruction set. Because array elements are "int" (that is, 32 bits), I think, there is no gain by using 64 bits MMX registers.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900