如何让gccgo生成矢量化代码?

huangapple go评论87阅读模式
英文:

how is it possible to get gccgo produce vectorized code?

问题

我正在尝试说服gccgo对以下代码片段进行向量化,但没有成功:

package foo

func Sum(v []float32) float32 {
    var sum float32 = 0
    for _, x := range v {
        sum += x
    }
    return sum
}

我正在验证生成的汇编代码:

$ gccgo -O3 -ffast-math -march=native -S test.go

gccgo版本是:

$ gccgo --version
gccgo (Ubuntu 4.9-20140406-0ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]

难道gccgo不应该能够对这段代码进行向量化吗?使用相同的gcc选项的等效C代码可以完美地使用AVX指令进行向量化...

更新

这里是相应的C示例:

#include <stdlib.h>

float sum(float *v, size_t n) {
    size_t i;
    float sum = 0;
    for(i = 0; i < n; i++) {
        sum += v[i];
    }
    return sum;
}

使用以下命令进行编译:

$ gcc -O3 -ffast-math -march=native -S test.c
英文:

I'm trying to convince gccgo without success to vectorize the following snippet:

package foo

func Sum(v []float32) float32 {
    var sum float32 = 0
    for _, x := range v {
        sum += x
    }
    return sum
} 

I'm verifying the assembly generated by:

$ gccgo -O3 -ffast-math -march=native -S test.go

gccgo version is:

$ gccgo --version
gccgo (Ubuntu 4.9-20140406-0ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]

Isn't gccgo supposed to be able to vectorize this code? the equivalent C code
with the same gcc options is perfectly vectorized with AVX instructions...

UPDATE

here you have the corresponding C example:

#include &lt;stdlib.h&gt;

float sum(float *v, size_t n) {
    size_t i;
    float sum = 0;
    for(i = 0; i &lt; n; i++) {
        sum += v[i];
    }
    return sum;
}

compile with:

$ gcc -O3 -ffast-math -march=native -S test.c

答案1

得分: 1

为什么不直接使用gcc构建.so或.a文件,并从Go中调用C函数呢?

英文:

Why not just build the .so or .a with gcc and call the c function from go?

huangapple
  • 本文由 发表于 2014年5月10日 01:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/23570235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定