告诉GCC专门展开循环 [英] Tell gcc to specifically unroll a loop

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

问题描述

我怎么能告诉GCC解开一个特定的循环?
我已经使用CUDA SDK,其中循环可以使用手动解开的#pragma解开。是否有GCC类似的功能?我GOOGLE了一下,但没有找到任何东西。

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优化(串......),因为看到在<一个href=\"http://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas\">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:

#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功能属性文档的关于这个问题的更详细的论文。举个例子:

  • 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有多好是展开反反复循环(我这样做是为了让降价打我的code不错)。这些例子应编译正常,虽然。

    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专门展开循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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