From a47bbca0ea40ac007d64682c3ba51fa4158b2d3f Mon Sep 17 00:00:00 2001 From: Joost VandeVondele Date: Mon, 5 Dec 2016 18:58:12 +0100 Subject: [PATCH 1/1] Refactor bonus and penalty calculation (#917) * Refactor bonus and penalty calculation Compute common terms in a helper function. No functional change. * Further refactoring Remove some parenthesis that are now useless. Define prevSq once, use repeatedly. No functional change. bench: 5884767 (bench of previous patch is wrong) --- src/search.cpp | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index db94ed5d..2fa2ce7e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -153,6 +153,9 @@ namespace { {1, 0, 0, 0, 0, 1, 1 ,1}, }; + Value bonus(Depth depth) { int d = depth / ONE_PLY ; return Value(d * d + 2 * d - 2); } + Value penalty(Depth depth) { int d = depth / ONE_PLY ; return -Value(d * d + 4 * d + 1); } + const size_t HalfDensitySize = std::extent::value; EasyMoveManager EasyMove; @@ -616,6 +619,7 @@ namespace { ss->counterMoves = nullptr; (ss+1)->skipEarlyPruning = false; (ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE; + Square prevSq = to_sq((ss-1)->currentMove); // Step 4. Transposition table lookup. We don't want the score of a partial // search to overwrite a previous full search TT value, so we use a different @@ -638,21 +642,12 @@ namespace { // If ttMove is quiet, update killers, history, counter move on TT hit if (ttValue >= beta && ttMove) { - int d = depth / ONE_PLY; - if (!pos.capture_or_promotion(ttMove)) - { - Value bonus = Value(d * d + 2 * d - 2); - update_stats(pos, ss, ttMove, nullptr, 0, bonus); - } + update_stats(pos, ss, ttMove, nullptr, 0, bonus(depth)); // Extra penalty for a quiet TT move in previous ply when it gets refuted if ((ss-1)->moveCount == 1 && !pos.captured_piece()) - { - Value penalty = Value(d * d + 4 * d + 1); - Square prevSq = to_sq((ss-1)->currentMove); - update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty); - } + update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth)); } return ttValue; } @@ -1140,33 +1135,20 @@ moves_loop: // When in check search starts from here : inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()]; else if (bestMove) { - int d = depth / ONE_PLY; // Quiet best move: update killers, history and countermoves if (!pos.capture_or_promotion(bestMove)) - { - Value bonus = Value(d * d + 2 * d - 2); - update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus); - } + update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus(depth)); // Extra penalty for a quiet TT move in previous ply when it gets refuted if ((ss-1)->moveCount == 1 && !pos.captured_piece()) - { - Value penalty = Value(d * d + 4 * d + 1); - Square prevSq = to_sq((ss-1)->currentMove); - update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty); - } + update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth)); } // Bonus for prior countermove that caused the fail low else if ( depth >= 3 * ONE_PLY && !pos.captured_piece() && is_ok((ss-1)->currentMove)) - { - int d = depth / ONE_PLY; - Value bonus = Value(d * d + 2 * d - 2); - Square prevSq = to_sq((ss-1)->currentMove); - update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus); - } + update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus(depth)); tte->save(posKey, value_to_tt(bestValue, ss->ply), bestValue >= beta ? BOUND_LOWER : -- 2.39.2