+
+void Tablebases::filter_root_moves(Position& pos, Search::RootMoves& rootMoves) {
+
+ Hits = 0;
+ RootInTB = false;
+ UseRule50 = Options["Syzygy50MoveRule"];
+ ProbeDepth = Options["SyzygyProbeDepth"] * ONE_PLY;
+ Cardinality = Options["SyzygyProbeLimit"];
+
+ // Skip TB probing when no TB found: !TBLargest -> !TB::Cardinality
+ if (Cardinality > MaxCardinality)
+ {
+ Cardinality = MaxCardinality;
+ ProbeDepth = DEPTH_ZERO;
+ }
+
+ if (Cardinality < popcount(pos.pieces()) || pos.can_castle(ANY_CASTLING))
+ return;
+
+ // If the current root position is in the tablebases, then RootMoves
+ // contains only moves that preserve the draw or the win.
+ RootInTB = root_probe(pos, rootMoves, TB::Score);
+
+ if (RootInTB)
+ Cardinality = 0; // Do not probe tablebases during the search
+
+ else // If DTZ tables are missing, use WDL tables as a fallback
+ {
+ // Filter out moves that do not preserve the draw or the win.
+ RootInTB = root_probe_wdl(pos, rootMoves, TB::Score);
+
+ // Only probe during search if winning
+ if (TB::Score <= VALUE_DRAW)
+ Cardinality = 0;
+ }
+
+ if (RootInTB)
+ {
+ Hits = rootMoves.size();
+
+ if (!UseRule50)
+ TB::Score = TB::Score > VALUE_DRAW ? VALUE_MATE - MAX_PLY - 1
+ : TB::Score < VALUE_DRAW ? -VALUE_MATE + MAX_PLY + 1
+ : VALUE_DRAW;
+ }
+}