1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 17:19:36 +00:00

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)
This commit is contained in:
Joost VandeVondele 2016-12-05 18:58:12 +01:00 committed by Marco Costalba
parent 0d33466bcd
commit a47bbca0ea

View file

@ -153,6 +153,9 @@ namespace {
{1, 0, 0, 0, 0, 1, 1 ,1}, {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; const size_t HalfDensitySize = std::extent<decltype(HalfDensity)>::value;
EasyMoveManager EasyMove; EasyMoveManager EasyMove;
@ -616,6 +619,7 @@ namespace {
ss->counterMoves = nullptr; ss->counterMoves = nullptr;
(ss+1)->skipEarlyPruning = false; (ss+1)->skipEarlyPruning = false;
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE; (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 // 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 // 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 ttMove is quiet, update killers, history, counter move on TT hit
if (ttValue >= beta && ttMove) if (ttValue >= beta && ttMove)
{ {
int d = depth / ONE_PLY;
if (!pos.capture_or_promotion(ttMove)) if (!pos.capture_or_promotion(ttMove))
{ update_stats(pos, ss, ttMove, nullptr, 0, bonus(depth));
Value bonus = Value(d * d + 2 * d - 2);
update_stats(pos, ss, ttMove, nullptr, 0, bonus);
}
// Extra penalty for a quiet TT move in previous ply when it gets refuted // Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece()) if ((ss-1)->moveCount == 1 && !pos.captured_piece())
{ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth));
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);
}
} }
return ttValue; 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()]; : inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
else if (bestMove) else if (bestMove)
{ {
int d = depth / ONE_PLY;
// Quiet best move: update killers, history and countermoves // Quiet best move: update killers, history and countermoves
if (!pos.capture_or_promotion(bestMove)) if (!pos.capture_or_promotion(bestMove))
{ update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus(depth));
Value bonus = Value(d * d + 2 * d - 2);
update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus);
}
// Extra penalty for a quiet TT move in previous ply when it gets refuted // Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece()) if ((ss-1)->moveCount == 1 && !pos.captured_piece())
{ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth));
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);
}
} }
// Bonus for prior countermove that caused the fail low // Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY else if ( depth >= 3 * ONE_PLY
&& !pos.captured_piece() && !pos.captured_piece()
&& is_ok((ss-1)->currentMove)) && is_ok((ss-1)->currentMove))
{ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus(depth));
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);
}
tte->save(posKey, value_to_tt(bestValue, ss->ply), tte->save(posKey, value_to_tt(bestValue, ss->ply),
bestValue >= beta ? BOUND_LOWER : bestValue >= beta ? BOUND_LOWER :