GCC -msse2 不会生成 SIMD 代码
GCC -msse2 does not generate SIMD code
我试图弄清楚为什么 g++ 不生成 SIMD 代码。
信息GCC/OS/CPU:
$ gcc -v
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
$ cat /proc/cpuinfo
...
model name : Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
...
这是我的C++代码:
#include <iostream>
#include <cstdlib>
//function that fills an array with random numbers
template<class T>
void fillArray(T *array, int n){
srand(1);
for (int i = 0; i < n; i++) {
array[i] = (float) (rand() % 10);
}
}
// function that computes the dotprod of two vectors (loop unrolled)
float dotCPP(float *src1, float *src2, int n){
float dest = 0;
for (int i = 0; i < n; i+=2) {
dest += (src1[i] * src2[i]) + (src1[i+1] * src2[i+1]);
}
return dest;
}
int main(int argc, char *argv[])
{
const int n = 1200000;
float *a = new float[n]; //allocate data on the heap
float something_else; //store result
fillArray<float>(a,n); //function that fills the array with random numbers
something_else = dotCPP(a, a, n); //call function and store return value
return 0;
}
我编译代码:
makefile:
CXX = g++
CXXFLGS = -g -Wall -std=c++11 -msse2 -O3
SRC = main.o dot.o
EXEC = dot
$(EXEC): $(SRC)
$(CXX) $(CXXFLGS) $(SRC) -o $(EXEC)
main.o: dot.cpp
$(CXX) $(CXXFLGS) -c dot.cpp -o main.o
并使用 gdb 检查生成的代码:
$gdb dot
...
(gdb) b dotCPP
(gdb) r
...
(gdb) disass
Dump of assembler code for function dotCPP(float*, float*, int):
=> 0x08048950 <+0>: push %ebx
0x08048951 <+1>: mov 0x10(%esp),%ebx
0x08048955 <+5>: mov 0x8(%esp),%edx
0x08048959 <+9>: mov 0xc(%esp),%ecx
0x0804895d <+13>: test %ebx,%ebx
0x0804895f <+15>: jle 0x8048983 <dotCPP(float*, float*, int)+51>
0x08048961 <+17>: xor %eax,%eax
0x08048963 <+19>: fldz
0x08048965 <+21>: lea 0x0(%esi),%esi
0x08048968 <+24>: flds (%edx,%eax,4)
0x0804896b <+27>: fmuls (%ecx,%eax,4)
0x0804896e <+30>: flds 0x4(%edx,%eax,4)
0x08048972 <+34>: fmuls 0x4(%ecx,%eax,4)
0x08048976 <+38>: add $0x2,%eax
0x08048979 <+41>: cmp %eax,%ebx
0x0804897b <+43>: faddp %st,%st(1)
0x0804897d <+45>: faddp %st,%st(1)
0x0804897f <+47>: jg 0x8048968 <dotCPP(float*, float*, int)+24>
0x08048981 <+49>: pop %ebx
0x08048982 <+50>: ret
0x08048983 <+51>: fldz
0x08048985 <+53>: pop %ebx
0x08048986 <+54>: ret
End of assembler dump.
现在我错过了什么还是 gcc 应该使用 xmm 寄存器?
我非常感谢任何有助于我理解为什么 gcc 不生成使用 xmm 寄存器的代码的建议。
如果您需要更多信息,请告诉我。
-march=core2
意味着gcc可以假设(以及64位ISA)最多SSSE3(例如MMX,SSE,SSE2,SSE3)可用。
然后,-mfpmath=sse
可以强制使用 SSE 进行浮点运算(64 位模式下的默认值),而不是 387(32 位-m32
模式下的默认值)。
请参阅手册页中的"英特尔 386 和 AMD x86-64 选项"部分。
不幸的是,您仍然有 32 位模式和 32 位 ABI 的限制。 例如,只有寄存器XMM0 .. XMM7
可用; XMM8 .. XMM15
仅在 64 位模式下可用。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何抽象模板函数中使用的 simd 代码
- 如何在没有性能命中的情况下抽象SIMD代码来处理不同的数据类型
- 手动SIMD代码可负担性
- 在使用英特尔内部函数对 SIMD 代码进行编程时,如何强制使用 vmovapd 而不是 vmovupd?
- SIMD/SSE代码在Windows 8上清楚地运行在Windows 7和Linux上
- 使用 SIMD 管理累积(单个)值的清理代码循环的方法是什么
- GCC -msse2 不会生成 SIMD 代码
- 如何使用 gcc 编译 SIMD 代码
- 为什么我的散点代码的性能比Vc SIMD更好