Home > Windows Development > How to determine programmatically if your processor has hyperthreading, SIMD and other processing capabilities with __cpuid

How to determine programmatically if your processor has hyperthreading, SIMD and other processing capabilities with __cpuid

September 22, 2012 Leave a comment Go to comments

One of the machine intrinsics in Visual Studio is the __cpuid function, which returns a 4-byte array containing various bit-wise information about the processor hardware capabilities.

While all the gruesome details can be found on MSDN’s __cpuid, __cpuindex page, here is the basic principle:

int cpuinfo[4];
__cpuid(cpuinfo, 1);

bool hasHT = (cpuinfo[3] & (1 << 28)) > 0;

std::cout << "Hyperthreading on CPU is " << (hasHT? "supported" : "not supported") << std::endl;

The first parameter to __cpuid is a pointer to the array to store the results in. The second parameter – 1 here – specifies what type of information to return. In brief:

  • 0 – identification strings
  • 1 – CPU capabilities
  • 2 – Encoded cache information on SSE3 and SSE4-enabled processors
  • 4 – Cache and threading information
  • 5 – Monitoring information
  • 6 – Temperature and power management information
  • 10 – Architectural performance data

Other values are available on some processor types. Once again, detailed information can be found at the above link. The code example above returns the CPU capabilities array, and looks at bit 28 of the 4th element which is set to 1 if hyperthreading is available and 0 if not (note, on AMD processors, it returns 1 if the processor has multiple cores and 0 if not).

Advertisements

Share your thoughts! Note: to post source code, enclose it in [code lang=...] [/code] tags. Valid values for 'lang' are cpp, csharp, xml, javascript, php etc. To post compiler errors or other text that is best read monospaced, use 'text' as the value for lang.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: