gpt4 book ai didi

c - 展开嵌套的 for 循环 - C

转载 作者:行者123 更新时间:2023-11-30 17:13:54 25 4
gpt4 key购买 nike

我在展开嵌套 for 循环时遇到问题。我理解这个概念,我正在尝试将其付诸实践,但我在编辑 for 循环中的语句以匹配展开时遇到了麻烦。

如果有人可以向我展示有效的展开方式并引导我完成它,那将是一个巨大的帮助。

这是我想要展开的循环部分:

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);
}
}

更新:这是正确的吗?

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);
}
}

最佳答案

设循环为:

for(int i = 0; i < x; ++i)
for(int j = 0; j < y; ++j)
dosomething(i, j);

它可以展开为:

for(int i = 0; i < x; i += 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);
}
}

不确定这会带来多少好处。您应该在展开后分析您的代码。

关于c - 展开嵌套的 for 循环 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com