views:

714

answers:

5

Is it possible to detect the system/processor architecture while the program is running (under windows and under linux) in c++?

+3  A: 

On Windows, you may use __cpuid. On Linux, you can open("/proc/cpuinfo") and look through it.

Here is an example on Windows, based on the example in the MSDN page:

#include <intrin.h>

bool cpuSupports64()
{
    int CPUInfo[4];
    __cpuid(CPUInfo, 0);
    return (CPUInfo[3] & 0x20000000) || false;
}
Hosam Aly
/proc/cpuinfo doesn't have any explicit Word size information.
Amit
@Amit: What about `clflush size`?
Hosam Aly
@Hosam Aly: Thanks. I didn't know about it. What does 'clflush' stand for btw?
Amit
Can you please give an example on how to do it in windows?
Levo
Oh well. On my 32-bit OS and 32-bit hardware cflush size is still 64!
Amit
@Amit: What's your 32-bit processor type?
Hosam Aly
@Hosam: Intel Celeron M 440. Not sure, what you mean my "type".
Amit
@Amit: Hmmm... I'm not sure. Maybe I'm wrong, or maybe your processor supports some form of 64-bit!
Hosam Aly
@Levo: I have created an example.
Hosam Aly
@Amit: /proc/cpuinfo has a line called "flags :" and if that line has "lm" then the cpu is x64. lm = LongMode
Malkocoglu
It doesn't have the 'lm' flag and I knew my processor is 32-bit :)
Amit
A: 

All software approaches will only give you the architecture of the OS you are using. If you are using a 32-bit OS on a 64-bit Hardware, that is what you will get. Among the software approaches are the size of a pointer, the size of a Integer data type or a Float data type. On Linux, you can do a 'uname -a'.

Amit
-2? Care to comment ?
Amit
It's wrong, for starters. If you know you're running on an Intel i7, you just know it's a 64 bits CPU. And "system architecture", the tile of the question reasonably includes the OS anyway.
MSalters
I am running a 32-bit OS on a Intel i7, what software architecture am I on? Will I be able to take the benefits of a 64-bit hardware?
Amit
Answer: Probably Win32 or Linux-i386, and no. But you can ask your own questions here on SO, don't need to do that in a comment to another question.
MSalters
@MSalters: Can you explain what was wrong in my answer?
Amit
I didn't downvote this but:1) Your answer is incorrect: CPUID provides you the actual processor information even when you're running onder 32-bit OS.2) Your answer omits 32-bit process running on a 64-bit OS scenario. (which might be the case here)
ssg
+2  A: 

Use GetSystemInfo() or GetNativeSystemInfo() functions under Windows depending on your purpose. I don't know how to do it on Linux though.

ssg
A: 

Depending on what you intend to do with this information (e.g. select the fastest handcoded assembly code for a specific CPU), under Linux you might want to read /proc/cpuinfo, specifically: the "flags" section, so you can choose between SSE/SSE2 implementation vs. MMX implementation vs. whatever.

Big endian system vs. little endian system is a bit more complicated, refer to: http://en.wikipedia.org/wiki/Endianess

digitalarbeiter
+1  A: 

Under Linux, you can use the uname system call. It fills in this user-allocated struct:

  struct utsname {
           char sysname[];    /* Operating system name (e.g., "Linux") */
           char nodename[];   /* Name within "some implementation-defined
                                 network" */
           char release[];    /* OS release (e.g., "2.6.28") */
           char version[];    /* OS version */
           char machine[];    /* Hardware identifier */
       #ifdef _GNU_SOURCE
           char domainname[]; /* NIS or YP domain name */
       #endif
       };

The machine field will identify the architecture.

David Joyner