unrolling nested for loops - C -
i'm having trouble unrolling nested for
loops. understand concept, i'm trying put practice, i'm getting tripped on editing statements within for
loops match unrolling.
if show me efficient unroll , walk me through that'd huge help.
here loop section want unroll:
for (i=1 ; < width-1 ; ++i) { (j = 1 ; j < height-1 ; ++j) { n = getneighbors(prv, i, j); /* i'm confused */ mask = (prev[i][j] << 1); next[i][j] = !(((n >> prev[i][j]) ^ 3) ^ mask); } }
update: correct?
for (i=1 ; < width-1 ; i+=4) { (j = 1 ; j < height-1 ; j+=4) { n = getneighbors(prv, i, j); mask = (prev[i][j] << 1); next[i][j] = !(((n >> prev[i][j]) ^ 3) ^ mask); n = getneighbors(prv, i, j+1); mask = (prev[i][j+1] << 1); next[i][j+1] = !(((n >> prev[i][j+1]) ^ 3) ^ mask); n = getneighbors(prv, i, j+2); mask = (prev[i][j+2] << 1); next[i][j+2] = !(((n >> prev[i][j+2]) ^ 3) ^ mask); n = getneighbors(prv, i, j+3); mask = (prev[i][j+3] << 1); next[i][j+3] = !(((n >> prev[i][j+3]) ^ 3) ^ mask); } (j = 1 ; j < height-1 ; j+=4) { n = getneighbors(prv, i+1, j); mask = (prev[i+1][j] << 1); next[i+1][j] = !(((n >> prev[i+1][j]) ^ 3) ^ mask); n = getneighbors(prv, i+1, j+1); mask = (prev[i+!][j+1] << 1); next[i+1][j+1] = !(((n >> prev[i+1][j+1]) ^ 3) ^ mask); n = getneighbors(prv, i+1, j+2); mask = (prev[i+1][j+2] << 1); next[i+1][j+2] = !(((n >> prev[i+1][j+2]) ^ 3) ^ mask); n = getneighbors(prv, i+1, j+3); mask = (prev[i+1][j+3] << 1); next[i+1][j+3] = !(((n >> prev[i+1][j+3]) ^ 3) ^ mask); } (j = 1 ; j < height-1 ; j+=4) { n = getneighbors(prv, i+2, j); mask = (prev[i+2][j] << 1); next[i+2][j] = !(((n >> prev[i+2][j]) ^ 3) ^ mask); n = getneighbors(prv, i+2, j+1); mask = (prev[i+2][j+1] << 1); next[i+2][j+1] = !(((n >> prev[i+2][j+1]) ^ 3) ^ mask); n = getneighbors(prv, i+2, j+2); mask = (prev[i+2][j+2] << 1); next[i+2][j+2] = !(((n >> prev[i+2][j+2]) ^ 3) ^ mask); n = getneighbors(prv, i+2, j+3); mask = (prev[i+2][j+3] << 1); next[i+2][j+3] = !(((n >> prev[i+2][j+3]) ^ 3) ^ mask); } (j = 1 ; j < height-1 ; j+=4) { n = getneighbors(prv, i+3, j); mask = (prev[i+3][j] << 1); next[i+3][j] = !(((n >> prev[i+3][j]) ^ 3) ^ mask); n = getneighbors(prv, i+3, j+1); mask = (prev[i][j+1] << 1); next[i+3][j+1] = !(((n >> prev[i+3][j+1]) ^ 3) ^ mask); n = getneighbors(prv, i+3, j+2); mask = (prev[i][j+2] << 1); next[i+3][j+2] = !(((n >> prev[i+3][j+2]) ^ 3) ^ mask); n = getneighbors(prv, i+3, j+3); mask = (prev[i+3][j+3] << 1); next[i+3][j+3] = !(((n >> prev[i+3][j+3]) ^ 3) ^ mask); } }
let loop :
for(int = 0; < x; ++i) for(int j = 0; j < y; ++j) dosomething(i, j);
it can unrolled :
for(int = 0; < x; += 4) { for(int j = 0; j < y; j += 4) { dosomething(i, j); dosomething(i, j + 1); dosomething(i, j + 2); dosomething(i, j + 3); } for(int j = 0; j < y; j += 4) { dosomething(i + 1, j); dosomething(i + 1, j + 1); dosomething(i + 1, j + 2); dosomething(i + 1, j + 3); } for(int j = 0; j < y; j += 4) { dosomething(i + 2, j); dosomething(i + 2, j + 1); dosomething(i + 2, j + 2); dosomething(i + 2, j + 3); } for(int j = 0; j < y; j += 4) { dosomething(i + 3, j); dosomething(i + 3, j + 1); dosomething(i + 3, j + 2); dosomething(i + 3, j + 3); } }
not sure how benefit have. should profile code after such unrolling.
Comments
Post a Comment