added new movegen type: quiets | bench 5105878

This commit is contained in:
rektdie committed 2025-10-12 23:40:18 +02:00
1 parent 17a0a97896
commit e8aec47b71
8 files changed
+100 -158

No files matched your search

+1 -1
View File
@@ -84,7 +84,7 @@ void Board::SetByFen(std::string_view fen) {
occupied = colors[White] | colors[Black]; occupied = colors[White] | colors[Black];
hashKey = UTILS::GetHashKey(*this); hashKey = UTILS::GetHashKey(*this);
MOVEGEN::GenThreatMaps(*this); MOVEGEN::GenThreatMaps(*this);
MOVEGEN::GenerateMoves<All>(*this); MOVEGEN::GenerateMoves<All>(*this, true);
int whiteKingFile = (pieces[King] & colors[White]).getLS1BIndex() % 8; int whiteKingFile = (pieces[King] & colors[White]).getLS1BIndex() % 8;
int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8; int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8;
+4 -4
View File
@@ -72,7 +72,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
bool plusOne = moveDist(rng); bool plusOne = moveDist(rng);
for (int count = 0; count < RAND_MOVES + plusOne; count++) { for (int count = 0; count < RAND_MOVES + plusOne; count++) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1); std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
bool isLegal = board.MakeMove(board.moveList[dist(rng)]); bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
@@ -82,7 +82,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
continue; continue;
} }
ctx->positionHistory[board.positionIndex] = board.hashKey; ctx->positionHistory[board.positionIndex] = board.hashKey;
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
if (IsGameOver(board, ctx)) break; if (IsGameOver(board, ctx)) break;
} }
} }
@@ -179,7 +179,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
positions++; positions++;
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
} }
if (!SEARCH::IsDraw(board, ctx.get())) { if (!SEARCH::IsDraw(board, ctx.get())) {
@@ -399,4 +399,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
std::cout << std::endl; std::cout << std::endl;
} }
} }
+4 -3
View File
@@ -4,8 +4,9 @@
#include "board.h" #include "board.h"
enum MovegenMode { enum MovegenMode {
All, Quiet,
Noisy Noisy,
All
}; };
namespace MOVEGEN { namespace MOVEGEN {
@@ -37,7 +38,7 @@ template <MovegenMode mode>
void GenKingMoves(Board &board); void GenKingMoves(Board &board);
template <MovegenMode mode> template <MovegenMode mode>
void GenerateMoves(Board &board); void GenerateMoves(Board &board, bool clear);
void GenThreatMaps(Board &board); void GenThreatMaps(Board &board);
+81 -141
View File
@@ -11,8 +11,8 @@ void GenPawnMoves(Board &board) {
Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8]; Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8];
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
pushes &= lastRank; Bitboard noisyPushes = pushes & lastRank;
if (board.enPassantTarget != noEPTarget) { if (board.enPassantTarget != noEPTarget) {
if (attacks.IsSet(board.enPassantTarget)) { if (attacks.IsSet(board.enPassantTarget)) {
@@ -35,8 +35,8 @@ void GenPawnMoves(Board &board) {
captures.PopBit(targetSquare); captures.PopBit(targetSquare);
} }
while (pushes) { while (noisyPushes) {
int pushSquare = pushes.getLS1BIndex(); int pushSquare = noisyPushes.getLS1BIndex();
// Only promotions because NOISY // Only promotions because NOISY
board.AddMove(Move(square, pushSquare, knightPromotion)); 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, rookPromotion));
board.AddMove(Move(square, pushSquare, queenPromotion)); board.AddMove(Move(square, pushSquare, queenPromotion));
pushes.PopBit(pushSquare); noisyPushes.PopBit(pushSquare);
} }
pawns.PopBit(square); 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) { while (pushes) {
int pushSquare = pushes.getLS1BIndex(); int pushSquare = pushes.getLS1BIndex();
// Checking for promotion if (!lastRank.IsSet(pushSquare)) {
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 (std::abs(square - pushSquare) == 16) { if (std::abs(square - pushSquare) == 16) {
board.AddMove(Move(square, pushSquare, doublePawnPush)); board.AddMove(Move(square, pushSquare, doublePawnPush));
} else { } else {
@@ -76,22 +64,6 @@ void GenPawnMoves(Board &board) {
pushes.PopBit(pushSquare); 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); pawns.PopBit(square);
@@ -108,7 +80,7 @@ void GenKnightMoves(Board &board) {
Bitboard captures = moves & board.colors[!board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove];
moves &= ~captures; moves &= ~captures;
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
while (captures) { while (captures) {
int targetSquare = captures.getLS1BIndex(); int targetSquare = captures.getLS1BIndex();
@@ -118,25 +90,18 @@ void GenKnightMoves(Board &board) {
} }
knights.PopBit(square); 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); knights.PopBit(square);
} }
} }
@@ -151,7 +116,7 @@ void GenRookMoves(Board &board) {
Bitboard captures = moves & board.colors[!board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove];
moves &= ~captures; moves &= ~captures;
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
while (captures) { while (captures) {
int targetSquare = captures.getLS1BIndex(); int targetSquare = captures.getLS1BIndex();
@@ -161,24 +126,17 @@ void GenRookMoves(Board &board) {
} }
rooks.PopBit(square); rooks.PopBit(square);
continue;
} }
while (moves) { if constexpr (mode == Quiet || mode == All) {
int targetSquare = moves.getLS1BIndex(); while (moves) {
int targetSquare = moves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet)); board.AddMove(Move(square, targetSquare, quiet));
moves.PopBit(targetSquare); moves.PopBit(targetSquare);
} }
}
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
rooks.PopBit(square); rooks.PopBit(square);
} }
@@ -194,7 +152,7 @@ void GenBishopMoves(Board &board) {
Bitboard captures = moves & board.colors[!board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove];
moves &= ~captures; moves &= ~captures;
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
while (captures) { while (captures) {
int targetSquare = captures.getLS1BIndex(); int targetSquare = captures.getLS1BIndex();
@@ -204,24 +162,17 @@ void GenBishopMoves(Board &board) {
} }
bishops.PopBit(square); bishops.PopBit(square);
continue;
} }
while (moves) { if constexpr (mode == Quiet || mode == All) {
int targetSquare = moves.getLS1BIndex(); while (moves) {
int targetSquare = moves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet)); board.AddMove(Move(square, targetSquare, quiet));
moves.PopBit(targetSquare); moves.PopBit(targetSquare);
} }
}
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
bishops.PopBit(square); bishops.PopBit(square);
} }
@@ -237,7 +188,7 @@ void GenQueenMoves(Board &board) {
Bitboard captures = moves & board.colors[!board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove];
moves &= ~captures; moves &= ~captures;
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
while (captures) { while (captures) {
int targetSquare = captures.getLS1BIndex(); int targetSquare = captures.getLS1BIndex();
@@ -247,24 +198,17 @@ void GenQueenMoves(Board &board) {
} }
queens.PopBit(square); queens.PopBit(square);
continue;
} }
while (moves) { if constexpr (mode == Quiet || mode == All) {
int targetSquare = moves.getLS1BIndex(); while (moves) {
int targetSquare = moves.getLS1BIndex();
board.AddMove(Move(square, targetSquare, quiet)); board.AddMove(Move(square, targetSquare, quiet));
moves.PopBit(targetSquare); moves.PopBit(targetSquare);
} }
}
while (captures) {
int targetSquare = captures.getLS1BIndex();
board.AddMove(Move(square, targetSquare, capture));
captures.PopBit(targetSquare);
}
queens.PopBit(square); queens.PopBit(square);
} }
@@ -277,7 +221,7 @@ void GenKingMoves(Board &board) {
Bitboard captures = moves & board.colors[!board.sideToMove]; Bitboard captures = moves & board.colors[!board.sideToMove];
moves &= ~board.colors[!board.sideToMove]; moves &= ~board.colors[!board.sideToMove];
if constexpr (mode == Noisy) { if constexpr (mode == Noisy || mode == All) {
while (captures) { while (captures) {
int square = captures.getLS1BIndex(); int square = captures.getLS1BIndex();
@@ -286,57 +230,53 @@ void GenKingMoves(Board &board) {
captures.PopBit(square); captures.PopBit(square);
} }
return;
} }
while (moves) { if constexpr (mode == Quiet || mode == All) {
int square = moves.getLS1BIndex(); while (moves) {
int square = moves.getLS1BIndex();
board.AddMove(Move(kingSquare, square, quiet)); 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));
moves.PopBit(square);
} }
}
if (board.castlingRights & kingRight) { // Generating castles
U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0; int kingRight = board.sideToMove ? blackKingRight : whiteKingRight;
U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70; int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight;
int targetSquare = board.sideToMove ? g8 : g1;
if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 if (board.castlingRights & queenRight) {
&& !(mask & board.colorThreats[!board.sideToMove])) { U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f;
board.AddMove(Move(kingSquare, targetSquare, kingCastle)); 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 <MovegenMode mode> template <MovegenMode mode>
void GenerateMoves(Board &board) { void GenerateMoves(Board &board, bool clear) {
board.ResetMoves(); if (clear) {
board.pieceThreats = std::array<Bitboard, 6>(); board.ResetMoves();
board.colorThreats = std::array<Bitboard, 2>(); board.pieceThreats = {};
board.colorThreats = {};
}
GenThreatMaps(board); GenThreatMaps(board);
GenPawnMoves<mode>(board); GenPawnMoves<mode>(board);
+2 -2
View File
@@ -3,7 +3,7 @@
#include "movegen.h" #include "movegen.h"
static U64 HelperPerft(Board board, int depth) { static U64 HelperPerft(Board board, int depth) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
if (depth == 0) return 1ULL; if (depth == 0) return 1ULL;
@@ -19,7 +19,7 @@ static U64 HelperPerft(Board board, int depth) {
} }
void Perft(Board &board, int depth) { void Perft(Board &board, int depth) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
U64 totalNodes = 0; U64 totalNodes = 0;
+5 -4
View File
@@ -321,7 +321,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
const int fpScore = bestScore + 100; const int fpScore = bestScore + 100;
MOVEGEN::GenerateMoves<Noisy>(board); MOVEGEN::GenerateMoves<Noisy>(board, true);
SortMoves(board, ply, ctx); 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()) && (!entry.bestMove || !entry.bestMove.IsQuiet())
&& !(ttHit && entry.depth >= probcutDepth && entry.score < probcutBeta)) { && !(ttHit && entry.depth >= probcutDepth && entry.score < probcutBeta)) {
MOVEGEN::GenerateMoves<Noisy>(board); MOVEGEN::GenerateMoves<Noisy>(board, true);
SortMoves(board, ply, ctx); SortMoves(board, ply, ctx);
const int seeThreshold = (probcutBeta - staticEval) * 15 / 16; 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; return score;
} }
} }
MOVEGEN::GenerateMoves<Quiet>(board, false);
} else {
MOVEGEN::GenerateMoves<All>(board, true);
} }
MOVEGEN::GenerateMoves<All>(board);
SortMoves(board, ply, ctx); SortMoves(board, ply, ctx);
int score = -inf; int score = -inf;
+1 -1
View File
@@ -45,7 +45,7 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
} }
} }
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
} }
static double ReadParam(const std::string& param, const std::string &command) { static double ReadParam(const std::string& param, const std::string &command) {
+2 -2
View File
@@ -111,7 +111,7 @@ int parseSquare(std::string_view str) {
} }
Move parseMove(Board &board, std::string_view str) { Move parseMove(Board &board, std::string_view str) {
MOVEGEN::GenerateMoves<All>(board); MOVEGEN::GenerateMoves<All>(board, true);
int from = parseSquare(str.substr(0, 2)); int from = parseSquare(str.substr(0, 2));
int to = parseSquare(str.substr(2, 2)); int to = parseSquare(str.substr(2, 2));
@@ -202,4 +202,4 @@ int ConvertToWhiteRelative(Board &board, int score) {
return board.sideToMove == White ? score : -score; return board.sideToMove == White ? score : -score;
} }
} }