如何在Visual C ++中检测rdtscp支持? [英] How to detect rdtscp support in Visual C++?
问题描述
我在MSVC 2012上运行了一段代码:
I have a piece of code running on MSVC 2012:
#include <windows.h>
#include <intrin.h>
UINT64 gettime() {
try {
unsigned int ui;
return __rdtscp(&ui);
}
catch (...) {
return __rdtsc();
}
}
我试图使用__rdtscp()获取时间戳;但是,在不支持__rdtscp()的平台上,我想改用__rdtsc().
I was trying to use __rdtscp() to get the timestamp; however, on the platform where the __rdtscp() is not supported, I want to switch to __rdtsc() instead.
上面的代码不起作用;如果不支持__rdtscp(),则该程序仅会崩溃(在某些VM上).那么,有什么方法可以检测到是否支持__rdtscp(),但又不会使程序崩溃?
The above code doesn't work; the program simply crashed if the __rdtscp() is not supported (on certain VMs). So is there any way I can detect if the __rdtscp() is supported, but without crashing the program?
推荐答案
从MSDN for rdtscp:
From MSDN for rdtscp:
要确定对此指令的硬件支持,请调用InfoType = 0x80000001的__cpuid内在函数,并检查CPUInfo [3](EDX)的位27.如果支持该指令,则该位为1,否则为0.
To determine hardware support for this instruction, call the __cpuid intrinsic with InfoType=0x80000001 and check bit 27 of CPUInfo[3] (EDX). This bit is 1 if the instruction is supported, and 0 otherwise.
这篇关于如何在Visual C ++中检测rdtscp支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!