title: Programmatically detect CPU architecture at runtime
body: <p>Is it possible to check the CPU architecture(is 64 or 32 bits) in a x86/x86-64 CPU without gathering information from the OS or some API, by means of low level code (C/C++ or assembly)?</p>
  
  <p>One could check for the size of a pointer, like discussed <a href="https://stackoverflow.com/questions/5928218/how-to-programatically-detect-a-64-bit-or-32-bit-machine">here</a>, but, from what I understand, this way lets you know the OS architecture is compiled for, because a 64 bit CPU running a 32 bit OS would report wrong results.</p>
  
  <p>Maybe some <code>cpuid</code> instruction or similar could do the trick but I couldn't find anything that fully satisfies these requirements. If you think it's not possible, I would appreciate a good reason to justify this (this may be a less objective answer). If you are wondering, this is for a CPU detection software.</p>
  
  <p>Edit:
  In <a href="https://stackoverflow.com/questions/2569021/determine-32-64-bit-architecture-in-assembly">Determine 32/64 bit architecture in assembly</a>, OP does not get some specific answer that explain how to do it, just that you should use cpuid, and links to <a href="https://stackoverflow.com/questions/1917761/how-do-you-detect-the-cpu-architecture-type-during-run-time-with-gcc-and-inline">How do you detect the CPU architecture type during run-time with GCC and inline asm?</a>, that shows a good answer to me, but it isn't as complete as the answer that I marked as accepted, because Remy Lebeau gives a detailed explanation and tells which specific cpuid bit must be queried, instead of just writing some code, and I find it more appropiate for my question, because it is explained at a higher level scope(I never mentioned gcc, the second post does)</p>