Skip to content

Instantly share code, notes, and snippets.

@aprell
Created January 17, 2022 17:17
Show Gist options
  • Save aprell/677c77791d5e1da3b1f76c518f40665b to your computer and use it in GitHub Desktop.
Save aprell/677c77791d5e1da3b1f76c518f40665b to your computer and use it in GitHub Desktop.
Linearization of array subscripts
#include <stdio.h>
void standard(int A, int B, int C, int array[A][B][C]) {
for (int i = 0; i < A; i++) {
for (int j = 0; j < B; j++) {
for (int k = 0; k < C; k++) {
printf("%d\n", array[i][j][k]);
}
}
}
}
void linearized(int A, int B, int C, int array[]) {
for (int i = 0; i < A; i++) {
for (int j = 0; j < B; j++) {
for (int k = 0; k < C; k++) {
printf("%d\n", array[i * B * C + j * C + k]);
}
}
}
}
// See loop collapsing
void collapsed(int A, int B, int C, int array[]) {
for (int n = 0; n < A * B * C; n++) {
int i = (n / (B * C)) % A;
int j = (n / C) % B;
int k = (n / 1) % C;
printf("%d\n", array[i * B * C + j * C + k]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment