c++ - 如何用 SSE 编译一个特定的类

标签 c++ gcc sse

我有两个类做同样的事情,但一个使用 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/

相关文章:

C++ 指针返回 NULL

c++ - 尾随返回类型供引用

c++ - 编译器选择仿函数而不是同名函数

gcc - 有没有办法让 sublime text 或任何其他编辑器使用 Windows 10 上的 windows linux 子系统上的 gcc 或 g++?

c++ - 自动矢量化不起作用

c++ - 使用纯虚方法在C++中初始化抽象类

ios - 配置脚本和ldid

gcc - GCC -MM 标志的 "system headers"的确切含义

x86 - 8 个打包的 32 位 float 的水平总和

c++ - 如何在 SSE 中使用 imm8?