Page 1 of 1

A Combination

Posted: Sun Apr 19, 2015 1:54 pm
by grant
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

Re: A Combination

Posted: Sun Apr 19, 2015 10:47 pm
by Gerd Isenberg
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

Re: A Combination

Posted: Mon Apr 20, 2015 10:58 am
by grant
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

Re: A Combination

Posted: Mon Apr 20, 2015 4:17 pm
by Gerd Isenberg
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

Re: A Combination

Posted: Mon Apr 20, 2015 4:30 pm
by grant
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

Re: A Combination

Posted: Mon Apr 20, 2015 5:08 pm
by Gerd Isenberg
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.

Re: A Combination

Posted: Mon Apr 20, 2015 5:30 pm
by grant
Yes nothing is for free, and those nasty details get me every time.