Skip to content

Commit

Permalink
⚡ Refactor move encoding methods and stop encoding special move flags…
Browse files Browse the repository at this point in the history
… individually (#622)
  • Loading branch information
eduherminio committed Jan 22, 2024
1 parent 485bce8 commit 46c80bb
Show file tree
Hide file tree
Showing 14 changed files with 1,426 additions and 483 deletions.
686 changes: 613 additions & 73 deletions src/Lynx.Benchmark/MakeUnmakeMove_implementation_Benchmark.cs

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions src/Lynx.Benchmark/MakeUnmakeMove_integration_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,10 +1465,10 @@ internal static void GeneratePawnMoves(ref int localIndex, Move[] movePool, Make
var targetRank = (singlePushSquare / 8) + 1;
if (targetRank == 1 || targetRank == 8) // Promotion
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.Q + offset);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.R + offset);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.N + offset);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.B + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.Q + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.R + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.N + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.B + offset);
}
else if (!capturesOnly)
{
Expand All @@ -1483,7 +1483,7 @@ internal static void GeneratePawnMoves(ref int localIndex, Move[] movePool, Make
if (!position.OccupancyBitBoards[2].GetBit(doublePushSquare)
&& ((sourceRank == 2 && position.Side == Side.Black) || (sourceRank == 7 && position.Side == Side.White)))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, doublePushSquare, piece, isDoublePawnPush: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeDoublePawnPush(sourceSquare, doublePushSquare, piece);
}
}
}
Expand All @@ -1494,7 +1494,7 @@ internal static void GeneratePawnMoves(ref int localIndex, Move[] movePool, Make
if (position.EnPassant != BoardSquare.noSquare && attacks.GetBit(position.EnPassant))
// We assume that position.OccupancyBitBoards[oppositeOccupancy].GetBit(targetSquare + singlePush) == true
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, (int)position.EnPassant, piece, isCapture: TRUE, isEnPassant: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeEnPassant(sourceSquare, (int)position.EnPassant, piece);
}

// Captures
Expand All @@ -1507,14 +1507,14 @@ internal static void GeneratePawnMoves(ref int localIndex, Move[] movePool, Make
var targetRank = (targetSquare / 8) + 1;
if (targetRank == 1 || targetRank == 8) // Capture with promotion
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.Q + offset, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.R + offset, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.N + offset, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.B + offset, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.Q + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.R + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.N + offset);
movePool[localIndex++] = MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.B + offset);
}
else
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeCapture(sourceSquare, targetSquare, piece);
}
}
}
Expand Down Expand Up @@ -1548,7 +1548,7 @@ internal static void GenerateCastlingMoves(ref int localIndex, Move[] movePool,
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.f1, position, oppositeSide)
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.g1, position, oppositeSide))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, Constants.WhiteShortCastleKingSquare, piece, isShortCastle: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeShortCastle(sourceSquare, Constants.WhiteShortCastleKingSquare, piece);
}

if (((position.Castle & (int)CastlingRights.WQ) != default)
Expand All @@ -1559,7 +1559,7 @@ internal static void GenerateCastlingMoves(ref int localIndex, Move[] movePool,
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.d1, position, oppositeSide)
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.c1, position, oppositeSide))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, Constants.WhiteLongCastleKingSquare, piece, isLongCastle: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeLongCastle(sourceSquare, Constants.WhiteLongCastleKingSquare, piece);
}
}
else
Expand All @@ -1572,7 +1572,7 @@ internal static void GenerateCastlingMoves(ref int localIndex, Move[] movePool,
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.f8, position, oppositeSide)
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.g8, position, oppositeSide))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, Constants.BlackShortCastleKingSquare, piece, isShortCastle: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeShortCastle(sourceSquare, Constants.BlackShortCastleKingSquare, piece);
}

