我有两个类做同样的事情,但一个使用 SSE4.2 而另一个不使用。我已经在检测代码是否在支持 SSE4.2 并使用对应类的 CPU 上运行,但我正在努力编译 SSE4.2 类。
我希望编译器只对这个类使用 SSE4.2 优化,而不对其余代码使用,所以我不能使用 -msse4.2
。
我阅读了#pragma GCC target("sse4.2")
,但我仍然在包含的 SSE4.2-Header 中遇到编译错误:
nmmintrin.h:31:3: error: #error "SSE4.2 instruction set not enabled"
如何在启用 SSE4.2 优化并禁用其余代码的情况下编译此类?
我正在使用 GCC 4.8 和 Android NDK 10d。
我的类(class)是这样的:
#include "MyClassWithSSE42.h"
#pragma GCC target("sse4.2")
#include <nmmintrin.h>
uint32_t MyClassWithSSE42::CRC32byte(const uint32_t *p, const uint32_t startValue)
{
uint32_t c = _mm_crc32_u32(startValue, p[0]);
c = _mm_crc32_u32(c, p[1]);
c = _mm_crc32_u32(c, p[2]);
c = _mm_crc32_u32(c, p[3]);
c = _mm_crc32_u32(c, p[4]);
c = _mm_crc32_u32(c, p[5]);
c = _mm_crc32_u32(c, p[6]);
return _mm_crc32_u32(c, p[7]);
}
最佳答案
我不知道 Android 工具链,但在桌面上我会在一个单独的目标文件中编译该类,并将其与其余代码链接。
g++ -msse4.2 -c MyClassWithSSE42.c++ -o MyClassWithSSE42.o # Compile only
g++ your_other_files.c++ MyClassWithSSE42.o # Compile and link
关于c++ - 如何用 SSE 编译一个特定的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106136/