Skip to content

Commit

Permalink
⚡ Check sliding pieces attacks last in IsSquaredAttacked (#369)
Browse files Browse the repository at this point in the history
* Check sliding pieces attacks last in `IsSquaredAttacked`
* Make 'oppositeColorIndex' faster
  • Loading branch information
eduherminio committed Aug 21, 2023
1 parent f97e38c commit df15872
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
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

0 comments on commit df15872

Please sign in to comment.