Mapping of squares for BitBoard.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Fguy64
Posts: 814
Joined: Sat May 09, 2009 4:51 pm
Location: Toronto

Mapping of squares for BitBoard.

Post by Fguy64 »

Greetings, I was just looking into this. The standard mapping, which I have seen termed "little endian", starts with the least significant bit mapped to square a1 and the most significant bit mapped to h8. Apparently there is some sort of mathematical advantage to this mapping.

I am wondering if this so called "mathematical advantage" is just a c++ thing, which depends on the way c++ lays out it's arrays.

I have always worked with arrays or strings in java, and I know in java I have always thought of array element (0,0), or just index=0 in the case of a string, as square a8 and array element (7,7) or index=63 as square h1. Which is the same general layout as a fen string.

So the question is whether or not so called optimal bit/square mappings are language dependant.

Thanks.
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 7:10 pm
Location: Prague

Re: Mapping of squares for BitBoard.

Post by tvrzsky »

Don't know about language dependency but regarding C (or C++) it really seems as a matter of taste. I would even say that your "standard mapping" is quite rare maybe though I use it myself.
See P.P.S in this message and the subsequent thread:
http://www.talkchess.com/forum/viewtopi ... 67&t=16068
Fguy64
Posts: 814
Joined: Sat May 09, 2009 4:51 pm
Location: Toronto

Re: Mapping of squares for BitBoard.

Post by Fguy64 »

tvrzsky wrote:Don't know about language dependency but regarding C (or C++) it really seems as a matter of taste. I would even say that your "standard mapping" is quite rare maybe though I use it myself.
See P.P.S in this message and the subsequent thread:
http://www.talkchess.com/forum/viewtopi ... 67&t=16068
Sorry, which mapping is rare? The way I think of array element (0,0) as square a8? I suppose that is just taste, it really doesn't matter, but it is consistent with the x,y co-ordinate system in a java drawing surface in which x increases to the right ang y increases downward, and the layout of a fen string is consistent with that also.

So the point is, if I understand correctly, it really doesn't matter if the LSB to MSB mapping is from a8 to h1 or from a1 to h8. ?

But if you are saying instead that the a1-h8 mapping is rare, well I am surprised, since most of the websites I have read seem to suggest it is standard.
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 7:10 pm
Location: Prague

Re: Mapping of squares for BitBoard.

Post by tvrzsky »

Well, my 2 cents:
Basically there are 4 different ways of mapping (considering only sane ones :D).
It is important to clarify, in which direction do you count the squares, if along files (a1=0, a8=7, h8=63) or along ranks (a1=0, h1=7, h8=63). With my "maybe rare" statement I referred to the former case. It was at least my impression after reading of various code snippets that most people either start with a8=0 or goes along ranks with a1=0.
I think that more substantial is what do you want to do with your bitboards. If you start with a1 or a8 means only flipping of the board in the manner of changing of piece colors, so it is trully mater of taste (personally I do not like a8=0, it feels unnatural and similarity with FEN is not relevant for me, I do not read FENs, it does my engine automatically instead of me :) ). More important is whether you goes along ranks or files because it has consequencies in the way you can use bitboards (if you have files or ranks in bytes of bitboard or how you can generate pawn moves and so on).
Fguy64
Posts: 814
Joined: Sat May 09, 2009 4:51 pm
Location: Toronto

Re: Mapping of squares for BitBoard.

Post by Fguy64 »

Ok Filip, thanks for that.

I'm gonna go with a8=0, b8=1, ..., a7=8, ..., g1 = 62, h1=63 for the aforementioned reasons.
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 7:10 pm
Location: Prague

Re: Mapping of squares for BitBoard.

Post by tvrzsky »

Fguy64 wrote:Ok Filip, thanks for that.

I'm gonna go with a8=0, b8=1, ..., a7=8, ..., g1 = 62, h1=63 for the aforementioned reasons.
Haha, so I was right in the end, the method which I prefer is really rare for some strange reason ... :lol:
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Mapping of squares for BitBoard.

Post by bob »

