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