BitBoard representations of the board

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: BitBoard representations of the board

Post by bob »

Gerd Isenberg wrote:
bob wrote:that question I can answer for me...

A1 = bit 0 (LSB), B1 = bit 1, ..., H1 = bit 7, ..., H8 = bit 63.

I chose this to make the bits line up with the BSF/BSR way of counting where LSB = 0, MSB = 63. For the rest, the property File(sq) = 0 for A1-A8 and File(sq) is defined simply as sq >> 3. Ditto for rank where Rank(A1) = 0, Rank(A8) = 7, where Rank(sq) is defined is sq & 7.

Any mapping can be made to work however as mine used to be backward because of the way the Cray does big scanning in the opposite way of Intel.
The question whether a1, h1, a8 or h8 is least significant square is independent on how you count or scan bits from 0 to 63 in a 64-bit word. Since K10 and future intel (SSE5 something) will have leading zero count (even faster direct path instruction on K10), you may switch back again to save the xor 63 ;-)

lzc even leaves deterministic 64 for empty sets.
Aha. stolen from Cray to appease the crypto-guys. :)
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: BitBoard representations of the board

Post by Aleks Peshkov »

Chan Rasjid wrote:My bitboard basics are these. If anyone sees anything silly, please let me know.

Code: Select all

#define RANK_1           (u64)0xff 
//bitboard.h ...
enum {
    A8, B8, C8, D8, E8, F8, G8, H8,
    A7, B7, C7, D7, E7, F7, G7, H7,
    A6, B6, C6, D6, E6, F6, G6, H6,
    A5, B5, C5, D5, E5, F5, G5, H5,
    A4, B4, C4, D4, E4, F4, G4, H4,
    A3, B3, C3, D3, E3, F3, G3, H3,
    A2, B2, C2, D2, E2, F2, G2, H2,
    A1, B1, C1, D1, E1, F1, G1, H1
};

#define Rank1   (RANK_1)
#define Rank2   &#40;RANK_1 << 8&#41;
#define Rank3   &#40;RANK_1 << 16&#41;
#define Rank4   &#40;RANK_1 << 24&#41;
#define Rank5   &#40;RANK_1 << 32&#41;
#define Rank6   &#40;RANK_1 << 40&#41;
#define Rank7   &#40;RANK_1 << 48&#41;
#define Rank8   &#40;RANK_1 << 56&#41;
Wrong. Your RANK_1 constant maps to A8...H8 rank.
Chan Rasjid
Posts: 588
Joined: Thu Mar 09, 2006 4:47 pm
Location: Singapore

Re: BitBoard representations of the board

Post by Chan Rasjid »

Chan Rasjid wrote:
My bitboard basics are these. If anyone sees anything silly, please let me know.
Code:

#define RANK_1 (u64)0xff
//bitboard.h ...
enum {
A8, B8, C8, D8, E8, F8, G8, H8,
A7, B7, C7, D7, E7, F7, G7, H7,
A6, B6, C6, D6, E6, F6, G6, H6,
A5, B5, C5, D5, E5, F5, G5, H5,
A4, B4, C4, D4, E4, F4, G4, H4,
A3, B3, C3, D3, E3, F3, G3, H3,
A2, B2, C2, D2, E2, F2, G2, H2,
A1, B1, C1, D1, E1, F1, G1, H1
};

#define Rank1 (RANK_1)
#define Rank2 (RANK_1 << 8)
#define Rank3 (RANK_1 << 16)
#define Rank4 (RANK_1 << 24)
#define Rank5 (RANK_1 << 32)
#define Rank6 (RANK_1 << 40)
#define Rank7 (RANK_1 << 48)
#define Rank8 (RANK_1 << 56)
Wrong. Your RANK_1 constant maps to A8...H8 rank.
Should be correct. I use RANK1, RANK2 ... RANK8 and not 0 - 7.

#define RANK(sq) ((sq) >> 3)
#define FILE(sq) ((sq) & 3)

Rasjid
Aleks Peshkov
Posts: 892
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: BitBoard representations of the board

Post by Aleks Peshkov »

There are no RANK(sq) or RANK1 definitions in the posted above code. Your macros RANK_1, Rank1 is a source of bug.
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: BitBoard representations of the board

Post by Gerd Isenberg »

Aleks Peshkov wrote:There are no RANK(sq) or RANK1 definitions in the posted above code. Your macros RANK_1, Rank1 is a source of bug.
Yes, usually 1.Rank covers squares from A1 to H1 but not A8 to H8.
For internal representation it don't cares - but it would confuse me as well.
Chan Rasjid
Posts: 588
Joined: Thu Mar 09, 2006 4:47 pm
Location: Singapore

Re: BitBoard representations of the board

Post by Chan Rasjid »

Code: Select all

Quote&#58;
Chan Rasjid wrote&#58;
My bitboard basics are these. If anyone sees anything silly, please let me know.
Code&#58;

