- // Step 18. Check for split
- if ( TM.active_threads() > 1
- && bestValue < beta
- && depth >= MinimumSplitDepth
- && Iteration <= 99
- && TM.available_thread_exists(threadID)
- && !AbortSearch
- && !TM.thread_should_stop(threadID)
- && TM.split(pos, ss, ply, NULL, beta, &bestValue,
- depth, mateThreat, &moveCount, &mp, threadID, false))
- break;
- }
-
- // Step 19. Check for mate and stalemate
- // All legal moves have been searched and if there were
- // no legal moves, it must be mate or stalemate.
- // If one move was excluded return fail low.
- if (!moveCount)
- return excludedMove ? beta - 1 : (pos.is_check() ? value_mated_in(ply) : VALUE_DRAW);
-
- // Step 20. Update tables
- // If the search is not aborted, update the transposition table,
- // history counters, and killer moves.
- if (AbortSearch || TM.thread_should_stop(threadID))
- return bestValue;
-
- if (bestValue < beta)
- TT.store(posKey, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
- else
- {
- TM.incrementBetaCounter(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 < TM.active_threads());
-
- 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 || TM.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)
- && !pvNode
- && 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)