From e9f5a15d6bb414fe144d2abb70881d89f86c590a Mon Sep 17 00:00:00 2001 From: rektdie Date: Mon, 10 Mar 2025 19:05:29 +0100 Subject: [PATCH] introduced TTable class --- source/benchmark.h | 1 + source/search.cpp | 8 ++++---- source/search.h | 3 +-- source/tt.cpp | 8 ++++---- source/tt.h | 35 ++++++++++++++++++++++++++++++----- source/uci.cpp | 4 ++-- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/source/benchmark.h b/source/benchmark.h index e98d2d2..69c9c98 100644 --- a/source/benchmark.h +++ b/source/benchmark.h @@ -1,3 +1,4 @@ +#pragma once #include void RunBenchmark(); diff --git a/source/search.cpp b/source/search.cpp index e28b619..b1d35ce 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -25,7 +25,7 @@ static int ScoreMove(Board &board, Move &move) { if (attackerType == nullPieceType || targetType == nullPieceType) return 0; - TTEntry *current = &TTable[board.hashKey % hashSize]; + TTEntry *current = TT.GetRawEntry(board.hashKey); if (current->bestMove == move) { return 50000; } @@ -151,7 +151,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { // if NOT PV node then we try to hit the TTable if (beta - alpha == 1) { - SearchResults entry = ReadEntry(board.hashKey, depth, alpha, beta); + SearchResults entry = TT.ReadEntry(board.hashKey, depth, alpha, beta); if (entry.score != invalidEntry) { return entry; } @@ -236,7 +236,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { killerMoves[1][ply] = killerMoves[0][ply]; killerMoves[0][ply] = board.moveList[i]; - WriteEntry(board.hashKey, depth, score, CutNode, Move()); + TT.WriteEntry(board.hashKey, depth, score, CutNode, Move()); return score; } @@ -250,7 +250,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { } } - WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); + TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove); if (searchStopped) return 0; return results; } diff --git a/source/search.h b/source/search.h index 9554d74..77cf87a 100644 --- a/source/search.h +++ b/source/search.h @@ -9,7 +9,6 @@ constexpr int maxDepth = 64; extern U64 nodes; extern bool benchStarted; -constexpr int invalidEntry = 111111; constexpr int inf = 100000; class SearchResults { @@ -74,4 +73,4 @@ constexpr int moveScoreTable[6][6] = { 100, 200, 300, 400, 500, 600 }; -void ListScores(Board &board); +void ListScores(Board &board); \ No newline at end of file diff --git a/source/tt.cpp b/source/tt.cpp index 55d246e..c92ceae 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -77,8 +77,8 @@ U64 GetHashKey(Board &board) { return key; } -SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { - TTEntry *current = &TTable[hashKey % hashSize]; +SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { + TTEntry *current = &table[hashKey % hashSize]; if (current->hashKey == hashKey) { if (current->depth >= depth) { @@ -96,8 +96,8 @@ SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { return invalidEntry; } -void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) { - TTEntry *current = &TTable[hashKey % hashSize]; +void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) { + TTEntry *current = &table[hashKey % hashSize]; current->hashKey = hashKey; current->nodeType = nodeType; diff --git a/source/tt.h b/source/tt.h index 68d4db6..6e24179 100644 --- a/source/tt.h +++ b/source/tt.h @@ -18,7 +18,7 @@ void InitZobrist(); U64 GetHashKey(Board &board); class TTEntry { -public: + public: U64 hashKey; int depth; int score; @@ -27,8 +27,33 @@ public: }; // 5 MB -inline int hashSize = 5000000 / sizeof(TTEntry); -inline std::vector TTable(hashSize); +inline int hashSize = (5 * 1000000) / sizeof(TTEntry); +constexpr int invalidEntry = 111111; -SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta); -void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); +class TTable { +private: + std::vector table; +public: + TTable() { + table.reserve(hashSize); + } + + void Resize(int size) { + table.resize(size); + } + + void Clear() { + table.clear(); + } + + TTEntry* GetRawEntry(U64 &hashKey) { + TTEntry *current = &table[hashKey % hashSize]; + return current; + } + + SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta); + + void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); +}; + +inline TTable TT; \ No newline at end of file diff --git a/source/uci.cpp b/source/uci.cpp index 5c5a9cb..9b1bc6c 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -160,7 +160,7 @@ void ParseGo(Board &board, std::string &command) { static void SetOption(std::string &command) { if (command.find("Hash") != std::string::npos) { hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); - TTable.resize(hashSize); + TT.Resize(hashSize); } } @@ -208,7 +208,7 @@ void UCILoop(Board &board) { // parse UCI "ucinewgame" command if (input.find("ucinewgame") != std::string::npos) { board.SetByFen(StartingFen); - TTable.clear(); + TT.Clear(); continue; }