- 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]))
- {
- ss[ply].reduction = pv_reduction(depth, moveCount);
- if (ss[ply].reduction)
- {
- 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)
- value = -search_pv(pos, ss, -beta, -alpha, newDepth, 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);
- 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, &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;
-
- // We don't want the score of a partial search to overwrite a previous full search
- // TT value, so we use a different position key in case of an excluded move exsists.
- Key posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key();
-
- // Transposition table lookup
- tte = TT.retrieve(posKey);
- ttMove = (tte ? tte->move() : MOVE_NONE);
-
- if (tte && ok_to_use_TT(tte, depth, beta, ply))
- {
- ss[ply].currentMove = ttMove; // Can be MOVE_NONE
- return value_from_tt(tte->value(), ply);
- }
-
- isCheck = pos.is_check();
-
- // Calculate depth dependant futility pruning parameters
- const int FutilityMoveCountMargin = 3 + (1 << (3 * int(depth) / 8));
-
- // Evaluate the position statically
- if (!isCheck)
- {
- if (tte && (tte->type() & VALUE_TYPE_EVAL))
- staticValue = value_from_tt(tte->value(), ply);
- else
- {
- staticValue = evaluate(pos, ei, threadID);
- ss[ply].evalInfo = &ei;
- }
-
- ss[ply].eval = staticValue;
- futilityValue = staticValue + FutilityMargins[int(depth)]; //FIXME: Remove me, only for split
- staticValue = refine_eval(tte, staticValue, ply); // Enhance accuracy with TT value if possible
- update_gains(pos, ss[ply - 1].currentMove, ss[ply - 1].eval, ss[ply].eval);
- }
-
- // Static null move pruning. We're betting that the opponent doesn't have
- // a move that will reduce the score by more than FutilityMargins[int(depth)]
- // if we do a null move.
- if ( !isCheck
- && allowNullmove
- && depth < RazorDepth
- && staticValue - FutilityMargins[int(depth)] >= beta)
- return staticValue - FutilityMargins[int(depth)];
-
- // Null move search
- if ( allowNullmove
- && depth > OnePly
- && !isCheck
- && !value_is_mate(beta)
- && ok_to_do_nullmove(pos)
- && staticValue >= beta - NullMoveMargin)
- {
- ss[ply].currentMove = MOVE_NULL;
-
- pos.do_null_move(st);
-
- // Null move dynamic reduction based on depth
- int R = 3 + (depth >= 5 * OnePly ? depth / 8 : 0);
-
- // Null move dynamic reduction based on value
- if (staticValue - beta > PawnValueMidgame)
- R++;
-
- nullValue = -search(pos, ss, -(beta-1), depth-R*OnePly, ply+1, false, threadID);
-
- pos.undo_null_move();
-
- if (nullValue >= beta)
- {
- if (depth < 6 * OnePly)
- return beta;
-
- // Do zugzwang verification search
- Value v = search(pos, ss, beta, depth-5*OnePly, ply, false, threadID);
- if (v >= beta)
- return beta;
- } else {
- // The null move failed low, which means that we may be faced with
- // some kind of threat. If the previous move was reduced, check if
- // the move that refuted the null move was somehow connected to the
- // move which was reduced. If a connection is found, return a fail
- // low score (which will cause the reduced move to fail high in the
- // parent node, which will trigger a re-search with full depth).
- 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(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);
-
- // 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)