我在 Visual Studio 2008 中工作,在项目设置中我看到“激活扩展指令集”选项,我可以将其设置为无、SSE 或 SSE2
所以编译器会尝试将指令批处理在一起,以便使用 SIMD 指令?
在如何优化代码方面是否有任何规则可以遵循,以便编译器可以使用这些扩展生成高效的汇编程序?
例如,目前我正在研究光线追踪器。着色器接受一些输入并根据输入计算输出颜色,如下所示:
PixelData data = RayTracer::gatherPixelData(pixel.x, pixel.y);
Color col = shadePixel(data);
例如,编写着色器代码以便在一个指令调用中着色 4 个不同的像素是否有益?像这样:
PixelData data1 = RayTracer::gatherPixelData(pixel1.x, pixel1.y);
...
shadePixels(data1, data2, data3, data4, &col1out, &col2out, &col3out, &col4out);
一次处理多个数据单元。这是否有利于使编译器使用 SSE 指令?
谢谢!
最佳答案
i'm working in Visual Studio 2008 and in the project settings I see the option for "activate Extended Instruction set" which I can set to None, SSE or SSE2
So the compiler will try to batch instructions together in order to make use of SIMD instructions?
不,编译器不会自己使用 vector 指令。它将使用标量 SSE 指令而不是 x87 指令。
您所描述的称为“自动矢量化”。 Microsoft 编译器不这样做,Intel compilers做。
在 Microsoft 编译器上,您可以使用 intrinsics执行手动 SSE 优化。
关于c++如何编写编译器可以轻松针对SIMD优化的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4026703/