64+32 == 96 challenge

Discussion of chess software programming and technical issues.

Moderator: Ras

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

64+32 == 96 challenge

Post by phhnguyen »

I am been trying some configurations to create bitboards for a large board of 90 cells (Xiangqi). The purpose is to compare them for pros-cons as well as speeds.

The first try is to use an array of 3 integer 32 bit. Everything is fine, the bitboard size (use sizeof) is exactly 12 byes (96 bit).

Now, I am trying to create other bitboard with one 64-bit integer and one 32-bit integer. (it is actually a Bob's suggestion long time ago).

Code: Select all

class Bitboard {
  int64_t a;
  int32_t b;
};
However the first problem here is the size of the bitboard now is not 12 bytes / 96 bits but 16 bytes / 128 bits - a redundancy of 4 bytes.

I have some understandings the way computer allocs memory but still curious: if there is anyway to make that class to be exactly 12 bytes?
mar
Posts: 2679
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: 64+32 == 96 challenge

Post by mar »

phhnguyen wrote:I am been trying some configurations to create bitboards for a large board of 90 cells (Xiangqi). The purpose is to compare them for pros-cons as well as speeds.

The first try is to use an array of 3 integer 32 bit. Everything is fine, the bitboard size (use sizeof) is exactly 12 byes (96 bit).

Now, I am trying to create other bitboard with one 64-bit integer and one 32-bit integer. (it is actually a Bob's suggestion long time ago).

Code: Select all

class Bitboard {
  int64_t a;
  int32_t b;
};
However the first problem here is the size of the bitboard now is not 12 bytes / 96 bits but 16 bytes / 128 bits - a redundancy of 4 bytes.

I have some understandings the way computer allocs memory but still curious: if there is anyway to make that class to be exactly 12 bytes?
Of course the compiler has to pad the structure, so each element of an array of structures has proper alignment; which is actually necessary on some platforms.

You can try to use #pragma pack(n), in your case 4, but I'd discourage you to do so; on x86/x64 it will work.
So I would happily use 2x uint64_t (128 bits) instead (or live with the fact the struct is 4-byte padded)
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: 64+32 == 96 challenge

Post by phhnguyen »

Thanks, got the idea and I will give it a try, before giving up :)
2xuint64 will be another try after this one.
AlvaroBegue
Posts: 932
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: 64+32 == 96 challenge

Post by AlvaroBegue »

You can make a struct with three 32-bit integers and then it will only take 12 bytes. Since you have a 64-bit integer in there, the compiler makes the size of the struct a multiple of 8 bytes so the 64-bit integers are always aligned.

You could also just use a 128-bit integer type, which in gcc is `unsigned __int128'.
User avatar
phhnguyen
Posts: 1541
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: 64+32 == 96 challenge

Post by phhnguyen »

I have tried already 3xuint32. Just need to consider uint64+uint32 since it is an old suggestion. Yes I will try with an int128, thanks.