X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fsearch.cpp;h=7ab74998295aa864750a2643a9bd56a5cb6e3bd5;hp=6ee1ea02a17b1e45d9dc603a75ec23a6d8e0d50e;hb=f148a8f6ccbb57c440910ecfd4845c7f497b5404;hpb=2572055c87f04832e43d63f30f26de04cadb729c diff --git a/src/search.cpp b/src/search.cpp index 6ee1ea02..7ab74998 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -285,7 +285,7 @@ namespace { Value root_search(Position& pos, SearchStack* ss, RootMoveList& rml, Value* alphaPtr, Value* betaPtr); template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, bool allowNullmove, int threadID, Move excludedMove = MOVE_NONE); + Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int threadID); template Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int threadID); @@ -313,7 +313,7 @@ namespace { void poll(); void ponderhit(); void wait_for_stop_or_ponderhit(); - void init_ss_array(SearchStack* ss); + void init_ss_array(SearchStack* ss, int size); void print_pv_info(const Position& pos, SearchStack* ss, Value alpha, Value beta, Value value); #if !defined(_MSC_VER) @@ -633,7 +633,7 @@ namespace { // Initialize TT.new_search(); H.clear(); - init_ss_array(ss); + init_ss_array(ss, PLY_MAX_PLUS_2); ValueByIteration[1] = rml.get_move_score(0); p.reset_ply(); Iteration = 1; @@ -876,7 +876,7 @@ namespace { alpha = -VALUE_INFINITE; // Full depth PV search, done on first move or after a fail high - value = -search(pos, ss+1, -beta, -alpha, newDepth, false, 0); + value = -search(pos, ss+1, -beta, -alpha, newDepth, 0); } else { @@ -893,7 +893,7 @@ namespace { if (ss->reduction) { // Reduced depth non-pv search using alpha as upperbound - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, 0); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, 0); doFullDepthSearch = (value > alpha); } } @@ -903,12 +903,12 @@ namespace { { // Full depth non-pv search using alpha as upperbound ss->reduction = Depth(0); - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth, true, 0); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth, 0); // If we are above alpha then research at same depth but as PV // to get a correct score or eventually a fail high above beta. if (value > alpha) - value = -search(pos, ss+1, -beta, -alpha, newDepth, false, 0); + value = -search(pos, ss+1, -beta, -alpha, newDepth, 0); } } @@ -1031,8 +1031,7 @@ namespace { // search<>() is the main search function for both PV and non-PV nodes template - Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, - bool allowNullmove, int threadID, Move excludedMove) { + Value search(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int threadID) { assert(alpha >= -VALUE_INFINITE && alpha <= VALUE_INFINITE); assert(beta > alpha && beta <= VALUE_INFINITE); @@ -1044,7 +1043,8 @@ namespace { EvalInfo ei; StateInfo st; const TTEntry* tte; - Move ttMove, move; + Key posKey; + Move ttMove, move, excludedMove; Depth ext, newDepth; Value bestValue, value, oldAlpha; Value refinedValue, nullValue, futilityValueScaled; // Non-PV specific @@ -1083,7 +1083,8 @@ namespace { // 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 exists. - Key posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key(); + excludedMove = ss->excludedMove; + posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key(); tte = TT.retrieve(posKey); ttMove = (tte ? tte->move() : MOVE_NONE); @@ -1144,7 +1145,7 @@ namespace { // We're betting that the opponent doesn't have a move that will reduce // the score by more than futility_margin(depth) if we do a null move. if ( !PvNode - && allowNullmove + && !ss->skipNullMove && depth < RazorDepth && refinedValue >= beta + futility_margin(depth, 0) && !isCheck @@ -1157,7 +1158,7 @@ namespace { // at least beta. Otherwise we do a null move if static value is not more than // NullMoveMargin under beta. if ( !PvNode - && allowNullmove + && !ss->skipNullMove && depth > OnePly && refinedValue >= beta - (depth >= 4 * OnePly ? NullMoveMargin : 0) && !isCheck @@ -1175,8 +1176,13 @@ namespace { pos.do_null_move(st); + (ss+1)->skipNullMove = true; + nullValue = depth-R*OnePly < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), threadID) - : - search(pos, ss+1, -beta, -alpha, depth-R*OnePly, false, threadID); + : - search(pos, ss+1, -beta, -alpha, depth-R*OnePly, threadID); + + (ss+1)->skipNullMove = false; + pos.undo_null_move(); if (nullValue >= beta) @@ -1186,8 +1192,14 @@ namespace { nullValue = beta; // Do zugzwang verification search at high depths - if ( depth < 6 * OnePly - || search(pos, ss, alpha, beta, depth-5*OnePly, false, threadID) >= beta) + if (depth < 6 * OnePly) + return nullValue; + + ss->skipNullMove = true; + Value v = search(pos, ss, alpha, beta, depth-5*OnePly, threadID); + ss->skipNullMove = false; + + if (v >= beta) return nullValue; } else @@ -1215,7 +1227,11 @@ namespace { && (PvNode || (!isCheck && ss->eval >= beta - IIDMargin))) { Depth d = (PvNode ? depth - 2 * OnePly : depth / 2); - search(pos, ss, alpha, beta, d, false, threadID); + + ss->skipNullMove = true; + search(pos, ss, alpha, beta, d, threadID); + ss->skipNullMove = false; + ttMove = ss->pv[ply]; tte = TT.retrieve(posKey); } @@ -1263,7 +1279,11 @@ namespace { if (abs(ttValue) < VALUE_KNOWN_WIN) { Value b = ttValue - SingularExtensionMargin; - Value v = search(pos, ss, b - 1, b, depth / 2, false, threadID, move); + ss->excludedMove = move; + ss->skipNullMove = true; + Value v = search(pos, ss, b - 1, b, depth / 2, threadID); + ss->skipNullMove = false; + ss->excludedMove = MOVE_NONE; if (v < ttValue - SingularExtensionMargin) ext = OnePly; @@ -1311,7 +1331,7 @@ namespace { // The first move in list is the expected PV if (PvNode && moveCount == 1) value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), threadID) - : - search(pos, ss+1, -beta, -alpha, newDepth, false, threadID); + : - search(pos, ss+1, -beta, -alpha, newDepth, threadID); else { // Step 14. Reduced depth search @@ -1329,7 +1349,7 @@ namespace { { Depth d = newDepth - ss->reduction; value = d < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID) - : - search(pos, ss+1, -(alpha+1), -alpha, d, true, threadID); + : - search(pos, ss+1, -(alpha+1), -alpha, d, threadID); doFullDepthSearch = (value > alpha); } @@ -1342,7 +1362,7 @@ namespace { assert(newDepth - OnePly >= OnePly); ss->reduction = OnePly; - value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID); + value = -search(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, threadID); doFullDepthSearch = (value > alpha); } ss->reduction = Depth(0); // Restore original reduction @@ -1352,14 +1372,14 @@ namespace { if (doFullDepthSearch) { value = newDepth < OnePly ? -qsearch(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID) - : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, true, threadID); + : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, threadID); // Step extra. pv search (only in PV nodes) // Search only for possible new PV nodes, if instead value >= beta then // parent node fails low with value <= alpha and tries another move. if (PvNode && value > alpha && value < beta) value = newDepth < OnePly ? -qsearch(pos, ss+1, -beta, -alpha, Depth(0), threadID) - : - search(pos, ss+1, -beta, -alpha, newDepth, false, threadID); + : - search(pos, ss+1, -beta, -alpha, newDepth, threadID); } } @@ -1720,7 +1740,7 @@ namespace { Value localAlpha = sp->alpha; Depth d = newDepth - ss->reduction; value = d < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), threadID) - : - search(pos, ss+1, -(localAlpha+1), -localAlpha, d, true, threadID); + : - search(pos, ss+1, -(localAlpha+1), -localAlpha, d, threadID); doFullDepthSearch = (value > localAlpha); } @@ -1733,7 +1753,7 @@ namespace { ss->reduction = OnePly; Value localAlpha = sp->alpha; - value = -search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, true, threadID); + value = -search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, threadID); doFullDepthSearch = (value > localAlpha); } ss->reduction = Depth(0); // Restore original reduction @@ -1744,14 +1764,14 @@ namespace { { Value localAlpha = sp->alpha; value = newDepth < OnePly ? -qsearch(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), threadID) - : - search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth, true, threadID); + : - search(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth, threadID); // Step extra. pv search (only in PV nodes) // Search only for possible new PV nodes, if instead value >= beta then // parent node fails low with value <= alpha and tries another move. if (PvNode && value > localAlpha && value < sp->beta) value = newDepth < OnePly ? -qsearch(pos, ss+1, -sp->beta, -sp->alpha, Depth(0), threadID) - : - search(pos, ss+1, -sp->beta, -sp->alpha, newDepth, false, threadID); + : - search(pos, ss+1, -sp->beta, -sp->alpha, newDepth, threadID); } // Step 16. Undo move @@ -2213,14 +2233,21 @@ namespace { } - // init_ss_array() does a fast reset of the first entries of a SearchStack array + // init_ss_array() does a fast reset of the first entries of a SearchStack + // array and of all the excludedMove and skipNullMove entries. - void init_ss_array(SearchStack* ss) { + void init_ss_array(SearchStack* ss, int size) { - for (int i = 0; i < 3; i++, ss++) + for (int i = 0; i < size; i++, ss++) { - ss->init(i); - ss->initKillers(); + ss->excludedMove = MOVE_NONE; + ss->skipNullMove = false; + + if (i < 3) + { + ss->init(i); + ss->initKillers(); + } } } @@ -2752,7 +2779,7 @@ namespace { continue; // Find a quick score for the move - init_ss_array(ss); + init_ss_array(ss, PLY_MAX_PLUS_2); pos.do_move(cur->move, st); moves[count].move = cur->move; moves[count].score = -qsearch(pos, ss+1, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 0);