#define RANK_1 &#40;u64&#41;0xff
//bitboard.h ...
enum &#123;
A8, B8, C8, D8, E8, F8, G8, H8,
A7, B7, C7, D7, E7, F7, G7, H7,
A6, B6, C6, D6, E6, F6, G6, H6,
A5, B5, C5, D5, E5, F5, G5, H5,
A4, B4, C4, D4, E4, F4, G4, H4,
A3, B3, C3, D3, E3, F3, G3, H3,
A2, B2, C2, D2, E2, F2, G2, H2,
A1, B1, C1, D1, E1, F1, G1, H1
&#125;;

#define Rank1 &#40;RANK_1&#41;
#define Rank2 &#40;RANK_1 << 8&#41;
#define Rank3 &#40;RANK_1 << 16&#41;
#define Rank4 &#40;RANK_1 << 24&#41;
#define Rank5 &#40;RANK_1 << 32&#41;
#define Rank6 &#40;RANK_1 << 40&#41;
#define Rank7 &#40;RANK_1 << 48&#41;
#define Rank8 &#40;RANK_1 << 56&#41;
Wrong. Your RANK_1 constant maps to A8...H8 rank.

Should be correct. I use RANK1, RANK2 ... RANK8 and not 0 - 7.

#define RANK&#40;sq&#41; (&#40;sq&#41; >> 3&#41;
#define FILE&#40;sq&#41; (&#40;sq&#41; & 3&#41;
The comment "Should be correct. I use RANK1, RANK2 ... RANK8 and not 0 - 7." should be ignored as it is confusing. There is no RANK1, RANK2,...
There are no RANK(sq) or RANK1 definitions in the posted above code. Your macros RANK_1, Rank1 is a source of bug.
Everything in C is a likely source of bugs to me as I simply don't understand C concepts well. Macros can be source of bugs but it seems using #define for constants should be very safe - they just are transformed to C constant expressions on preprocessing. But I may miss something. I need
#define RANK_1 (u64)0xff
because MSVC++ does not accept the suffix ULL which is needed by GCC.

There is a way to introduce bugs. We use conditional compilation to switch evaluation factors on/off :-
in ev.h :-

#define ENABLE_KING_SAFETY 1

in ev.c:-

#if ENABLE_KING_SAFETY
etc...
#endif

and if we have plenty of these in evaluation codes which are changed often, we are in trouble. So I now do this with cut_and_paste:-

#if defined(ENABLE_KING_SAFETY)
#if ENABLE_KING_SAFETY
etc...
#endif
#else
printf("Warning...")
#endif

Rasjid
Chan Rasjid
Posts: 588
Joined: Thu Mar 09, 2006 4:47 pm
Location: Singapore

Re: BitBoard representations of the board

Post by Chan Rasjid »

Hello,
Yes, usually 1.Rank covers squares from A1 to H1 but not A8 to H8.
For internal representation it don't cares - but it would confuse me as well.
I don't understand. I have
#define RANK_1 0xffULL
and it is used to define all ranks with

#define Rank1 (RANK_1)
#define Rank2 (RANK_1 << 8)
#define Rank3 (RANK_1 << 16)

RANK_2, RANK_3, ... don't exist.

My program plays a complete game of chess!

Rasjid
Uri Blass
Posts: 10309
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: BitBoard representations of the board

Post by Uri Blass »

Chan Rasjid wrote:Hello,
Yes, usually 1.Rank covers squares from A1 to H1 but not A8 to H8.
For internal representation it don't cares - but it would confuse me as well.
I don't understand. I have
#define RANK_1 0xffULL
and it is used to define all ranks with

#define Rank1 (RANK_1)
#define Rank2 (RANK_1 << 8)
#define Rank3 (RANK_1 << 16)

RANK_2, RANK_3, ... don't exist.

My program plays a complete game of chess!

Rasjid
I do not understand what you do not understand.
The meaning of Rank1 in your program is squares A8-H8
Of course it is possible to write in that way but people usually think about Rank1 in chess as the squares A1-H1.

If you already started with A8-H8 as Rank1 and you have a working program then you probably got the habit of thinking of Rank1 as A8-H8 and I do not suggest changing it because it may be more confusing for you.

Uri
Chan Rasjid
Posts: 588
Joined: Thu Mar 09, 2006 4:47 pm
Location: Singapore

Re: BitBoard representations of the board

Post by Chan Rasjid »

I do not understand what you do not understand.
The meaning of Rank1 in your program is squares A8-H8
Of course it is possible to write in that way but people usually think about Rank1 in chess as the squares A1-H1.

If you already started with A8-H8 as Rank1 and you have a working program then you probably got the habit of thinking of Rank1 as A8-H8 and I do not suggest changing it because it may be more confusing for you.

Uri
Ok, I now understand.
Yes, usually 1.Rank covers squares from A1 to H1 but not A8 to H8.
! That's the problem of shorthand script. Calling getline() has :-
"Yes, usually 1."
"Rank covers squares from A1 to H1 but not A8 to H8."

Chess programming has 1.rank when RANK(sq) == 0
or 0xffULL. I also have ROW(sq) ranks from the starting rank for a side.

Rasjid