From e8aec47b7131fa7e63d82bfabc43a20bd1459608 Mon Sep 17 00:00:00 2001 From: rektdie Date: Sun, 12 Oct 2025 23:40:18 +0200 Subject: [PATCH] added new movegen type: quiets | bench 5105878 --- source/board.cpp | 2 +- source/datagen.cpp | 8 +- source/movegen.h | 7 +- source/movegen.tpp | 222 +++++++++++++++++---------------------------- source/perft.cpp | 4 +- source/search.cpp | 9 +- source/uci.cpp | 2 +- source/utils.cpp | 4 +- 8 files changed, 100 insertions(+), 158 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 1c2cc4a..bdb8a16 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -84,7 +84,7 @@ void Board::SetByFen(std::string_view fen) { occupied = colors[White] | colors[Black]; hashKey = UTILS::GetHashKey(*this); MOVEGEN::GenThreatMaps(*this); - MOVEGEN::GenerateMoves(*this); + MOVEGEN::GenerateMoves(*this, true); int whiteKingFile = (pieces[King] & colors[White]).getLS1BIndex() % 8; int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8; diff --git a/source/datagen.cpp b/source/datagen.cpp index f3e0f16..bcc8847 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -72,7 +72,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) { bool plusOne = moveDist(rng); for (int count = 0; count < RAND_MOVES + plusOne; count++) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); bool isLegal = board.MakeMove(board.moveList[dist(rng)]); @@ -82,7 +82,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) { continue; } ctx->positionHistory[board.positionIndex] = board.hashKey; - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); if (IsGameOver(board, ctx)) break; } } @@ -179,7 +179,7 @@ static void PlayGames(int id, std::atomic& positions, std::atomic& st positions++; - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); } if (!SEARCH::IsDraw(board, ctx.get())) { @@ -399,4 +399,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int std::cout << std::endl; } -} \ No newline at end of file +} diff --git a/source/movegen.h b/source/movegen.h index d9f2fa0..7528835 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -4,8 +4,9 @@ #include "board.h" enum MovegenMode { - All, - Noisy + Quiet, + Noisy, + All }; namespace MOVEGEN { @@ -37,7 +38,7 @@ template void GenKingMoves(Board &board); template -void GenerateMoves(Board &board); +void GenerateMoves(Board &board, bool clear); void GenThreatMaps(Board &board); diff --git a/source/movegen.tpp b/source/movegen.tpp index 583ce95..161947f 100644 --- a/source/movegen.tpp +++ b/source/movegen.tpp @@ -11,8 +11,8 @@ void GenPawnMoves(Board &board) { Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8]; - if constexpr (mode == Noisy) { - pushes &= lastRank; + if constexpr (mode == Noisy || mode == All) { + Bitboard noisyPushes = pushes & lastRank; if (board.enPassantTarget != noEPTarget) { if (attacks.IsSet(board.enPassantTarget)) { @@ -35,8 +35,8 @@ void GenPawnMoves(Board &board) { captures.PopBit(targetSquare); } - while (pushes) { - int pushSquare = pushes.getLS1BIndex(); + while (noisyPushes) { + int pushSquare = noisyPushes.getLS1BIndex(); // Only promotions because NOISY board.AddMove(Move(square, pushSquare, knightPromotion)); @@ -44,29 +44,17 @@ void GenPawnMoves(Board &board) { board.AddMove(Move(square, pushSquare, rookPromotion)); board.AddMove(Move(square, pushSquare, queenPromotion)); - pushes.PopBit(pushSquare); + noisyPushes.PopBit(pushSquare); } pawns.PopBit(square); - continue; - } else { - // Checking for en passant - if (board.enPassantTarget != noEPTarget) { - if (attacks.IsSet(board.enPassantTarget)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - } + } + if constexpr (mode == Quiet || mode == All) { while (pushes) { int pushSquare = pushes.getLS1BIndex(); - // Checking for promotion - if (lastRank.IsSet(pushSquare)) { - board.AddMove(Move(square, pushSquare, knightPromotion)); - board.AddMove(Move(square, pushSquare, bishopPromotion)); - board.AddMove(Move(square, pushSquare, rookPromotion)); - board.AddMove(Move(square, pushSquare, queenPromotion)); - } else { + if (!lastRank.IsSet(pushSquare)) { if (std::abs(square - pushSquare) == 16) { board.AddMove(Move(square, pushSquare, doublePawnPush)); } else { @@ -76,22 +64,6 @@ void GenPawnMoves(Board &board) { pushes.PopBit(pushSquare); } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(targetSquare)) { - board.AddMove(Move(square, targetSquare, knightPromoCapture)); - board.AddMove(Move(square, targetSquare, bishopPromoCapture)); - board.AddMove(Move(square, targetSquare, rookPromoCapture)); - board.AddMove(Move(square, targetSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, targetSquare, capture)); - } - - captures.PopBit(targetSquare); - } } pawns.PopBit(square); @@ -108,7 +80,7 @@ void GenKnightMoves(Board &board) { Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~captures; - if constexpr (mode == Noisy) { + if constexpr (mode == Noisy || mode == All) { while (captures) { int targetSquare = captures.getLS1BIndex(); @@ -118,25 +90,18 @@ void GenKnightMoves(Board &board) { } knights.PopBit(square); - continue; - } else { - while (moves) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - } + + if constexpr (mode == Quiet || mode == All) { + while (moves) { + int targetSquare = moves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + moves.PopBit(targetSquare); + } + } + knights.PopBit(square); } } @@ -151,7 +116,7 @@ void GenRookMoves(Board &board) { Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~captures; - if constexpr (mode == Noisy) { + if constexpr (mode == Noisy || mode == All) { while (captures) { int targetSquare = captures.getLS1BIndex(); @@ -161,24 +126,17 @@ void GenRookMoves(Board &board) { } rooks.PopBit(square); - continue; } - while (moves) { - int targetSquare = moves.getLS1BIndex(); + if constexpr (mode == Quiet || mode == All) { + while (moves) { + int targetSquare = moves.getLS1BIndex(); - board.AddMove(Move(square, targetSquare, quiet)); + board.AddMove(Move(square, targetSquare, quiet)); - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } + moves.PopBit(targetSquare); + } + } rooks.PopBit(square); } @@ -194,7 +152,7 @@ void GenBishopMoves(Board &board) { Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~captures; - if constexpr (mode == Noisy) { + if constexpr (mode == Noisy || mode == All) { while (captures) { int targetSquare = captures.getLS1BIndex(); @@ -204,24 +162,17 @@ void GenBishopMoves(Board &board) { } bishops.PopBit(square); - continue; } - while (moves) { - int targetSquare = moves.getLS1BIndex(); + if constexpr (mode == Quiet || mode == All) { + while (moves) { + int targetSquare = moves.getLS1BIndex(); - board.AddMove(Move(square, targetSquare, quiet)); + board.AddMove(Move(square, targetSquare, quiet)); - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } + moves.PopBit(targetSquare); + } + } bishops.PopBit(square); } @@ -237,7 +188,7 @@ void GenQueenMoves(Board &board) { Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~captures; - if constexpr (mode == Noisy) { + if constexpr (mode == Noisy || mode == All) { while (captures) { int targetSquare = captures.getLS1BIndex(); @@ -247,24 +198,17 @@ void GenQueenMoves(Board &board) { } queens.PopBit(square); - continue; } - while (moves) { - int targetSquare = moves.getLS1BIndex(); + if constexpr (mode == Quiet || mode == All) { + while (moves) { + int targetSquare = moves.getLS1BIndex(); - board.AddMove(Move(square, targetSquare, quiet)); + board.AddMove(Move(square, targetSquare, quiet)); - moves.PopBit(targetSquare); - } - - while (captures) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } + moves.PopBit(targetSquare); + } + } queens.PopBit(square); } @@ -277,7 +221,7 @@ void GenKingMoves(Board &board) { Bitboard captures = moves & board.colors[!board.sideToMove]; moves &= ~board.colors[!board.sideToMove]; - if constexpr (mode == Noisy) { + if constexpr (mode == Noisy || mode == All) { while (captures) { int square = captures.getLS1BIndex(); @@ -286,57 +230,53 @@ void GenKingMoves(Board &board) { captures.PopBit(square); } - return; } - while (moves) { - int square = moves.getLS1BIndex(); + if constexpr (mode == Quiet || mode == All) { + while (moves) { + int square = moves.getLS1BIndex(); - board.AddMove(Move(kingSquare, square, quiet)); - - moves.PopBit(square); - } - - while (captures) { - int square = captures.getLS1BIndex(); - - Move move(kingSquare, square, capture); - board.AddMove(move); - - captures.PopBit(square); - } - - // Generating castles - int kingRight = board.sideToMove ? blackKingRight : whiteKingRight; - int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight; - - if (board.castlingRights & queenRight) { - U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f; - U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c; - int targetSquare = board.sideToMove ? c8 : c1; - if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 - && !(mask & board.colorThreats[!board.sideToMove])) { - board.AddMove(Move(kingSquare, targetSquare, queenCastle)); + board.AddMove(Move(kingSquare, square, quiet)); + moves.PopBit(square); } - } - if (board.castlingRights & kingRight) { - U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; - U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; - int targetSquare = board.sideToMove ? g8 : g1; - if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 - && !(mask & board.colorThreats[!board.sideToMove])) { - board.AddMove(Move(kingSquare, targetSquare, kingCastle)); + // Generating castles + int kingRight = board.sideToMove ? blackKingRight : whiteKingRight; + int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight; + + if (board.castlingRights & queenRight) { + U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f; + U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c; + int targetSquare = board.sideToMove ? c8 : c1; + if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 + && !(mask & board.colorThreats[!board.sideToMove])) { + board.AddMove(Move(kingSquare, targetSquare, queenCastle)); + + } } + + if (board.castlingRights & kingRight) { + U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; + U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; + int targetSquare = board.sideToMove ? g8 : g1; + if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 + && !(mask & board.colorThreats[!board.sideToMove])) { + board.AddMove(Move(kingSquare, targetSquare, kingCastle)); + } + } + } } template -void GenerateMoves(Board &board) { - board.ResetMoves(); - board.pieceThreats = std::array(); - board.colorThreats = std::array(); +void GenerateMoves(Board &board, bool clear) { + if (clear) { + board.ResetMoves(); + board.pieceThreats = {}; + board.colorThreats = {}; + } + GenThreatMaps(board); GenPawnMoves(board); diff --git a/source/perft.cpp b/source/perft.cpp index 46dbe3e..263fd2a 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -3,7 +3,7 @@ #include "movegen.h" static U64 HelperPerft(Board board, int depth) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); if (depth == 0) return 1ULL; @@ -19,7 +19,7 @@ static U64 HelperPerft(Board board, int depth) { } void Perft(Board &board, int depth) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); U64 totalNodes = 0; diff --git a/source/search.cpp b/source/search.cpp index d0a4ac6..6d25c5f 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -321,7 +321,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear const int fpScore = bestScore + 100; - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); SortMoves(board, ply, ctx); @@ -486,7 +486,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC && (!entry.bestMove || !entry.bestMove.IsQuiet()) && !(ttHit && entry.depth >= probcutDepth && entry.score < probcutBeta)) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); SortMoves(board, ply, ctx); const int seeThreshold = (probcutBeta - staticEval) * 15 / 16; @@ -526,10 +526,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC return score; } } + MOVEGEN::GenerateMoves(board, false); + } else { + MOVEGEN::GenerateMoves(board, true); } - MOVEGEN::GenerateMoves(board); - SortMoves(board, ply, ctx); int score = -inf; diff --git a/source/uci.cpp b/source/uci.cpp index 36a47f0..fa59d93 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -45,7 +45,7 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search } } - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); } static double ReadParam(const std::string& param, const std::string &command) { diff --git a/source/utils.cpp b/source/utils.cpp index b5588bb..5caf46d 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -111,7 +111,7 @@ int parseSquare(std::string_view str) { } Move parseMove(Board &board, std::string_view str) { - MOVEGEN::GenerateMoves(board); + MOVEGEN::GenerateMoves(board, true); int from = parseSquare(str.substr(0, 2)); int to = parseSquare(str.substr(2, 2)); @@ -202,4 +202,4 @@ int ConvertToWhiteRelative(Board &board, int score) { return board.sideToMove == White ? score : -score; } -} \ No newline at end of file +}