grant wrote:Hi
Again I find myself coming up with an idea and not being at my computer to test it out, so apologies for this.
I was reading through some posts about generating a bitboard of pinned pieces and found this post by Gerd
Code: Select all
rookWise = rookAttacks(occ, kiSq);
potPinned = rookWise & ownPieces;
xrays = rookWise ^ rookAttacks(occ ^ potPinned, kiSq);
pinners = xrays & oppoRookOrQueen;
pinned = 0;
while ( pinners )
{
pinnerSq = bitScanForward(pinners);
pinned |= potPinned & obstructed[pinnerSq][kiSq]);
pinners &= pinners - 1;
}
... same for bishops/queens
After finding the pinners and potentially pinned pieces, we could OR these together as board occupation since pinners are always further from the king than potentially pinned, and use "magic" to lookup the pre-calculated pinned pieces bitboard.
Code: Select all
rookWise = rookAttacks(occ, kiSq);
potPinned = rookWise & ownPieces;
xrays = rookWise ^ rookAttacks(occ ^ potPinned, kiSq);
pinners = xrays & oppoRookOrQueen;
pinned = pinnedByRook(pinners | (potPinned & pinMask), kiSq);
U64 pinnedByRook(U64 occ, int square)
{
index = (int)((occ * rookPinMagic[square]) >> rookPinShift[square]);
return *(pinIndecies[square] + index);
}
... same for bishops/queens
Not having tested this, I am assuming that magic numbers exist. My first guess at the table size would be around 600K which is a bit of a downer but may be less depending on the magics. At least it saves looping through all the pinners.
Grant
This is one of those cases where intuition should be ignored and testing should be done. It is like the comments I get about "jeez, a bubble sort in your q-search or to SEE sort captures? Why use something so slow? Because often I am sorting 1-2-3 moves and bubble sort is as fast as anything and faster than most.
In this case, if you think about playing actual games of chess, pins are pretty rare if you are only talking about 'absolute pins" (which is a piece pinned to the king so that it can't move at all, rook/knight pinned by a bishop, rook pinned by a bishop, queen pinned by a bishop/rook. Those are _very_ rare and having more than one absolute pin in a position is beyond rare. More normal pins, such as knight pinned on a rook by a bishop might be more interesting in this light, but I doubt anyone tries to evaluate those except as part of piece scoring anyway where this "set" would not make much sense.
So, the bottom line is that you are trying to do something efficient for what is void operation for the most part, or a set of one at best...
Testing will show that the magic computation will _way_ outweigh the speed gained in those rare positions where there are multiple pins...