- // Transposition table lookup. At PV nodes, we don't use the TT for
- // pruning, but only for move ordering. This is to avoid problems in
- // the following areas:
- //
- // * Repetition draw detection
- // * Fifty move rule detection
- // * Searching for a mate
- // * Printing of full PV line
- //
- tte = TT.retrieve(pos.get_key());
- ttMove = (tte ? tte->move() : MOVE_NONE);
-
- // Go with internal iterative deepening if we don't have a TT move
- if ( UseIIDAtPVNodes
- && depth >= 5*OnePly
- && ttMove == MOVE_NONE)
- {
- search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
- ttMove = ss[ply].pv[ply];
- tte = TT.retrieve(pos.get_key());
- }
-
- isCheck = pos.is_check();
- if (!isCheck)
- {
- // Update gain statistics of the previous move that lead
- // us in this position.
- EvalInfo ei;
- ss[ply].eval = evaluate(pos, ei, threadID);
- update_gains(pos, ss[ply - 1].currentMove, ss[ply - 1].eval, ss[ply].eval);
- }
-
- // Initialize a MovePicker object for the current position, and prepare
- // to search all moves
- mateThreat = pos.has_mate_threat(opposite_color(pos.side_to_move()));
- CheckInfo ci(pos);
- MovePicker mp = MovePicker(pos, ttMove, depth, H, &ss[ply]);
-
- // Loop through all legal moves until no moves remain or a beta cutoff
- // occurs.
- while ( alpha < beta
- && (move = mp.get_next_move()) != MOVE_NONE
- && !thread_should_stop(threadID))
- {
- assert(move_is_ok(move));
-
- singleEvasion = (isCheck && mp.number_of_evasions() == 1);
- moveIsCheck = pos.move_is_check(move, ci);
- captureOrPromotion = pos.move_is_capture_or_promotion(move);
-
- // Decide the new search depth
- ext = extension(pos, move, true, captureOrPromotion, moveIsCheck, singleEvasion, mateThreat, &dangerous);
-
- // Singular extension search. We extend the TT move if its value is much better than
- // its siblings. To verify this we do a reduced search on all the other moves but the
- // ttMove, if result is lower then ttValue minus a margin then we extend ttMove.
- if ( depth >= 6 * OnePly
- && tte
- && move == tte->move()
- && ext < OnePly
- && is_lower_bound(tte->type())
- && tte->depth() >= depth - 3 * OnePly)
- {
- Value ttValue = value_from_tt(tte->value(), ply);
-
- if (abs(ttValue) < VALUE_KNOWN_WIN)
- {
- Value excValue = search(pos, ss, ttValue - SingleReplyMargin, depth / 2, ply, false, threadID, move);
-
- if (excValue < ttValue - SingleReplyMargin)
- ext = OnePly;
- }
- }
-
- newDepth = depth - OnePly + ext;
-
- // Update current move
- movesSearched[moveCount++] = ss[ply].currentMove = move;
-
- // Make and search the move
- pos.do_move(move, st, ci, moveIsCheck);
-
- if (moveCount == 1) // The first move in list is the PV
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
- else
- {
- // Try to reduce non-pv search depth by one ply if move seems not problematic,
- // if the move fails high will be re-searched at full depth.
- bool doFullDepthSearch = true;
-
- if ( depth >= 3*OnePly
- && !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && !move_is_killer(move, ss[ply]))
- {
- double red = 0.5 + ln(moveCount) * ln(depth / 2) / 6.0;
- if (red >= 1.0)
- {
- ss[ply].reduction = Depth(int(floor(red * int(OnePly))));
- value = -search(pos, ss, -alpha, newDepth-ss[ply].reduction, ply+1, true, threadID);
- doFullDepthSearch = (value > alpha);
- }
- }
-
- if (doFullDepthSearch) // Go with full depth non-pv search
- {
- ss[ply].reduction = Depth(0);
- value = -search(pos, ss, -alpha, newDepth, ply+1, true, threadID);
- if (value > alpha && value < beta)
- {
- // When the search fails high at ply 1 while searching the first
- // move at the root, set the flag failHighPly1. This is used for
- // time managment: We don't want to stop the search early in
- // such cases, because resolving the fail high at ply 1 could
- // result in a big drop in score at the root.
- if (ply == 1 && RootMoveNumber == 1)
- Threads[threadID].failHighPly1 = true;
-
- // A fail high occurred. Re-search at full window (pv search)
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, ply+1, threadID);
- Threads[threadID].failHighPly1 = false;
- }
- }
- }
- pos.undo_move(move);
-
- assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
-
- // New best move?
- if (value > bestValue)
- {
- bestValue = value;
- if (value > alpha)
- {
- alpha = value;
- update_pv(ss, ply);
- if (value == value_mate_in(ply + 1))
- ss[ply].mateKiller = move;
- }
- // If we are at ply 1, and we are searching the first root move at
- // ply 0, set the 'Problem' variable if the score has dropped a lot
- // (from the computer's point of view) since the previous iteration.
- if ( ply == 1
- && Iteration >= 2
- && -value <= IterationInfo[Iteration-1].value - ProblemMargin)
- Problem = true;
- }
-
- // Split?
- if ( ActiveThreads > 1
- && bestValue < beta
- && depth >= MinimumSplitDepth
- && Iteration <= 99
- && idle_thread_exists(threadID)
- && !AbortSearch
- && !thread_should_stop(threadID)
- && split(pos, ss, ply, &alpha, &beta, &bestValue, VALUE_NONE,
- depth, &moveCount, &mp, threadID, true))
- break;
- }
-
- // All legal moves have been searched. A special case: If there were
- // no legal moves, it must be mate or stalemate.
- if (moveCount == 0)
- return (isCheck ? value_mated_in(ply) : VALUE_DRAW);
-
- // If the search is not aborted, update the transposition table,
- // history counters, and killer moves.
- if (AbortSearch || thread_should_stop(threadID))
- return bestValue;
-
- if (bestValue <= oldAlpha)
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
-
- else if (bestValue >= beta)
- {
- BetaCounter.add(pos.side_to_move(), depth, threadID);
- move = ss[ply].pv[ply];
- if (!pos.move_is_capture_or_promotion(move))
- {
- update_history(pos, move, depth, movesSearched, moveCount);
- update_killers(move, ss[ply]);
- }
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, move);
- }
- else
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, depth, ss[ply].pv[ply]);
-
- return bestValue;
- }
-
-
- // search() is the search function for zero-width nodes.
-
- Value search(Position& pos, SearchStack ss[], Value beta, Depth depth,
- int ply, bool allowNullmove, int threadID, Move excludedMove) {
-
- assert(beta >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
- assert(ply >= 0 && ply < PLY_MAX);
- assert(threadID >= 0 && threadID < ActiveThreads);
-
- Move movesSearched[256];
- EvalInfo ei;
- StateInfo st;
- const TTEntry* tte;
- Move ttMove, move;
- Depth ext, newDepth;
- Value bestValue, staticValue, nullValue, value, futilityValue, futilityValueScaled;
- bool isCheck, singleEvasion, moveIsCheck, captureOrPromotion, dangerous;
- bool mateThreat = false;
- int moveCount = 0;
- futilityValue = staticValue = bestValue = value = -VALUE_INFINITE;
-
- if (depth < OnePly)
- return qsearch(pos, ss, beta-1, beta, Depth(0), ply, threadID);
-
- // Initialize, and make an early exit in case of an aborted search,
- // an instant draw, maximum ply reached, etc.
- init_node(ss, ply, threadID);
-
- // After init_node() that calls poll()
- if (AbortSearch || thread_should_stop(threadID))
- return Value(0);
-
- if (pos.is_draw() || ply >= PLY_MAX - 1)
- return VALUE_DRAW;
-
- // Mate distance pruning
- if (value_mated_in(ply) >= beta)
- return beta;
-
- if (value_mate_in(ply + 1) < beta)
- return beta - 1;