如何检查CPU是否支持SSE3指令集?
以下代码是否有效用于检查 CPU 是否支持 SSE3 指令集?
使用 IsProcessorFeaturePresent( )
功能显然在 Windows XP 上不起作用。
bool CheckSSE3()
{
int CPUInfo[4] = {-1};
//-- Get number of valid info ids
__cpuid(CPUInfo, 0);
int nIds = CPUInfo[0];
//-- Get info for id "1"
if (nIds >= 1)
{
__cpuid(CPUInfo, 1);
bool bSSE3NewInstructions = (CPUInfo[2] & 0x1) || false;
return bSSE3NewInstructions;
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我创建了一个 GitHub 重现,它将检测所有主要 x86 ISA 扩展的 CPU 和操作系统支持:https://github .com/Mysticial/FeatureDetector
这是一个较短的版本:
首先您需要访问CPUID指令:
然后您可以运行以下代码:
注意,这仅检测CPU是否支持该指令。要实际运行它们,您还需要操作系统支持。
具体来说,需要操作系统支持:
ymm
寄存器的指令。有关如何检测此问题的信息,请参阅 Andy Lutomirski 的回答。zmm
和掩码寄存器的指令。检测操作系统对 AVX512 的支持与 AVX 相同,但使用标志0xe6
而不是0x6
。I've created a GitHub repro that will detect CPU and OS support for all the major x86 ISA extensions: https://github.com/Mysticial/FeatureDetector
Here's a shorter version:
First you need to access the CPUID instruction:
Then you can run the following code:
Note that this only detects whether the CPU supports the instructions. To actually run them, you also need to have operating system support.
Specifically, operating system support is required for:
ymm
registers. See Andy Lutomirski's answer for how to detect this.zmm
and mask registers. Detecting OS support for AVX512 is the same as with AVX, but using the flag0xe6
instead of0x6
.Mysticial 的答案有点危险——它解释了如何检测 CPU 支持而不是操作系统支持。您需要使用_xgetbv来检查操作系统是否启用了所需的CPU扩展状态。请参阅此处了解其他来源。 甚至 gcc 也犯了同样的错误。 代码的主要内容是:
Mysticial's answer is a bit dangerous -- it explains how to detect CPU support but not OS support. You need to use _xgetbv to check whether the OS has enabled the required CPU extended state. See here for another source. Even gcc has made the same mistake. The meat of the code is:
经过一番谷歌搜索后,我还找到了英特尔的解决方案:
链接:https://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th- Generation-intel-core-processor-family< /a>
另请注意,GCC 有一些您可以使用的特殊内在函数(请参阅:https://gcc.gnu.org/onlinedocs /gcc-4.9.2/gcc/X86-Built-in-Functions.html ):
如果将其与上面的信息放在一起,一切都会解决 美好的。
After quite a bit of googling, I also found the solutions from Intel:
Link: https://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family
Also note that GCC has some special intrinsics that you can use (see: https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/X86-Built-in-Functions.html ):
If you put this together with the information above, it'll all work out fine.
添加到 Abhiroop 的答案:
在 Linux 上,您可以运行此 shell 命令来查找您的 CPU 支持的功能
cat /proc/cpuinfo | grep 标志 | uniq
在我的机器上打印
To add to Abhiroop's answer:
On linux, you can run this shell command to find out the features supported by your CPU
cat /proc/cpuinfo | grep flags | uniq
On my machine this prints
在 Mac OS 上,这是有效的:
在我的机器上,它输出以下内容:
正如您所看到的以粗体书写的指令,支持 SSE3 和许多其他 SIMD 指令。
On a Mac OS this works:
In my machine it outputs this:
As you can see with the instructions written in bold, SSE3 and bunch of other SIMD instructions are supported.
在 linux 或 wsl2 上,来自 util-linux 存储库的 lscpu 命令也可以完成这项工作。
例如:
Alternativley on linux or wsl2 the
lscpu
command from the util-linux repository will do the job.E.g:
或“POSIX 方式”:
结果:
Or the "POSIX way":
Results: