Loop Unrolling Multi-Dimesional Array

I am trying to unroll the inner i and j loops within this multi-dimensional array which spits out a block image. Unfortunately, the image does not match the color of the original image probably because filter->get(i,j) gets altered in a way that I don't want it to. If anyone can give me insight, that will be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
double
applyFilter(struct Filter *filter, cs1300bmp *input, cs1300bmp *output)
{

     long long cycStart, cycStop;

     cycStart = rdtscll();

    output -> width = input -> width;
    output -> height = input -> height;
int a = filter -> getDivisor();
int n = filter -> getSize();
for (int plane = 0; plane < 3; plane++){
    for(int row = 1; row < (input -> height) - 1 ; row = row + 1) {
        for(int col = 1; col < (input -> width) - 1; col = col + 1) {
            int value = 0;
            int val1, val2;
            for (int j = 0; j < n; j++) {
                for (int i = 0; i < n; i+=2) {
                    val1 = val1 + input -> color[plane][row + i - 1][col + j - 1]
                    * filter -> get(i, j);
                    val2 = val2 + input -> color[plane][row + i][col + j -1] * filter->get(i+1,j);
                }
            }
            value = (val1 + val2) / a;
            if ( value  < 0 ) { value = 0; }
            if ( value  > 255 ) { value = 255; }
            output -> color[plane][row][col] = value;
        }

    }
}

 cycStop = rdtscll();
 double diff = cycStop - cycStart;
 double diffPerPixel = diff / (output -> width * output -> height);
 fprintf(stderr, "Took %f cycles to process, or %f cycles per pixel\n",
  diff, diff / (output -> width * output -> height));

 return diffPerPixel;
} 
Why are you doing this?

Unless you have hard profiling data to support the effort then leave loop unrolling to the compiler.
Just trying to optimize my code by unrolling the i and j loop.
We are up to the day of having computers with lots of clock cycles every second where optimization should not be first on your mind (this does not apply to everything, just most things that you would see the average programmer working on). You should worry about making your code as clean and neat as possible, rather than trying to write everything in an extremely efficient manner.
Last edited on
Topic archived. No new replies allowed.