SSE SSE2 and SSE3 for GNU C++

C++OptimizationSimdSseSse2

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Begin familiar with the contents of Intel's Optimization Reference Manual is 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.

Do spend 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).

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 one and 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.

Update 2012-10-04: A nice little Linux Journal article on 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 guide".

Solution 2 - C++

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

Flags: -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/>;.

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).

Solution 3 - C++

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

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

GCC Online Manual - i386 and x86_64 Options

Solution 4 - C++

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

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

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

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

Solution 5 - C++

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyoitsfrancisView Question on Stackoverflow
Solution 1 - C++timdayView Answer on Stackoverflow
Solution 2 - C++Sebastian MachView Answer on Stackoverflow
Solution 3 - C++DevSolarView Answer on Stackoverflow
Solution 4 - C++KornelView Answer on Stackoverflow
Solution 5 - C++Zan LynxView Answer on Stackoverflow