Bitboard question (from xiphos code)

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
xr_a_y
Posts: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Bitboard question (from xiphos code)

Post by xr_a_y »

In xiphos SEE, one can read

Code: Select all

  else if (sq == pos->ep_sq && p == PAWN)
  {
    occ ^= (1ULL << (sq ^ 8));
    gain[0] = piece_value[PAWN];
  }
about the case where the destination square (sq) of the initial move is the ep move.

Can someone explain this

Code: Select all

     occ ^= (1ULL << (sq ^ 8)); 
, I don't get it. :oops:

Thanks
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Bitboard question (from xiphos code)

Post by hgm »

I have never looked at any Xiphos code, but it seems to me (sq ^ 8) is the square that contains the victim Pawn if sq is the to-square. The line removes the Pawn from the 'occupied' bitboard.
User avatar
xr_a_y
Posts: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Re: Bitboard question (from xiphos code)

Post by xr_a_y »

That was of course what I suspected but now I understand, this is toggling the fourth bit ... so between 24 (b01000) and 31(b11111) this removes the 1 in the fourth bit so it gives the number minus 8 and between 32 (b100000) and 39 (b100111) this add a 1 in the fourth bit so it gives the number plus 8.
Tricky ... Bitboards are amazing ...
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Bitboard question (from xiphos code)

Post by Sven »

xr_a_y wrote: Mon Dec 31, 2018 4:11 pm That was of course what I suspected but now I understand, this is toggling the fourth bit ... so between 24 (b01000) and 31(b11111) this removes the 1 in the fourth bit so it gives the number minus 8 and between 32 (b100000) and 39 (b100111) this add a 1 in the fourth bit so it gives the number plus 8.
Tricky ... Bitboards are amazing ...
For me this is not so much about bitboards but about 6-bit square numbering (although you may state that this numbering system is most common in bitboard engines ...). In a system where squares are numbered from 0 to 63 rank by rank (so that bits 0-2 contain the file part and bits 3-5 the rank part) the operation "sq ^ 8" toggles the rank betwen 0 and 1, 2 and 3, 4 and 5, or 6 and 7, which can be useful in some areas, one of them being the manipulation of square numbers related to en passant.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
User avatar
Look
Posts: 364
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: Bitboard question (from xiphos code)

Post by Look »

xr_a_y wrote: Mon Dec 31, 2018 2:13 pm In xiphos SEE, one can read

Code: Select all

  else if (sq == pos->ep_sq && p == PAWN)
  {
    occ ^= (1ULL << (sq ^ 8));
    gain[0] = piece_value[PAWN];
  }
about the case where the destination square (sq) of the initial move is the ep move.

Can someone explain this

Code: Select all

     occ ^= (1ULL << (sq ^ 8)); 
, I don't get it. :oops:

Thanks
The line better has been a named function, so you can comprehend what it is doing.
Farewell.
D Sceviour
Posts: 570
Joined: Mon Jul 20, 2015 5:06 pm

Re: Bitboard question (from xiphos code)

Post by D Sceviour »

This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
Ratosh
Posts: 77
Joined: Mon Apr 16, 2018 6:56 pm

Re: Bitboard question (from xiphos code)

Post by Ratosh »

A few square tricks:
  • Invert the square rank: (square xor 56)
  • Relative square: (square xor color * 56)
  • Invert rank: (rank xor 7)
  • Relative rank: (rank xor color * 7)
  • Square file: (square and 7)
  • Square rank: (rank >> 3)
  • Mirror file: (file xor 7)
User avatar
Kotlov
Posts: 266
Joined: Fri Jul 10, 2015 9:23 pm
Location: Russia

Re: Bitboard question (from xiphos code)

Post by Kotlov »

D Sceviour wrote: Mon Dec 31, 2018 5:51 pm This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
No, is ordinary.
Eugene Kotlov
Hedgehog 2.1 64-bit coming soon...
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Bitboard question (from xiphos code)

Post by Sven »

D Sceviour wrote: Mon Dec 31, 2018 5:51 pm This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
I also found (sq ^ 8) at one place in Rodent 1.6. Since I do not have many open-source chess engine versions on my harddisk I guess this is sort of a random hit and probably many others have already used that trick.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Bitboard question (from xiphos code)

Post by hgm »

D Sceviour wrote: Mon Dec 31, 2018 5:51 pm This a very fast way of finding the en passant square for either move color. I have never seen the use of XOR (sq ^ 8) in any other code before. Is Xiphos the first to use this?
Micro-Max (which is a mailbox engine) has used it from the beginning (i.e. the first version that implemented e.p.):

Code: Select all

      if(p<3&y==E)H=y^16;                      /* shift capt.sqr. H if e.p.*/
Which, in the more verbose 'maximax.txt' version, reads as:

Code: Select all

            if(PieceType<3 & ToSqr==epSqr)
              CaptSqr = ToSqr^16;                      /* shift CaptSqr if e.p.    */
PieceType < 3 test for Pawns (which have PieceType 1 and 2). Of course it uses 16 here rather than 8 because it uses a 0x88 numbering scheme for the squares.

It uses a similar trick in getting the two squares next to the Rook for testing castling pseudo-legality: sqr^1 and sqr^2, independent of which corner the Rook is in. Unfortunately this only works on the king side for boards with a width that is a multiple of 4, which was a problem in Fairy-Max.