template <NodeType PvNode>
Depth extension(const Position& pos, Move m, bool captureOrPromotion, bool moveIsCheck, bool singleEvasion, bool mateThreat, bool* dangerous);
- void init_node(SearchStack* ss, int ply, int threadID);
void update_pv(SearchStack* ss, int ply);
void sp_update_pv(SearchStack* pss, SearchStack* ss, int ply);
bool connected_moves(const Position& pos, Move m1, Move m2);
bool value_is_mate(Value value);
bool move_is_killer(Move m, SearchStack* ss);
bool ok_to_do_nullmove(const Position& pos);
- bool ok_to_prune(const Position& pos, Move m, Move threat);
bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply);
+ bool connected_threat(const Position& pos, Move m, Move threat);
Value refine_eval(const TTEntry* tte, Value defaultEval, int ply);
void update_history(const Position& pos, Move move, Depth depth, Move movesSearched[], int moveCount);
void update_killers(Move m, SearchStack* ss);
refinedValue = bestValue = value = -VALUE_INFINITE;
oldAlpha = alpha;
- if (depth < OnePly)
- return qsearch<PvNode>(pos, ss, alpha, beta, Depth(0), threadID);
+ // Step 1. Initialize node and poll. Polling can abort search
+ TM.incrementNodeCounter(threadID);
+ ss->init(ply);
+ (ss + 2)->initKillers();
- // Step 1. Initialize node and poll
- // Polling can abort search.
- init_node(ss, ply, threadID);
+ if (threadID == 0 && ++NodesSincePoll > NodesBetweenPolls)
+ {
+ NodesSincePoll = 0;
+ poll();
+ }
// Step 2. Check for aborted search and immediate draw
if (AbortSearch || TM.thread_should_stop(threadID))
pos.do_null_move(st);
- nullValue = -search<NonPV>(pos, ss+1, -beta, -alpha, depth-R*OnePly, false, threadID);
-
+ nullValue = depth-R*OnePly < OnePly ? -qsearch<NonPV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+ : - search<NonPV>(pos, ss+1, -beta, -alpha, depth-R*OnePly, false, threadID);
pos.undo_null_move();
if (nullValue >= beta)
{
// Move count based pruning
if ( moveCount >= futility_move_count(depth)
- && ok_to_prune(pos, move, ss->threatMove)
+ && !(ss->threatMove && connected_threat(pos, move, ss->threatMove))
&& bestValue > value_mated_in(PLY_MAX))
continue;
// Step extra. pv search (only in PV nodes)
// The first move in list is the expected PV
if (PvNode && moveCount == 1)
- value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+ value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+ : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
else
{
// Step 14. Reduced depth search
ss->reduction = reduction<PvNode>(depth, moveCount);
if (ss->reduction)
{
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID);
+ Depth d = newDepth - ss->reduction;
+ value = d < OnePly ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID)
+ : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true, threadID);
+
doFullDepthSearch = (value > alpha);
}
// if the move fails high again then go with full depth search.
if (doFullDepthSearch && ss->reduction > 2 * OnePly)
{
+ assert(newDepth - OnePly >= OnePly);
+
ss->reduction = OnePly;
value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID);
doFullDepthSearch = (value > alpha);
if (doFullDepthSearch)
{
ss->reduction = Depth(0);
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, true, threadID);
+ value = newDepth < OnePly ? -qsearch<NonPV>(pos, ss+1, -(alpha+1), -alpha, Depth(0), threadID)
+ : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, true, 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 = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+ value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
+ : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
}
}
int ply = pos.ply();
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);
+ TM.incrementNodeCounter(threadID);
+ ss->init(ply);
+ // Check for an instant draw or maximum ply reached
if (pos.is_draw() || ply >= PLY_MAX - 1)
return VALUE_DRAW;
{
// Move count based pruning
if ( moveCount >= futility_move_count(sp->depth)
- && ok_to_prune(pos, move, ss->threatMove)
+ && !(ss->threatMove && connected_threat(pos, move, ss->threatMove))
&& sp->bestValue > value_mated_in(PLY_MAX))
{
lock_grab(&(sp->lock));
lock_release(&(sp->lock));
}
- // init_node() is called at the beginning of all the search functions
- // (search() qsearch(), and so on) and initializes the
- // search stack object corresponding to the current node. Once every
- // NodesBetweenPolls nodes, init_node() also calls poll(), which polls
- // for user input and checks whether it is time to stop the search.
-
- void init_node(SearchStack* ss, int ply, int threadID) {
-
- assert(ply >= 0 && ply < PLY_MAX);
- assert(threadID >= 0 && threadID < TM.active_threads());
-
- TM.incrementNodeCounter(threadID);
-
- if (threadID == 0)
- {
- NodesSincePoll++;
- if (NodesSincePoll >= NodesBetweenPolls)
- {
- poll();
- NodesSincePoll = 0;
- }
- }
- ss->init(ply);
- (ss + 2)->initKillers();
- }
-
// update_pv() is called whenever a search returns a value > alpha.
// It updates the PV in the SearchStack object corresponding to the
// current node.
}
- // ok_to_prune() tests whether it is safe to forward prune a move. Only
- // non-tactical moves late in the move list close to the leaves are
- // candidates for pruning.
+ // connected_threat() tests whether it is safe to forward prune a move or if
+ // is somehow coonected to the threat move returned by null search.
- bool ok_to_prune(const Position& pos, Move m, Move threat) {
+ bool connected_threat(const Position& pos, Move m, Move threat) {
assert(move_is_ok(m));
- assert(threat == MOVE_NONE || move_is_ok(threat));
+ assert(threat && move_is_ok(threat));
assert(!pos.move_is_check(m));
assert(!pos.move_is_capture_or_promotion(m));
assert(!pos.move_is_passed_pawn_push(m));
Square mfrom, mto, tfrom, tto;
- // Prune if there isn't any threat move
- if (threat == MOVE_NONE)
- return true;
-
mfrom = move_from(m);
mto = move_to(m);
tfrom = move_from(threat);
// Case 1: Don't prune moves which move the threatened piece
if (mfrom == tto)
- return false;
+ return true;
// Case 2: If the threatened piece has value less than or equal to the
// value of the threatening piece, don't prune move which defend it.
&& ( pos.midgame_value_of_piece_on(tfrom) >= pos.midgame_value_of_piece_on(tto)
|| pos.type_of_piece_on(tfrom) == KING)
&& pos.move_attacks_square(m, tto))
- return false;
+ return true;
// Case 3: If the moving piece in the threatened move is a slider, don't
// prune safe moves which block its ray.
if ( piece_is_slider(pos.piece_on(tfrom))
&& bit_is_set(squares_between(tfrom, tto), mto)
&& pos.see_sign(m) >= 0)
- return false;
+ return true;
- return true;
+ return false;
}
// Wait for thread termination
for (int i = 1; i < MAX_THREADS; i++)
- while (threads[i].state != THREAD_TERMINATED);
+ while (threads[i].state != THREAD_TERMINATED) {}
// Now we can safely destroy the locks
for (int i = 0; i < MAX_THREADS; i++)