- if (nullValue == value_mated_in(ply + 2))
- mateThreat = true;
-
- ss[ply].threatMove = ss[ply + 1].currentMove;
- if ( depth < ThreatDepth
- && ss[ply - 1].reduction
- && connected_moves(pos, ss[ply - 1].currentMove, ss[ply].threatMove))
- return beta - 1;
- }
- }
- // Null move search not allowed, try razoring
- else if ( !value_is_mate(beta)
- && !isCheck
- && depth < RazorDepth
- && staticValue < beta - (NullMoveMargin + 16 * depth)
- && ss[ply - 1].currentMove != MOVE_NULL
- && ttMove == MOVE_NONE
- && !pos.has_pawn_on_7th(pos.side_to_move()))
- {
- Value rbeta = beta - (NullMoveMargin + 16 * depth);
- 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 && ss[ply].eval >= beta - IIDMargin)
- {
- search(pos, ss, beta, Min(depth/2, depth-2*OnePly), ply, false, threadID);
- ttMove = ss[ply].pv[ply];
- tte = TT.retrieve(posKey);
- }
-
- // 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);
-
- // 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;
-
- moveIsCheck = pos.move_is_check(move, ci);
- singleEvasion = (isCheck && mp.number_of_evasions() == 1);
- captureOrPromotion = pos.move_is_capture_or_promotion(move);
-
- // Decide the new search depth
- ext = extension(pos, move, false, 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 >= 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 (excValue < ttValue - SingleReplyMargin)
- ext = OnePly;
- }
- }
-
- newDepth = depth - OnePly + ext;
-
- // Update current move
- movesSearched[moveCount++] = ss[ply].currentMove = move;
-
- // Futility pruning
- if ( !isCheck
- && !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && 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
- Depth predictedDepth = newDepth;
-
- //FIXME: We are ignoring condition: depth >= 3*OnePly, BUG??
- ss[ply].reduction = nonpv_reduction(depth, moveCount);
- if (ss[ply].reduction)
- predictedDepth -= ss[ply].reduction;
-
- if (predictedDepth < SelectiveDepth)
- {
- int preFutilityValueMargin = 0;
- if (predictedDepth >= OnePly)
- preFutilityValueMargin = FutilityMargins[int(predictedDepth)];
-
- preFutilityValueMargin += H.gain(pos.piece_on(move_from(move)), move_to(move)) + 45;
-
- futilityValueScaled = ss[ply].eval + preFutilityValueMargin - 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.
- bool doFullDepthSearch = true;
-
- if ( depth >= 3*OnePly
- && !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && !move_is_killer(move, ss[ply]))
- {
- ss[ply].reduction = nonpv_reduction(depth, moveCount);
- if (ss[ply].reduction)
- {
- value = -search(pos, ss, -(beta-1), newDepth-ss[ply].reduction, ply+1, true, threadID);
- doFullDepthSearch = (value >= beta);
- }
- }
-
- if (doFullDepthSearch) // 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, //FIXME: SMP & futilityValue
- 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)
- 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];
- TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, move);
- if (!pos.move_is_capture_or_promotion(move))
- {
- update_history(pos, move, depth, movesSearched, moveCount);
- update_killers(move, ss[ply]);
- }
-
- }
-
- 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, futilityBase, futilityValue;
- bool isCheck, enoughMaterial, moveIsCheck, evasionPrunable;
- const TTEntry* tte = NULL;
- int moveCount = 0;
- bool pvNode = (beta - alpha != 1);
- Value oldAlpha = alpha;
-
- // 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;
-
- // Transposition table lookup. At PV nodes, we don't use the TT for
- // pruning, but only for move ordering.
- tte = TT.retrieve(pos.get_key());
- ttMove = (tte ? tte->move() : MOVE_NONE);
-
- if (!pvNode && tte && ok_to_use_TT(tte, depth, beta, ply))
- {
- assert(tte->type() != VALUE_TYPE_EVAL);
-
- ss[ply].currentMove = ttMove; // Can be MOVE_NONE
- return value_from_tt(tte->value(), ply);
- }
-
- isCheck = pos.is_check();
-
- // Evaluate the position statically
- if (isCheck)
- staticValue = -VALUE_INFINITE;
- else if (tte && (tte->type() & VALUE_TYPE_EVAL))
- staticValue = value_from_tt(tte->value(), ply);
- else
- staticValue = evaluate(pos, ei, threadID);
-
- if (!isCheck)
- {
- ss[ply].eval = staticValue;
- update_gains(pos, ss[ply - 1].currentMove, ss[ply - 1].eval, ss[ply].eval);
- }
-
- // Initialize "stand pat score", and return it immediately if it is
- // at least beta.
- bestValue = staticValue;
-
- if (bestValue >= beta)
- {
- // Store the score to avoid a future costly evaluation() call
- if (!isCheck && !tte && ei.futilityMargin[pos.side_to_move()] == 0)
- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EV_LO, Depth(-127*OnePly), MOVE_NONE);
-
- return bestValue;
- }
-
- if (bestValue > alpha)
- alpha = bestValue;
-
- // If we are near beta then try to get a cutoff pushing checks a bit further
- bool deepChecks = depth == -OnePly && staticValue >= beta - PawnValueMidgame / 8;
-
- // Initialize a MovePicker object for the current position, and prepare
- // to search the moves. Because the depth is <= 0 here, only captures,
- // queen promotions and checks (only if depth == 0 or depth == -OnePly
- // and we are near beta) will be generated.
- MovePicker mp = MovePicker(pos, ttMove, deepChecks ? Depth(0) : depth, H);
- CheckInfo ci(pos);
- enoughMaterial = pos.non_pawn_material(pos.side_to_move()) > RookValueMidgame;
- futilityBase = staticValue + FutilityMarginQS + ei.futilityMargin[pos.side_to_move()];
-
- // Loop through the moves until no moves remain or a beta cutoff
- // occurs.
- while ( alpha < beta
- && (move = mp.get_next_move()) != MOVE_NONE)
- {
- assert(move_is_ok(move));
-
- moveIsCheck = pos.move_is_check(move, ci);
-
- // Update current move
- moveCount++;
- ss[ply].currentMove = move;
-
- // Futility pruning
- if ( enoughMaterial
- && !isCheck
- && !pvNode
- && !moveIsCheck
- && move != ttMove
- && !move_is_promotion(move)
- && !pos.move_is_passed_pawn_push(move))
- {
- futilityValue = futilityBase
- + pos.endgame_value_of_piece_on(move_to(move))
- + (move_is_ep(move) ? PawnValueEndgame : Value(0));
-
- if (futilityValue < alpha)
- {
- if (futilityValue > bestValue)
- bestValue = futilityValue;
- continue;
- }
- }
-
- // Detect blocking evasions that are candidate to be pruned
- evasionPrunable = isCheck
- && bestValue != -VALUE_INFINITE
- && !pos.move_is_capture(move)
- && pos.type_of_piece_on(move_from(move)) != KING
- && !pos.can_castle(pos.side_to_move());
-
- // Don't search moves with negative SEE values
- if ( (!isCheck || evasionPrunable)
- && move != ttMove
- && !move_is_promotion(move)
- && pos.see_sign(move) < 0)
- continue;
-
- // Make and search the move
- pos.do_move(move, st, ci, moveIsCheck);
- value = -qsearch(pos, ss, -beta, -alpha, depth-OnePly, ply+1, threadID);
- 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);
- }
- }
- }