Using SSE instructions with gcc without inline assembly

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


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