Value root_search(Position& pos, SearchStack* ss, RootMoveList& rml, Value* alphaPtr, Value* betaPtr);
template <NodeType PvNode>
- 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 <NodeType PvNode>
Value qsearch(Position& pos, SearchStack* ss, Value alpha, Value beta, Depth depth, int threadID);
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)
int64_t nodes_searched() { return TM.nodes_searched(); }
+/// init_search() is called during startup. It initializes various lookup tables
+
+void init_search() {
+
+ int d; // depth (OnePly == 2)
+ int hd; // half depth (OnePly == 1)
+ int mc; // moveCount
+
+ // Init reductions array
+ for (hd = 1; hd < 64; hd++) for (mc = 1; mc < 64; mc++)
+ {
+ double pvRed = log(double(hd)) * log(double(mc)) / 3.0;
+ double nonPVRed = log(double(hd)) * log(double(mc)) / 1.5;
+ ReductionMatrix[PV][hd][mc] = (int8_t) ( pvRed >= 1.0 ? floor( pvRed * int(OnePly)) : 0);
+ ReductionMatrix[NonPV][hd][mc] = (int8_t) (nonPVRed >= 1.0 ? floor(nonPVRed * int(OnePly)) : 0);
+ }
+
+ // Init futility margins array
+ for (d = 0; d < 16; d++) for (mc = 0; mc < 64; mc++)
+ FutilityMarginsMatrix[d][mc] = 112 * int(log(double(d * d) / 2) / log(2.0) + 1) - 8 * mc + 45;
+
+ // Init futility move count array
+ for (d = 0; d < 32; d++)
+ FutilityMoveCountArray[d] = 3 + (1 << (3 * d / 8));
+}
+
+
+// SearchStack::init() initializes a search stack. Used at the beginning of a
+// new search from the root.
+void SearchStack::init(int ply) {
+
+ pv[ply] = pv[ply + 1] = MOVE_NONE;
+ currentMove = threatMove = MOVE_NONE;
+ reduction = Depth(0);
+ eval = VALUE_NONE;
+}
+
+void SearchStack::initKillers() {
+
+ mateKiller = MOVE_NONE;
+ for (int i = 0; i < KILLER_MAX; i++)
+ killers[i] = MOVE_NONE;
+}
+
+
/// perft() is our utility to verify move generation is bug free. All the legal
/// moves up to given depth are generated and counted and the sum returned.
}
-/// init_search() is called during startup. It initializes various lookup tables
-
-void init_search() {
-
- // Init our reduction lookup tables
- for (int i = 1; i < 64; i++) // i == depth (OnePly = 1)
- for (int j = 1; j < 64; j++) // j == moveNumber
- {
- double pvRed = log(double(i)) * log(double(j)) / 3.0;
- double nonPVRed = log(double(i)) * log(double(j)) / 1.5;
- ReductionMatrix[PV][i][j] = (int8_t) ( pvRed >= 1.0 ? floor( pvRed * int(OnePly)) : 0);
- ReductionMatrix[NonPV][i][j] = (int8_t) (nonPVRed >= 1.0 ? floor(nonPVRed * int(OnePly)) : 0);
- }
-
- // Init futility margins array
- for (int i = 0; i < 16; i++) // i == depth (OnePly = 2)
- for (int j = 0; j < 64; j++) // j == moveNumber
- {
- // FIXME: test using log instead of BSR
- FutilityMarginsMatrix[i][j] = (i < 2 ? 0 : 112 * bitScanReverse32(i * i / 2)) - 8 * j + 45;
- }
-
- // Init futility move count array
- for (int i = 0; i < 32; i++) // i == depth (OnePly = 2)
- FutilityMoveCountArray[i] = 3 + (1 << (3 * i / 8));
-}
-
-
-// SearchStack::init() initializes a search stack. Used at the beginning of a
-// new search from the root.
-void SearchStack::init(int ply) {
-
- pv[ply] = pv[ply + 1] = MOVE_NONE;
- currentMove = threatMove = MOVE_NONE;
- reduction = Depth(0);
- eval = VALUE_NONE;
-}
-
-void SearchStack::initKillers() {
-
- mateKiller = MOVE_NONE;
- for (int i = 0; i < KILLER_MAX; i++)
- killers[i] = MOVE_NONE;
-}
-
namespace {
// id_loop() is the main iterative deepening loop. It calls root_search
Value id_loop(const Position& pos, Move searchMoves[]) {
- Position p(pos);
+ Position p(pos, pos.thread());
SearchStack ss[PLY_MAX_PLUS_2];
Move EasyMove = MOVE_NONE;
Value value, alpha = -VALUE_INFINITE, beta = VALUE_INFINITE;
// 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;
alpha = -VALUE_INFINITE;
// Full depth PV search, done on first move or after a fail high
- value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, 0);
+ value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, 0);
}
else
{
if (ss->reduction)
{
// Reduced depth non-pv search using alpha as upperbound
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, 0);
+ value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, 0);
doFullDepthSearch = (value > alpha);
}
}
{
// Full depth non-pv search using alpha as upperbound
ss->reduction = Depth(0);
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth, true, 0);
+ value = -search<NonPV>(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<PV>(pos, ss+1, -beta, -alpha, newDepth, false, 0);
+ value = -search<PV>(pos, ss+1, -beta, -alpha, newDepth, 0);
}
}
// search<>() is the main search function for both PV and non-PV nodes
template <NodeType PvNode>
- 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);
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
// 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);
// 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
// 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
pos.do_null_move(st);
+ (ss+1)->skipNullMove = true;
+
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);
+ : - search<NonPV>(pos, ss+1, -beta, -alpha, depth-R*OnePly, threadID);
+
+ (ss+1)->skipNullMove = false;
+
pos.undo_null_move();
if (nullValue >= beta)
nullValue = beta;
// Do zugzwang verification search at high depths
- if ( depth < 6 * OnePly
- || search<NonPV>(pos, ss, alpha, beta, depth-5*OnePly, false, threadID) >= beta)
+ if (depth < 6 * OnePly)
+ return nullValue;
+
+ ss->skipNullMove = true;
+ Value v = search<NonPV>(pos, ss, alpha, beta, depth-5*OnePly, threadID);
+ ss->skipNullMove = false;
+
+ if (v >= beta)
return nullValue;
}
else
&& (PvNode || (!isCheck && ss->eval >= beta - IIDMargin)))
{
Depth d = (PvNode ? depth - 2 * OnePly : depth / 2);
- search<PvNode>(pos, ss, alpha, beta, d, false, threadID);
+
+ ss->skipNullMove = true;
+ search<PvNode>(pos, ss, alpha, beta, d, threadID);
+ ss->skipNullMove = false;
+
ttMove = ss->pv[ply];
tte = TT.retrieve(posKey);
}
if (abs(ttValue) < VALUE_KNOWN_WIN)
{
Value b = ttValue - SingularExtensionMargin;
- Value v = search<NonPV>(pos, ss, b - 1, b, depth / 2, false, threadID, move);
+ ss->excludedMove = move;
+ ss->skipNullMove = true;
+ Value v = search<NonPV>(pos, ss, b - 1, b, depth / 2, threadID);
+ ss->skipNullMove = false;
+ ss->excludedMove = MOVE_NONE;
if (v < ttValue - SingularExtensionMargin)
ext = OnePly;
// The first move in list is the expected PV
if (PvNode && moveCount == 1)
value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
- : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+ : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, threadID);
else
{
// Step 14. Reduced depth search
{
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);
+ : - search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, threadID);
doFullDepthSearch = (value > alpha);
}
assert(newDepth - OnePly >= OnePly);
ss->reduction = OnePly;
- value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, true, threadID);
+ value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, newDepth-ss->reduction, threadID);
doFullDepthSearch = (value > alpha);
}
ss->reduction = Depth(0); // Restore original reduction
if (doFullDepthSearch)
{
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);
+ : - search<NonPV>(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<PV>(pos, ss+1, -beta, -alpha, Depth(0), threadID)
- : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, false, threadID);
+ : - search<PV>(pos, ss+1, -beta, -alpha, newDepth, threadID);
}
}
int moveCount;
value = -VALUE_INFINITE;
- Position pos(*sp->pos);
+ Position pos(*sp->pos, threadID);
CheckInfo ci(pos);
int ply = pos.ply();
SearchStack* ss = sp->sstack[threadID] + 1;
// Step 12. Futility pruning (is omitted in PV nodes)
if ( !PvNode
+ && !captureOrPromotion
&& !isCheck
&& !dangerous
- && !captureOrPromotion
&& !move_is_castle(move))
{
// Move count based pruning
// If the move fails high will be re-searched at full depth.
bool doFullDepthSearch = true;
- if ( !dangerous
- && !captureOrPromotion
+ if ( !captureOrPromotion
+ && !dangerous
&& !move_is_castle(move)
&& !move_is_killer(move, ss))
{
if (ss->reduction)
{
Value localAlpha = sp->alpha;
- value = -search<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, true, threadID);
+ Depth d = newDepth - ss->reduction;
+ value = d < OnePly ? -qsearch<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), threadID)
+ : - search<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, d, threadID);
doFullDepthSearch = (value > localAlpha);
}
// 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 localAlpha = sp->alpha;
- value = -search<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, true, threadID);
+ value = -search<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth-ss->reduction, threadID);
doFullDepthSearch = (value > localAlpha);
}
+ ss->reduction = Depth(0); // Restore original reduction
}
// Step 15. Full depth search
if (doFullDepthSearch)
{
- ss->reduction = Depth(0);
Value localAlpha = sp->alpha;
- value = -search<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, newDepth, true, threadID);
+ value = newDepth < OnePly ? -qsearch<NonPV>(pos, ss+1, -(localAlpha+1), -localAlpha, Depth(0), threadID)
+ : - search<NonPV>(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 = -search<PV>(pos, ss+1, -sp->beta, -sp->alpha, newDepth, false, threadID);
+ value = newDepth < OnePly ? -qsearch<PV>(pos, ss+1, -sp->beta, -sp->alpha, Depth(0), threadID)
+ : - search<PV>(pos, ss+1, -sp->beta, -sp->alpha, newDepth, threadID);
}
// Step 16. Undo move
}
- // 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();
+ }
}
}
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<PV>(pos, ss+1, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 0);