Page 1 of 1

Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 2:13 pm
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

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 3:17 pm
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.

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 4:11 pm
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 ...

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 5:34 pm
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.

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 5:50 pm
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.

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 5:51 pm
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?

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 5:53 pm
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)

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 7:02 pm
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.

Re: Bitboard question (from xiphos code)

Posted: Mon Dec 31, 2018 7:03 pm
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.

Re: Bitboard question (from xiphos code)

Posted: Tue Jan 01, 2019 9:30 am
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.