Skip to content

Commit

Permalink
Refactor bonus and penalty calculation (#917)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
vondele authored and mcostalba committed Dec 5, 2016
1 parent 0d33466 commit a47bbca
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions src/search.cpp
Expand Up @@ -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<decltype(HalfDensity)>::value;

EasyMoveManager EasyMove;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -1140,33 +1135,20 @@ namespace {
: 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 :
Expand Down

0 comments on commit a47bbca

Please sign in to comment.