Skip to content

Commit

Permalink
⚡ Optimize castling and en-passant moves Zobrist hashing (#577)
Browse files Browse the repository at this point in the history
- Optimize castling and en-passant moves Zobrist hashing
- Better align struct
  • Loading branch information
eduherminio authored Jan 4, 2024
1 parent bde7e04 commit adba5ff
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 66 deletions.
41 changes: 21 additions & 20 deletions src/Lynx.Benchmark/MakeUnmakeMove_implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ public struct MakeMovePosition

public BoardSquare EnPassant { get; private set; }

public int Castle { get; private set; }
public byte Castle { get; private set; }

public MakeMovePosition(string fen) : this(FENParser.ParseFEN(fen))
{
}

public MakeMovePosition((BitBoard[] PieceBitBoards, BitBoard[] OccupancyBitBoards, Side Side, int Castle, BoardSquare EnPassant,
public MakeMovePosition((BitBoard[] PieceBitBoards, BitBoard[] OccupancyBitBoards, Side Side, byte Castle, BoardSquare EnPassant,
int HalfMoveClock/*, int FullMoveCounter*/) parsedFEN)
{
PieceBitBoards = parsedFEN.PieceBitBoards;
Expand Down Expand Up @@ -326,7 +326,7 @@ public MakeMovePosition(MakeMovePosition position, Move move) : this(position)
public MakeMoveGameState MakeMove_Original(Move move)
{
int capturedPiece = -1;
int castleCopy = Castle;
var castleCopy = Castle;
BoardSquare enpassantCopy = EnPassant;

var oldSide = Side;
Expand Down Expand Up @@ -549,7 +549,7 @@ public void UnmakeMove_Original(Move move, MakeMoveGameState gameState)
public MakeMoveGameStateWithZobristKey MakeMove_WithZobristKey(Move move)
{
int capturedPiece = -1;
int castleCopy = Castle;
var castleCopy = Castle;
BoardSquare enpassantCopy = EnPassant;
long uniqueIdentifierCopy = UniqueIdentifier;

Expand Down Expand Up @@ -814,11 +814,11 @@ public readonly struct MakeMoveGameState
{
public readonly int CapturedPiece;

public readonly int Castle;
public readonly byte Castle;

public readonly BoardSquare EnPassant;

public MakeMoveGameState(int capturedPiece, int castle, BoardSquare enpassant)
public MakeMoveGameState(int capturedPiece, byte castle, BoardSquare enpassant)
{
CapturedPiece = capturedPiece;
Castle = castle;
Expand All @@ -828,15 +828,15 @@ public MakeMoveGameState(int capturedPiece, int castle, BoardSquare enpassant)

public readonly struct MakeMoveGameStateWithZobristKey
{
public readonly int CapturedPiece;
public readonly long ZobristKey;

public readonly int Castle;
public readonly int CapturedPiece;

public readonly BoardSquare EnPassant;

public readonly long ZobristKey;
public readonly byte Castle;

public MakeMoveGameStateWithZobristKey(int capturedPiece, int castle, BoardSquare enpassant, long zobristKey)
public MakeMoveGameStateWithZobristKey(int capturedPiece, byte castle, BoardSquare enpassant, long zobristKey)
{
CapturedPiece = capturedPiece;
Castle = castle;
Expand All @@ -845,7 +845,7 @@ public MakeMoveGameStateWithZobristKey(int capturedPiece, int castle, BoardSquar
}
}

#region ;(
#region

public static class MakeMoveZobristTable
{
Expand Down Expand Up @@ -897,7 +897,7 @@ public static long SideHash()
/// <param name="castle"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long CastleHash(int castle)
public static long CastleHash(byte castle)
{
long combinedHash = 0;

Expand Down Expand Up @@ -976,8 +976,9 @@ public static long PositionHash(MakeMovePosition position)

public static class MakeMoveMoveGenerator
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

#if DEBUG
private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
#endif
private const int TRUE = 1;

/// <summary>
Expand All @@ -988,16 +989,16 @@ public static class MakeMoveMoveGenerator
{
(int origin, BitBoard _) => MakeMoveAttacks.PawnAttacks[(int)Side.White, origin],
(int origin, BitBoard _) => MakeMoveAttacks.KnightAttacks[origin],
(int origin, BitBoard occupancy) => MakeMoveAttacks.BishopAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.RookAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.QueenAttacks(origin, occupancy),
MakeMoveAttacks.BishopAttacks,
MakeMoveAttacks.RookAttacks,
MakeMoveAttacks.QueenAttacks,
(int origin, BitBoard _) => MakeMoveAttacks.KingAttacks[origin],

(int origin, BitBoard _) => MakeMoveAttacks.PawnAttacks[(int)Side.Black, origin],
(int origin, BitBoard _) => MakeMoveAttacks.KnightAttacks[origin],
(int origin, BitBoard occupancy) => MakeMoveAttacks.BishopAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.RookAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.QueenAttacks(origin, occupancy),
MakeMoveAttacks.BishopAttacks,
MakeMoveAttacks.RookAttacks,
MakeMoveAttacks.QueenAttacks,
(int origin, BitBoard _) => MakeMoveAttacks.KingAttacks[origin],
};

Expand Down
40 changes: 21 additions & 19 deletions src/Lynx.Benchmark/MakeUnmakeMove_integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ public struct MakeMovePosition

public BoardSquare EnPassant { get; private set; }

public int Castle { get; private set; }
public byte Castle { get; private set; }

public MakeMovePosition(string fen) : this(FENParser.ParseFEN(fen))
{
}

public MakeMovePosition((BitBoard[] PieceBitBoards, BitBoard[] OccupancyBitBoards, Side Side, int Castle, BoardSquare EnPassant,
public MakeMovePosition((BitBoard[] PieceBitBoards, BitBoard[] OccupancyBitBoards, Side Side, byte Castle, BoardSquare EnPassant,
int HalfMoveClock/*, int FullMoveCounter*/) parsedFEN)
{
PieceBitBoards = parsedFEN.PieceBitBoards;
Expand Down Expand Up @@ -484,7 +484,7 @@ public MakeMovePosition(MakeMovePosition position, Move move) : this(position)
public MakeMoveGameState MakeMove_Original(Move move)
{
int capturedPiece = -1;
int castleCopy = Castle;
var castleCopy = Castle;
BoardSquare enpassantCopy = EnPassant;

var oldSide = Side;
Expand Down Expand Up @@ -606,7 +606,7 @@ public MakeMoveGameState MakeMove_Original(Move move)
public void MakeMove_PassOut(Move move, out MakeMoveGameState_PassOut gameState)
{
int capturedPiece = -1;
int castleCopy = Castle;
var castleCopy = Castle;
BoardSquare enpassantCopy = EnPassant;

var oldSide = Side;
Expand Down Expand Up @@ -728,7 +728,7 @@ public void MakeMove_PassOut(Move move, out MakeMoveGameState_PassOut gameState)
public void MakeMove_PassRef(Move move, ref MakeMoveGameState_PassRef gameState)
{
int capturedPiece = -1;
int castleCopy = Castle;
var castleCopy = Castle;
BoardSquare enpassantCopy = EnPassant;

var oldSide = Side;
Expand Down Expand Up @@ -1212,13 +1212,13 @@ public readonly bool WasProduceByAValidMove()

public readonly struct MakeMoveGameState
{
public readonly int CapturedPiece;
public readonly byte Castle;

public readonly int Castle;
public readonly int CapturedPiece;

public readonly BoardSquare EnPassant;

public MakeMoveGameState(int capturedPiece, int castle, BoardSquare enpassant)
public MakeMoveGameState(int capturedPiece, byte castle, BoardSquare enpassant)
{
CapturedPiece = capturedPiece;
Castle = castle;
Expand All @@ -1230,11 +1230,11 @@ public struct MakeMoveGameState_PassOut
{
public int CapturedPiece;

public int Castle;
public byte Castle;

public BoardSquare EnPassant;

public MakeMoveGameState_PassOut(int capturedPiece, int castle, BoardSquare enpassant)
public MakeMoveGameState_PassOut(int capturedPiece, byte castle, BoardSquare enpassant)
{
CapturedPiece = capturedPiece;
Castle = castle;
Expand All @@ -1247,13 +1247,13 @@ public struct MakeMoveGameState_PassRef
#pragma warning disable S1104 // Fields should not have public accessibility
public int CapturedPiece;

public int Castle;
public byte Castle;

public BoardSquare EnPassant;
#pragma warning restore S1104 // Fields should not have public accessibility
}

#region ;(
#region

public static class MakeMoveZobristTable
{
Expand Down Expand Up @@ -1384,7 +1384,9 @@ public static long PositionHash(MakeMovePosition position)

public static class MakeMoveMoveGenerator
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
#if DEBUG
private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
#endif

private const int TRUE = 1;

Expand All @@ -1396,16 +1398,16 @@ public static class MakeMoveMoveGenerator
{
(int origin, BitBoard _) => MakeMoveAttacks.PawnAttacks[(int)Side.White, origin],
(int origin, BitBoard _) => MakeMoveAttacks.KnightAttacks[origin],
(int origin, BitBoard occupancy) => MakeMoveAttacks.BishopAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.RookAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.QueenAttacks(origin, occupancy),
MakeMoveAttacks.BishopAttacks,
MakeMoveAttacks.RookAttacks,
MakeMoveAttacks.QueenAttacks,
(int origin, BitBoard _) => MakeMoveAttacks.KingAttacks[origin],

(int origin, BitBoard _) => MakeMoveAttacks.PawnAttacks[(int)Side.Black, origin],
(int origin, BitBoard _) => MakeMoveAttacks.KnightAttacks[origin],
(int origin, BitBoard occupancy) => MakeMoveAttacks.BishopAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.RookAttacks(origin, occupancy),
(int origin, BitBoard occupancy) => MakeMoveAttacks.QueenAttacks(origin, occupancy),
MakeMoveAttacks.BishopAttacks,
MakeMoveAttacks.RookAttacks,
MakeMoveAttacks.QueenAttacks,
(int origin, BitBoard _) => MakeMoveAttacks.KingAttacks[origin],
};

Expand Down
Loading

0 comments on commit adba5ff

Please sign in to comment.