与AVX/AVX2一起使用的OS X的最低版本是什么? [英] What is the minimum version of OS X for use with AVX/AVX2?

查看:146
本文介绍了与AVX/AVX2一起使用的OS X的最低版本是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像绘制例程,该例程针对SSE,SSE2,SSE3,SSE4.1,SSE4.2,AVX和AVX2进行了多次编译. 我的程序通过检查CPUID标志来动态调度这些二进制变体之一.

I have an image drawing routine which is compiled multiple times for SSE, SSE2, SSE3, SSE4.1, SSE4.2, AVX and AVX2. My program dynamically dispatches one of these binary variations by checking CPUID flags.

在Windows上,我检查Windows的版本,如果操作系统不支持AVX/AVX2,则禁用它. (例如,仅Windows 7 SP1或更高版本支持AVX/AVX2.)

On Windows, I check the version of Windows and disable AVX/AVX2 dispatch if the OS doesn't support them. (For example, only Windows 7 SP1 or later supports AVX/AVX2.)

我想在Mac OS X上做同样的事情,但是我不确定哪个版本的OS X支持AVX/AVX2.

I want to do the same thing on Mac OS X, but I'm not sure what version of OS X supports AVX/AVX2.

请注意,我想知道的是与AVX/AVX2一起使用的OS X的最低版本.不是支持AVX/AVX2的机器型号.

Note that what I want to know is the minimum version of OS X for use with AVX/AVX2. Not machine models which are capable of AVX/AVX2.

推荐答案

为检测指令集功能,我引用了两个源文件:

For detecting instruction set features there are two source files I reference:

  1. Mysticial的 cpu_x86.cpp
  2. Agner Fog的 instrset_detect.cpp
  1. Mysticial's cpu_x86.cpp
  2. Agner Fog's instrset_detect.cpp

如果您的操作系统支持AVX和其他功能,这两个文件都将告诉您如何通过AVX2以及XOP,FMA3,FMA4检测SSE.

Both of these files will tell you how to detect SSE through AVX2 as well as XOP, FMA3, FMA4, if your OS supports AVX and other features.

我已经习惯了Agner的代码(MSVC,GCC,Clang和ICC的一个源文件),所以让我们先来看一下.

I am used to Agner's code (one source file for MSVC, GCC, Clang, ICC) so let's look at that first.

以下是instrset_detect.cpp中用于检测AVX的相关代码片段:

Here are the relevant code fragments from instrset_detect.cpp for detecting AVX:

iset = 0;                                              // default value
int abcd[4] = {0,0,0,0};                               // cpuid results
cpuid(abcd, 0);                                        // call cpuid function 0
//....
iset = 6;                                              // 6: SSE4.2 supported
if ((abcd[2] & (1 << 27)) == 0) return iset;           // no OSXSAVE
if ((xgetbv(0) & 6) != 6)       return iset;           // AVX not enabled in O.S.
if ((abcd[2] & (1 << 28)) == 0) return iset;           // no AVX
iset = 7;                                              // 7: AVX supported

,其中xgetbv定义为

// Define interface to xgetbv instruction
static inline int64_t xgetbv (int ctr) {    
#if (defined (_MSC_FULL_VER) && _MSC_FULL_VER >= 160040000) || (defined (__INTEL_COMPILER) && __INTEL_COMPILER >= 1200) // Microsoft or Intel compiler supporting _xgetbv intrinsic

    return _xgetbv(ctr);                                   // intrinsic function for XGETBV

#elif defined(__GNUC__)                                    // use inline assembly, Gnu/AT&T syntax

   uint32_t a, d;
   __asm("xgetbv" : "=a"(a),"=d"(d) : "c"(ctr) : );
   return a | (uint64_t(d) << 32);

#else  // #elif defined (_WIN32)                           // other compiler. try inline assembly with masm/intel/MS syntax

  //see the source file
}

我没有包含cpuid函数(请参见源代码),我从xgetbv中删除了非GCC内联汇编,以简化答案.

I did not include the cpuid function (see the source code) and I removed the non GCC inline assembly from xgetbv to make the answer shorter.

这是Mysticial的cpu_x86.cpp中的detect_OS_AVX(),用于检测AVX:

Here is the detect_OS_AVX() from Mysticial's cpu_x86.cpp for detecting AVX:

bool cpu_x86::detect_OS_AVX(){
    //  Copied from: http://stackoverflow.com/a/22521619/922184

    bool avxSupported = false;

    int cpuInfo[4];
    cpuid(cpuInfo, 1);

    bool osUsesXSAVE_XRSTORE = (cpuInfo[2] & (1 << 27)) != 0;
    bool cpuAVXSuport = (cpuInfo[2] & (1 << 28)) != 0;

    if (osUsesXSAVE_XRSTORE && cpuAVXSuport)
    {
        uint64_t xcrFeatureMask = xgetbv(_XCR_XFEATURE_ENABLED_MASK);
        avxSupported = (xcrFeatureMask & 0x6) == 0x6;
    }

    return avxSupported;
}

Mystical显然是从

Mystical apparently came up with this solution from this answer.

请注意,两个源文件基本上都具有相同的作用:检查OSXSAVE位27,从CPUID检查AVX位28,从xgetbv检查结果.

Notice that both source files do basically the same thing: check the OSXSAVE bit 27, check the AVX bit 28 from CPUID, check a result from xgetbv.

这篇关于与AVX/AVX2一起使用的OS X的最低版本是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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