Skip to content

Commit fa43857

Browse files
committed
Change how TT is handled in qSearch
Bench: 18354635
1 parent e395a6b commit fa43857

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

src/board.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,5 +560,36 @@ class Board {
560560
return blackPawns & ~combinedSpans;
561561
}
562562
}
563+
564+
template <PieceColor color>
565+
[[nodiscard]] uint64_t promotionCandidates() const {
566+
if (color == WHITE) {
567+
const uint64_t whitePawns = getPieceBoard<WHITE_PAWN>();
568+
569+
return whitePawns & RANK_7;
570+
} else {
571+
const uint64_t blackPawns = getPieceBoard<BLACK_PAWN>();
572+
573+
return blackPawns & RANK_2;
574+
}
575+
}
576+
577+
template <PieceColor color>
578+
[[nodiscard]] Piece getHighestValuePiece() const {
579+
// Get the highest value piece on the board for color
580+
constexpr Piece startingPiece = color == WHITE ? WHITE_QUEEN : BLACK_QUEEN;
581+
constexpr Piece endingPiece = color == WHITE ? WHITE_PAWN : BLACK_PAWN;
582+
583+
for (int pieceIndex = startingPiece; pieceIndex >= endingPiece; pieceIndex -= 2) {
584+
const Piece piece = static_cast<Piece>(pieceIndex);
585+
586+
if (getPieceBoard(piece)) {
587+
return piece;
588+
}
589+
}
590+
591+
assert(false);
592+
return EMPTY;
593+
}
563594
};
564595
} // namespace Zagreus

src/search.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ int pvSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Searc
200200
#ifdef TRACE_SEARCH
201201
stats.ttProbes++;
202202

203-
if (ttScore != NO_TT_SCORE) {
203+
if (ttEntry) {
204204
stats.ttHits++;
205205
}
206206
#endif
@@ -500,6 +500,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
500500
const std::chrono::time_point<std::chrono::steady_clock>& endTime, SearchStack& searchStack) {
501501
assert(nodeType != ROOT);
502502
constexpr bool isPV = nodeType == PV;
503+
constexpr PieceColor opponentColor = !color;
503504

504505
if (board.isDraw()) {
505506
return DRAW_SCORE;
@@ -512,30 +513,29 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
512513
}
513514
}
514515

515-
if (!isPV) {
516-
TTEntry* ttEntry = nullptr;
517-
const int16_t score = tt->probePosition(board.getZobristHash(), depth, alpha, beta, board.getPly(), ttEntry);
518-
519516
#ifdef TRACE_SEARCH
520-
stats.qTtProbes++;
521-
#endif
517+
stats.qTtProbes++;
522518

523-
if (score != NO_TT_SCORE) {
524-
#ifdef TRACE_SEARCH
525-
stats.qTtHits++;
519+
if (tt->getEntry(board.getZobristHash()) != nullptr) {
520+
stats.qTtHits++;
521+
}
526522
#endif
527-
return score;
523+
524+
TTEntry* ttEntry = nullptr;
525+
const int16_t ttScore = tt->probePosition(board.getZobristHash(), depth, alpha, beta, board.getPly(), ttEntry);
526+
527+
if (!isPV) {
528+
if (ttScore != NO_TT_SCORE) {
529+
return ttScore;
528530
}
529531
}
530532

531533
stats.qNodesSearched += 1;
532534

533535
const bool isInCheck = board.isKingInCheck<color>();
534-
int bestScore;
536+
int bestScore = -MATE_SCORE;
535537

536-
if (isInCheck) {
537-
bestScore = -MATE_SCORE;
538-
} else {
538+
if (!isInCheck) {
539539
bestScore = Evaluation(board).evaluate();
540540

541541
// Stand pat
@@ -544,9 +544,16 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
544544
#ifdef TRACE_SEARCH
545545
stats.ttWrites++;
546546
#endif
547-
tt->savePosition(board.getZobristHash(), depth, board.getPly(), bestScore, NO_MOVE, BETA);
547+
548+
if (!ttEntry) {
549+
tt->savePosition(board.getZobristHash(), depth, board.getPly(), bestScore, NO_MOVE, BETA);
550+
}
548551
}
549552

553+
# ifdef TRACE_SEARCH
554+
stats.standpatPrunes++;
555+
# endif
556+
550557
return bestScore;
551558
}
552559

@@ -598,8 +605,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
598605

599606
legalMoves += 1;
600607

601-
const int score =
602-
-qSearch<!color, nodeType>(engine, board, -beta, -alpha, depth - 1, stats, endTime, searchStack);
608+
const int score = -qSearch<!color, nodeType>(engine, board, -beta, -alpha, depth - 1, stats, endTime, searchStack);
603609

604610
board.unmakeMove();
605611

@@ -611,14 +617,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
611617
alpha = score;
612618

613619
if (score >= beta) {
614-
if (!engine.isSearchStopped()) {
615-
#ifdef TRACE_SEARCH
616-
stats.ttWrites++;
617-
#endif
618-
tt->savePosition(board.getZobristHash(), depth, board.getPly(), score, move, BETA);
619-
}
620-
621-
return score;
620+
break;
622621
}
623622
}
624623
}
@@ -628,7 +627,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
628627
bestScore = -MATE_SCORE + board.getPly();
629628
}
630629

631-
const TTNodeType ttNodeType = (isPV && bestMove != NO_MOVE) ? EXACT : ALPHA;
630+
const TTNodeType ttNodeType = bestScore >= beta ? BETA : ALPHA;
632631

633632
if (!engine.isSearchStopped()) {
634633
#ifdef TRACE_SEARCH
@@ -728,6 +727,7 @@ void SearchStats::printTrace(Engine& engine, int numPositions) const {
728727
printStatLine("Check Extensions", checkExtensions);
729728

730729
ss << "\n--- Quiescence Search ---\n";
730+
printStatLine("Standpat Prunes", standpatPrunes);
731731
printStatLine("SEE Prunes", seePrunes);
732732

733733
ss << "-------------------------";
@@ -755,6 +755,7 @@ SearchStats& SearchStats::operator+=(const SearchStats& other) {
755755
checkExtensions += other.checkExtensions;
756756
singularExtensions += other.singularExtensions;
757757
singularAttempts += other.singularAttempts;
758+
standpatPrunes += other.standpatPrunes;
758759
seePrunes += other.seePrunes;
759760

760761
return *this;

src/search.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct SearchStats {
7979
uint64_t singularExtensions = 0;
8080
uint64_t singularAttempts = 0;
8181
uint64_t seePrunes = 0;
82+
uint64_t standpatPrunes = 0;
8283

8384
void clearTrace();
8485
void printTrace(Engine& engine, int numPositions = 1) const;

0 commit comments

Comments
 (0)