Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SSE instructions with gcc without inline assembly

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?

like image 941
pythonic Avatar asked Apr 25 '12 06:04

pythonic


3 Answers

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

like image 127
hroptatyr Avatar answered Oct 15 '22 13:10

hroptatyr


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

like image 18
Paul R Avatar answered Oct 15 '22 12:10

Paul R


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.

like image 5
Crashworks Avatar answered Oct 15 '22 12:10

Crashworks