Preface
So after a long time of C only work I came back to Delphi and found out that there are some new things in Delphi. One being NativeInt.
To my surprise I discovered that Delphi and C handle their "native integer"1 types different for x86-64. Delphi NativeInt seems to behave like C void * and Delphi Pointer which is contrary to what I would expect from the names.
In Delphi NativeInt is 64 bit in size. Expressed in Code:
SizeOf(NativeInt) = SizeOf(Pointer) = SizeOf(Int64) = 8
C has only 64 bit pointers. int remains 32 bit. Expressed in Code2:
sizeof(int) == 4 != sizeof(void *) == 8
Even the Free Pascal Compiler3 agrees on the size of NativeInt.
Question
Why was 64 bit chosen for Delphi NativeInt and 32 bits for C int?
Of course both are valid according to the language documentation/specification. However, "the language allows for it" is not really a helpful answer.
I guess it has to do with speed of execution as this is the main selling point of C today. Wikipedia and other sources all say that x86-64 do have 64 bit operand registers. However, they also state that the default operand size is 32 bit. So maybe operations on 64 bit operands are slower compared to 32 bit operands? Or maybe the 64 bit registers can do 2 32 bit operations at the same time? Is that a reason?
Is there maybe another reason the creators of the compilers did choose these sizes?
Footnotes
- I am comparing Delphi NativeInt to C int because the name/specificaion suggests that they have similar purpose. I know there is also Delphi Integer which behaves like C int on x68 and x86-64 in Delphi.
- sizeof() returns the size as multiple of char in C. However, char is 1 byte on x86-64.
- It does so in Delphi mode and default mode for NativeInt. The other integer types in default mode are a whole other can of worms.