Hints for programming on system 64 bit?

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
phhnguyen
Posts: 1524
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Hints for programming on system 64 bit?

Post by phhnguyen »

I still works on both system 32 and 64 bit, but will soon migrate completely to 64 bit. I mainly use Win 7 64 bit + Visual studio and Ubuntu 64 bit + gcc.

Just few "basic" questions but very important for designing a program for 64 bit:

- Which integer is faster: int (32 bit) or int64_t? Or should I change all int to int64 in my program?
- For accessing an array of int and an array of int64 which one is faster?
- When allocate a memory block, what is the unit size? 64 bytes?

Any hinds for designing a 64 bit chess program?

Many thanks for any helps.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Programming on system 64 bit?

Post by bob »

phhnguyen wrote:I still works on both system 32 and 64 bit, but will soon migrate completely to 64 bit. I mainly use Win 7 64 bit + Visual studio and Ubuntu 64 bit + gcc.

Just few "basic" questions but very important for designing a program for 64 bit:

- Which integer is faster: int (32 bit) or int64_t? Or should I change all int to int64 in my program?
- For accessing an array of int and an array of int64 which one is faster?
- When allocate a memory block, what is the unit size? 64 bytes?

Any hinds for designing a 64 bit chess program?

Many thanks for any helps.
There is no simple answer. 64 bits is native inside the CPU. But that is 8 bytes. Arrays of 8 byte things can be harmful to cache performance for obvious reasons. In general, I find 64 bit arithmetic to be best, but when dealing with arrays, you have to consider that memory/cache footprint you create when you use an array of 64 bit values. If the values are 8 bits only, an array of chars is often faster, even though the CPU has to jump thru hoops to expand a single byte to 64 bits if you are doing any math with the value, such as when indexing another array or such.

This is a question best answered by some benchmarking. The answer is often complex and there are lots of inter-dependencies that make this very difficult to answer without testing...
User avatar
Houdini
Posts: 1471
Joined: Tue Mar 16, 2010 12:00 am

Re: Hints for programming on system 64 bit?

Post by Houdini »

Under Windows and VS generally speaking one should continue to use 32-bit integers, except in the cases where 64-bit integers are really required (e.g. for node counts or for bitboards).
The "integer" type in VS has remained 32-bit even for 64-bit Windows.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Hints for programming on system 64 bit?

Post by mcostalba »

The only reason why C standard does not mandate the size of 'int' type is exactly this one !

Integer type 'int' is defined so that is the best to be used to store _common_ numbers.

So the simple answer is: keep using 'int' for integers and the compiler will produce the best code out of that, indipendently from the specific platform.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: Hints for programming on system 64 bit?

Post by rbarreira »

I never saw any indication that 32-bit types are slower than 64-bit types on x86-64.

So I agree with Marco - use 64-bit types when needed, 32-bit types otherwise. This will also improve the effectiveness of the caches.
Gerd Isenberg
Posts: 2251
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Hints for programming on system 64 bit?

Post by Gerd Isenberg »

mcostalba wrote:The only reason why C standard does not mandate the size of 'int' type is exactly this one !

Integer type 'int' is defined so that is the best to be used to store _common_ numbers.

So the simple answer is: keep using 'int' for integers and the compiler will produce the best code out of that, indipendently from the specific platform.
Probably not signed int for x86-64. To index an array with a 32-bit integer one finally needs a 64-bit register. Unsigned int has implicit zero-extension, while signed int requires an explicit sign extension, and slows things down.