A Combination

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
grant
Posts: 67
Joined: Mon Aug 06, 2007 2:42 pm
Location: London, England

A Combination

Post by grant » Sun Apr 19, 2015 11:54 am

Hi
I put up a post a few years ago about using magics to get a bitboard of pinned pieces, and the general concensus was that it was not worth the computational effort and extra lookups to get this information, as pinned pieces are rare.

However, since we all have to determine whether or not we are in check, with only a little extra effort (and lookup) I can combine am-I-in-check with pinned pieces:-

Code: Select all

U64 attacks = RookMagic(kingSquare, RksQns(them) & allOccupied;
	int rankOccupation = (attacks >> (kingSquare & 0x38)) & 0xFF;
	U64 fileOccupation = (attacks >> (kingSquare & 7)) & 0x0101010101010101;
	int rookPinInfo = RankPinMagic(kingSquare & 7, rankOccupation) | FilePinMagic(kingSquare >> 3, fileOccupation);
	attacks = BishopMagic(kingSquare, BpsQns(them)) & allOccupied;
	int bishopPinInfo = BishopPinMagic(kingSquare, attacks);
Both rookPinInfo and bishopPinInfo contain the following:-
If (...PinInfo & 7) is zero then not-in-check, else is the distance to the checking piece and ((...PinInfo >> 3) & 7) is the direction from the king to the checking piece.
If (...PinInfo & 0x3FFFFFC0) is zero then no-pinned-pieces, else provides distance and direction of pinned pieces AND the pinners for all 8 directions.

There are the extra 64-bit multiplys and the 286K lookup table, but if you only generate legal moves then this could possibly be an improvement. This will only work with 64-bit multiplication.

Grant

Gerd Isenberg
Posts: 2127
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: A Combination

Post by Gerd Isenberg » Sun Apr 19, 2015 8:47 pm

Hi Grant,
reading your old post you refer
http://www.talkchess.com/forum/viewtopic.php?t=22550
I don't exactly understand how your new code should work.

Gerd

grant
Posts: 67
Joined: Mon Aug 06, 2007 2:42 pm
Location: London, England

Re: A Combination

Post by grant » Mon Apr 20, 2015 8:58 am

Hi Gerd

I'll try to explain.

Let's say we put a white king on A8 and a black bishop on H1. BishopMagic() returns the diagonal B7 to H1 which we 'and' with allOccupied. We use this in BishopPinMagic() to return check and pinned pieces information.

If there's nothing inbetween king and bishop, BishopPinMagic() returns 0x00000027. The 1st 6 bits reserved for check (3 bits for distance, 3 bits for direction), 7 squares to bishop in direction 4 (in my program). If there are 2 or more pieces inbetween, zero is returned i.e not in check and no pinned pieces. If there is one piece inbetween (black or white), the distance and direction to this piece is returned along with distance and direction to the bishop.
Let's say there's something on B7. BishopPinMagic() will return 0x00000E40. The 1st 6 bits are check info, in this case zero. The next 24 bits are 4 sets of 6 bits in direction order (3 bits for distance to 1st piece, 3 bits for distance to 2nd piece, located in the 1st set of 6 bits direction 4). A simple function extracts this info into a bitboard to determine the colour of this piece at move generation.

The PinMagic calculation requires the normally redundant board edge bits so for rooks this would mean 14 bits per square. Way too big. So I shift the bits to calculate rank and file seperately, the results of which are or'ed giving the same information as the bishops. Some of the magics were a little tricky to find but I managed to get them all.

Hope you understand.

Grant

Gerd Isenberg
Posts: 2127
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: A Combination

Post by Gerd Isenberg » Mon Apr 20, 2015 2:17 pm

grant wrote:Hi Gerd

I'll try to explain.

Let's say we put a white king on A8 and a black bishop on H1. BishopMagic() returns the diagonal B7 to H1 which we 'and' with allOccupied. We use this in BishopPinMagic() to return check and pinned pieces information.

Grant
Hi Grant,

I see, but along with your code and sample, BishopMagic also returns the diagonal B7 to H1 if there is no bishop or queen on H1 or on the diagonal at all?!

Gerd

grant
Posts: 67
Joined: Mon Aug 06, 2007 2:42 pm
Location: London, England

Re: A Combination

Post by grant » Mon Apr 20, 2015 2:30 pm

Gerd

I would, of course, check the diagonal for bishops/queens and if there are none there is no point running that piece of code.
This is a sample to demonstrate that while checking to see if you are in check (as you normally do) you can also return information on pinned pieces.

Grant

Gerd Isenberg
Posts: 2127
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: A Combination

Post by Gerd Isenberg » Mon Apr 20, 2015 3:08 pm

grant wrote:Gerd

I would, of course, check the diagonal for bishops/queens and if there are none there is no point running that piece of code.
This is a sample to demonstrate that while checking to see if you are in check (as you normally do) you can also return information on pinned pieces.

Grant
Ahh those nasty details, still you have to check up 4 rays each for potential pinners/checker.

grant
Posts: 67
Joined: Mon Aug 06, 2007 2:42 pm
Location: London, England

Re: A Combination

Post by grant » Mon Apr 20, 2015 3:30 pm

Yes nothing is for free, and those nasty details get me every time.

Post Reply