1

I´m using VS2012 C++ Windows 7 and I need to get information about CPU multithreading to calculate the number of available logic processors.

I´m using this code (from This SO Post)

typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;

uint32_t registers[4];
__asm__ __volatile__ ("cpuid " :
                      "=a" (registers[0]),
                      "=b" (registers[1]),
                      "=c" (registers[2]),
                      "=d" (registers[3])
                      : "a" (1), "c" (0));

unsigned CPUFeatureSet = registers[3];
bool hyperthreading = CPUFeatureSet & (1 << 28);

This assembly does not compile, given the following error:

error C2065: '__asm__' : undeclared identifier

I´ve tried changing to __asm __volatile and putting everything in a single line as:

__asm __volatile ("cpuid " :   "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3])  : "a" (1), "c" (0));

This did not work also, leading to:

error C2400: inline assembler syntax error in 'opcode'; found '('

Help appreciated to solve that.

Community
  • 1
  • 1
Mendes
  • 17,489
  • 35
  • 150
  • 263
  • That's gcc syntax, it won't work in VS like that. You will need to `mov` the inputs and outputs yourself, but I would be surprised if here was no winapi function you could just call to get this information. – Jester Mar 08 '16 at 18:05
  • Buf... I can´t write assembler... Where should the `mov` goes to ? – Mendes Mar 08 '16 at 18:06
  • Also the syntax is entirely different. Look it up on MSDN. – edmz Mar 08 '16 at 19:07
  • 2
    Besides it being _GCC_ inline assembly templates that won't work with MSVC++, the other issue is that the _ASM_ inline assembly isn't available when developing 64-bit applications.You can create separate object files built using _MASM_, but inline assembler in 64-bit code isn't allowed.See Jester's answer for a solution using compiler intrinsics. In your case you could be creatinging 32-bit program but __ asm__ isn't an MSVC keyword, although *__asm* is – Michael Petch Mar 08 '16 at 19:37

2 Answers2

6

If you insist on using cpuid, you should use the __cpuid() intrinsic function. The msdn page even comes with sample code. Something like this:

#include <intrin.h>

void foo()
{
    uint32_t registers[4];
    __cpuid(registers, 1);
    unsigned CPUFeatureSet = registers[3];
    // ...
}
Jester
  • 56,577
  • 4
  • 81
  • 125
  • 3
    And there is an ``__cpuidex`` as well if you need to get to the CPUID extensions that require the sub function in ``ECX`` – Chuck Walbourn Mar 08 '16 at 21:30
2

You can query the architecture of the current machine, including processor and core count, and NUMA architecture, using the provided APIs.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • Which one gives me Number of `Processors Cores` and `Number of Logic Processors` as shown in Windows 10 Task Management->Performance ??? – Mendes Mar 08 '16 at 18:39
  • 2
    You can also use [GetLogicalProcessorInformation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194.aspx) for more details. – Chuck Walbourn Mar 08 '16 at 21:29