在非Haswell处理器上禁用AVX2功能 [英] Disable AVX2 functions on non-Haswell processors

查看:186
本文介绍了在非Haswell处理器上禁用AVX2功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些AVX2代码在Haswell i7处理器上运行。在非Haswell处理器上也使用相同的代码库,其中相同的代码应该用它们的SSE等同替换。我想知道有没有一种方式编译器忽略非Haswell处理器上的AVX2指令。我需要类似:

I have written some AVX2 code to run on a Haswell i7 processor. The same codebase is also used on non-Haswell processors, where the same code should be replaced with their SSE equivalents. I was wondering is there a way for the compiler to ignore AVX2 instructions on non-Haswell processors. I need something like:

public void useSSEorAVX(...){
    IF (compiler directive detected AVX2)
        AVX2 code (this part is ready)
    ELSE
        SSE code  (this part is also ready)
    }
}

现在我在编译之前注释掉相关代码,但必须有一些更有效的方法来做到这一点。我使用Ubuntu和gcc。感谢您的帮助。

Right now I am commenting out related code before compiling but there must be some more efficient way to do this. I am using Ubuntu and gcc. Thanks for your help.

推荐答案

如果您只想在编译时执行此操作,可以这样做:

If you just want to do this at compile-time then you can do this:

#ifdef __AVX2__
    // AVX2 code
#elif __SSE__
    // SSE code
#else
    // scalar code
#endif

请注意,当使用 gcc -mavx2 ... 编译时, __ AVX2 __ 会自动定义。类似地,对于 __ SSE __ 。 (注意,你可以检查你的编译器预定义的任何给定的命令行切换使用斜体 gcc -dM -E -mavx2 - < / dev / null 。)

Note that when you compile with gcc -mavx2 ... then __AVX2__ gets defined automatically. Similarly for __SSE__. (Note also that you can check what's pre-defined by your compiler for any given command line switching using the incantation gcc -dM -E -mavx2 - < /dev/null.)

如果你想做运行时调度,那么这会更复杂一些。

If you want to do run-time dispatching though then that's a little more complicated.

这篇关于在非Haswell处理器上禁用AVX2功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