std::copy(newPv.begin(), newPv.begin() + 3, pv);
StateInfo st[2];
- pos.do_move(newPv[0], st[0], pos.gives_check(newPv[0]));
- pos.do_move(newPv[1], st[1], pos.gives_check(newPv[1]));
+ pos.do_move(newPv[0], st[0]);
+ pos.do_move(newPv[1], st[1]);
expectedPosKey = pos.key();
pos.undo_move(newPv[1]);
pos.undo_move(newPv[0]);
{1, 0, 0, 0, 0, 1, 1 ,1},
};
+ Value bonus(Depth depth) { int d = depth / ONE_PLY ; return Value(d * d + 2 * d - 2); }
+ Value penalty(Depth depth) { int d = depth / ONE_PLY ; return -Value(d * d + 4 * d + 1); }
+
const size_t HalfDensitySize = std::extent<decltype(HalfDensity)>::value;
EasyMoveManager EasyMove;
cnt = 1, nodes++;
else
{
- pos.do_move(m, st, pos.gives_check(m));
+ pos.do_move(m, st);
cnt = leaf ? MoveList<LEGAL>(pos).size() : perft<false>(pos, depth - ONE_PLY);
nodes += cnt;
pos.undo_move(m);
void Thread::search() {
- Stack stack[MAX_PLY+7], *ss = stack+5; // To allow referencing (ss-5) and (ss+2)
+ Stack stack[MAX_PLY+7], *ss = stack+4; // To allow referencing (ss-4) and (ss+2)
Value bestValue, alpha, beta, delta;
Move easyMove = MOVE_NONE;
MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr);
- std::memset(ss-5, 0, 8 * sizeof(Stack));
+ std::memset(ss-4, 0, 7 * sizeof(Stack));
bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE;
if (thisThread->resetCalls.load(std::memory_order_relaxed))
{
thisThread->resetCalls = false;
- thisThread->callsCnt = 0;
+ // At low node count increase the checking rate to about 0.1% of nodes
+ // otherwise use a default value.
+ thisThread->callsCnt = Limits.nodes ? std::min((int64_t)4096, Limits.nodes / 1024)
+ : 4096;
}
- if (++thisThread->callsCnt > 4096)
+
+ if (--thisThread->callsCnt <= 0)
{
for (Thread* th : Threads)
th->resetCalls = true;
ss->counterMoves = nullptr;
(ss+1)->skipEarlyPruning = false;
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
+ Square prevSq = to_sq((ss-1)->currentMove);
// Step 4. Transposition table lookup. We don't want the score of a partial
// search to overwrite a previous full search TT value, so we use a different
// If ttMove is quiet, update killers, history, counter move on TT hit
if (ttValue >= beta && ttMove)
{
- int d = depth / ONE_PLY;
-
if (!pos.capture_or_promotion(ttMove))
- {
- Value bonus = Value(d * d + 2 * d - 2);
- update_stats(pos, ss, ttMove, nullptr, 0, bonus);
- }
+ update_stats(pos, ss, ttMove, nullptr, 0, bonus(depth));
// Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
- {
- Value penalty = Value(d * d + 4 * d + 1);
- Square prevSq = to_sq((ss-1)->currentMove);
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty);
- }
+ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth));
}
return ttValue;
}
{
ss->currentMove = move;
ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)];
- pos.do_move(move, st, pos.gives_check(move));
+ pos.do_move(move, st);
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
pos.undo_move(move);
if (value >= rbeta)
&& !pos.see_ge(move, Value(-35 * lmrDepth * lmrDepth)))
continue;
}
- else if ( depth < 7 * ONE_PLY
- && !extension
- && !pos.see_ge(move, Value(-35 * depth / ONE_PLY * depth / ONE_PLY)))
+ else if (depth < 7 * ONE_PLY && !extension)
+ {
+ Value v = -Value(399 + 35 * depth / ONE_PLY * depth / ONE_PLY);
+
+ if (PvNode)
+ v += beta - alpha - 1;
+
+ if (!pos.see_ge(move, v))
continue;
+ }
}
// Speculative prefetch as early as possible
// Decrease reduction for moves that escape a capture. Filter out
// castling moves, because they are coded as "king captures rook" and
- // hence break make_move(). Also use see() instead of see_sign(),
- // because the destination square is empty.
+ // hence break make_move().
else if ( type_of(move) == NORMAL
- && type_of(pos.piece_on(to_sq(move))) != PAWN
&& !pos.see_ge(make_move(to_sq(move), from_sq(move)), VALUE_ZERO))
r -= 2 * ONE_PLY;
: inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
else if (bestMove)
{
- int d = depth / ONE_PLY;
// Quiet best move: update killers, history and countermoves
if (!pos.capture_or_promotion(bestMove))
- {
- Value bonus = Value(d * d + 2 * d - 2);
- update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus);
- }
+ update_stats(pos, ss, bestMove, quietsSearched, quietCount, bonus(depth));
// Extra penalty for a quiet TT move in previous ply when it gets refuted
if ((ss-1)->moveCount == 1 && !pos.captured_piece())
- {
- Value penalty = Value(d * d + 4 * d + 1);
- Square prevSq = to_sq((ss-1)->currentMove);
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty);
- }
+ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, penalty(depth));
}
// Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY
&& !pos.captured_piece()
&& is_ok((ss-1)->currentMove))
- {
- int d = depth / ONE_PLY;
- Value bonus = Value(d * d + 2 * d - 2);
- Square prevSq = to_sq((ss-1)->currentMove);
- update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus);
- }
+ update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus(depth));
tte->save(posKey, value_to_tt(bestValue, ss->ply),
bestValue >= beta ? BOUND_LOWER :
if (!pv[0])
return false;
- pos.do_move(pv[0], st, pos.gives_check(pv[0]));
+ pos.do_move(pv[0], st);
TTEntry* tte = TT.probe(pos.key(), ttHit);
if (ttHit)