2 Copyright (c) 2013 Ronald de Man
3 This file may be redistributed and/or modified without restrictions.
5 tbprobe.cpp contains the Stockfish-specific routines of the
6 tablebase probing code. It should be relatively easy to adapt
7 this code to other chess engines.
14 #include "../position.h"
15 #include "../movegen.h"
16 #include "../bitboard.h"
17 #include "../search.h"
25 extern Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
28 int Tablebases::MaxCardinality = 0;
30 // Given a position with 6 or fewer pieces, produce a text string
31 // of the form KQPvKRP, where "KQP" represents the white pieces if
32 // mirror == 0 and the black pieces if mirror == 1.
33 static void prt_str(Position& pos, char *str, int mirror)
39 color = !mirror ? WHITE : BLACK;
40 for (pt = KING; pt >= PAWN; --pt)
41 for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
42 *str++ = pchr[6 - pt];
45 for (pt = KING; pt >= PAWN; --pt)
46 for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
47 *str++ = pchr[6 - pt];
51 // Given a position, produce a 64-bit material signature key.
52 // If the engine supports such a key, it should equal the engine's key.
53 static uint64 calc_key(Position& pos, int mirror)
60 color = !mirror ? WHITE : BLACK;
61 for (pt = PAWN; pt <= KING; ++pt)
62 for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
63 key ^= Zobrist::psq[WHITE][pt][i - 1];
65 for (pt = PAWN; pt <= KING; ++pt)
66 for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
67 key ^= Zobrist::psq[BLACK][pt][i - 1];
72 // Produce a 64-bit material key corresponding to the material combination
73 // defined by pcs[16], where pcs[1], ..., pcs[6] is the number of white
74 // pawns, ..., kings and pcs[9], ..., pcs[14] is the number of black
76 static uint64 calc_key_from_pcs(int *pcs, int mirror)
83 color = !mirror ? 0 : 8;
84 for (pt = PAWN; pt <= KING; ++pt)
85 for (i = 0; i < pcs[color + pt]; i++)
86 key ^= Zobrist::psq[WHITE][pt][i];
88 for (pt = PAWN; pt <= KING; ++pt)
89 for (i = 0; i < pcs[color + pt]; i++)
90 key ^= Zobrist::psq[BLACK][pt][i];
95 bool is_little_endian() {
104 static ubyte decompress_pairs(struct PairsData *d, uint64 idx)
106 static const bool isLittleEndian = is_little_endian();
107 return isLittleEndian ? decompress_pairs<true >(d, idx)
108 : decompress_pairs<false>(d, idx);
111 // probe_wdl_table and probe_dtz_table require similar adaptations.
112 static int probe_wdl_table(Position& pos, int *success)
115 struct TBHashEntry *ptr2;
122 // Obtain the position's material signature key.
123 key = pos.material_key();
126 if (key == (Zobrist::psq[WHITE][KING][0] ^ Zobrist::psq[BLACK][KING][0]))
129 ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
130 for (i = 0; i < HSHMAX; i++)
131 if (ptr2[i].key == key) break;
142 prt_str(pos, str, ptr->key != key);
143 if (!init_table_wdl(ptr, str)) {
149 // Memory barrier to ensure ptr->ready = 1 is not reordered.
153 __asm__ __volatile__ ("" ::: "memory");
160 int bside, mirror, cmirror;
161 if (!ptr->symmetric) {
162 if (key != ptr->key) {
165 bside = (pos.side_to_move() == WHITE);
167 cmirror = mirror = 0;
168 bside = !(pos.side_to_move() == WHITE);
171 cmirror = pos.side_to_move() == WHITE ? 0 : 8;
172 mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
176 // p[i] is to contain the square 0-63 (A1-H8) for a piece of type
177 // pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king.
178 // Pieces of the same type are guaranteed to be consecutive.
179 if (!ptr->has_pawns) {
180 struct TBEntry_piece *entry = (struct TBEntry_piece *)ptr;
181 ubyte *pc = entry->pieces[bside];
182 for (i = 0; i < entry->num;) {
183 Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
184 (PieceType)(pc[i] & 0x07));
186 p[i++] = pop_lsb(&bb);
189 idx = encode_piece(entry, entry->norm[bside], p, entry->factor[bside]);
190 res = decompress_pairs(entry->precomp[bside], idx);
192 struct TBEntry_pawn *entry = (struct TBEntry_pawn *)ptr;
193 int k = entry->file[0].pieces[0][0] ^ cmirror;
194 Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
197 p[i++] = pop_lsb(&bb) ^ mirror;
199 int f = pawn_file(entry, p);
200 ubyte *pc = entry->file[f].pieces[bside];
201 for (; i < entry->num;) {
202 bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
203 (PieceType)(pc[i] & 0x07));
205 p[i++] = pop_lsb(&bb) ^ mirror;
208 idx = encode_pawn(entry, entry->file[f].norm[bside], p, entry->file[f].factor[bside]);
209 res = decompress_pairs(entry->file[f].precomp[bside], idx);
212 return ((int)res) - 2;
215 static int probe_dtz_table(Position& pos, int wdl, int *success)
222 // Obtain the position's material signature key.
223 uint64 key = pos.material_key();
225 if (DTZ_table[0].key1 != key && DTZ_table[0].key2 != key) {
226 for (i = 1; i < DTZ_ENTRIES; i++)
227 if (DTZ_table[i].key1 == key) break;
228 if (i < DTZ_ENTRIES) {
229 struct DTZTableEntry table_entry = DTZ_table[i];
231 DTZ_table[i] = DTZ_table[i - 1];
232 DTZ_table[0] = table_entry;
234 struct TBHashEntry *ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
235 for (i = 0; i < HSHMAX; i++)
236 if (ptr2[i].key == key) break;
243 int mirror = (ptr->key != key);
244 prt_str(pos, str, mirror);
245 if (DTZ_table[DTZ_ENTRIES - 1].entry)
246 free_dtz_entry(DTZ_table[DTZ_ENTRIES-1].entry);
247 for (i = DTZ_ENTRIES - 1; i > 0; i--)
248 DTZ_table[i] = DTZ_table[i - 1];
249 load_dtz_table(str, calc_key(pos, mirror), calc_key(pos, !mirror));
253 ptr = DTZ_table[0].entry;
259 int bside, mirror, cmirror;
260 if (!ptr->symmetric) {
261 if (key != ptr->key) {
264 bside = (pos.side_to_move() == WHITE);
266 cmirror = mirror = 0;
267 bside = !(pos.side_to_move() == WHITE);
270 cmirror = pos.side_to_move() == WHITE ? 0 : 8;
271 mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
275 if (!ptr->has_pawns) {
276 struct DTZEntry_piece *entry = (struct DTZEntry_piece *)ptr;
277 if ((entry->flags & 1) != bside && !entry->symmetric) {
281 ubyte *pc = entry->pieces;
282 for (i = 0; i < entry->num;) {
283 Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
284 (PieceType)(pc[i] & 0x07));
286 p[i++] = pop_lsb(&bb);
289 idx = encode_piece((struct TBEntry_piece *)entry, entry->norm, p, entry->factor);
290 res = decompress_pairs(entry->precomp, idx);
292 if (entry->flags & 2)
293 res = entry->map[entry->map_idx[wdl_to_map[wdl + 2]] + res];
295 if (!(entry->flags & pa_flags[wdl + 2]) || (wdl & 1))
298 struct DTZEntry_pawn *entry = (struct DTZEntry_pawn *)ptr;
299 int k = entry->file[0].pieces[0] ^ cmirror;
300 Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
303 p[i++] = pop_lsb(&bb) ^ mirror;
305 int f = pawn_file((struct TBEntry_pawn *)entry, p);
306 if ((entry->flags[f] & 1) != bside) {
310 ubyte *pc = entry->file[f].pieces;
311 for (; i < entry->num;) {
312 bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
313 (PieceType)(pc[i] & 0x07));
315 p[i++] = pop_lsb(&bb) ^ mirror;
318 idx = encode_pawn((struct TBEntry_pawn *)entry, entry->file[f].norm, p, entry->file[f].factor);
319 res = decompress_pairs(entry->file[f].precomp, idx);
321 if (entry->flags[f] & 2)
322 res = entry->map[entry->map_idx[f][wdl_to_map[wdl + 2]] + res];
324 if (!(entry->flags[f] & pa_flags[wdl + 2]) || (wdl & 1))
331 // Add underpromotion captures to list of captures.
332 static ExtMove *add_underprom_caps(Position& pos, ExtMove *stack, ExtMove *end)
334 ExtMove *moves, *extra = end;
336 for (moves = stack; moves < end; moves++) {
337 Move move = moves->move;
338 if (type_of(move) == PROMOTION && !pos.empty(to_sq(move))) {
339 (*extra++).move = (Move)(move - (1 << 12));
340 (*extra++).move = (Move)(move - (2 << 12));
341 (*extra++).move = (Move)(move - (3 << 12));
348 static int probe_ab(Position& pos, int alpha, int beta, int *success)
352 ExtMove *moves, *end;
355 // Generate (at least) all legal non-ep captures including (under)promotions.
356 // It is OK to generate more, as long as they are filtered out below.
357 if (!pos.checkers()) {
358 end = generate<CAPTURES>(pos, stack);
359 // Since underpromotion captures are not included, we need to add them.
360 end = add_underprom_caps(pos, stack, end);
362 end = generate<EVASIONS>(pos, stack);
364 for (moves = stack; moves < end; moves++) {
365 Move capture = moves->move;
366 if (!pos.capture(capture) || type_of(capture) == ENPASSANT
367 || !pos.legal(capture))
369 pos.do_move(capture, st, pos.gives_check(capture));
370 v = -probe_ab(pos, -beta, -alpha, success);
371 pos.undo_move(capture);
372 if (*success == 0) return 0;
382 v = probe_wdl_table(pos, success);
383 if (*success == 0) return 0;
385 *success = 1 + (alpha > 0);
393 // Probe the WDL table for a particular position.
394 // If *success != 0, the probe was successful.
395 // The return value is from the point of view of the side to move:
397 // -1 : loss, but draw under 50-move rule
399 // 1 : win, but draw under 50-move rule
401 int Tablebases::probe_wdl(Position& pos, int *success)
406 v = probe_ab(pos, -2, 2, success);
408 // If en passant is not possible, we are done.
409 if (pos.ep_square() == SQ_NONE)
411 if (!(*success)) return 0;
413 // Now handle en passant.
415 // Generate (at least) all legal en passant captures.
417 ExtMove *moves, *end;
421 end = generate<CAPTURES>(pos, stack);
423 end = generate<EVASIONS>(pos, stack);
425 for (moves = stack; moves < end; moves++) {
426 Move capture = moves->move;
427 if (type_of(capture) != ENPASSANT
428 || !pos.legal(capture))
430 pos.do_move(capture, st, pos.gives_check(capture));
431 int v0 = -probe_ab(pos, -2, 2, success);
432 pos.undo_move(capture);
433 if (*success == 0) return 0;
434 if (v0 > v1) v1 = v0;
439 // Check whether there is at least one legal non-ep move.
440 for (moves = stack; moves < end; moves++) {
441 Move capture = moves->move;
442 if (type_of(capture) == ENPASSANT) continue;
443 if (pos.legal(capture)) break;
445 if (moves == end && !pos.checkers()) {
446 end = generate<QUIETS>(pos, end);
447 for (; moves < end; moves++) {
448 Move move = moves->move;
453 // If not, then we are forced to play the losing ep capture.
462 // This routine treats a position with en passant captures as one without.
463 static int probe_dtz_no_ep(Position& pos, int *success)
467 wdl = probe_ab(pos, -2, 2, success);
468 if (*success == 0) return 0;
470 if (wdl == 0) return 0;
473 return wdl == 2 ? 1 : 101;
476 ExtMove *moves, *end = NULL;
480 // Generate at least all legal non-capturing pawn moves
481 // including non-capturing promotions.
483 end = generate<NON_EVASIONS>(pos, stack);
485 end = generate<EVASIONS>(pos, stack);
487 for (moves = stack; moves < end; moves++) {
488 Move move = moves->move;
489 if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move)
492 pos.do_move(move, st, pos.gives_check(move));
493 int v = -Tablebases::probe_wdl(pos, success);
495 if (*success == 0) return 0;
497 return v == 2 ? 1 : 101;
501 dtz = 1 + probe_dtz_table(pos, wdl, success);
503 if (wdl & 1) dtz += 100;
504 return wdl >= 0 ? dtz : -dtz;
509 for (moves = stack; moves < end; moves++) {
510 Move move = moves->move;
511 if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN
514 pos.do_move(move, st, pos.gives_check(move));
515 int v = -Tablebases::probe_dtz(pos, success);
517 if (*success == 0) return 0;
518 if (v > 0 && v + 1 < best)
525 end = generate<NON_EVASIONS>(pos, stack);
527 end = generate<EVASIONS>(pos, stack);
528 for (moves = stack; moves < end; moves++) {
530 Move move = moves->move;
531 if (!pos.legal(move))
533 pos.do_move(move, st, pos.gives_check(move));
534 if (st.rule50 == 0) {
535 if (wdl == -2) v = -1;
537 v = probe_ab(pos, 1, 2, success);
538 v = (v == 2) ? 0 : -101;
541 v = -Tablebases::probe_dtz(pos, success) - 1;
544 if (*success == 0) return 0;
552 static int wdl_to_dtz[] = {
556 // Probe the DTZ table for a particular position.
557 // If *success != 0, the probe was successful.
558 // The return value is from the point of view of the side to move:
559 // n < -100 : loss, but draw under 50-move rule
560 // -100 <= n < -1 : loss in n ply (assuming 50-move counter == 0)
562 // 1 < n <= 100 : win in n ply (assuming 50-move counter == 0)
563 // 100 < n : win, but draw under 50-move rule
565 // The return value n can be off by 1: a return value -n can mean a loss
566 // in n+1 ply and a return value +n can mean a win in n+1 ply. This
567 // cannot happen for tables with positions exactly on the "edge" of
570 // This implies that if dtz > 0 is returned, the position is certainly
571 // a win if dtz + 50-move-counter <= 99. Care must be taken that the engine
572 // picks moves that preserve dtz + 50-move-counter <= 99.
574 // If n = 100 immediately after a capture or pawn move, then the position
575 // is also certainly a win, and during the whole phase until the next
576 // capture or pawn move, the inequality to be preserved is
577 // dtz + 50-movecounter <= 100.
579 // In short, if a move is available resulting in dtz + 50-move-counter <= 99,
580 // then do not accept moves leading to dtz + 50-move-counter == 100.
582 int Tablebases::probe_dtz(Position& pos, int *success)
585 int v = probe_dtz_no_ep(pos, success);
587 if (pos.ep_square() == SQ_NONE)
589 if (*success == 0) return 0;
591 // Now handle en passant.
595 ExtMove *moves, *end;
599 end = generate<CAPTURES>(pos, stack);
601 end = generate<EVASIONS>(pos, stack);
603 for (moves = stack; moves < end; moves++) {
604 Move capture = moves->move;
605 if (type_of(capture) != ENPASSANT
606 || !pos.legal(capture))
608 pos.do_move(capture, st, pos.gives_check(capture));
609 int v0 = -probe_ab(pos, -2, 2, success);
610 pos.undo_move(capture);
611 if (*success == 0) return 0;
612 if (v0 > v1) v1 = v0;
615 v1 = wdl_to_dtz[v1 + 2];
620 if (v1 >= 0 || v1 < -100)
622 } else if (v > 100) {
628 } else if (v1 >= 0) {
631 for (moves = stack; moves < end; moves++) {
632 Move move = moves->move;
633 if (type_of(move) == ENPASSANT) continue;
634 if (pos.legal(move)) break;
636 if (moves == end && !pos.checkers()) {
637 end = generate<QUIETS>(pos, end);
638 for (; moves < end; moves++) {
639 Move move = moves->move;
652 // Check whether there has been at least one repetition of positions
653 // since the last capture or pawn move.
654 static int has_repeated(StateInfo *st)
657 int i = 4, e = std::min(st->rule50, st->pliesFromNull);
660 StateInfo *stp = st->previous->previous;
662 stp = stp->previous->previous;
663 if (stp->key == st->key)
671 static Value wdl_to_Value[5] = {
672 -VALUE_MATE + MAX_PLY + 1,
676 VALUE_MATE - MAX_PLY - 1
679 // Use the DTZ tables to filter out moves that don't preserve the win or draw.
680 // If the position is lost, but DTZ is fairly high, only keep moves that
683 // A return value false indicates that not all probes were successful and that
684 // no moves were filtered out.
685 bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score)
689 int dtz = probe_dtz(pos, &success);
690 if (!success) return false;
695 for (size_t i = 0; i < rootMoves.size(); i++) {
696 Move move = rootMoves[i].pv[0];
697 pos.do_move(move, st, pos.gives_check(move));
699 if (pos.checkers() && dtz > 0) {
701 if (generate<LEGAL>(pos, s) == s)
705 if (st.rule50 != 0) {
706 v = -Tablebases::probe_dtz(pos, &success);
710 v = -Tablebases::probe_wdl(pos, &success);
711 v = wdl_to_dtz[v + 2];
715 if (!success) return false;
716 rootMoves[i].score = (Value)v;
719 // Obtain 50-move counter for the root position.
720 // In Stockfish there seems to be no clean way, so we do it like this:
721 int cnt50 = st.previous->rule50;
723 // Use 50-move counter to determine whether the root position is
724 // won, lost or drawn.
727 wdl = (dtz + cnt50 <= 100) ? 2 : 1;
729 wdl = (-dtz + cnt50 <= 100) ? -2 : -1;
731 // Determine the score to report to the user.
732 score = wdl_to_Value[wdl + 2];
733 // If the position is winning or losing, but too few moves left, adjust the
734 // score to show how close it is to winning or losing.
735 // NOTE: int(PawnValueEg) is used as scaling factor in score_to_uci().
736 if (wdl == 1 && dtz <= 100)
737 score = (Value)(((200 - dtz - cnt50) * int(PawnValueEg)) / 200);
738 else if (wdl == -1 && dtz >= -100)
739 score = -(Value)(((200 + dtz - cnt50) * int(PawnValueEg)) / 200);
741 // Now be a bit smart about filtering out moves.
743 if (dtz > 0) { // winning (or 50-move rule draw)
745 for (size_t i = 0; i < rootMoves.size(); i++) {
746 int v = rootMoves[i].score;
747 if (v > 0 && v < best)
751 // If the current phase has not seen repetitions, then try all moves
752 // that stay safely within the 50-move budget, if there are any.
753 if (!has_repeated(st.previous) && best + cnt50 <= 99)
755 for (size_t i = 0; i < rootMoves.size(); i++) {
756 int v = rootMoves[i].score;
757 if (v > 0 && v <= max)
758 rootMoves[j++] = rootMoves[i];
760 } else if (dtz < 0) { // losing (or 50-move rule draw)
762 for (size_t i = 0; i < rootMoves.size(); i++) {
763 int v = rootMoves[i].score;
767 // Try all moves, unless we approach or have a 50-move rule draw.
768 if (-best * 2 + cnt50 < 100)
770 for (size_t i = 0; i < rootMoves.size(); i++) {
771 if (rootMoves[i].score == best)
772 rootMoves[j++] = rootMoves[i];
775 // Try all moves that preserve the draw.
776 for (size_t i = 0; i < rootMoves.size(); i++) {
777 if (rootMoves[i].score == 0)
778 rootMoves[j++] = rootMoves[i];
781 rootMoves.resize(j, Search::RootMove(MOVE_NONE));
786 // Use the WDL tables to filter out moves that don't preserve the win or draw.
787 // This is a fallback for the case that some or all DTZ tables are missing.
789 // A return value false indicates that not all probes were successful and that
790 // no moves were filtered out.
791 bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score)
795 int wdl = Tablebases::probe_wdl(pos, &success);
796 if (!success) return false;
797 score = wdl_to_Value[wdl + 2];
804 for (size_t i = 0; i < rootMoves.size(); i++) {
805 Move move = rootMoves[i].pv[0];
806 pos.do_move(move, st, pos.gives_check(move));
807 int v = -Tablebases::probe_wdl(pos, &success);
809 if (!success) return false;
810 rootMoves[i].score = (Value)v;
816 for (size_t i = 0; i < rootMoves.size(); i++) {
817 if (rootMoves[i].score == best)
818 rootMoves[j++] = rootMoves[i];
820 rootMoves.resize(j, Search::RootMove(MOVE_NONE));