c++如何编写编译器可以轻松针对SIMD优化的代码?

标签 c++ compiler-construction simd

我在 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/

相关文章:

compiler-construction - 路由算法

c++ - 如何使用我自己的库 C++ ubuntu

c++ - 将 key 和 iv 保存到文件 AES 实现 Crypto++

c++ - 在 C++ 中动态分配一个字符串数组

c# - C# 中的运算符 '=' 链接 - 这个测试肯定会通过吗?

c++ - "C++ most vexing parse"的运行时行为

C# 向量化数组加法

c - 如何将 __m128d simd vector 的内容存储为 double 而不将其作为 union 访问?

c++ - 如何从原始数据 C++ 在 X11 中创建游标

c++ - 有效地矢量化图像 block 处理?