12
votes

I am interested in using the SSE vector instructions of x86-64 with gcc and don't want to use any inline assembly for that. Is there a way I can do that in C? If so, can someone give me an example?

3

3 Answers

18
votes

Yes, you can use the intrinsics in the *mmintrin.h headers (emmintrin.h, xmmintrin.h, etc, depending on what level of SSE you want to use). This is generally preferable to using assembler for many reasons.

#include <emmintrin.h>

int main(void)
{
    __m128i a = _mm_set_epi32(4, 3, 2, 1);
    __m128i b = _mm_set_epi32(7, 6, 5, 4);
    __m128i c = _mm_add_epi32(a, b);

    // ...
    
    return 0;
}

Note that this approach works for most x86 and x86-64 compilers on various platforms, e.g. gcc, clang and Intel's ICC on Linux/Mac OS X/Windows and even Microsoft's Visual C/C++ (Windows only, of course).

6
votes

Find the *intrin.h headers in your gcc includes (/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/include/ here).

Maybe noteworthy, the header immintrin.h includes all other intrins according to the features you allow (using -msse2 or -mavx for instance).

5
votes

What you want are intrinsics, which look like library functions but are actually built into the compiler so they translate into specific machine code.

Paul R and hroptatyr describe where to find GCC's documentation. Microsoft also has good documentation on the intrinsics in their compiler; even if you are using GCC, you might find MS' description of the idea a better tutorial.