我有一个图像绘制例程,它是为SSE,SSE2,SSE3,SSE4.1,SSE4.2,AVX和AVX2多次编译的。我的程序通过检查CPUID标志动态地分发这些二进制变体之一。
在Windows上,如果操作系统不支持AVX/AVX 2,我将检查Windows版本并禁用AVX/AVX 2调度。(例如,只有Windows7 SP1或更高版本支持AVX/AVX 2。)
我想在Mac上做同样的事情,但我不确定OS的哪个版本支持AVX/AVX 2。
请注意,我想知道的是用于AVX/AVX 2的OS的最低版本。不是机器模型,可以AVX/AVX 2。
发布于 2016-07-13 07:38:22
为了检测指令集特性,我引用了两个源文件:
这两个文件都将告诉您如何通过AVX2以及XOP、FMA3、FMA4来检测SSE,如果操作系统支持AVX和其他功能的话。
我已经习惯了Agner的代码( MSVC、GCC、Clang、ICC的一个源文件),所以让我们先看一下。
下面是instrset_detect.cpp中用于检测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内联程序集。
下面是神秘detect_OS_AVX()的cpu_x86.cpp,用于检测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;
}神秘的显然是这个答案提出的这个解决方案。
请注意,这两个源文件所做的工作基本上是相同的:检查OSXSAVE位27,从CPUID检查AVX位28,检查xgetbv的结果。
发布于 2016-07-12 03:01:20
对于AVX来说,答案很简单:
您至少需要OS X 10.6.7
请注意,只有构建10J3250和10J4138才能支持它。
对于AVX2,为10.8.4构建12E3067或12E4022
https://stackoverflow.com/questions/38319245
复制相似问题