views:

670

answers:

3

How do I determine the word size of my CPU? If I understand correct an int should be one word right? I'm not sure if I am correct. So should just printing sizeof(int) would be enough to determine the word size of my processor?

+2  A: 

sizeof(int) is not always the "word" size of your CPU. The most important question here is why you want to know the word size.... are you trying to do some kind of run-time and CPU specific optimization?

That being said, on Windows with Intel processors, the nominal word size will be either 32 or 64 bits and you can easily figure this out:

  • if your program is compiled for 32-bits, then the nominal word size is 32-bits
  • if you have compiled a 64-bit program then then the nominal word size is 64-bits.

This answer sounds trite, but its true to the first order. But there are some important subtleties. Even though the x86 registers on a modern Intel or AMD processor are 64-bits wide; you can only (easily) use their 32-bit widths in 32-bit programs - even though you may be running a 64-bit operating system. This will be true on Linux and OSX as well.

Moreover, on most modern CPU's the data bus width is wider than the standard ALU registers (EAX, EBX, ECX, etc). This bus width can vary, some systems have 128 bit, or even 192 bit wide busses.

If you are concerned about performance, then you also need to understand how the L1 and L2 data caches work. Note that some modern CPU's have an L3 cache. Caches including a unit called the Write Buffer

Foredecker
isn't sizeof(int) done at compile time, which means that it's the size it's compiled for, not the size of the computer that's running it?
FryGuy
yes - that is exactly correct.
Foredecker
+1  A: 

an int should be one word right?

As I understand it, that depends on the data size model. For an explanation for UNIX Systems, 64-bit and Data Size Neutrality. For example Linux 32-bit is ILP32, and Linux 64-bit is LP64. I am not sure about the difference across Window systems and versions, other than I believe all 32-bit Window systems are ILP32.

How do I determine the word size of my CPU?

That depends. Which version of C standard are you assuming. What platforms are we talking. Is this a compile or run time determination you're trying to make.

The C header file <limits.h> may defines WORD_BIT and/or __WORDSIZE.

mctylr
These things are determined by the compiler and don't have a directly relationship to the actual size (width) of the CPU's word size. Said another way, these things are defined by the compiler developer, not determined by the physical characteristics of the CPU itself.
Foredecker
Such as a cross-compiler environment, true. I should of clarified that if someone wants to write code that is aware of the word size of their *target* host, they can use something the limits.h header file.
mctylr
This of course is for the target environment, not the _capabilities_ of the target CPU. Such as any late model x86 from Intel or AMD, that is capable of being used either as a 32 or 64-bit processor. Depending on the OS being run the CPU may be utilized as either a 32 or 64-bit processor. In 32-bit mode, the registers cannot be access as 64-bit (8 byte) _word_ registers, even though the CPU has 64-bit registers.
mctylr
+1  A: 

Your assumption about sizeof(int) is untrue; see this.

Since you must know the processor, OS and compiler at compilation time, the word size can be inferred using predefined architecture/OS/compiler macros provided by the compiler.

However while of simpler and most RISC processors, word size, bus width, register size and memory organisation are often consistently one value, this may not be true to more complex CISC and DSP architectures with various sizes for floating point registers, accumulators, bus width, cache width, general purpose registers etc.

Of course it begs the question why you might need to know this? Generally you would use the type appropriate to the application, and trust the compiler to provide any optimisation. If optimisation is what you think you need this information for, then you would probably be better off using the C99 'fast' types. If you need to optimise a specific algorithm, implement it for a number of types and profile it.

Clifford
I fixed the broken/incorrect links if anyone was confused before!
Clifford