Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

dividing matrix into small blocks


hi i want to divide a matrix of 512 x 512 dimension into small matrices of 8 x 8  blocks . how can i do it???
thanks
0
sanko50
Asked:
sanko50
  • 2
  • 2
1 Solution
 
skypalaeCommented:
are you kidding that you can't do it yourself? ok, ok .. here's some code. since you haven't specified any details i don't care about indexes. if they don't fit you, change the code. but you get the idea.



int i, j, k, l ; // just indexes
int old_matrix [512][512] ;
int new_matrix [64][64][8][8] ; // there will be 64x64 array of 8x8 matrices

// one possibility .... do it the hard way
for (i=0; i<64; i++)
    for (j=0; j<64; j++)
        for (k=0; k<8; k++)
            for (l=0; l<8; l++)
                new_matrix [i][j][k][l] = old_matrix [(i*8)+k][(j*8)+l] ; // you can replace the (i*8) by (i<<3) and same with j. should be little faster

// second possibility .. bit faster i think. but the indexes could be damaged. you have to check yourself
memcpy (new_matrix, old_matrix, 512*512*sizeof(int)) ;

S.
0
 
rstaveleyCommented:
> you can replace the (i*8) by (i<<3) and same with j. should be little faster

Most compilers will generate the same code in both cases. I'd stick with the multiplication, because it is easier to read.
0
 
skypalaeCommented:
Yes, rstaveley, youre right. I use VC++ and i thought that compiling it in 'debug' mode without any optimizations would make different code, but it surprisingly created exactly same thing.

Anyway, have you checked the difference between (i*9) and ((i<<3)+i) ?? The first thing compiles to 'imul ecx,ecx,9' which i would expect, but the second one compiles into:

mov         edx,dword ptr [ebp-4]
mov         eax,dword ptr [ebp-4]
lea         ecx,[eax+edx*8]

which is completely different from what i've expected (well, not completely, but almost completely). I've done some research on different mlutipliers, checked the assembler code. It really looked interesting.

And what's the point? That when i tried to compile it in 'release' mode using all optimizations the resulting assembler code was exactly the same. So don't use any shifting or other'shortcuts', because the compiler will recognize it and compile the code its own way no matter how you try to fool it.

S.
0
 
rstaveleyCommented:
If I optimise ((i<<3)+i) on VC7.1 with /Ox, I get...

      lea      eax, DWORD PTR [eax+eax*8]

...which makes more sense, because you get the whole shooting match in one 80386 instruction. Isn't it funny, though, to see it using a multiplication instead of the well-intended shift? The main trouble with attempting to help the compiler, is that it is tempting to think that we are still writing code for the 8086 instruction set :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now