Pinned Pieces With Bitboards

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

StackFish5
Posts: 18
Joined: Fri Dec 24, 2021 5:48 pm
Full name: Andrew Zhuo

Pinned Pieces With Bitboards

Post by StackFish5 »

While reading an article on CPW regarding checks and pinned pieces: https://www.chessprogramming.org/Checks ... Bitboards)
I didn't fully understand this function:

Code: Select all

 pinned = 0;
pinner = xrayRookAttacks(occupiedBB, ownPieces, squareOfKing) & opRQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}
pinner = xrayBishopAttacks(occupiedBB, ownPieces, squareOfKing) & opBQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}

My question is what the function obstructed() does. Moreover, I would like to know whether opRQ refers to the rooks and queens. And whether opBQ refers to the bishops and queens.

Thanks.
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Pinned Pieces With Bitboards

Post by dangi12012 »

I found the CPW to be not up do date with some implementations.
There is a more efficient way to resolve pins and checks which in essence literally takes one & instruction.

A pinned knight cannot move.
A pinned slider can only move along its pinned direction including the enemy piece that pins it.
No piece can be pinned multiple times.

Special piece:
Pawns. Pins for move and take work like sliders.
EP has some special extra pins.

The trick is that pins are two bitboards: HV, D12 - its the absolute optimum I found.
I also have some optimal code regarding check evasion. IMO I really solved BBs like no one else.

So a full legal only knight move BB looks like this:
uint64_t moves = Lookup[knightsquare] & EnemyOrEmpty & checkmask & ~(pinHV | pinD12)

Translated:
From all Knightsquares take those which are enemy or empty. Take those who stop a check if it exists. But only if the knight is not pinned.
Adding an "if" makes this 10x slower.

Please check out the URL in my signature for exactly the question you are asking!
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

Re: Pinned Pieces With Bitboards

Post by tcusr »

StackFish5 wrote: Fri Apr 01, 2022 4:15 am While reading an article on CPW regarding checks and pinned pieces: https://www.chessprogramming.org/Checks ... Bitboards)
I didn't fully understand this function:

Code: Select all

 pinned = 0;
pinner = xrayRookAttacks(occupiedBB, ownPieces, squareOfKing) & opRQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}
pinner = xrayBishopAttacks(occupiedBB, ownPieces, squareOfKing) & opBQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}

My question is what the function obstructed() does. Moreover, I would like to know whether opRQ refers to the rooks and queens. And whether opBQ refers to the bishops and queens.

Thanks.
yes opRQ contains both opposite rooks and queens, it's useful because the queen is just a rook + bishop, so you can save computation.
obstructed returns the bits between sq (the possible pinner) and squareOfKing
StackFish5
Posts: 18
Joined: Fri Dec 24, 2021 5:48 pm
Full name: Andrew Zhuo

Re: Pinned Pieces With Bitboards

Post by StackFish5 »

tcusr wrote: Fri Apr 01, 2022 3:59 pm
StackFish5 wrote: Fri Apr 01, 2022 4:15 am While reading an article on CPW regarding checks and pinned pieces: https://www.chessprogramming.org/Checks ... Bitboards)
I didn't fully understand this function:

Code: Select all

 pinned = 0;
pinner = xrayRookAttacks(occupiedBB, ownPieces, squareOfKing) & opRQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}
pinner = xrayBishopAttacks(occupiedBB, ownPieces, squareOfKing) & opBQ;
while ( pinner ) {
   int sq  = bitScanForward(pinner);
   pinned |= obstructed(sq, squareOfKing) & ownPieces;
   pinner &= pinner - 1;
}

My question is what the function obstructed() does. Moreover, I would like to know whether opRQ refers to the rooks and queens. And whether opBQ refers to the bishops and queens.

Thanks.
yes opRQ contains both opposite rooks and queens, it's useful because the queen is just a rook + bishop, so you can save computation.
obstructed returns the bits between sq (the possible pinner) and squareOfKing
So the obstructed function is the same function as the one under Pure Calculation from https://www.chessprogramming.org/Square ... tackBySide ?
tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

Re: Pinned Pieces With Bitboards

Post by tcusr »

yes
StackFish5
Posts: 18
Joined: Fri Dec 24, 2021 5:48 pm
Full name: Andrew Zhuo

Re: Pinned Pieces With Bitboards

Post by StackFish5 »

Thank you to everyone that responded!