How to determine programmatically if your processor has hyperthreading, SIMD and other processing capabilities with __cpuid
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).
-
August 16, 2013 at 13:56C++11 / Boost: Multi-threading – The Parallel Aggregation Pattern | Katy's Code