From 3225b9c665992f0a6cac391717af68bdeead22b7 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sat, 12 Apr 2025 20:39:00 +0200 Subject: [PATCH] encapsualted position index and array | bench 1857933 --- source/benchmark.cpp | 3 --- source/board.cpp | 3 ++- source/board.h | 5 ++--- source/datagen.cpp | 10 +++------- source/perft.cpp | 2 -- source/search.cpp | 16 ++++++++-------- source/search.h | 5 ++++- source/uci.cpp | 3 --- 8 files changed, 19 insertions(+), 28 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 4c24ac1..4eef752 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,14 +65,12 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - positionIndex = 0; // Clearing TT.Clear(); SearchContext ctx; - Stopwatch sw; for (int i = 0; i < fenPositions.size(); i++) { board.SetByFen(fenPositions[i]); @@ -81,5 +79,4 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index eabfae5..9a427ca 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -26,6 +26,8 @@ void Board::Reset() { moveList = std::array(); + positionIndex = 0; + currentMoveIndex = 0; hashKey = 0ULL; @@ -391,7 +393,6 @@ bool Board::MakeMove(Move move) { } positionIndex++; - positionHistory[positionIndex] = hashKey; return true; } diff --git a/source/board.h b/source/board.h index 5ac1cbc..702589a 100644 --- a/source/board.h +++ b/source/board.h @@ -10,9 +10,6 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline int positionIndex = 0; -inline U64 positionHistory[1000]; - class Board { public: Board() { @@ -37,6 +34,8 @@ public: uint8_t castlingRights = 0; int enPassantTarget = noEPTarget; + short positionIndex = 0; + int halfMoves = 0; int fullMoves = 1; diff --git a/source/datagen.cpp b/source/datagen.cpp index 573f47a..2e33f63 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -62,9 +62,8 @@ static void PlayRandMoves(Board &board) { MOVEGEN::GenerateMoves(board); } -static bool IsGameOver(Board &board) { - bool isDraw = SEARCH::IsDraw(board); - if (isDraw) return true; +static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { + if (SEARCH::IsDraw(board, ctx)) return true; int moveSeen = 0; @@ -73,7 +72,6 @@ static bool IsGameOver(Board &board) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - positionIndex--; moveSeen++; } @@ -99,8 +97,6 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector& positions, U64 targetPositions, std::vector(board); - while (!IsGameOver(board)) { + while (!IsGameOver(board, ctx)) { SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; diff --git a/source/perft.cpp b/source/perft.cpp index 9f0ca50..46dbe3e 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -14,7 +14,6 @@ static U64 HelperPerft(Board board, int depth) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; nodes += HelperPerft(copy, depth - 1); - positionIndex--; } return nodes; } @@ -30,7 +29,6 @@ void Perft(Board &board, int depth) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; U64 nodeCount = HelperPerft(copy, depth - 1); - positionIndex--; totalNodes += nodeCount; board.moveList[i].PrintMove(); diff --git a/source/search.cpp b/source/search.cpp index a7c2599..a15d302 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -92,9 +92,9 @@ void ListScores(Board &board, int ply, SearchContext& ctx) { } } -static bool IsThreefold(Board &board) { - for (int i = 0; i < positionIndex; i++) { - if (positionHistory[i] == board.hashKey) { +static bool IsThreefold(Board &board, SearchContext& ctx) { + for (int i = 0; i < board.positionIndex; i++) { + if (ctx.positionHistory[i] == board.hashKey) { // repetition found return true; } @@ -113,8 +113,8 @@ static bool IsInsuffMat(Board &board) { && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); } -bool IsDraw(Board &board) { - return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); +bool IsDraw(Board &board, SearchContext& ctx) { + return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); } static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { @@ -172,9 +172,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; - positionIndex--; if (score >= beta) { return score; @@ -217,7 +217,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } ctx.pvLine.SetLength(ply); - if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; + if (ply && (IsDraw(board, ctx))) return 0; // if NOT PV node then we try to hit the TTable @@ -287,6 +287,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); @@ -316,7 +317,6 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC } moveSeen++; - positionIndex--; if (ctx.searchStopped) return 0; diff --git a/source/search.h b/source/search.h index 8225ad3..5a39544 100644 --- a/source/search.h +++ b/source/search.h @@ -114,6 +114,8 @@ public: std::array, 2> killerMoves{}; + std::array positionHistory{}; + Stopwatch sw; SearchContext(){ @@ -121,6 +123,7 @@ public: history.Clear(); sw.Restart(); killerMoves = {}; + positionHistory = {}; } }; @@ -130,7 +133,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -bool IsDraw(Board &board); +bool IsDraw(Board &board, SearchContext& ctx); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605, diff --git a/source/uci.cpp b/source/uci.cpp index 01e131a..11b1370 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -125,7 +125,6 @@ void UCILoop(Board &board) { // parse UCI "position" command if (input.find("position") != std::string::npos) { ParsePosition(board, input); - positionIndex = 0; continue; } @@ -139,8 +138,6 @@ void UCILoop(Board &board) { ctx.killerMoves = {}; ctx.history.Clear(); - - positionIndex = 0; continue; }