我在展開嵌套for
循環時遇到問題。我理解這個概念,我試圖將其付諸實踐,但是我正在編輯我的for
循環中的語句,以適應展開過程,因此絆倒了我。展開嵌套for循環 - C
如果有人能夠向我展示一個有效的展開,並讓我通過它,這將是一個巨大的幫助。
這裏是環部分,我想展開:
for (i=1 ; i < WIDTH-1 ; ++i)
{
for (j = 1 ; j < HEIGHT-1 ; ++j)
{
n = getNeighbors(prv, i, j); /* This is where I'm confused */
mask = (prev[i][j] << 1);
next[i][j] = !(((n >> prev[i][j])^3)^mask);
}
}
UPDATE: 這會是正確的嗎?
for (i=1 ; i < WIDTH-1 ; i+=4)
{
for (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);
}
for (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);
}
for (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);
}
for (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);
}
}
什麼是'prv'?你試圖通過展開循環來實現什麼?你終於想要一個單一的循環或根本沒有循環? –
爲什麼不讓編譯器負責爲您展開循環? –
WIDTH和HEIGHT常量?這些值需要展開。 – QuentinUK