- #1

- 62

- 0

## Homework Statement

I need to optimize this given code that rotates an image 90 degrees so it runs at least three times faster:

Code:

```
void naive_rotate(int dim, pixel *src, pixel *dst)
{
int i, j;
for (i = 0; i < dim; i++)
for (j = 0; j < dim; j++)
dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];
}
```

## Homework Equations

N/A

## The Attempt at a Solution

The function I write is tested with the variable dim equal to multiples of 32. I tried unrolling the loop but I keep getting an error that the expected values have changed when it reaches dim = 96. My code is this:

Code:

```
void rotate(int dim, pixel *src, pixel *dst)
{
int i;
int j;
for (j = 0; j < dim; j++){
for (i = 0; i < dim; i+=4){
dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];
dst[RIDX(dim-1-(j+1), i+1, dim)] = src[RIDX(i+1, j+1, dim)];
dst[RIDX(dim-1-(j+2), i+2, dim)] = src[RIDX(i+2, j+2, dim)];
dst[RIDX(dim-1-(j+3), i+3, dim)] = src[RIDX(i+3, j+3, dim)];
}
}
for (; j < dim; j++)
for (; i < dim; i++)
dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];
}
```