用于 GNU C++ 的 SSE SSE2 和 SSE3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/661338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:33:49  来源:igfitidea点击:

SSE SSE2 and SSE3 for GNU C++

c++coptimization

提问by yoitsfrancis

Is there a simple tutorial for me to get up to speed in SSE, SSE2 and SSE3 in GNU C++? How can you do code optimization in SSE?

是否有一个简单的教程可以让我快速掌握 GNU C++ 中的 SSE、SSE2 和 SSE3?你如何在SSE中进行代码优化?

回答by timday

Sorry don't know of a tutorial.

抱歉不知道教程。

Your best bet (IMHO) is to use SSE via the "intrinsic" functions Intel provides to wrap (generally) single SSE instructions. These are made available via a set of include files named *mmintrin.h e.g xmmintrin.h is the original SSE instruction set.

您最好的选择(恕我直言)是通过英特尔提供的“内在”函数使用 SSE 来包装(通常)单个 SSE 指令。这些是通过一组名为 *mmintrin.h 的包含文件提供的,例如 xmmintrin.h 是原始的 SSE 指令集。

Begin familiar with the contents of Intel's Optimization Reference Manualis a good idea (see section 4.3.1.2 for an example of intrinsics) and the SIMD sections are essential reading. The instruction set reference manuals are pretty helpful too, in that each instruction's documentation includes the "intrinsic" function it corresponds to.

开始熟悉 Intel 的优化参考手册的内容是个好主意(有关内在函数的示例,请参阅第 4.3.1.2 节)并且 SIMD 部分是必不可少的阅读。指令集参考手册也非常有用,因为每个指令的文档都包含它对应的“内在”功能。

Dospend some time inspecting the assembler produced by the compiler from intrinsics (you'll learn a lot) and on profiling/performance measurement (you'll avoid wasting time SSE-ing code for little return on the effort).

花一些时间检查由内部函数编译器产生的汇编(你会学到很多东西)和剖析/性能测试(你会避免浪费时间SSE-ING代码上的功夫很少的回报)。

Update 2011-05-31:There is some very nice coverage of intrinsics and vectorization in Agner Fog's optimization PDFs(thanks) although it's a bit spread about (e.g section 12 of the first oneand section 5 of the second one). These aren't exactly tutorial material (in fact there's a "these manuals are not for beginners" warning) but they do rightly treat SIMD (whether used via asm, intrinsics or compiler vectorization) as just one part of the larger optimization toolbox.

2011 年 5 月 31 日更新:Agner Fog 的优化 PDF 中有一些非常好的内在函数和矢量化覆盖(谢谢),尽管它有点分散(例如第一个的第 12节和第二个的第 5 节)。这些并不完全是教程材料(实际上有“这些手册不适合初学者”警告),但它们确实正确地将 SIMD(无论是通过 asm、内在函数还是编译器矢量化使用)视为更大优化工具箱的一部分。

Update 2012-10-04:A nice little Linux Journal articleon gcc vector intrinsics deserves a mention here. More general than just SSE (covers PPC and ARM extensions too). There's a good collection of references on the last page, which drew my attention to Intel's "intrinsics manual".

更新2012年10月4日:一个可爱的小Linux杂志文章海合会矢量内在这里值得一提。比 SSE 更通用(也涵盖 PPC 和 ARM 扩展)。最后一页有很多参考资料,这让我注意到了英特尔的“内在手册”

回答by Sebastian Mach

The most simple optimization to use is to allow gcc to emit SSE code.

最简单的优化是允许 gcc 发出 SSE 代码。

Flags: -msse, -msse2, -msse3, -march=, -mfpmath=sse

标志:-msse、-msse2、-msse3、-march=、-mfpmath=sse

For a more concise list about 386 options, see http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/i386-and-x86_002d64-Options.html#i386-and-x86_002d64-Options, more exact documentation for your specific compiler version is there: http://gcc.gnu.org/onlinedocs/.

有关 386 个选项的更简洁列表,请参阅http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/i386-and-x86_002d64-Options.html#i386-and-x86_002d64-Options,更准确您的特定编译器版本的文档在那里:http: //gcc.gnu.org/onlinedocs/

For optimization, always check out Agner Fog's: http://agner.org/optimize/. I think he doesn't have SSE tutorials for intrinsics, but he has some really neat std-c++ tricks and also provides lots of information about coding SSE assembly (which can often be transcribed to intrinsics).

对于优化,请始终查看 Agner Fog 的:http://agner.org/optimize/ 。我认为他没有关于内在函数的 SSE 教程,但他有一些非常简洁的 std-c++ 技巧,并且还提供了许多关于编码 SSE 程序集的信息(通常可以转录为内在函数)。

回答by DevSolar

Check out the -mtune and -march options, -msse*, and -mfpmath of course. All of those enable GCC to do SSE-specific optimizations.

当然,请查看 -mtune 和 -march 选项、-msse* 和 -mfpmath。所有这些都使 GCC 能够进行特定于 SSE 的优化。

Anything beyond that is the realm of Assembler, I am afraid.

恐怕任何超出此范围的都是汇编程序的领域。

GCC Online Manual - i386 and x86_64 Options

GCC 在线手册 - i386 和 x86_64 选项

回答by Kornel

MSDN has pretty good description of SSE compiler built-ins (and those built-ins are de-facto standard, they even work in clang/XCode).

MSDN 对 SSE 编译器内置程序有很好的描述(这些内置程序是事实上的标准,它们甚至可以在 clang/XCode 中工作)。

The nice thing about that reference is that it shows equivalent pseudocode, so e.g. you can learn that ADDPDinstruction is:

该引用的好处在于它显示了等效的伪代码,因此例如您可以了解到ADDPD指令是:

r0 := a0 + b0
r1 := a1 + b1

And here's good description of a cryptic shuffle instruction: http://www.songho.ca/misc/sse/sse.html

这是对神秘的随机播放指令的很好的描述:http: //www.songho.ca/misc/sse/sse.html

回答by Zan Lynx

A simple tutorial? Not that I know of.

一个简单的教程?从来没听说过。

But any information about using MMX or any version of SSE will be useful for learning, whether for GCC or for ICC or VC.

但是任何关于使用 MMX 或任何版本的 SSE 的信息都对学习有用,无论是 GCC 还是 ICC 或 VC。

To learn about GCC's vector extensions, type "info gcc" and go to Node: Vector Extensions.

要了解 GCC 的矢量扩展,请输入“info gcc”并转到节点:矢量扩展。