Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Check sliding pieces attacks last in IsSquaredAttacked #369

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/Lynx/Attacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,40 @@ public static bool IsSquaredAttacked(int squareIndex, Side sideToMove, BitBoard[
{
Utils.Assert(sideToMove != Side.Both);

var offset = Utils.PieceOffset(sideToMove);
var sideToMoveInt = (int)sideToMove;
var offset = Utils.PieceOffset(sideToMoveInt);

// I tried to order them from most to least likely
return
IsSquareAttackedByPawns(squareIndex, sideToMove, offset, piecePosition)
IsSquareAttackedByPawns(squareIndex, sideToMoveInt, offset, piecePosition)
|| IsSquareAttackedByKing(squareIndex, offset, piecePosition)
|| IsSquareAttackedByKnights(squareIndex, offset, piecePosition)
|| IsSquareAttackedByBishops(squareIndex, offset, piecePosition, occupancy, out var bishopAttacks)
|| IsSquareAttackedByRooks(squareIndex, offset, piecePosition, occupancy, out var rookAttacks)
|| IsSquareAttackedByQueens(offset, bishopAttacks, rookAttacks, piecePosition)
|| IsSquareAttackedByKing(squareIndex, offset, piecePosition);
|| IsSquareAttackedByQueens(offset, bishopAttacks, rookAttacks, piecePosition);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSquareInCheck(int squareIndex, Side sideToMove, BitBoard[] piecePosition, BitBoard[] occupancy)
{
Utils.Assert(sideToMove != Side.Both);

var offset = Utils.PieceOffset(sideToMove);
var sideToMoveInt = (int)sideToMove;
var offset = Utils.PieceOffset(sideToMoveInt);

// I tried to order them from most to least likely
return
IsSquareAttackedByRooks(squareIndex, offset, piecePosition, occupancy, out var rookAttacks)
|| IsSquareAttackedByBishops(squareIndex, offset, piecePosition, occupancy, out var bishopAttacks)
|| IsSquareAttackedByQueens(offset, bishopAttacks, rookAttacks, piecePosition)
|| IsSquareAttackedByKnights(squareIndex, offset, piecePosition)
|| IsSquareAttackedByPawns(squareIndex, sideToMove, offset, piecePosition);
|| IsSquareAttackedByPawns(squareIndex, sideToMoveInt, offset, piecePosition);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsSquareAttackedByPawns(int squareIndex, Side sideToMove, int offset, BitBoard[] pieces)
private static bool IsSquareAttackedByPawns(int squareIndex, int sideToMove, int offset, BitBoard[] pieces)
{
var oppositeColorIndex = ((int)sideToMove + 1) % 2;
var oppositeColorIndex = sideToMove ^ 1;

return (PawnAttacks[oppositeColorIndex, squareIndex] & pieces[offset]) != default;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Lynx/MoveGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,13 @@ private static bool IsAnyPawnMoveValid(Position position, int offset)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsAnyCastlingMoveValid(Position position, int offset)
{
var piece = (int)Piece.K + offset;
var oppositeSide = (Side)Utils.OppositeSide(position.Side);

int sourceSquare = position.PieceBitBoards[piece].GetLS1BIndex(); // There's for sure only one

// Castles
if (position.Castle != default)
{
var piece = (int)Piece.K + offset;
var oppositeSide = (Side)Utils.OppositeSide(position.Side);

int sourceSquare = position.PieceBitBoards[piece].GetLS1BIndex(); // There's for sure only one

if (position.Side == Side.White)
{
bool ise1Attacked = Attacks.IsSquaredAttackedBySide((int)BoardSquare.e1, position, oppositeSide);
Expand Down