C语言 告诉 gcc 专门展开一个循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4071690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tell gcc to specifically unroll a loop
提问by Nils
How can I tell GCC to unroll a particular loop?
I have used the CUDA SDK where loops can be unrolled manually using #pragma unroll. Is there a similar feature for gcc? I googled a bit but could not find anything.
如何告诉 GCC 展开特定循环?我使用了 CUDA SDK,其中可以使用#pragma unroll. gcc 有类似的功能吗?我用谷歌搜索了一下,但找不到任何东西。
回答by Philip Conrad
GCC gives you a few different ways of handling this:
GCC 为您提供了几种不同的处理方式:
Use #pragma directives, like
#pragma GCC optimize ("string"...), as seen in the GCC docs. Note that the pragma makes the optimizations globalfor the remaining functions. If you used#pragma push_optionsandpop_optionsmacros cleverly, you could probably define this around just one function like so:#pragma GCC push_options #pragma GCC optimize ("unroll-loops") //add 5 to each element of the int array. void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } } #pragma GCC pop_optionsAnnotate individual functions with GCC's attribute syntax: check the GCC function attributedocs for a more detailed dissertation on the subject. An example:
//add 5 to each element of the int array. __attribute__((optimize("unroll-loops"))) void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } }
使用#pragma 指令,如GCC 文档中
#pragma GCC optimize ("string"...)所见。请注意,编译指示使其余函数的优化全局化。如果您巧妙地使用和宏,您可能可以仅围绕一个函数定义它,如下所示:#pragma push_optionspop_options#pragma GCC push_options #pragma GCC optimize ("unroll-loops") //add 5 to each element of the int array. void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } } #pragma GCC pop_options使用GCC 的属性语法注释单个函数:查看GCC 函数属性文档以获得关于该主题的更详细的论文。一个例子:
//add 5 to each element of the int array. __attribute__((optimize("unroll-loops"))) void add5(int a[20]) { int i = 19; for(; i > 0; i--) { a[i] += 5; } }
Note:I'm not sure how good GCC is at unrolling reverse-iterated loops (I did it to get Markdown to play nice with my code). The examples should compile fine, though.
注意:我不确定 GCC 在展开反向迭代循环方面有多好(我这样做是为了让 Markdown 能够很好地处理我的代码)。不过,这些示例应该可以很好地编译。
回答by Frederik Deweerdt
GCC 8 has gained a new pragma that allows you to control how loop unrolling is done:
GCC 8 获得了一个新的编译指示,允许您控制循环展开的方式:
#pragma GCC unroll n
#pragma GCC unroll n
Quoting from the manual:
引用手册:
You can use this pragma to control how many times a loop should be unrolled. It must be placed immediately before a for, while or do loop or a #pragma GCC ivdep, and applies only to the loop that follows. n is an integer constant expression specifying the unrolling factor. The values of 0 and 1 block any unrolling of the loop.
您可以使用此编译指示来控制应该展开循环的次数。它必须紧接在 for、while 或 do 循环或 #pragma GCC ivdep 之前,并且仅适用于随后的循环。n 是指定展开因子的整数常量表达式。0 和 1 的值会阻止循环的任何展开。
回答by Jerry Coffin
-funroll-loopsmight be helpful (though it turns on loop-unrolling globally, not per-loop). I'm not sure whether there's a #pragmato do the same...
-funroll-loops可能会有所帮助(尽管它会全局打开循环展开,而不是每个循环)。我不确定是否有#pragma同样的事情...

