diff --git a/src/search.cpp b/src/search.cpp index 8363f221..ffa37ab6 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -62,8 +62,10 @@ constexpr int futility_move_count(bool improving, Depth depth) { return improving ? (3 + depth * depth) : (3 + depth * depth) / 2; } -// Guarantee evaluation does not hit the tablebase range -constexpr Value to_static_eval(const Value v) { +// Add correctionHistory value to raw staticEval and guarantee evaluation does not hit the tablebase range +Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) { + auto cv = w.correctionHistory[pos.side_to_move()][pawn_structure_index(pos)]; + v += cv * std::abs(cv) / 16384; return std::clamp(int(v), VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1); } @@ -747,13 +749,7 @@ Value Search::Worker::search( else if (PvNode) Eval::NNUE::hint_common_parent_position(pos); - Value newEval = - ss->staticEval - + thisThread->correctionHistory[us][pawn_structure_index(pos)] - * std::abs(thisThread->correctionHistory[us][pawn_structure_index(pos)]) - / 16384; - - ss->staticEval = eval = to_static_eval(newEval); + ss->staticEval = eval = to_corrected_static_eval(unadjustedStaticEval, *thisThread, pos); // ttValue can be used as a better position evaluation (~7 Elo) if (ttValue != VALUE_NONE && (tte->bound() & (ttValue > eval ? BOUND_LOWER : BOUND_UPPER))) @@ -762,14 +758,7 @@ Value Search::Worker::search( else { unadjustedStaticEval = ss->staticEval = eval = evaluate(pos, thisThread->optimism[us]); - - Value newEval = - ss->staticEval - + thisThread->correctionHistory[us][pawn_structure_index(pos)] - * std::abs(thisThread->correctionHistory[us][pawn_structure_index(pos)]) - / 16384; - - ss->staticEval = eval = to_static_eval(newEval); + ss->staticEval = eval = to_corrected_static_eval(unadjustedStaticEval, *thisThread, pos); // Static evaluation is saved as it was before adjustment by correction history tte->save(posKey, VALUE_NONE, ss->ttPv, BOUND_NONE, DEPTH_NONE, Move::none(), @@ -1513,15 +1502,8 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta, if ((unadjustedStaticEval = ss->staticEval = bestValue = tte->eval()) == VALUE_NONE) unadjustedStaticEval = ss->staticEval = bestValue = evaluate(pos, thisThread->optimism[us]); - - Value newEval = - ss->staticEval - + thisThread->correctionHistory[us][pawn_structure_index(pos)] - * std::abs( - thisThread->correctionHistory[us][pawn_structure_index(pos)]) - / 16384; - - ss->staticEval = bestValue = to_static_eval(newEval); + ss->staticEval = bestValue = + to_corrected_static_eval(unadjustedStaticEval, *thisThread, pos); // ttValue can be used as a better position evaluation (~13 Elo) if (ttValue != VALUE_NONE @@ -1534,15 +1516,8 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta, unadjustedStaticEval = ss->staticEval = bestValue = (ss - 1)->currentMove != Move::null() ? evaluate(pos, thisThread->optimism[us]) : -(ss - 1)->staticEval; - - Value newEval = - ss->staticEval - + thisThread->correctionHistory[us][pawn_structure_index(pos)] - * std::abs( - thisThread->correctionHistory[us][pawn_structure_index(pos)]) - / 16384; - - ss->staticEval = bestValue = to_static_eval(newEval); + ss->staticEval = bestValue = + to_corrected_static_eval(unadjustedStaticEval, *thisThread, pos); } // Stand pat. Return immediately if static value is at least beta