From 002bf4d8dbf804ce8303e01733341a0922af2e71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Nicolet?= Date: Mon, 21 Aug 2017 18:59:24 +0200 Subject: [PATCH] Avoid constructing an empty tuple in qsearch Avoid constructing, passing as a parameter and binding a useless empty tuple of pointers in the qsearch move picker constructor. Also reformat the scoring function in movepicker.cpp and do some cleaning in evaluate.cpp while there. No functional change. --- src/bitboard.h | 1 + src/evaluate.cpp | 20 +++++++++----------- src/movepick.cpp | 35 ++++++++++++++++++++++------------- src/movepick.h | 2 +- src/search.cpp | 3 +-- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/bitboard.h b/src/bitboard.h index e390b018..c9f199ee 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -39,6 +39,7 @@ const std::string pretty(Bitboard b); } +const Bitboard AllSquares = ~Bitboard(0); const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL; const Bitboard FileABB = 0x0101010101010101ULL; diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 873a339d..8cb0d11c 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -216,11 +216,7 @@ namespace { const Score Hanging = S( 48, 27); const Score ThreatByPawnPush = S( 38, 22); const Score HinderPassedPawn = S( 7, 0); - - // Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by - // a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only - // happen in Chess960 games. - const Score TrappedBishopA1H1 = S(50, 50); + const Score TrappedBishopA1H1 = S( 50, 50); #undef S #undef V @@ -412,11 +408,11 @@ namespace { const Color Them = (Us == WHITE ? BLACK : WHITE); const Square Up = (Us == WHITE ? NORTH : SOUTH); - const Bitboard Camp = (Us == WHITE ? ~Bitboard(0) ^ Rank6BB ^ Rank7BB ^ Rank8BB - : ~Bitboard(0) ^ Rank1BB ^ Rank2BB ^ Rank3BB); + const Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB + : AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB); const Square ksq = pos.square(Us); - Bitboard kingOnlyDefended, b, b1, b2, safe, other; + Bitboard kingOnlyDefended, undefended, b, b1, b2, safe, other; int kingDanger; // King shelter and enemy pawns storm @@ -431,8 +427,10 @@ namespace { & ~attackedBy2[Us]; // ... and those which are not defended at all in the larger king ring - b = attackedBy[Them][ALL_PIECES] & ~attackedBy[Us][ALL_PIECES] - & kingRing[Us] & ~pos.pieces(Them); + undefended = attackedBy[Them][ALL_PIECES] + & ~attackedBy[Us][ALL_PIECES] + & kingRing[Us] + & ~pos.pieces(Them); // Initialize the 'kingDanger' variable, which will be transformed // later into a king danger score. The initial value is based on the @@ -442,7 +440,7 @@ namespace { kingDanger = kingAttackersCount[Them] * kingAttackersWeight[Them] + 102 * kingAdjacentZoneAttacksCount[Them] + 201 * popcount(kingOnlyDefended) - + 143 * (popcount(b) + !!pos.pinned_pieces(Us)) + + 143 * (popcount(undefended) + !!pos.pinned_pieces(Us)) - 848 * !pos.count(Them) - 9 * mg_value(score) / 8 + 40; diff --git a/src/movepick.cpp b/src/movepick.cpp index 3b6ecbbf..1c0de699 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -66,6 +66,7 @@ namespace { /// search captures, promotions, and some checks) and how important good move /// ordering is at the current node. +/// MovePicker constructor for the main search MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, const PieceToHistory** ch, Move cm, Move* killers_p) : pos(p), mainHistory(mh), contHistory(ch), countermove(cm), @@ -78,9 +79,9 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHist stage += (ttMove == MOVE_NONE); } -MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, - const PieceToHistory** ch, Square s) - : pos(p), mainHistory(mh), contHistory(ch) { +/// MovePicker constructor for quiescence search +MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, Square s) + : pos(p), mainHistory(mh) { assert(d <= DEPTH_ZERO); @@ -104,14 +105,14 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHist stage += (ttMove == MOVE_NONE); } +/// MovePicker constructor for ProbCut: we generate captures with SEE higher +/// than or equal to the given threshold. MovePicker::MovePicker(const Position& p, Move ttm, Value th) : pos(p), threshold(th) { assert(!pos.checkers()); stage = PROBCUT; - - // In ProbCut we generate captures with SEE higher than or equal to the given threshold ttMove = ttm && pos.pseudo_legal(ttm) && pos.capture(ttm) @@ -123,22 +124,30 @@ MovePicker::MovePicker(const Position& p, Move ttm, Value th) /// score() assigns a numerical value to each move in a list, used for sorting. /// Captures are ordered by Most Valuable Victim (MVV), preferring captures /// near our home rank. Quiets are ordered using the histories. -template +template void MovePicker::score() { + static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type"); + for (auto& m : *this) - if (T == CAPTURES || (T == EVASIONS && pos.capture(m))) - m.value = PieceValue[MG][pos.piece_on(to_sq(m))] - - (T == EVASIONS ? Value(type_of(pos.moved_piece(m))) - : Value(200 * relative_rank(pos.side_to_move(), to_sq(m)))); - else if (T == QUIETS) + if (Type == CAPTURES) + m.value = PieceValue[MG][pos.piece_on(to_sq(m))] + - Value(200 * relative_rank(pos.side_to_move(), to_sq(m))); + + else if (Type == QUIETS) m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] + (*contHistory[0])[pos.moved_piece(m)][to_sq(m)] + (*contHistory[1])[pos.moved_piece(m)][to_sq(m)] + (*contHistory[3])[pos.moved_piece(m)][to_sq(m)]; - else // Quiet evasions - m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28); + else // Type == EVASIONS + { + if (pos.capture(m)) + m.value = PieceValue[MG][pos.piece_on(to_sq(m))] + - Value(type_of(pos.moved_piece(m))); + else + m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28); + } } /// next_move() is the most important method of the MovePicker class. It returns diff --git a/src/movepick.h b/src/movepick.h index db0ba3c4..66fc1bdb 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -94,7 +94,7 @@ public: MovePicker(const MovePicker&) = delete; MovePicker& operator=(const MovePicker&) = delete; MovePicker(const Position&, Move, Value); - MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Square); + MovePicker(const Position&, Move, Depth, const ButterflyHistory*, Square); MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Move, Move*); Move next_move(bool skipQuiets = false); diff --git a/src/search.cpp b/src/search.cpp index 50a84d27..3a54bf01 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1246,8 +1246,7 @@ moves_loop: // When in check search starts from here // to search the moves. Because the depth is <= 0 here, only captures, // queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will // be generated. - const PieceToHistory* contHist[4] = {}; - MovePicker mp(pos, ttMove, depth, &pos.this_thread()->mainHistory, contHist, to_sq((ss-1)->currentMove)); + MovePicker mp(pos, ttMove, depth, &pos.this_thread()->mainHistory, to_sq((ss-1)->currentMove)); // Loop through the moves until no moves remain or a beta cutoff occurs while ((move = mp.next_move()) != MOVE_NONE) -- 2.39.2