introduced TTable class

This commit is contained in:
rektdie committed 2025-03-10 19:05:29 +01:00
1 parent 9622a28db5
commit e9f5a15d6b
6 files changed
+38 -13

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
#pragma once
#include <string> #include <string>
void RunBenchmark(); void RunBenchmark();
+4 -4
View File
@@ -25,7 +25,7 @@ static int ScoreMove(Board &board, Move &move) {
if (attackerType == nullPieceType || targetType == nullPieceType) return 0; if (attackerType == nullPieceType || targetType == nullPieceType) return 0;
TTEntry *current = &TTable[board.hashKey % hashSize]; TTEntry *current = TT.GetRawEntry(board.hashKey);
if (current->bestMove == move) { if (current->bestMove == move) {
return 50000; 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 NOT PV node then we try to hit the TTable
if (beta - alpha == 1) { 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) { if (entry.score != invalidEntry) {
return entry; return entry;
} }
@@ -236,7 +236,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) {
killerMoves[1][ply] = killerMoves[0][ply]; killerMoves[1][ply] = killerMoves[0][ply];
killerMoves[0][ply] = board.moveList[i]; killerMoves[0][ply] = board.moveList[i];
WriteEntry(board.hashKey, depth, score, CutNode, Move()); TT.WriteEntry(board.hashKey, depth, score, CutNode, Move());
return score; 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; if (searchStopped) return 0;
return results; return results;
} }
-1
View File
@@ -9,7 +9,6 @@ constexpr int maxDepth = 64;
extern U64 nodes; extern U64 nodes;
extern bool benchStarted; extern bool benchStarted;
constexpr int invalidEntry = 111111;
constexpr int inf = 100000; constexpr int inf = 100000;
class SearchResults { class SearchResults {
+4 -4
View File
@@ -77,8 +77,8 @@ U64 GetHashKey(Board &board) {
return key; return key;
} }
SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) { SearchResults TTable::ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
TTEntry *current = &TTable[hashKey % hashSize]; TTEntry *current = &table[hashKey % hashSize];
if (current->hashKey == hashKey) { if (current->hashKey == hashKey) {
if (current->depth >= depth) { if (current->depth >= depth) {
@@ -96,8 +96,8 @@ SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta) {
return invalidEntry; return invalidEntry;
} }
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) { void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
TTEntry *current = &TTable[hashKey % hashSize]; TTEntry *current = &table[hashKey % hashSize];
current->hashKey = hashKey; current->hashKey = hashKey;
current->nodeType = nodeType; current->nodeType = nodeType;
+27 -2
View File
@@ -27,8 +27,33 @@ public:
}; };
// 5 MB // 5 MB
inline int hashSize = 5000000 / sizeof(TTEntry); inline int hashSize = (5 * 1000000) / sizeof(TTEntry);
inline std::vector<TTEntry> TTable(hashSize); constexpr int invalidEntry = 111111;
class TTable {
private:
std::vector<TTEntry> 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); SearchResults ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove); void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
};
inline TTable TT;
+2 -2
View File
@@ -160,7 +160,7 @@ void ParseGo(Board &board, std::string &command) {
static void SetOption(std::string &command) { static void SetOption(std::string &command) {
if (command.find("Hash") != std::string::npos) { if (command.find("Hash") != std::string::npos) {
hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry); 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 // parse UCI "ucinewgame" command
if (input.find("ucinewgame") != std::string::npos) { if (input.find("ucinewgame") != std::string::npos) {
board.SetByFen(StartingFen); board.SetByFen(StartingFen);
TTable.clear(); TT.Clear();
continue; continue;
} }