Created
January 17, 2022 17:17
-
-
Save aprell/677c77791d5e1da3b1f76c518f40665b to your computer and use it in GitHub Desktop.
Linearization of array subscripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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