英文:
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 <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;
}
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论