- // Null move search not allowed, try razoring
- else if ( !value_is_mate(beta)
- && depth < RazorDepth
- && approximateEval < beta - RazorApprMargins[int(depth) - 2]
- && ss[ply - 1].currentMove != MOVE_NULL
- && ttMove == MOVE_NONE
- && !pos.has_pawn_on_7th(pos.side_to_move()))
- {
- Value rbeta = beta - RazorMargins[int(depth) - 2];
- Value v = qsearch(pos, ss, rbeta-1, rbeta, Depth(0), ply, threadID);
- if (v < rbeta)
- return v;
- }
-
- // Go with internal iterative deepening if we don't have a TT move
- if (UseIIDAtNonPVNodes && ttMove == MOVE_NONE && depth >= 8*OnePly &&
- !isCheck && evaluate(pos, ei, threadID) >= beta - IIDMargin)
- {
- search(pos, ss, beta, Min(depth/2, depth-2*OnePly), ply, false, threadID);
- ttMove = ss[ply].pv[ply];
- tte = TT.retrieve(pos.get_key());
- }
-
- // Initialize a MovePicker object for the current position, and prepare
- // to search all moves.
- MovePicker mp = MovePicker(pos, ttMove, depth, H, &ss[ply]);
- CheckInfo ci(pos);
- futilityValue = VALUE_NONE;
- useFutilityPruning = depth < SelectiveDepth && !isCheck;
-
- // Calculate depth dependant futility pruning parameters
- const int FutilityMoveCountMargin = 3 + (1 << (3 * int(depth) / 8));
- const int FutilityValueMargin = 112 * bitScanReverse32(int(depth) * int(depth) / 2);
-
- // Avoid calling evaluate() if we already have the score in TT
- if (tte && (tte->type() & VALUE_TYPE_EVAL))
- futilityValue = value_from_tt(tte->value(), ply) + FutilityValueMargin;
-
- // Loop through all legal moves until no moves remain or a beta cutoff occurs
- while ( bestValue < beta
- && (move = mp.get_next_move()) != MOVE_NONE
- && !thread_should_stop(threadID))
- {
- assert(move_is_ok(move));
-
- if (move == excludedMove)
- continue;
-
- singleReply = (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, false, captureOrPromotion, moveIsCheck, singleReply, mateThreat, &dangerous);
-
- // We want to extend the TT move if it is much better then remaining ones.
- // To verify this we do a reduced search on all the other moves but the ttMove,
- // if result is lower then TT value minus a margin then we assume ttMove is the
- // only one playable. It is a kind of relaxed single reply extension.
- if ( depth >= 8 * OnePly
- && tte
- && move == tte->move()
- && !excludedMove // Do not allow recursive single-reply search
- && 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 search result is well below the foreseen score of the ttMove then we
- // assume ttMove is the only one realistically playable and we extend it.
- if (excValue < ttValue - SingleReplyMargin)
- ext = OnePly;
- }
- }
-
- newDepth = depth - OnePly + ext;
-
- // Update current move
- movesSearched[moveCount++] = ss[ply].currentMove = move;
-
- // Futility pruning
- if ( useFutilityPruning
- && !dangerous
- && !captureOrPromotion
- && move != ttMove)
- {
- // Move count based pruning
- if ( moveCount >= FutilityMoveCountMargin
- && ok_to_prune(pos, move, ss[ply].threatMove)
- && bestValue > value_mated_in(PLY_MAX))
- continue;
-
- // Value based pruning
- if (futilityValue == VALUE_NONE)
- futilityValue = evaluate(pos, ei, threadID) + FutilityValueMargin;
-
- futilityValueScaled = futilityValue - moveCount * IncrementalFutilityMargin;
-
- if (futilityValueScaled < beta)
- {
- if (futilityValueScaled > bestValue)
- bestValue = futilityValueScaled;
- continue;
- }
- }
-
- // Make and search the move
- pos.do_move(move, st, ci, moveIsCheck);
-
- // 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.
- if ( depth >= 3*OnePly
- && moveCount >= LMRNonPVMoves
- && !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && !move_is_killer(move, ss[ply]))
- {
- ss[ply].reduction = OnePly;
- value = -search(pos, ss, -(beta-1), newDepth-OnePly, ply+1, true, threadID);
- }
- else
- value = beta; // Just to trigger next condition
-
- if (value >= beta) // Go with full depth non-pv search
- {
- ss[ply].reduction = Depth(0);
- value = -search(pos, ss, -(beta-1), newDepth, ply+1, true, threadID);
- }
- pos.undo_move(move);
-
- assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
-
- // New best move?
- if (value > bestValue)
- {
- bestValue = value;
- if (value >= beta)
- update_pv(ss, ply);
-
- if (value == value_mate_in(ply + 1))
- ss[ply].mateKiller = move;
- }
-
- // Split?
- if ( ActiveThreads > 1
- && bestValue < beta
- && depth >= MinimumSplitDepth
- && Iteration <= 99
- && idle_thread_exists(threadID)
- && !AbortSearch
- && !thread_should_stop(threadID)
- && split(pos, ss, ply, &beta, &beta, &bestValue, futilityValue, approximateEval,
- depth, &moveCount, &mp, threadID, false))
- 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 excludedMove ? beta - 1 : (pos.is_check() ? 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 < beta)
- TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
- else
- {
- 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(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, move);
- }
-
- assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
-
- return bestValue;
- }
-
-
- // qsearch() is the quiescence search function, which is called by the main
- // search function when the remaining depth is zero (or, to be more precise,
- // less than OnePly).
-
- Value qsearch(Position& pos, SearchStack ss[], Value alpha, Value beta,
- Depth depth, int ply, int threadID) {
-
- assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE);
- assert(beta >= -VALUE_INFINITE && beta <= VALUE_INFINITE);
- assert(depth <= 0);
- assert(ply >= 0 && ply < PLY_MAX);
- assert(threadID >= 0 && threadID < ActiveThreads);
-
- EvalInfo ei;
- StateInfo st;
- Move ttMove, move;
- Value staticValue, bestValue, value, futilityValue;
- bool isCheck, enoughMaterial, moveIsCheck;
- const TTEntry* tte = NULL;
- int moveCount = 0;
- bool pvNode = (beta - alpha != 1);