I tried looping over an array in different ways using loop unrolling.
#define MYSIZE 8
int main()
{
int A[MYSIZE];
int B[MYSIZE];
int C[MYSIZE];
int i = 0;
while(i < MYSIZE)
{
A[i] = i;
i++;
}
/* LOOP 1 */
i = 0;
while (i< MYSIZE)
{
B[i+0] = A[i+0];
B[i+1] = A[i+1];
B[i+2] = A[i+2];
B[i+3] = A[i+3];
i += 4;
}
/* LOOP 2 */
i = 0;
while (i < MYSIZE)
{
C[i] = A[i++];
C[i] = A[i++];
C[i] = A[i++];
C[i] = A[i++];
}
printf(" i | A | B | C|\r\n");
i = 0;
while (i < MYSIZE)
{
printf(" %d | %d | %d | %d |\r\n",i,A[i],B[i],C[i]);
i++;
}
}
Which does give me this result:
i | A | B | C |
0 | 0 | 0 | 1578166688 |
1 | 1 | 1 | 0 |
2 | 2 | 2 | 1 |
3 | 3 | 3 | 2 |
4 | 4 | 4 | 3 |
5 | 5 | 5 | 4 |
6 | 6 | 6 | 5 |
7 | 7 | 7 | 6 |
I thought that A,B and C should contain the same numbers. As I understood i++, LOOP 2 should be the same as:
/* LOOP 3 */
i = 0
while(i < MYSIZE)
{
C[i] = A[i];
i++;
C[i] = A[i];
i++;
C[i] = A[i];
i++;
C[i] = A[i];
i++;
}
Which is not the case. LOOP 3 actually works fine but LOOP 2 does not. What am I doing wrong ?