Detect processor type programmatically (Linux)
I'm writing a program in gcc that has asm language for performance-critical sections, and I want to ensure at run-time the program is running on the correct architecture. In particular, I need to differentiate ia64 (Itanium) and x86_64 on Linux.
开发者_开发问答Do I just read /proc/cpuinfo? Seems clunky, but I can't find an appropriate kernel call.
IA64 and x86_64 are entirely different architectures. A program built for one will simply not run on the other, so your question doesn't make sense (in fact, incompatibility with the x86 instruction set is one of the reasons for IA64's lackluster market penetration, because IA64-based systems couldn't run the wide array of existing, legacy 32-bit x86-based applicaitons).
The best you could do is compile two different versions of the program, and then use a wrapper script to select the appropriate binary when the user invokes the program.
/proc/cpuinfo
is the standard interface to CPU information on linux.
It is the best option, as it is intended to be architecture-independent.
The answer is already posted, but I'll add this:
If the machine is x86 or x64, you can get more detailed information through the cpuid
instruction.
http://en.wikipedia.org/wiki/CPUID
精彩评论