// Different node types, used as a template parameter
enum NodeType { NonPV, PV };
+ // Sizes and phases of the skip-blocks, used for distributing search depths across the threads
+ const int skipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
+ const int skipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 };
+
// Razoring and futility margin based on depth
const int razor_margin[4] = { 483, 570, 603, 554 };
Value futility_margin(Depth d) { return Value(150 * d / ONE_PLY); }
// History and stats update bonus, based on depth
Value stat_bonus(Depth depth) {
int d = depth / ONE_PLY ;
- return Value(d * d + 2 * d - 2);
+ return d > 17 ? VALUE_ZERO : Value(d * d + 2 * d - 2);
}
// Skill structure is used to implement strength limit
std::cout << sync_endl;
}
-// Sizes and phases of the skip-blocks, used for distributing search depths across the threads.
-static int skipsize[20] = {1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};
-static int phase [20] = {0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7};
-// Thread::search() is the main iterative deepening loop. It calls search()
-// repeatedly with increasing depth until the allocated thinking time has been
-// consumed, the user stops the search, or the maximum search depth is reached.
+/// Thread::search() is the main iterative deepening loop. It calls search()
+/// repeatedly with increasing depth until the allocated thinking time has been
+/// consumed, the user stops the search, or the maximum search depth is reached.
void Thread::search() {
MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr);
std::memset(ss-4, 0, 7 * sizeof(Stack));
+ for(int i = 4; i > 0; i--)
+ (ss-i)->counterMoves = &this->counterMoveHistory[NO_PIECE][0]; // Use as sentinel
bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE;
multiPV = std::min(multiPV, rootMoves.size());
- int hIdx = (idx - 1) % 20; // helper index, cycle after 20 threads
-
// Iterative deepening loop until requested to stop or the target depth is reached
while ( (rootDepth += ONE_PLY) < DEPTH_MAX
&& !Signals.stop
&& (!Limits.depth || Threads.main()->rootDepth / ONE_PLY <= Limits.depth))
{
- // skip half of the plies in blocks depending on game ply and helper index.
- if (idx && ((rootDepth / ONE_PLY + rootPos.game_ply() + phase[hIdx]) / skipsize[hIdx]) % 2)
- continue;
+ // Distribute search depths across the threads
+ if (idx)
+ {
+ int i = (idx - 1) % 20;
+ if (((rootDepth / ONE_PLY + rootPos.game_ply() + skipPhase[i]) / skipSize[i]) % 2)
+ continue;
+ }
// Age out PV variability metric
if (mainThread)
assert(0 <= ss->ply && ss->ply < MAX_PLY);
ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE;
- ss->counterMoves = nullptr;
+ ss->counterMoves = &thisThread->counterMoveHistory[NO_PIECE][0];
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
Square prevSq = to_sq((ss-1)->currentMove);
// Step 4a. Tablebase probe
if (!rootNode && TB::Cardinality)
{
- int piecesCount = pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK);
+ int piecesCount = pos.count<ALL_PIECES>();
if ( piecesCount <= TB::Cardinality
&& (piecesCount < TB::Cardinality || depth >= TB::ProbeDepth)
&& (ss->staticEval >= beta - 35 * (depth / ONE_PLY - 6) || depth >= 13 * ONE_PLY)
&& pos.non_pawn_material(pos.side_to_move()))
{
- ss->currentMove = MOVE_NULL;
- ss->counterMoves = nullptr;
assert(eval - beta >= 0);
// Null move dynamic reduction based on depth and value
Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY;
+ ss->currentMove = MOVE_NULL;
+ ss->counterMoves = &thisThread->counterMoveHistory[NO_PIECE][0];
+
pos.do_null_move(st);
Value nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -beta+1)
: - search<NonPV>(pos, ss+1, -beta, -beta+1, depth-R, !cutNode, true);
Depth rdepth = depth - 4 * ONE_PLY;
assert(rdepth >= ONE_PLY);
- assert((ss-1)->currentMove != MOVE_NONE);
- assert((ss-1)->currentMove != MOVE_NULL);
+ assert(is_ok((ss-1)->currentMove));
MovePicker mp(pos, ttMove, rbeta - ss->staticEval);
{
ss->currentMove = move;
ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)];
+
pos.do_move(move, st);
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode, false);
pos.undo_move(move);
moves_loop: // When in check search starts from here
- const CounterMoveStats* cmh = (ss-1)->counterMoves;
- const CounterMoveStats* fmh = (ss-2)->counterMoves;
- const CounterMoveStats* fmh2 = (ss-4)->counterMoves;
+ const CounterMoveStats& cmh = *(ss-1)->counterMoves;
+ const CounterMoveStats& fmh = *(ss-2)->counterMoves;
+ const CounterMoveStats& fm2 = *(ss-4)->counterMoves;
+ const bool cm_ok = is_ok((ss-1)->currentMove);
+ const bool fm_ok = is_ok((ss-2)->currentMove);
+ const bool f2_ok = is_ok((ss-4)->currentMove);
MovePicker mp(pos, ttMove, depth, ss);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
{
if ( !captureOrPromotion
&& !givesCheck
- && (!pos.advanced_pawn_push(move) || pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 5000))
+ && (!pos.advanced_pawn_push(move) || pos.non_pawn_material() >= 5000))
{
// Move count based pruning
if (moveCountPruning)
// Countermoves based pruning
if ( lmrDepth < 3
- && (!cmh || (*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
- && (!fmh || (*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO)
- && (!fmh2 || (*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO || (cmh && fmh)))
+ && ((cmh[moved_piece][to_sq(move)] < VALUE_ZERO) || !cm_ok)
+ && ((fmh[moved_piece][to_sq(move)] < VALUE_ZERO) || !fm_ok)
+ && ((fm2[moved_piece][to_sq(move)] < VALUE_ZERO) || !f2_ok || (cm_ok && fm_ok)))
continue;
// Futility pruning: parent node
&& !pos.see_ge(make_move(to_sq(move), from_sq(move)), VALUE_ZERO))
r -= 2 * ONE_PLY;
- ss->history = (cmh ? (*cmh )[moved_piece][to_sq(move)] : VALUE_ZERO)
- + (fmh ? (*fmh )[moved_piece][to_sq(move)] : VALUE_ZERO)
- + (fmh2 ? (*fmh2)[moved_piece][to_sq(move)] : VALUE_ZERO)
+ ss->history = cmh[moved_piece][to_sq(move)]
+ + fmh[moved_piece][to_sq(move)]
+ + fm2[moved_piece][to_sq(move)]
+ thisThread->history.get(~pos.side_to_move(), move)
- 4000; // Correction factor
// Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY
&& !pos.captured_piece()
- && is_ok((ss-1)->currentMove))
+ && cm_ok)
update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth));
tte->save(posKey, value_to_tt(bestValue, ss->ply),
void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus) {
- CounterMoveStats* cmh = (ss-1)->counterMoves;
- CounterMoveStats* fmh1 = (ss-2)->counterMoves;
- CounterMoveStats* fmh2 = (ss-4)->counterMoves;
-
- if (cmh)
- cmh->update(pc, s, bonus);
-
- if (fmh1)
- fmh1->update(pc, s, bonus);
-
- if (fmh2)
- fmh2->update(pc, s, bonus);
+ for (int i : {1, 2, 4})
+ if (is_ok((ss-i)->currentMove))
+ (ss-i)->counterMoves->update(pc, s, bonus);
}
thisThread->history.update(c, move, bonus);
update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus);
- if ((ss-1)->counterMoves)
+ if (is_ok((ss-1)->currentMove))
{
Square prevSq = to_sq((ss-1)->currentMove);
thisThread->counterMoves.update(pos.piece_on(prevSq), prevSq, move);