Solved

loop merging

Posted on 2004-09-09
5
240 Views
Last Modified: 2010-04-15
hello everybody
     i have got a question for u , its very simple, can u suggest me different ways to merge the loop in the below programm. i want to elinimate the       z[320] or do indexing with three loops given



const double* C = Coefficients;  \*constant coeff*/
int i,j;
float   X[320];
double   Z[320];
double   realAccu;
for ( i=0; i<320; i++ )
{
    Z[i] = X[i] * C[2*i];
}
      
      /* create array Y */
      for ( i=0; i<64; i++ ) {
            realAccu = 0.0;
      }
            for ( j=0; j<5; j++ ) {
                  realAccu = realAccu + Z[i + j * 64];
            
            Y[i] = realAccu;
      }



0
Comment
Question by:san_ys
5 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12023938
for ( i=0; i<64; i++ ) {
          realAccu = 0.0;
}

the above is unnecessary you are just assigning 0.0 to realAccu 64 times
you can just replace the above for loop with

realAccu = 0.0
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12024779
Hi san_ys,

Your requirements are not very clear. Arrays Z and X are uninitialized when you use them. As Abhijit pointed out, one of the loops is redundant.

>i want to elinimate the       z[320] or do indexing with three loops given
You mean you wish to eliminate array Z from calculations or the just the loop which initializes Z (the initialization might be incorrect since X is uninitialized). Also, I am not sure what you mean by "do indexing with three loops given"
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 50 total points
ID: 12024833
Hi san_ys,
It looks like the loop scopes are not correct. You probably meant to have the last two loops nested, like:

     /* create array Y */
     for ( i=0; i<64; i++ ) {
          realAccu = 0.0;
          for ( j=0; j<5; j++ )
               realAccu = realAccu + Z[i + j * 64];
         
          Y[i] = realAccu;
     }

The inner loop should be eliminated (loop unrolling) by the compiler, but you can also do that manually:

     /* create array Y */
     for ( i=0; i<64; i++ ) {
          Y[i] = Z[i] + Z[64+i] + Z[128+i] + Z[192+i] + Z[256+i];
     }

...that's a matter of personal preference. If you feel that the algorithm is expressed more clearly with two nested loops, fine. A good compiler should create identical code in both cases.

You can also eliminate the creation of your Z array - its values are only used once. That's probably done best with a macro:

#define Z(i) (X[i] * C[2*(i)])

Then you can write

     /* create array Y */
     for ( i=0; i<64; i++ ) {
          Y[i] = Z(i) + Z(64+i) + Z(128+i) + Z(192+i) + Z(256+i);
     }
...ending up with a single loop.

Did you initialize the X array with some values?


Cheers!

Stefan
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now