Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply);
void update_pv(Move* pv, Move move, Move* childPv);
- void update_cm_stats(Stack* ss, Piece pc, Square s, int bonus);
+ void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus);
void update_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, int bonus);
} // namespace
for (Thread* th : Threads)
{
th->counterMoves.fill(MOVE_NONE);
- th->history.fill(0);
+ th->mainHistory.fill(0);
- for (auto& to : th->counterMoveHistory)
+ for (auto& to : th->contHistory)
for (auto& h : to)
h.fill(0);
- th->counterMoveHistory[NO_PIECE][0].fill(CounterMovePruneThreshold - 1);
+ th->contHistory[NO_PIECE][0].fill(CounterMovePruneThreshold - 1);
}
Threads.main()->callsCnt = 0;
std::memset(ss-4, 0, 7 * sizeof(Stack));
for (int i = 4; i > 0; i--)
- (ss-i)->history = &this->counterMoveHistory[NO_PIECE][0]; // Use as sentinel
+ (ss-i)->contHistory = &this->contHistory[NO_PIECE][0]; // Use as sentinel
bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE;
Value bestValue, value, ttValue, eval;
bool ttHit, inCheck, givesCheck, singularExtensionNode, improving;
bool captureOrPromotion, doFullDepthSearch, moveCountPruning, skipQuiets, ttCapture;
- Piece moved_piece;
+ Piece movedPiece;
int moveCount, quietCount;
// Step 1. Initialize node
assert(0 <= ss->ply && ss->ply < MAX_PLY);
ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE;
- ss->history = &thisThread->counterMoveHistory[NO_PIECE][0];
+ ss->contHistory = &thisThread->contHistory[NO_PIECE][0];
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
Square prevSq = to_sq((ss-1)->currentMove);
// Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY));
+ update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY));
}
// Penalty for a quiet ttMove that fails low
else if (!pos.capture_or_promotion(ttMove))
{
int penalty = -stat_bonus(depth);
- thisThread->history.update(pos.side_to_move(), ttMove, penalty);
- update_cm_stats(ss, pos.moved_piece(ttMove), to_sq(ttMove), penalty);
+ thisThread->mainHistory.update(pos.side_to_move(), ttMove, penalty);
+ update_continuation_histories(ss, pos.moved_piece(ttMove), to_sq(ttMove), penalty);
}
}
return ttValue;
eval = ss->staticEval = evaluate(pos);
// Can ttValue be used as a better position evaluation?
- if (ttValue != VALUE_NONE)
- if (tte->bound() & (ttValue > eval ? BOUND_LOWER : BOUND_UPPER))
- eval = ttValue;
+ if ( ttValue != VALUE_NONE
+ && (tte->bound() & (ttValue > eval ? BOUND_LOWER : BOUND_UPPER)))
+ eval = ttValue;
}
else
{
Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY;
ss->currentMove = MOVE_NULL;
- ss->history = &thisThread->counterMoveHistory[NO_PIECE][0];
+ ss->contHistory = &thisThread->contHistory[NO_PIECE][0];
pos.do_null_move(st);
Value nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -beta+1)
if (pos.legal(move))
{
ss->currentMove = move;
- ss->history = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)];
+ ss->contHistory = &thisThread->contHistory[pos.moved_piece(move)][to_sq(move)];
assert(depth >= 5 * ONE_PLY);
pos.do_move(move, st);
moves_loop: // When in check search starts from here
- const PieceToHistory& cmh = *(ss-1)->history;
- const PieceToHistory& fmh = *(ss-2)->history;
- const PieceToHistory& fm2 = *(ss-4)->history;
+ const PieceToHistory* contHist[] = { (ss-1)->contHistory, (ss-2)->contHistory, nullptr, (ss-4)->contHistory };
+ Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq];
- MovePicker mp(pos, ttMove, depth, ss);
+ MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory, contHist, countermove, ss->killers);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
improving = ss->staticEval >= (ss-2)->staticEval
/* || ss->staticEval == VALUE_NONE Already implicit in the previous condition */
extension = DEPTH_ZERO;
captureOrPromotion = pos.capture_or_promotion(move);
- moved_piece = pos.moved_piece(move);
+ movedPiece = pos.moved_piece(move);
givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates()
? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move)
// Countermoves based pruning
if ( lmrDepth < 3
- && (cmh[moved_piece][to_sq(move)] < CounterMovePruneThreshold)
- && (fmh[moved_piece][to_sq(move)] < CounterMovePruneThreshold))
+ && (*contHist[0])[movedPiece][to_sq(move)] < CounterMovePruneThreshold
+ && (*contHist[1])[movedPiece][to_sq(move)] < CounterMovePruneThreshold)
continue;
// Futility pruning: parent node
// Update the current move (this must be done after singular extension search)
ss->currentMove = move;
- ss->history = &thisThread->counterMoveHistory[moved_piece][to_sq(move)];
+ ss->contHistory = &thisThread->contHistory[movedPiece][to_sq(move)];
// Step 14. Make the move
pos.do_move(move, st, givesCheck);
&& !pos.see_ge(make_move(to_sq(move), from_sq(move))))
r -= 2 * ONE_PLY;
- ss->statScore = cmh[moved_piece][to_sq(move)]
- + fmh[moved_piece][to_sq(move)]
- + fm2[moved_piece][to_sq(move)]
- + thisThread->history[~pos.side_to_move()][from_to(move)]
- - 4000; // Correction factor
+ ss->statScore = thisThread->mainHistory[~pos.side_to_move()][from_to(move)]
+ + (*contHist[0])[movedPiece][to_sq(move)]
+ + (*contHist[1])[movedPiece][to_sq(move)]
+ + (*contHist[3])[movedPiece][to_sq(move)]
+ - 4000;
// Decrease/increase reduction by comparing opponent's stat score
if (ss->statScore > 0 && (ss-1)->statScore < 0)
// Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY));
+ update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY));
}
// Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY
&& !pos.captured_piece()
&& is_ok((ss-1)->currentMove))
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth));
+ update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth));
if (!excludedMove)
tte->save(posKey, value_to_tt(bestValue, ss->ply),
ss->staticEval = bestValue = evaluate(pos);
// Can ttValue be used as a better position evaluation?
- if (ttValue != VALUE_NONE)
- if (tte->bound() & (ttValue > bestValue ? BOUND_LOWER : BOUND_UPPER))
- bestValue = ttValue;
+ if ( ttValue != VALUE_NONE
+ && (tte->bound() & (ttValue > bestValue ? BOUND_LOWER : BOUND_UPPER)))
+ bestValue = ttValue;
}
else
ss->staticEval = bestValue =
// 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.
- MovePicker mp(pos, ttMove, depth, to_sq((ss-1)->currentMove));
+ const PieceToHistory* contHist[4] = {};
+ MovePicker mp(pos, ttMove, depth, &pos.this_thread()->mainHistory, contHist, 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)
}
- // update_cm_stats() updates countermove and follow-up move history
+ // update_continuation_histories() updates histories of the move pairs formed
+ // by moves at ply -1, -2, and -4 with current move.
- void update_cm_stats(Stack* ss, Piece pc, Square s, int bonus) {
+ void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {
for (int i : {1, 2, 4})
if (is_ok((ss-i)->currentMove))
- (ss-i)->history->update(pc, s, bonus);
+ (ss-i)->contHistory->update(pc, to, bonus);
}
Color c = pos.side_to_move();
Thread* thisThread = pos.this_thread();
- thisThread->history.update(c, move, bonus);
- update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus);
+ thisThread->mainHistory.update(c, move, bonus);
+ update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus);
if (is_ok((ss-1)->currentMove))
{
Square prevSq = to_sq((ss-1)->currentMove);
- thisThread->counterMoves[pos.piece_on(prevSq)][prevSq]=move;
+ thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] = move;
}
// Decrease all the other played quiet moves
for (int i = 0; i < quietsCnt; ++i)
{
- thisThread->history.update(c, quiets[i], -bonus);
- update_cm_stats(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
+ thisThread->mainHistory.update(c, quiets[i], -bonus);
+ update_continuation_histories(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
}
}