简体   繁体   中英

VS2012 support for __asm__ __volatile__ assembler code

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.

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];
    // ...
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM