如何告诉 GCC 展开特定循环?我使用了 CUDA SDK,其中可以使用 #pragma unroll 手动展开循环.gcc 有类似的功能吗?我用谷歌搜索了一下,但没有找到任何东西.
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 为您提供了几种不同的处理方式:
GCC gives you a few different ways of handling this:
使用#pragma 指令,例如#pragma GCC optimize ("string"...),如GCC 文档.请注意,编译指示对其余函数进行了全局优化.如果您巧妙地使用了 #pragma push_options 和 pop_options 宏,您可能可以仅围绕这样的一个函数来定义它:
Use #pragma directives, like #pragma GCC optimize ("string"...), as seen in the GCC docs. Note that the pragma makes the optimizations global for the remaining functions. If you used #pragma push_options and pop_options macros cleverly, you could probably define this around just one function like so:
使用 GCC 的属性语法注释单个函数:检查 GCC函数属性 docs 有关该主题的更详细的论文.一个例子:
Annotate individual functions with GCC's attribute syntax: check the GCC function attribute docs 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; } }注意:我不确定 GCC 在展开反向迭代循环方面有多好(我这样做是为了让 Markdown 能够很好地处理我的代码).不过,这些示例应该可以很好地编译.
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 专门展开一个循环
发布评论