[英]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.我有兴趣在 gcc 中使用 x86-64 的 SSE 向量指令,并且不想为此使用任何内联汇编。 Is there a way I can do that in C?
有没有办法在 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).是的,您可以使用 *mmintrin.h 标头中的 内在函数(
emmintrin.h
、 xmmintrin.h
等,具体取决于您要使用的 SSE 级别)。 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, eg gcc, clang and Intel's ICC on Linux/Mac OS X/Windows and even Microsoft's Visual C/C++ (Windows only, of course).请注意,这种方法适用于各种平台上的大多数 x86 和 x86-64 编译器,例如 Linux/Mac OS X/Windows 上的 gcc、clang 和 Intel 的 ICC,甚至是 Microsoft 的 Visual C/C++(当然仅适用于 Windows)。
Find the *intrin.h
headers in your gcc includes ( /usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/include/
here).在 gcc 中找到
*intrin.h
头文件(这里是/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/include/
)。
Maybe noteworthy, the header immintrin.h
includes all other intrins according to the features you allow (using -msse2
or -mavx
for instance).也许值得注意的是,头文件
immintrin.h
根据您允许的功能(例如使用-msse2
或-mavx
)包括所有其他-msse2
。
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. Paul R 和 hroptatyr 描述了在哪里可以找到 GCC 的文档。 Microsoft also has good documentation on the intrinsics in their compiler ;
Microsoft 在其编译器中也有关于内在函数的良好文档; even if you are using GCC, you might find MS' description of the idea a better tutorial.
即使您正在使用 GCC,您也可能会发现 MS 对这个想法的描述是一个更好的教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.