Solved

dividing matrix into small blocks

Posted on 2003-12-09
4
333 Views
Last Modified: 2010-04-15

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
Comment
Question by:sanko50
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
skypalae earned 30 total points
ID: 9910487
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9911764
> 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
 
LVL 4

Expert Comment

by:skypalae
ID: 9911878
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9912247
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question