Fguy64 wrote:Greetings, I was just looking into this. The standard mapping, which I have seen termed "little endian", starts with the least significant bit mapped to square a1 and the most significant bit mapped to h8. Apparently there is some sort of mathematical advantage to this mapping.

I am wondering if this so called "mathematical advantage" is just a c++ thing, which depends on the way c++ lays out it's arrays.

I have always worked with arrays or strings in java, and I know in java I have always thought of array element (0,0), or just index=0 in the case of a string, as square a8 and array element (7,7) or index=63 as square h1. Which is the same general layout as a fen string.

So the question is whether or not so called optimal bit/square mappings are language dependant.

Thanks.
bit to square is completely irrelevant. But you want to make the LSB bit number 0, the MSB as bit 63, because that is how the BSF/BSR instructions number the bits. So long as you keep the rightmost bit as bit zero, you are set. Doesn't matter whether you map bit 0 to a1, h1, a8 or h8.

In Crafty, when I renumbered things, I went thru a bit of an interesting mapping, as for me, bit 0 is a1, bit 7 is h1, ... bit 56 is a8, bit 63 is h8. If you look at a bitboard and want to visualize it, you have to mentally look at it through a mirror. I don't find this hard at all, although at first it took some concentration.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Mapping of squares for BitBoard.

Post by bob »

Fguy64 wrote:Greetings, I was just looking into this. The standard mapping, which I have seen termed "little endian", starts with the least significant bit mapped to square a1 and the most significant bit mapped to h8. Apparently there is some sort of mathematical advantage to this mapping.

I am wondering if this so called "mathematical advantage" is just a c++ thing, which depends on the way c++ lays out it's arrays.

I have always worked with arrays or strings in java, and I know in java I have always thought of array element (0,0), or just index=0 in the case of a string, as square a8 and array element (7,7) or index=63 as square h1. Which is the same general layout as a fen string.

So the question is whether or not so called optimal bit/square mappings are language dependant.

Thanks.
bit to square is completely irrelevant. But you want to make the LSB bit number 0, the MSB as bit 63, because that is how the BSF/BSR instructions number the bits. So long as you keep the rightmost bit as bit zero, you are set. Doesn't matter whether you map bit 0 to a1, h1, a8 or h8.

In Crafty, when I renumbered things, I went thru a bit of an interesting mapping, as for me, bit 0 is a1, bit 7 is h1, ... bit 56 is a8, bit 63 is h8. If you look at a bitboard and want to visualize it, you have to mentally look at it through a mirror. I don't find this hard at all, although at first it took some concentration.

I did this because I wanted rank 0 to be the first rank, rank 1 to be the second, etc. which means that any square numbered xxxyyy is rank xxx, file yyy, and I wanted file 1 to be 0, file 2 to be 1, ... as well.

To make that work with the intel way of numbering bits, required the mental gymnastics I mentioned above.
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Mapping of squares for BitBoard.

Post by Gerd Isenberg »

bob wrote: bit to square is completely irrelevant. But you want to make the LSB bit number 0, the MSB as bit 63, because that is how the BSF/BSR instructions number the bits.
So long as you keep the rightmost bit as bit zero, you are set.
I think LSB and MSB are defined by their arithmetical significance rather than how bitscan (or leading/trailing zero) count work, and what is first one or last one or left/right top/down. 0 or 2^0 is less significant than 63 or 2^63.

Anyway, you are right that any of the eight possible orthogonal mappings from 6-bit indices to 64 squares is fine and a matter of taste. I use the same mapping than you. Otherwise one may enumerate files a-h from 0..7 or 7..0, ranks from 1-8 -> 0..7 or 7..0, with 4 possible combinations in total. Additionally for the one-dimensional 8*8 array one may use rank or file at the little end, in total eight different orthogonal mappings.
SuneF
Posts: 127
Joined: Thu Sep 17, 2009 11:19 am

Re: Mapping of squares for BitBoard.

Post by SuneF »

tvrzsky wrote: It is important to clarify, in which direction do you count the squares, if along files (a1=0, a8=7, h8=63) or along ranks (a1=0, h1=7, h8=63). With my "maybe rare" statement I referred to the former case.
The former seems a tad more efficient at least for pawn attacks. You save a mask since you don't have to worry about the wrap around file.