0

I'm writing codes to get cpu information to identify a cpu. And I find answer from this is exactly what I need.

void getCpuid(std::uint32_t *p)
{
  __asm__ __volatile__ ("cpuid"
              : "=a"(p[0]),
                "=b"(p[1]),
                "=c"(p[2]),
                "=d"(p[3])
              : "0"(p[0]), "2"(p[2])
              : "memory");
}

Call it by: std::uint32_t cpuinfo[4]={1,0,0,0}; getCpuid(cpuinfo);.

When I compile these codes under a docker(arm-ubuntu 18.04 for Nvidia jetson), errors show up:

xxx.cpp:20:34: error: impossible constraint in 'asm'

Firstly, I thought some syntax errors(I don‘t know this asm keyword before and don't know anything about assemble codes), so I searched a lot threads, but none helps。But then, I tried compile this in my host device(x86 ubuntu 18.04). It compiles! And run it I get the cpu information.

Now, I'm confused.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
taptiptop
  • 13
  • 6
  • 3
    This is x86 code. It will not work on arm. If you are on linux you might be better off using `/proc/cpuinfo` depending on what you need. – Jester May 12 '22 at 15:05
  • @Jester Thanks for you quick reply. But which one is `x86 code`, 'asm', 'cpuid' or both? So, I need to execute system command like `cat`, `grep` or something else and extract useful information from that file, right? – taptiptop May 12 '22 at 15:17
  • 2
    `cpuid` is an Intel x86 command that is not available on ARM. `/proc/cpuinfo` is a file - you can read it from C++ using any filesystem API, like C++' `std::filestream`, or fopen/fread/fclose, or Unixy open/read/close. Then you have to parse the lines, naturally. – Seva Alekseyev May 12 '22 at 15:40
  • 2
    `cpuid` is an x86 instruction, and `"=a"`, `"=b"`, etc. are all x86-specific constraints. If those letters mean anything as constraints for ARM or AArch64, at least one of them means something that can't hold a `uint32_t`. https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html That's why it won't even compile, let alone assemble. – Peter Cordes May 12 '22 at 16:55
  • The C++ compiler translates a C++ program to machine code appropriate for your target. `__asm__` is a way to skip the translation and get a specific machine instruction. That makes it by nature specific to a single target architecture. – Ben Voigt May 12 '22 at 17:14
  • @SevaAlekseyev @PeterCordes @BenVoigt, Now, I learn that thanks to you all. I'm trying `/proc/cpuinfo` file method. – taptiptop May 13 '22 at 01:55

0 Answers0