if (((position.Castle & (int)CastlingRights.BQ) != default)
Expand All @@ -1583,7 +1583,7 @@ internal static void GenerateCastlingMoves(ref int localIndex, Move[] movePool,
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.d8, position, oppositeSide)
&& !MakeMoveAttacks.IsSquaredAttackedBySide((int)BoardSquare.c8, position, oppositeSide))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, Constants.BlackLongCastleKingSquare, piece, isLongCastle: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeLongCastle(sourceSquare, Constants.BlackLongCastleKingSquare, piece);
}
}
}
Expand Down Expand Up @@ -1617,7 +1617,7 @@ internal static void GeneratePieceMoves(ref int localIndex, Move[] movePool, int

if (position.OccupancyBitBoards[(int)Side.Both].GetBit(targetSquare))
{
movePool[localIndex++] = MoveExtensions.Encode(sourceSquare, targetSquare, piece, isCapture: TRUE);
movePool[localIndex++] = MoveExtensions.EncodeCapture(sourceSquare, targetSquare, piece, 1);
}
else if (!capturesOnly)
{
Expand Down
32 changes: 16 additions & 16 deletions src/Lynx.Benchmark/MoveGeneratorParallel_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ internal static IEnumerable<Move> GeneratePawnMoves(Position position, int offse
var targetRank = (singlePushSquare / 8) + 1;
if (targetRank == 1 || targetRank == 8) // Promotion
{
yield return MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.Q + offset);
yield return MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.R + offset);
yield return MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.N + offset);
yield return MoveExtensions.Encode(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.B + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.Q + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.R + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.N + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, singlePushSquare, piece, promotedPiece: (int)Piece.B + offset);
}
else if (!capturesOnly)
{
Expand All @@ -240,7 +240,7 @@ internal static IEnumerable<Move> GeneratePawnMoves(Position position, int offse
if (!position.OccupancyBitBoards[2].GetBit(doublePushSquare)
&& ((sourceRank == 2 && position.Side == Side.Black) || (sourceRank == 7 && position.Side == Side.White)))
{
yield return MoveExtensions.Encode(sourceSquare, doublePushSquare, piece, isDoublePawnPush: TRUE);
yield return MoveExtensions.EncodeDoublePawnPush(sourceSquare, doublePushSquare, piece);
}
}
}
Expand All @@ -251,7 +251,7 @@ internal static IEnumerable<Move> GeneratePawnMoves(Position position, int offse
if (position.EnPassant != BoardSquare.noSquare && attacks.GetBit(position.EnPassant))
// We assume that position.OccupancyBitBoards[oppositeOccupancy].GetBit(targetSquare + singlePush) == true
{
yield return MoveExtensions.Encode(sourceSquare, (int)position.EnPassant, piece, isCapture: TRUE, isEnPassant: TRUE);
yield return MoveExtensions.EncodeEnPassant(sourceSquare, (int)position.EnPassant, piece);
}

// Captures
Expand All @@ -264,14 +264,14 @@ internal static IEnumerable<Move> GeneratePawnMoves(Position position, int offse
var targetRank = (targetSquare / 8) + 1;
if (targetRank == 1 || targetRank == 8) // Capture with promotion
{
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.Q + offset, isCapture: TRUE);
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.R + offset, isCapture: TRUE);
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.N + offset, isCapture: TRUE);
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.B + offset, isCapture: TRUE);
yield return MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.Q + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.R + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.N + offset);
yield return MoveExtensions.EncodePromotion(sourceSquare, targetSquare, piece, promotedPiece: (int)Piece.B + offset);
}
else
{
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, isCapture: TRUE);
yield return MoveExtensions.EncodeCapture(sourceSquare, targetSquare, piece);
}
}
}
Expand All @@ -297,7 +297,7 @@ internal static IEnumerable<Move> GenerateCastlingMoves(Position position, int o
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.f1, position, oppositeSide)
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.g1, position, oppositeSide))
{
yield return MoveExtensions.Encode(sourceSquare, Constants.WhiteShortCastleKingSquare, piece, isShortCastle: TRUE);
yield return MoveExtensions.EncodeShortCastle(sourceSquare, Constants.WhiteShortCastleKingSquare, piece);
}

if (((position.Castle & (int)CastlingRights.WQ) != default)
Expand All @@ -308,7 +308,7 @@ internal static IEnumerable<Move> GenerateCastlingMoves(Position position, int o
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.d1, position, oppositeSide)
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.c1, position, oppositeSide))
{
yield return MoveExtensions.Encode(sourceSquare, Constants.WhiteLongCastleKingSquare, piece, isLongCastle: TRUE);
yield return MoveExtensions.EncodeLongCastle(sourceSquare, Constants.WhiteLongCastleKingSquare, piece);
}
}
else
Expand All @@ -320,7 +320,7 @@ internal static IEnumerable<Move> GenerateCastlingMoves(Position position, int o
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.f8, position, oppositeSide)
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.g8, position, oppositeSide))
{
yield return MoveExtensions.Encode(sourceSquare, Constants.BlackShortCastleKingSquare, piece, isShortCastle: TRUE);
yield return MoveExtensions.EncodeShortCastle(sourceSquare, Constants.BlackShortCastleKingSquare, piece);
}

if (((position.Castle & (int)CastlingRights.BQ) != default)
Expand All @@ -331,7 +331,7 @@ internal static IEnumerable<Move> GenerateCastlingMoves(Position position, int o
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.d8, position, oppositeSide)
&& !Attacks.IsSquareAttackedBySide((int)BoardSquare.c8, position, oppositeSide))
{
yield return MoveExtensions.Encode(sourceSquare, Constants.BlackLongCastleKingSquare, piece, isLongCastle: TRUE);
yield return MoveExtensions.EncodeLongCastle(sourceSquare, Constants.BlackLongCastleKingSquare, piece);
}
}
}
Expand All @@ -358,7 +358,7 @@ internal static IEnumerable<Move> GeneratePieceMoves(int piece, Position positio

if (position.OccupancyBitBoards[(int)Side.Both].GetBit(targetSquare))
{
yield return MoveExtensions.Encode(sourceSquare, targetSquare, piece, isCapture: TRUE);
yield return MoveExtensions.EncodeCapture(sourceSquare, targetSquare, piece, 1);
}
else if (!capturesOnly)
{
Expand Down

0 comments on commit 46c80bb

Please sign in to comment.