Imagine calculating a three dimensional array like this:
for (int i = 0; i < I; i++)
{
for (int j = 0; j < J; j++)
{
for (int k = 0; k < K; k++)
{
array[k + j * K + i * K * J] = someValue(i, j, k);
}
}
}
But the k + j * K + i * K * J
part is kinda expensive. Is it possible to tell the compiler to convert the loops into something like this?
array[0] = someValue(0, 0, 0);
array[1] = someValue(0, 0, 1);
array[2] = someValue(0, 0, 2);
array[3] = someValue(0, 1, 0);
...
This would ofcourse make the binaries larger, but would also speed up performance, if this code is executed a lot. Is it possible to do this? Or would I have to generate the code myself and paste it into the source file?