- TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, d, ss[ply].pv[ply]);
-
- assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
-
- return bestValue;
- }
-
-
- // sp_search() is used to search from a split point. This function is called
- // by each thread working at the split point. It is similar to the normal
- // search() function, but simpler. Because we have already probed the hash
- // table, done a null move search, and searched the first move before
- // splitting, we don't have to repeat all this work in sp_search(). We
- // also don't need to store anything to the hash table here: This is taken
- // care of after we return from the split point.
-
- void sp_search(SplitPoint* sp, int threadID) {
-
- assert(threadID >= 0 && threadID < ActiveThreads);
- assert(ActiveThreads > 1);
-
- Position pos(*sp->pos);
- CheckInfo ci(pos);
- SearchStack* ss = sp->sstack[threadID];
- Value value = -VALUE_INFINITE;
- Move move;
- int moveCount;
- bool isCheck = pos.is_check();
- bool useFutilityPruning = sp->depth < SelectiveDepth
- && !isCheck;
-
- const int FutilityMoveCountMargin = 3 + (1 << (3 * int(sp->depth) / 8));
-
- // Precalculate reduction parameters
- float LogLimit, Gradient, BaseReduction = 0.5;
- reduction_parameters(BaseReduction, 3.0, sp->depth, LogLimit, Gradient);
-
- while ( lock_grab_bool(&(sp->lock))
- && sp->bestValue < sp->beta
- && !thread_should_stop(threadID)
- && (move = sp->mp->get_next_move()) != MOVE_NONE)
- {
- moveCount = ++sp->moves;
- lock_release(&(sp->lock));
-
- assert(move_is_ok(move));
-
- bool moveIsCheck = pos.move_is_check(move, ci);
- bool captureOrPromotion = pos.move_is_capture_or_promotion(move);
-
- ss[sp->ply].currentMove = move;
-
- // Decide the new search depth
- bool dangerous;
- Depth ext = extension(pos, move, false, captureOrPromotion, moveIsCheck, false, false, &dangerous);
- Depth newDepth = sp->depth - OnePly + ext;
-
- // Prune?
- if ( useFutilityPruning
- && !dangerous
- && !captureOrPromotion)
- {
- // Move count based pruning
- if ( moveCount >= FutilityMoveCountMargin
- && ok_to_prune(pos, move, ss[sp->ply].threatMove)
- && sp->bestValue > value_mated_in(PLY_MAX))
- continue;
-
- // Value based pruning
- Value futilityValueScaled = sp->futilityValue - moveCount * IncrementalFutilityMargin;
-
- if (futilityValueScaled < sp->beta)
- {
- if (futilityValueScaled > sp->bestValue) // Less then 1% of cases
- {
- lock_grab(&(sp->lock));
- if (futilityValueScaled > sp->bestValue)
- sp->bestValue = futilityValueScaled;
- lock_release(&(sp->lock));
- }
- continue;
- }
- }
-
- // Make and search the move.
- StateInfo st;
- 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 ( !dangerous
- && !captureOrPromotion
- && !move_is_castle(move)
- && !move_is_killer(move, ss[sp->ply]))
- {
- ss[sp->ply].reduction = reduction(moveCount, LogLimit, BaseReduction, Gradient);
- if (ss[sp->ply].reduction)
- {
- value = -search(pos, ss, -(sp->beta-1), newDepth-ss[sp->ply].reduction, sp->ply+1, true, threadID);
- doFullDepthSearch = (value >= sp->beta);
- }
- }
-
- if (doFullDepthSearch) // Go with full depth non-pv search
- {
- ss[sp->ply].reduction = Depth(0);
- value = -search(pos, ss, -(sp->beta - 1), newDepth, sp->ply+1, true, threadID);
- }
- pos.undo_move(move);
-
- assert(value > -VALUE_INFINITE && value < VALUE_INFINITE);
-
- if (thread_should_stop(threadID))
- {
- lock_grab(&(sp->lock));
- break;
- }
-
- // New best move?
- if (value > sp->bestValue) // Less then 2% of cases
- {
- lock_grab(&(sp->lock));
- if (value > sp->bestValue && !thread_should_stop(threadID))
- {
- sp->bestValue = value;
- if (sp->bestValue >= sp->beta)
- {
- sp_update_pv(sp->parentSstack, ss, sp->ply);
- for (int i = 0; i < ActiveThreads; i++)
- if (i != threadID && (i == sp->master || sp->slaves[i]))
- Threads[i].stop = true;
-
- sp->finished = true;
- }
- }
- lock_release(&(sp->lock));
- }
- }
-
- /* Here we have the lock still grabbed */
-
- // If this is the master thread and we have been asked to stop because of
- // a beta cutoff higher up in the tree, stop all slave threads.
- if (sp->master == threadID && thread_should_stop(threadID))
- for (int i = 0; i < ActiveThreads; i++)
- if (sp->slaves[i])
- Threads[i].stop = true;
-
- sp->cpus--;
- sp->slaves[threadID] = 0;
-
- lock_release(&(sp->lock));
- }
-
-
- // sp_search_pv() is used to search from a PV split point. This function
- // is called by each thread working at the split point. It is similar to
- // the normal search_pv() function, but simpler. Because we have already
- // probed the hash table and searched the first move before splitting, we
- // don't have to repeat all this work in sp_search_pv(). We also don't
- // need to store anything to the hash table here: This is taken care of
- // after we return from the split point.
-
- void sp_search_pv(SplitPoint* sp, int threadID) {
-
- assert(threadID >= 0 && threadID < ActiveThreads);
- assert(ActiveThreads > 1);
-
- Position pos(*sp->pos);
- CheckInfo ci(pos);
- SearchStack* ss = sp->sstack[threadID];
- Value value = -VALUE_INFINITE;
- int moveCount;
- Move move;
-
- // Precalculate reduction parameters
- float LogLimit, Gradient, BaseReduction = 0.5;
- reduction_parameters(BaseReduction, 6.0, sp->depth, LogLimit, Gradient);
-
- while ( lock_grab_bool(&(sp->lock))
- && sp->alpha < sp->beta
- && !thread_should_stop(threadID)
- && (move = sp->mp->get_next_move()) != MOVE_NONE)