From d82af44bfe62a82d3510accaba14bc95897f6368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Fri, 5 Jan 2024 13:07:04 +0100 Subject: [PATCH] Make `ZobristTable.CastleHash` static --- src/Lynx/ZobristTable.cs | 56 +++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Lynx/ZobristTable.cs b/src/Lynx/ZobristTable.cs index ba648b853..75f3d6044 100644 --- a/src/Lynx/ZobristTable.cs +++ b/src/Lynx/ZobristTable.cs @@ -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]; @@ -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}") };