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"
18 #include "../bitcount.h"
26 extern Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
29 int Tablebases::MaxCardinality = 0;
31 // Given a position with 6 or fewer pieces, produce a text string
32 // of the form KQPvKRP, where "KQP" represents the white pieces if
33 // mirror == 0 and the black pieces if mirror == 1.
34 static void prt_str(Position& pos, char *str, int mirror)
40 color = !mirror ? WHITE : BLACK;
41 for (pt = KING; pt >= PAWN; --pt)
42 for (i = popcount<Max15>(pos.pieces(color, pt)); i > 0; i--)
43 *str++ = pchr[6 - pt];
46 for (pt = KING; pt >= PAWN; --pt)
47 for (i = popcount<Max15>(pos.pieces(color, pt)); i > 0; i--)
48 *str++ = pchr[6 - pt];
52 // Given a position, produce a 64-bit material signature key.
53 // If the engine supports such a key, it should equal the engine's key.
54 static uint64 calc_key(Position& pos, int mirror)
61 color = !mirror ? WHITE : BLACK;
62 for (pt = PAWN; pt <= KING; ++pt)
63 for (i = popcount<Max15>(pos.pieces(color, pt)); i > 0; i--)
64 key ^= Zobrist::psq[WHITE][pt][i - 1];
66 for (pt = PAWN; pt <= KING; ++pt)
67 for (i = popcount<Max15>(pos.pieces(color, pt)); i > 0; i--)
68 key ^= Zobrist::psq[BLACK][pt][i - 1];
73 // Produce a 64-bit material key corresponding to the material combination
74 // defined by pcs[16], where pcs[1], ..., pcs[6] is the number of white
75 // pawns, ..., kings and pcs[9], ..., pcs[14] is the number of black
77 static uint64 calc_key_from_pcs(int *pcs, int mirror)
84 color = !mirror ? 0 : 8;
85 for (pt = PAWN; pt <= KING; ++pt)
86 for (i = 0; i < pcs[color + pt]; i++)
87 key ^= Zobrist::psq[WHITE][pt][i];
89 for (pt = PAWN; pt <= KING; ++pt)
90 for (i = 0; i < pcs[color + pt]; i++)
91 key ^= Zobrist::psq[BLACK][pt][i];
96 bool is_little_endian() {
105 static ubyte decompress_pairs(struct PairsData *d, uint64 idx)
107 static const bool isLittleEndian = is_little_endian();
108 return isLittleEndian ? decompress_pairs<true >(d, idx)
109 : decompress_pairs<false>(d, idx);
112 // probe_wdl_table and probe_dtz_table require similar adaptations.
113 static int probe_wdl_table(Position& pos, int *success)
116 struct TBHashEntry *ptr2;
123 // Obtain the position's material signature key.
124 key = pos.material_key();
127 if (key == (Zobrist::psq[WHITE][KING][0] ^ Zobrist::psq[BLACK][KING][0]))
130 ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
131 for (i = 0; i < HSHMAX; i++)
132 if (ptr2[i].key == key) break;
143 prt_str(pos, str, ptr->key != key);
144 if (!init_table_wdl(ptr, str)) {
150 // Memory barrier to ensure ptr->ready = 1 is not reordered.
154 __asm__ __volatile__ ("" ::: "memory");
161 int bside, mirror, cmirror;
162 if (!ptr->symmetric) {
163 if (key != ptr->key) {
166 bside = (pos.side_to_move() == WHITE);
168 cmirror = mirror = 0;
169 bside = !(pos.side_to_move() == WHITE);
172 cmirror = pos.side_to_move() == WHITE ? 0 : 8;
173 mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
177 // p[i] is to contain the square 0-63 (A1-H8) for a piece of type
178 // pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king.
179 // Pieces of the same type are guaranteed to be consecutive.
180 if (!ptr->has_pawns) {
181 struct TBEntry_piece *entry = (struct TBEntry_piece *)ptr;
182 ubyte *pc = entry->pieces[bside];
183 for (i = 0; i < entry->num;) {
184 Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
185 (PieceType)(pc[i] & 0x07));
187 p[i++] = pop_lsb(&bb);
190 idx = encode_piece(entry, entry->norm[bside], p, entry->factor[bside]);
191 res = decompress_pairs(entry->precomp[bside], idx);
193 struct TBEntry_pawn *entry = (struct TBEntry_pawn *)ptr;
194 int k = entry->file[0].pieces[0][0] ^ cmirror;
195 Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
198 p[i++] = pop_lsb(&bb) ^ mirror;
200 int f = pawn_file(entry, p);
201 ubyte *pc = entry->file[f].pieces[bside];
202 for (; i < entry->num;) {
203 bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
204 (PieceType)(pc[i] & 0x07));
206 p[i++] = pop_lsb(&bb) ^ mirror;
209 idx = encode_pawn(entry, entry->file[f].norm[bside], p, entry->file[f].factor[bside]);
210 res = decompress_pairs(entry->file[f].precomp[bside], idx);
213 return ((int)res) - 2;
216 static int probe_dtz_table(Position& pos, int wdl, int *success)
223 // Obtain the position's material signature key.
224 uint64 key = pos.material_key();
226 if (DTZ_table[0].key1 != key && DTZ_table[0].key2 != key) {
227 for (i = 1; i < DTZ_ENTRIES; i++)
228 if (DTZ_table[i].key1 == key) break;
229 if (i < DTZ_ENTRIES) {
230 struct DTZTableEntry table_entry = DTZ_table[i];
232 DTZ_table[i] = DTZ_table[i - 1];
233 DTZ_table[0] = table_entry;
235 struct TBHashEntry *ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
236 for (i = 0; i < HSHMAX; i++)
237 if (ptr2[i].key == key) break;
244 int mirror = (ptr->key != key);
245 prt_str(pos, str, mirror);
246 if (DTZ_table[DTZ_ENTRIES - 1].entry)
247 free_dtz_entry(DTZ_table[DTZ_ENTRIES-1].entry);
248 for (i = DTZ_ENTRIES - 1; i > 0; i--)
249 DTZ_table[i] = DTZ_table[i - 1];
250 load_dtz_table(str, calc_key(pos, mirror), calc_key(pos, !mirror));
254 ptr = DTZ_table[0].entry;
260 int bside, mirror, cmirror;
261 if (!ptr->symmetric) {
262 if (key != ptr->key) {
265 bside = (pos.side_to_move() == WHITE);
267 cmirror = mirror = 0;
268 bside = !(pos.side_to_move() == WHITE);
271 cmirror = pos.side_to_move() == WHITE ? 0 : 8;
272 mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
276 if (!ptr->has_pawns) {
277 struct DTZEntry_piece *entry = (struct DTZEntry_piece *)ptr;
278 if ((entry->flags & 1) != bside && !entry->symmetric) {
282 ubyte *pc = entry->pieces;
283 for (i = 0; i < entry->num;) {
284 Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
285 (PieceType)(pc[i] & 0x07));
287 p[i++] = pop_lsb(&bb);
290 idx = encode_piece((struct TBEntry_piece *)entry, entry->norm, p, entry->factor);
291 res = decompress_pairs(entry->precomp, idx);
293 if (entry->flags & 2)
294 res = entry->map[entry->map_idx[wdl_to_map[wdl + 2]] + res];
296 if (!(entry->flags & pa_flags[wdl + 2]) || (wdl & 1))
299 struct DTZEntry_pawn *entry = (struct DTZEntry_pawn *)ptr;
300 int k = entry->file[0].pieces[0] ^ cmirror;
301 Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
304 p[i++] = pop_lsb(&bb) ^ mirror;
306 int f = pawn_file((struct TBEntry_pawn *)entry, p);
307 if ((entry->flags[f] & 1) != bside) {
311 ubyte *pc = entry->file[f].pieces;
312 for (; i < entry->num;) {
313 bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
314 (PieceType)(pc[i] & 0x07));
316 p[i++] = pop_lsb(&bb) ^ mirror;
319 idx = encode_pawn((struct TBEntry_pawn *)entry, entry->file[f].norm, p, entry->file[f].factor);
320 res = decompress_pairs(entry->file[f].precomp, idx);
322 if (entry->flags[f] & 2)
323 res = entry->map[entry->map_idx[f][wdl_to_map[wdl + 2]] + res];
325 if (!(entry->flags[f] & pa_flags[wdl + 2]) || (wdl & 1))
332 // Add underpromotion captures to list of captures.
333 static ExtMove *add_underprom_caps(Position& pos, ExtMove *stack, ExtMove *end)
335 ExtMove *moves, *extra = end;
337 for (moves = stack; moves < end; moves++) {
338 Move move = moves->move;
339 if (type_of(move) == PROMOTION && !pos.empty(to_sq(move))) {
340 (*extra++).move = (Move)(move - (1 << 12));
341 (*extra++).move = (Move)(move - (2 << 12));
342 (*extra++).move = (Move)(move - (3 << 12));
349 static int probe_ab(Position& pos, int alpha, int beta, int *success)
353 ExtMove *moves, *end;
356 // Generate (at least) all legal non-ep captures including (under)promotions.
357 // It is OK to generate more, as long as they are filtered out below.
358 if (!pos.checkers()) {
359 end = generate<CAPTURES>(pos, stack);
360 // Since underpromotion captures are not included, we need to add them.
361 end = add_underprom_caps(pos, stack, end);
363 end = generate<EVASIONS>(pos, stack);
367 for (moves = stack; moves < end; moves++) {
368 Move capture = moves->move;
369 if (!pos.capture(capture) || type_of(capture) == ENPASSANT
370 || !pos.legal(capture, ci.pinned))
372 pos.do_move(capture, st, pos.gives_check(capture, ci));
373 v = -probe_ab(pos, -beta, -alpha, success);
374 pos.undo_move(capture);
375 if (*success == 0) return 0;
385 v = probe_wdl_table(pos, success);
386 if (*success == 0) return 0;
388 *success = 1 + (alpha > 0);
396 // Probe the WDL table for a particular position.
397 // If *success != 0, the probe was successful.
398 // The return value is from the point of view of the side to move:
400 // -1 : loss, but draw under 50-move rule
402 // 1 : win, but draw under 50-move rule
404 int Tablebases::probe_wdl(Position& pos, int *success)
409 v = probe_ab(pos, -2, 2, success);
411 // If en passant is not possible, we are done.
412 if (pos.ep_square() == SQ_NONE)
414 if (!(*success)) return 0;
416 // Now handle en passant.
418 // Generate (at least) all legal en passant captures.
420 ExtMove *moves, *end;
424 end = generate<CAPTURES>(pos, stack);
426 end = generate<EVASIONS>(pos, stack);
430 for (moves = stack; moves < end; moves++) {
431 Move capture = moves->move;
432 if (type_of(capture) != ENPASSANT
433 || !pos.legal(capture, ci.pinned))
435 pos.do_move(capture, st, pos.gives_check(capture, ci));
436 int v0 = -probe_ab(pos, -2, 2, success);
437 pos.undo_move(capture);
438 if (*success == 0) return 0;
439 if (v0 > v1) v1 = v0;
444 // Check whether there is at least one legal non-ep move.
445 for (moves = stack; moves < end; moves++) {
446 Move capture = moves->move;
447 if (type_of(capture) == ENPASSANT) continue;
448 if (pos.legal(capture, ci.pinned)) break;
450 if (moves == end && !pos.checkers()) {
451 end = generate<QUIETS>(pos, end);
452 for (; moves < end; moves++) {
453 Move move = moves->move;
454 if (pos.legal(move, ci.pinned))
458 // If not, then we are forced to play the losing ep capture.
467 // This routine treats a position with en passant captures as one without.
468 static int probe_dtz_no_ep(Position& pos, int *success)
472 wdl = probe_ab(pos, -2, 2, success);
473 if (*success == 0) return 0;
475 if (wdl == 0) return 0;
478 return wdl == 2 ? 1 : 101;
481 ExtMove *moves, *end = NULL;
486 // Generate at least all legal non-capturing pawn moves
487 // including non-capturing promotions.
489 end = generate<NON_EVASIONS>(pos, stack);
491 end = generate<EVASIONS>(pos, stack);
493 for (moves = stack; moves < end; moves++) {
494 Move move = moves->move;
495 if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move)
496 || !pos.legal(move, ci.pinned))
498 pos.do_move(move, st, pos.gives_check(move, ci));
499 int v = -probe_ab(pos, -2, -wdl + 1, success);
501 if (*success == 0) return 0;
503 return v == 2 ? 1 : 101;
507 dtz = 1 + probe_dtz_table(pos, wdl, success);
509 if (wdl & 1) dtz += 100;
510 return wdl >= 0 ? dtz : -dtz;
515 for (moves = stack; moves < end; moves++) {
516 Move move = moves->move;
517 if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN
518 || !pos.legal(move, ci.pinned))
520 pos.do_move(move, st, pos.gives_check(move, ci));
521 int v = -Tablebases::probe_dtz(pos, success);
523 if (*success == 0) return 0;
524 if (v > 0 && v + 1 < best)
531 end = generate<NON_EVASIONS>(pos, stack);
533 end = generate<EVASIONS>(pos, stack);
534 for (moves = stack; moves < end; moves++) {
536 Move move = moves->move;
537 if (!pos.legal(move, ci.pinned))
539 pos.do_move(move, st, pos.gives_check(move, ci));
540 if (st.rule50 == 0) {
541 if (wdl == -2) v = -1;
543 v = probe_ab(pos, 1, 2, success);
544 v = (v == 2) ? 0 : -101;
547 v = -Tablebases::probe_dtz(pos, success) - 1;
550 if (*success == 0) return 0;
558 static int wdl_to_dtz[] = {
562 // Probe the DTZ table for a particular position.
563 // If *success != 0, the probe was successful.
564 // The return value is from the point of view of the side to move:
565 // n < -100 : loss, but draw under 50-move rule
566 // -100 <= n < -1 : loss in n ply (assuming 50-move counter == 0)
568 // 1 < n <= 100 : win in n ply (assuming 50-move counter == 0)
569 // 100 < n : win, but draw under 50-move rule
571 // The return value n can be off by 1: a return value -n can mean a loss
572 // in n+1 ply and a return value +n can mean a win in n+1 ply. This
573 // cannot happen for tables with positions exactly on the "edge" of
576 // This implies that if dtz > 0 is returned, the position is certainly
577 // a win if dtz + 50-move-counter <= 99. Care must be taken that the engine
578 // picks moves that preserve dtz + 50-move-counter <= 99.
580 // If n = 100 immediately after a capture or pawn move, then the position
581 // is also certainly a win, and during the whole phase until the next
582 // capture or pawn move, the inequality to be preserved is
583 // dtz + 50-movecounter <= 100.
585 // In short, if a move is available resulting in dtz + 50-move-counter <= 99,
586 // then do not accept moves leading to dtz + 50-move-counter == 100.
588 int Tablebases::probe_dtz(Position& pos, int *success)
591 int v = probe_dtz_no_ep(pos, success);
593 if (pos.ep_square() == SQ_NONE)
595 if (*success == 0) return 0;
597 // Now handle en passant.
601 ExtMove *moves, *end;
605 end = generate<CAPTURES>(pos, stack);
607 end = generate<EVASIONS>(pos, stack);
610 for (moves = stack; moves < end; moves++) {
611 Move capture = moves->move;
612 if (type_of(capture) != ENPASSANT
613 || !pos.legal(capture, ci.pinned))
615 pos.do_move(capture, st, pos.gives_check(capture, ci));
616 int v0 = -probe_ab(pos, -2, 2, success);
617 pos.undo_move(capture);
618 if (*success == 0) return 0;
619 if (v0 > v1) v1 = v0;
622 v1 = wdl_to_dtz[v1 + 2];
627 if (v1 >= 0 || v1 < -100)
629 } else if (v > 100) {
635 } else if (v1 >= 0) {
638 for (moves = stack; moves < end; moves++) {
639 Move move = moves->move;
640 if (type_of(move) == ENPASSANT) continue;
641 if (pos.legal(move, ci.pinned)) break;
643 if (moves == end && !pos.checkers()) {
644 end = generate<QUIETS>(pos, end);
645 for (; moves < end; moves++) {
646 Move move = moves->move;
647 if (pos.legal(move, ci.pinned))
659 // Check whether there has been at least one repetition of positions
660 // since the last capture or pawn move.
661 static int has_repeated(StateInfo *st)
664 int i = 4, e = std::min(st->rule50, st->pliesFromNull);
667 StateInfo *stp = st->previous->previous;
669 stp = stp->previous->previous;
670 if (stp->key == st->key)
678 static Value wdl_to_Value[5] = {
679 -VALUE_MATE + MAX_PLY + 1,
683 VALUE_MATE - MAX_PLY - 1
686 // Use the DTZ tables to filter out moves that don't preserve the win or draw.
687 // If the position is lost, but DTZ is fairly high, only keep moves that
690 // A return value false indicates that not all probes were successful and that
691 // no moves were filtered out.
692 bool Tablebases::root_probe(Position& pos, Search::RootMoveVector& rootMoves, Value& score)
696 int dtz = probe_dtz(pos, &success);
697 if (!success) return false;
703 for (size_t i = 0; i < rootMoves.size(); i++) {
704 Move move = rootMoves[i].pv[0];
705 pos.do_move(move, st, pos.gives_check(move, ci));
707 if (pos.checkers() && dtz > 0) {
709 if (generate<LEGAL>(pos, s) == s)
713 if (st.rule50 != 0) {
714 v = -Tablebases::probe_dtz(pos, &success);
718 v = -Tablebases::probe_wdl(pos, &success);
719 v = wdl_to_dtz[v + 2];
723 if (!success) return false;
724 rootMoves[i].score = (Value)v;
727 // Obtain 50-move counter for the root position.
728 // In Stockfish there seems to be no clean way, so we do it like this:
729 int cnt50 = st.previous->rule50;
731 // Use 50-move counter to determine whether the root position is
732 // won, lost or drawn.
735 wdl = (dtz + cnt50 <= 100) ? 2 : 1;
737 wdl = (-dtz + cnt50 <= 100) ? -2 : -1;
739 // Determine the score to report to the user.
740 score = wdl_to_Value[wdl + 2];
741 // If the position is winning or losing, but too few moves left, adjust the
742 // score to show how close it is to winning or losing.
743 // NOTE: int(PawnValueEg) is used as scaling factor in score_to_uci().
744 if (wdl == 1 && dtz <= 100)
745 score = (Value)(((200 - dtz - cnt50) * int(PawnValueEg)) / 200);
746 else if (wdl == -1 && dtz >= -100)
747 score = -(Value)(((200 + dtz - cnt50) * int(PawnValueEg)) / 200);
749 // Now be a bit smart about filtering out moves.
751 if (dtz > 0) { // winning (or 50-move rule draw)
753 for (size_t i = 0; i < rootMoves.size(); i++) {
754 int v = rootMoves[i].score;
755 if (v > 0 && v < best)
759 // If the current phase has not seen repetitions, then try all moves
760 // that stay safely within the 50-move budget, if there are any.
761 if (!has_repeated(st.previous) && best + cnt50 <= 99)
763 for (size_t i = 0; i < rootMoves.size(); i++) {
764 int v = rootMoves[i].score;
765 if (v > 0 && v <= max)
766 rootMoves[j++] = rootMoves[i];
768 } else if (dtz < 0) { // losing (or 50-move rule draw)
770 for (size_t i = 0; i < rootMoves.size(); i++) {
771 int v = rootMoves[i].score;
775 // Try all moves, unless we approach or have a 50-move rule draw.
776 if (-best * 2 + cnt50 < 100)
778 for (size_t i = 0; i < rootMoves.size(); i++) {
779 if (rootMoves[i].score == best)
780 rootMoves[j++] = rootMoves[i];
783 // Try all moves that preserve the draw.
784 for (size_t i = 0; i < rootMoves.size(); i++) {
785 if (rootMoves[i].score == 0)
786 rootMoves[j++] = rootMoves[i];
789 rootMoves.resize(j, Search::RootMove(MOVE_NONE));
794 // Use the WDL tables to filter out moves that don't preserve the win or draw.
795 // This is a fallback for the case that some or all DTZ tables are missing.
797 // A return value false indicates that not all probes were successful and that
798 // no moves were filtered out.
799 bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoveVector& rootMoves, Value& score)
803 int wdl = Tablebases::probe_wdl(pos, &success);
804 if (!success) return false;
805 score = wdl_to_Value[wdl + 2];
813 for (size_t i = 0; i < rootMoves.size(); i++) {
814 Move move = rootMoves[i].pv[0];
815 pos.do_move(move, st, pos.gives_check(move, ci));
816 int v = -Tablebases::probe_wdl(pos, &success);
818 if (!success) return false;
819 rootMoves[i].score = (Value)v;
825 for (size_t i = 0; i < rootMoves.size(); i++) {
826 if (rootMoves[i].score == best)
827 rootMoves[j++] = rootMoves[i];
829 rootMoves.resize(j, Search::RootMove(MOVE_NONE));