如何告诉编译器展开这个循环 [英] How to tell the compiler to unroll this loop

查看:534
本文介绍了如何告诉编译器展开这个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的循环,我在ARM处理器上运行。

I have the following loop that I am running on an ARM processor.

// pin here is pointer to some part of an array
for (i = 0; i < v->numelements; i++)
{
    pe   = pptr[i];
    peParent = pe->parent;

    SPHERE  *ps = (SPHERE *)(pe->data);

    pin[0] = FLOAT2FIX(ps->rad2);
    pin[1] = *peParent->procs->pe_intersect == &SphPeIntersect;
    fixifyVector( &pin[2], ps->center ); // Is an inline function

    pin = pin + 5;
}

通过循环的性能下降,我可以判断,编译器无法展开这个循环,因为当我做手工的展开,它变得相当快。我觉得编译器得到由指针混淆。我们可以使用限制关键字来帮助编译器在这里,或限制只保留给函数的参数?一般来说,我们怎么能告诉编译器展开它,不要担心指针。

By the slow performance of the loop, I can judge that the compiler was unable to unroll this loop, as when I manually do the unrolling, it becomes quite fast. I think the compiler is getting confused by the pin pointer. Can we use restrict keyword to help the compiler here, or is restrict only reserved for function parameters? In general how can we tell the compiler to unroll it and don't worry about the pin pointer.

推荐答案

要告诉GCC解开所有循环可以使用优化标志 -funroll-循环

To tell gcc to unroll all loops you can use the optimization flag -funroll-loops.

要解开只有特定的循环,你可以使用:

To unroll only a specific loop you can use:

__attribute__((optimize("unroll-loops")))

看到这个回答了解更多详情。

修改

如果编译器不能确定你需要使用 -funroll-全循环在进入循环迭代的次数。需要注意的是从文档摊开所有循环,即使他们的迭代次数是不确定的进入循环的时候,这通常会使程序运行更慢。

If the compiler cannot determine the number of iterations of the loop upon entry you will need to use -funroll-all-loops. Note that from the documentation: "Unroll all loops, even if their number of iterations is uncertain when the loop is entered. This usually makes programs run more slowly."

这篇关于如何告诉编译器展开这个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