Skip to content

Commit

Permalink
Make ZobristTable.CastleHash static
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 5, 2024
1 parent df7077f commit d82af44
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/Lynx/ZobristTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ public static class ZobristTable
private static readonly long BK_Hash = _table[(int)BoardSquare.c8, (int)Piece.p];
private static readonly long BQ_Hash = _table[(int)BoardSquare.d8, (int)Piece.p];

private static readonly long WK_Hash_XOR_WQ_Hash = WK_Hash ^ WQ_Hash;
private static readonly long WK_Hash_XOR_BK_Hash = WK_Hash ^ BK_Hash;
private static readonly long WK_Hash_XOR_BQ_Hash = WK_Hash ^ BQ_Hash;
private static readonly long WQ_Hash_XOR_BK_Hash = WQ_Hash ^ BK_Hash;
private static readonly long WQ_Hash_XOR_BQ_Hash = WQ_Hash ^ BQ_Hash;
private static readonly long BK_Hash_XOR_BQ_Hash = BK_Hash ^ BQ_Hash;

private static readonly long WK_Hash_XOR_WQ_Hash_XOR_BK_Hash = WK_Hash ^ WQ_Hash ^ BK_Hash;
private static readonly long WK_Hash_XOR_WQ_Hash_XOR_BQ_Hash = WK_Hash ^ WQ_Hash ^ BQ_Hash;
private static readonly long WK_Hash_XOR_BK_Hash_XOR_BQ_Hash = WK_Hash ^ BK_Hash ^ BQ_Hash;
private static readonly long WQ_Hash_XOR_BK_Hash_XOR_BQ_Hash = WQ_Hash ^ BK_Hash ^ BQ_Hash;

private static readonly long WK_Hash_XOR_WQ_Hash_XOR_BK_Hash_XOR_BQ_Hash = WK_Hash ^ WQ_Hash ^ BK_Hash ^ BQ_Hash;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long PieceHash(int boardSquare, int piece) => _table[boardSquare, piece];

Expand Down Expand Up @@ -66,27 +80,27 @@ public static long CastleHash(byte castle)
{
return castle switch
{
0 => 0, // - | -

(byte)CastlingRights.WK => WK_Hash, // K | -
(byte)CastlingRights.WQ => WQ_Hash, // Q | -
(byte)CastlingRights.BK => BK_Hash, // - | k
(byte)CastlingRights.BQ => BQ_Hash, // - | q

(byte)CastlingRights.WK | (byte)CastlingRights.WQ => WK_Hash ^ WQ_Hash, // KQ | -
(byte)CastlingRights.WK | (byte)CastlingRights.BK => WK_Hash ^ BK_Hash, // K | k
(byte)CastlingRights.WK | (byte)CastlingRights.BQ => WK_Hash ^ BQ_Hash, // K | q
(byte)CastlingRights.WQ | (byte)CastlingRights.BK => WQ_Hash ^ BK_Hash, // Q | k
(byte)CastlingRights.WQ | (byte)CastlingRights.BQ => WQ_Hash ^ BQ_Hash, // Q | q
(byte)CastlingRights.BK | (byte)CastlingRights.BQ => BK_Hash ^ BQ_Hash, // - | kq

(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BK => WK_Hash ^ WQ_Hash ^ BK_Hash, // KQ | k
(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BQ => WK_Hash ^ WQ_Hash ^ BQ_Hash, // KQ | q
(byte)CastlingRights.WK | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => WK_Hash ^ BK_Hash ^ BQ_Hash, // K | kq
(byte)CastlingRights.WQ | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => WQ_Hash ^ BK_Hash ^ BQ_Hash, // Q | kq

(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => // KQ | kq
WK_Hash ^ WQ_Hash ^ BK_Hash ^ BQ_Hash,
0 => 0, // - | -

(byte)CastlingRights.WK => WK_Hash, // K | -
(byte)CastlingRights.WQ => WQ_Hash, // Q | -
(byte)CastlingRights.BK => BK_Hash, // - | k
(byte)CastlingRights.BQ => BQ_Hash, // - | q

(byte)CastlingRights.WK | (byte)CastlingRights.WQ => WK_Hash_XOR_WQ_Hash, // KQ | -
(byte)CastlingRights.WK | (byte)CastlingRights.BK => WK_Hash_XOR_BK_Hash, // K | k
(byte)CastlingRights.WK | (byte)CastlingRights.BQ => WK_Hash_XOR_BQ_Hash, // K | q
(byte)CastlingRights.WQ | (byte)CastlingRights.BK => WQ_Hash_XOR_BK_Hash, // Q | k
(byte)CastlingRights.WQ | (byte)CastlingRights.BQ => WQ_Hash_XOR_BQ_Hash, // Q | q
(byte)CastlingRights.BK | (byte)CastlingRights.BQ => BK_Hash_XOR_BQ_Hash, // - | kq

(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BK => WK_Hash_XOR_WQ_Hash_XOR_BK_Hash, // KQ | k
(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BQ => WK_Hash_XOR_WQ_Hash_XOR_BQ_Hash, // KQ | q
(byte)CastlingRights.WK | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => WK_Hash_XOR_BK_Hash_XOR_BQ_Hash, // K | kq
(byte)CastlingRights.WQ | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => WQ_Hash_XOR_BK_Hash_XOR_BQ_Hash, // Q | kq

(byte)CastlingRights.WK | (byte)CastlingRights.WQ | (byte)CastlingRights.BK | (byte)CastlingRights.BQ => // KQ | kq
WK_Hash_XOR_WQ_Hash_XOR_BK_Hash_XOR_BQ_Hash,

_ => throw new($"Unexpected castle encoded number: {castle}")
};
Expand Down

0 comments on commit d82af44

Please sign in to comment.