diff --git a/DroidFish/build_copy_exe.xml b/DroidFish/build_copy_exe.xml index 58788c0..5b180d6 100644 --- a/DroidFish/build_copy_exe.xml +++ b/DroidFish/build_copy_exe.xml @@ -6,8 +6,6 @@ - - diff --git a/DroidFish/jni/Application.mk b/DroidFish/jni/Application.mk index 1d41207..2d75cf7 100644 --- a/DroidFish/jni/Application.mk +++ b/DroidFish/jni/Application.mk @@ -1,5 +1,5 @@ APP_PLATFORM := 11 -APP_ABI := all +APP_ABI := arm64-v8a armeabi-v7a armeabi mips64 x86 x86_64 APP_STL := gnustl_static APP_OPTIM := release NDK_TOOLCHAIN_VERSION := 4.9 diff --git a/DroidFish/jni/stockfish/benchmark.cpp b/DroidFish/jni/stockfish/benchmark.cpp index f2d1f06..75c7d1a 100644 --- a/DroidFish/jni/stockfish/benchmark.cpp +++ b/DroidFish/jni/stockfish/benchmark.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -23,23 +23,20 @@ #include #include -#include "misc.h" #include "position.h" -#include "search.h" -#include "thread.h" -#include "uci.h" using namespace std; namespace { const vector Defaults = { + "setoption name UCI_Chess960 value false", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10", "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11", "4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19", - "rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14", - "r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14", + "rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14 moves d4e6", + "r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14 moves g2g4", "r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15", "r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13", "r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16", @@ -52,7 +49,7 @@ const vector Defaults = { "3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26", "6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1", "3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1", - "2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1", + "2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1 moves g5g6 f3e3 g6g5 e3f3", "8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1", "7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1", "8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1", @@ -79,28 +76,35 @@ const vector Defaults = { "8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw // Mate and stalemate positions + "6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1", + "r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1", "8/8/8/8/8/6k1/6p1/6K1 w - -", - "5k2/5P2/5K2/8/8/8/8/8 b - -", - "8/8/8/8/8/4k3/4p3/4K3 w - -", - "8/8/8/8/8/5K2/8/3Q1k2 b - -", - "7k/7P/6K1/8/3B4/8/8/8 b - -" + "7k/7P/6K1/8/3B4/8/8/8 b - -", + + // Chess 960 + "setoption name UCI_Chess960 value true", + "bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w KQkq - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6", + "setoption name UCI_Chess960 value false" }; } // namespace -/// benchmark() runs a simple benchmark by letting Stockfish analyze a set -/// of positions for a given limit each. There are five parameters: the -/// transposition table size, the number of search threads that should -/// be used, the limit value spent for each position (optional, default is -/// depth 13), an optional file name where to look for positions in FEN -/// format (defaults are the positions defined above) and the type of the -/// limit value: depth (default), time in millisecs or number of nodes. +/// setup_bench() builds a list of UCI commands to be run by bench. There +/// are five parameters: TT size in MB, number of search threads that +/// should be used, the limit value spent for each position, a file name +/// where to look for positions in FEN format and the type of the limit: +/// depth, perft, nodes and movetime (in millisecs). +/// +/// bench -> search default positions up to depth 13 +/// bench 64 1 15 -> search default positions up to depth 15 (TT = 64MB) +/// bench 64 4 5000 current movetime -> search current position with 4 threads for 5 sec +/// bench 64 1 100000 default nodes -> search default positions for 100K nodes each +/// bench 16 1 5 default perft -> run a perft 5 on default positions -void benchmark(const Position& current, istream& is) { +vector setup_bench(const Position& current, istream& is) { - string token; - vector fens; - Search::LimitsType limits; + vector fens, list; + string go, token; // Assign default values to missing arguments string ttSize = (is >> token) ? token : "16"; @@ -109,21 +113,7 @@ void benchmark(const Position& current, istream& is) { string fenFile = (is >> token) ? token : "default"; string limitType = (is >> token) ? token : "depth"; - Options["Hash"] = ttSize; - Options["Threads"] = threads; - Search::clear(); - - if (limitType == "time") - limits.movetime = stoi(limit); // movetime is in millisecs - - else if (limitType == "nodes") - limits.nodes = stoi(limit); - - else if (limitType == "mate") - limits.mate = stoi(limit); - - else - limits.depth = stoi(limit); + go = "go " + limitType + " " + limit; if (fenFile == "default") fens = Defaults; @@ -139,7 +129,7 @@ void benchmark(const Position& current, istream& is) { if (!file.is_open()) { cerr << "Unable to open file " << fenFile << endl; - return; + exit(EXIT_FAILURE); } while (getline(file, fen)) @@ -149,35 +139,18 @@ void benchmark(const Position& current, istream& is) { file.close(); } - uint64_t nodes = 0; - TimePoint elapsed = now(); - Position pos; - - for (size_t i = 0; i < fens.size(); ++i) - { - StateListPtr states(new std::deque(1)); - pos.set(fens[i], Options["UCI_Chess960"], &states->back(), Threads.main()); - - cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl; - - if (limitType == "perft") - nodes += Search::perft(pos, limits.depth * ONE_PLY); + list.emplace_back("ucinewgame"); + list.emplace_back("setoption name Threads value " + threads); + list.emplace_back("setoption name Hash value " + ttSize); + for (const string& fen : fens) + if (fen.find("setoption") != string::npos) + list.emplace_back(fen); else { - limits.startTime = now(); - Threads.start_thinking(pos, states, limits); - Threads.main()->wait_for_search_finished(); - nodes += Threads.nodes_searched(); + list.emplace_back("position fen " + fen); + list.emplace_back(go); } - } - elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero' - - dbg_print(); // Just before exiting - - cerr << "\n===========================" - << "\nTotal time (ms) : " << elapsed - << "\nNodes searched : " << nodes - << "\nNodes/second : " << 1000 * nodes / elapsed << endl; + return list; } diff --git a/DroidFish/jni/stockfish/bitbase.cpp b/DroidFish/jni/stockfish/bitbase.cpp index 4170952..d206210 100644 --- a/DroidFish/jni/stockfish/bitbase.cpp +++ b/DroidFish/jni/stockfish/bitbase.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -117,7 +117,7 @@ namespace { if ( distance(ksq[WHITE], ksq[BLACK]) <= 1 || ksq[WHITE] == psq || ksq[BLACK] == psq - || (us == WHITE && (StepAttacksBB[PAWN][psq] & ksq[BLACK]))) + || (us == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK]))) result = INVALID; // Immediate win if a pawn can be promoted without getting captured @@ -125,13 +125,13 @@ namespace { && rank_of(psq) == RANK_7 && ksq[us] != psq + NORTH && ( distance(ksq[~us], psq + NORTH) > 1 - || (StepAttacksBB[KING][ksq[us]] & (psq + NORTH)))) + || (PseudoAttacks[KING][ksq[us]] & (psq + NORTH)))) result = WIN; // Immediate draw if it is a stalemate or a king captures undefended pawn else if ( us == BLACK - && ( !(StepAttacksBB[KING][ksq[us]] & ~(StepAttacksBB[KING][ksq[~us]] | StepAttacksBB[PAWN][psq])) - || (StepAttacksBB[KING][ksq[us]] & psq & ~StepAttacksBB[KING][ksq[~us]]))) + && ( !(PseudoAttacks[KING][ksq[us]] & ~(PseudoAttacks[KING][ksq[~us]] | PawnAttacks[~us][psq])) + || (PseudoAttacks[KING][ksq[us]] & psq & ~PseudoAttacks[KING][ksq[~us]]))) result = DRAW; // Position will be classified later @@ -157,7 +157,7 @@ namespace { const Result Bad = (Us == WHITE ? DRAW : WIN); Result r = INVALID; - Bitboard b = StepAttacksBB[KING][ksq[Us]]; + Bitboard b = PseudoAttacks[KING][ksq[Us]]; while (b) r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)] diff --git a/DroidFish/jni/stockfish/bitboard.cpp b/DroidFish/jni/stockfish/bitboard.cpp index e329ab5..e3c9140 100644 --- a/DroidFish/jni/stockfish/bitboard.cpp +++ b/DroidFish/jni/stockfish/bitboard.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,29 +26,22 @@ uint8_t PopCnt16[1 << 16]; int SquareDistance[SQUARE_NB][SQUARE_NB]; -Bitboard RookMasks [SQUARE_NB]; -Bitboard RookMagics [SQUARE_NB]; -Bitboard* RookAttacks[SQUARE_NB]; -unsigned RookShifts [SQUARE_NB]; - -Bitboard BishopMasks [SQUARE_NB]; -Bitboard BishopMagics [SQUARE_NB]; -Bitboard* BishopAttacks[SQUARE_NB]; -unsigned BishopShifts [SQUARE_NB]; - Bitboard SquareBB[SQUARE_NB]; Bitboard FileBB[FILE_NB]; Bitboard RankBB[RANK_NB]; Bitboard AdjacentFilesBB[FILE_NB]; -Bitboard InFrontBB[COLOR_NB][RANK_NB]; -Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB]; +Bitboard ForwardRanksBB[COLOR_NB][RANK_NB]; Bitboard BetweenBB[SQUARE_NB][SQUARE_NB]; Bitboard LineBB[SQUARE_NB][SQUARE_NB]; Bitboard DistanceRingBB[SQUARE_NB][8]; -Bitboard ForwardBB[COLOR_NB][SQUARE_NB]; +Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB]; Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB]; Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB]; Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB]; +Bitboard PawnAttacks[COLOR_NB][SQUARE_NB]; + +Magic RookMagics[SQUARE_NB]; +Magic BishopMagics[SQUARE_NB]; namespace { @@ -61,10 +54,7 @@ namespace { Bitboard RookTable[0x19000]; // To store rook attacks Bitboard BishopTable[0x1480]; // To store bishop attacks - typedef unsigned (Fn)(Square, Bitboard); - - void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[], - Bitboard masks[], unsigned shifts[], Square deltas[], Fn index); + void init_magics(Bitboard table[], Magic magics[], Square deltas[]); // bsf_index() returns the index into BSFTable[] to look up the bitscan. Uses // Matt Taylor's folding for 32 bit case, extended to 64 bit by Kim Walisch. @@ -173,14 +163,14 @@ void Bitboards::init() { AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0); for (Rank r = RANK_1; r < RANK_8; ++r) - InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]); + ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | RankBB[r]); for (Color c = WHITE; c <= BLACK; ++c) for (Square s = SQ_A1; s <= SQ_H8; ++s) { - ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)]; - PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)]; - PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s]; + ForwardFileBB [c][s] = ForwardRanksBB[c][rank_of(s)] & FileBB[file_of(s)]; + PawnAttackSpan[c][s] = ForwardRanksBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)]; + PassedPawnMask[c][s] = ForwardFileBB [c][s] | PawnAttackSpan[c][s]; } for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1) @@ -191,39 +181,43 @@ void Bitboards::init() { DistanceRingBB[s1][SquareDistance[s1][s2] - 1] |= s2; } - int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 }, - {}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } }; + int steps[][5] = { {}, { 7, 9 }, { 6, 10, 15, 17 }, {}, {}, {}, { 1, 7, 8, 9 } }; for (Color c = WHITE; c <= BLACK; ++c) - for (PieceType pt = PAWN; pt <= KING; ++pt) + for (PieceType pt : { PAWN, KNIGHT, KING }) for (Square s = SQ_A1; s <= SQ_H8; ++s) for (int i = 0; steps[pt][i]; ++i) { Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]); if (is_ok(to) && distance(s, to) < 3) - StepAttacksBB[make_piece(c, pt)][s] |= to; + { + if (pt == PAWN) + PawnAttacks[c][s] |= to; + else + PseudoAttacks[pt][s] |= to; + } } - Square RookDeltas[] = { NORTH, EAST, SOUTH, WEST }; + Square RookDeltas[] = { NORTH, EAST, SOUTH, WEST }; Square BishopDeltas[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST }; - init_magics(RookTable, RookAttacks, RookMagics, RookMasks, RookShifts, RookDeltas, magic_index); - init_magics(BishopTable, BishopAttacks, BishopMagics, BishopMasks, BishopShifts, BishopDeltas, magic_index); + init_magics(RookTable, RookMagics, RookDeltas); + init_magics(BishopTable, BishopMagics, BishopDeltas); for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1) { PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb(s1, 0); PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0); - for (Piece pc = W_BISHOP; pc <= W_ROOK; ++pc) + for (PieceType pt : { BISHOP, ROOK }) for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) { - if (!(PseudoAttacks[pc][s1] & s2)) + if (!(PseudoAttacks[pt][s1] & s2)) continue; - LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2; - BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]); + LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2; + BetweenBB[s1][s2] = attacks_bb(pt, s1, SquareBB[s2]) & attacks_bb(pt, s2, SquareBB[s1]); } } } @@ -255,17 +249,14 @@ namespace { // chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we // use the so called "fancy" approach. - void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[], - Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) { + void init_magics(Bitboard table[], Magic magics[], Square deltas[]) { + // Optimal PRNG seeds to pick the correct magics in the shortest time int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 }, { 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } }; Bitboard occupancy[4096], reference[4096], edges, b; - int age[4096] = {0}, current = 0, i, size; - - // attacks[s] is a pointer to the beginning of the attacks table for square 's' - attacks[SQ_A1] = table; + int epoch[4096] = {}, cnt = 0, size = 0; for (Square s = SQ_A1; s <= SQ_H8; ++s) { @@ -277,8 +268,13 @@ namespace { // all the attacks for each possible subset of the mask and so is 2 power // the number of 1s of the mask. Hence we deduce the size of the shift to // apply to the 64 or 32 bits word to get the index. - masks[s] = sliding_attack(deltas, s, 0) & ~edges; - shifts[s] = (Is64Bit ? 64 : 32) - popcount(masks[s]); + Magic& m = magics[s]; + m.mask = sliding_attack(deltas, s, 0) & ~edges; + m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask); + + // Set the offset for the attacks table of the square. We have individual + // table sizes for each square with "Fancy Magic Bitboards". + m.attacks = s == SQ_A1 ? table : magics[s - 1].attacks + size; // Use Carry-Rippler trick to enumerate all subsets of masks[s] and // store the corresponding sliding attack bitboard in reference[]. @@ -288,17 +284,12 @@ namespace { reference[size] = sliding_attack(deltas, s, b); if (HasPext) - attacks[s][pext(b, masks[s])] = reference[size]; + m.attacks[pext(b, m.mask)] = reference[size]; size++; - b = (b - masks[s]) & masks[s]; + b = (b - m.mask) & m.mask; } while (b); - // Set the offset for the table of the next square. We have individual - // table sizes for each square with "Fancy Magic Bitboards". - if (s < SQ_H8) - attacks[s + 1] = attacks[s] + size; - if (HasPext) continue; @@ -306,28 +297,30 @@ namespace { // Find a magic for square 's' picking up an (almost) random number // until we find the one that passes the verification test. - do { - do - magics[s] = rng.sparse_rand(); - while (popcount((magics[s] * masks[s]) >> 56) < 6); + for (int i = 0; i < size; ) + { + for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6; ) + m.magic = rng.sparse_rand(); // A good magic must map every possible occupancy to an index that // looks up the correct sliding attack in the attacks[s] database. // Note that we build up the database for square 's' as a side - // effect of verifying the magic. - for (++current, i = 0; i < size; ++i) + // effect of verifying the magic. Keep track of the attempt count + // and save it in epoch[], little speed-up trick to avoid resetting + // m.attacks[] after every failed attempt. + for (++cnt, i = 0; i < size; ++i) { - unsigned idx = index(s, occupancy[i]); + unsigned idx = m.index(occupancy[i]); - if (age[idx] < current) + if (epoch[idx] < cnt) { - age[idx] = current; - attacks[s][idx] = reference[i]; + epoch[idx] = cnt; + m.attacks[idx] = reference[i]; } - else if (attacks[s][idx] != reference[i]) + else if (m.attacks[idx] != reference[i]) break; } - } while (i < size); + } } } } diff --git a/DroidFish/jni/stockfish/bitboard.h b/DroidFish/jni/stockfish/bitboard.h index 715f6c4..c9f199e 100644 --- a/DroidFish/jni/stockfish/bitboard.h +++ b/DroidFish/jni/stockfish/bitboard.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -39,6 +39,7 @@ const std::string pretty(Bitboard b); } +const Bitboard AllSquares = ~Bitboard(0); const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL; const Bitboard FileABB = 0x0101010101010101ULL; @@ -65,15 +66,41 @@ extern Bitboard SquareBB[SQUARE_NB]; extern Bitboard FileBB[FILE_NB]; extern Bitboard RankBB[RANK_NB]; extern Bitboard AdjacentFilesBB[FILE_NB]; -extern Bitboard InFrontBB[COLOR_NB][RANK_NB]; -extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB]; +extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB]; extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB]; extern Bitboard LineBB[SQUARE_NB][SQUARE_NB]; extern Bitboard DistanceRingBB[SQUARE_NB][8]; -extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB]; +extern Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB]; extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB]; extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB]; extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB]; +extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB]; + + +/// Magic holds all magic bitboards relevant data for a single square +struct Magic { + Bitboard mask; + Bitboard magic; + Bitboard* attacks; + unsigned shift; + + // Compute the attack's index using the 'magic bitboards' approach + unsigned index(Bitboard occupied) const { + + if (HasPext) + return unsigned(pext(occupied, mask)); + + if (Is64Bit) + return unsigned(((occupied & mask) * magic) >> shift); + + unsigned lo = unsigned(occupied) & unsigned(mask); + unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32); + return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift; + } +}; + +extern Magic RookMagics[SQUARE_NB]; +extern Magic BishopMagics[SQUARE_NB]; /// Overloads of bitwise operators between a Bitboard and a Square for testing @@ -153,28 +180,28 @@ inline Bitboard between_bb(Square s1, Square s2) { } -/// in_front_bb() returns a bitboard representing all the squares on all the ranks +/// forward_ranks_bb() returns a bitboard representing all the squares on all the ranks /// in front of the given one, from the point of view of the given color. For -/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2. +/// instance, forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2. -inline Bitboard in_front_bb(Color c, Rank r) { - return InFrontBB[c][r]; +inline Bitboard forward_ranks_bb(Color c, Square s) { + return ForwardRanksBB[c][rank_of(s)]; } -/// forward_bb() returns a bitboard representing all the squares along the line +/// forward_file_bb() returns a bitboard representing all the squares along the line /// in front of the given one, from the point of view of the given color: -/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s) +/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s) -inline Bitboard forward_bb(Color c, Square s) { - return ForwardBB[c][s]; +inline Bitboard forward_file_bb(Color c, Square s) { + return ForwardFileBB[c][s]; } /// pawn_attack_span() returns a bitboard representing all the squares that can be /// attacked by a pawn of the given color when it moves along its file, starting /// from the given square: -/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s); +/// PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s)); inline Bitboard pawn_attack_span(Color c, Square s) { return PawnAttackSpan[c][s]; @@ -183,7 +210,7 @@ inline Bitboard pawn_attack_span(Color c, Square s) { /// passed_pawn_mask() returns a bitboard mask which can be used to test if a /// pawn of the given color and on the given square is a passed pawn: -/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s) +/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s) inline Bitboard passed_pawn_mask(Color c, Square s) { return PassedPawnMask[c][s]; @@ -210,50 +237,25 @@ template<> inline int distance(Square x, Square y) { return distance(rank_ /// attacks_bb() returns a bitboard representing all the squares attacked by a -/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index() -/// looks up the index using the 'magic bitboards' approach. -template -inline unsigned magic_index(Square s, Bitboard occupied) { - - extern Bitboard RookMasks[SQUARE_NB]; - extern Bitboard RookMagics[SQUARE_NB]; - extern unsigned RookShifts[SQUARE_NB]; - extern Bitboard BishopMasks[SQUARE_NB]; - extern Bitboard BishopMagics[SQUARE_NB]; - extern unsigned BishopShifts[SQUARE_NB]; - - Bitboard* const Masks = Pt == ROOK ? RookMasks : BishopMasks; - Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics; - unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts; - - if (HasPext) - return unsigned(pext(occupied, Masks[s])); - - if (Is64Bit) - return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]); - - unsigned lo = unsigned(occupied) & unsigned(Masks[s]); - unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32); - return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s]; -} +/// piece of type Pt (bishop or rook) placed on 's'. template inline Bitboard attacks_bb(Square s, Bitboard occupied) { - extern Bitboard* RookAttacks[SQUARE_NB]; - extern Bitboard* BishopAttacks[SQUARE_NB]; - - return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index(s, occupied)]; + const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s]; + return m.attacks[m.index(occupied)]; } -inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) { +inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) { - switch (type_of(pc)) + assert(pt != PAWN); + + switch (pt) { case BISHOP: return attacks_bb(s, occupied); - case ROOK : return attacks_bb(s, occupied); + case ROOK : return attacks_bb< ROOK>(s, occupied); case QUEEN : return attacks_bb(s, occupied) | attacks_bb(s, occupied); - default : return StepAttacksBB[pc][s]; + default : return PseudoAttacks[pt][s]; } } @@ -291,7 +293,7 @@ inline Square lsb(Bitboard b) { inline Square msb(Bitboard b) { assert(b); - return Square(63 - __builtin_clzll(b)); + return Square(63 ^ __builtin_clzll(b)); } #elif defined(_WIN64) && defined(_MSC_VER) diff --git a/DroidFish/jni/stockfish/bitcount.h b/DroidFish/jni/stockfish/bitcount.h deleted file mode 100644 index 7609da4..0000000 --- a/DroidFish/jni/stockfish/bitcount.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2008 Tord Romstad (Glaurung author) - Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad - - Stockfish is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Stockfish is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef BITCOUNT_H_INCLUDED -#define BITCOUNT_H_INCLUDED - -#include - -#include "types.h" - -enum BitCountType { - CNT_64, - CNT_64_MAX15, - CNT_32, - CNT_32_MAX15, - CNT_HW_POPCNT -}; - -/// Determine at compile time the best popcount<> specialization according to -/// whether the platform is 32 or 64 bit, the maximum number of non-zero -/// bits to count and if the hardware popcnt instruction is available. -const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32; -const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15; - - -/// popcount() counts the number of non-zero bits in a bitboard -template inline int popcount(Bitboard); - -template<> -inline int popcount(Bitboard b) { - b -= (b >> 1) & 0x5555555555555555ULL; - b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); - b = ((b >> 4) + b) & 0x0F0F0F0F0F0F0F0FULL; - return (b * 0x0101010101010101ULL) >> 56; -} - -template<> -inline int popcount(Bitboard b) { - b -= (b >> 1) & 0x5555555555555555ULL; - b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); - return (b * 0x1111111111111111ULL) >> 60; -} - -template<> -inline int popcount(Bitboard b) { - unsigned w = unsigned(b >> 32), v = unsigned(b); - v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits - w -= (w >> 1) & 0x55555555; - v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits - w = ((w >> 2) & 0x33333333) + (w & 0x33333333); - v = ((v >> 4) + v + (w >> 4) + w) & 0x0F0F0F0F; - return (v * 0x01010101) >> 24; -} - -template<> -inline int popcount(Bitboard b) { - unsigned w = unsigned(b >> 32), v = unsigned(b); - v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits - w -= (w >> 1) & 0x55555555; - v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits - w = ((w >> 2) & 0x33333333) + (w & 0x33333333); - return ((v + w) * 0x11111111) >> 28; -} - -template<> -inline int popcount(Bitboard b) { - -#ifndef USE_POPCNT - - assert(false); - return b != 0; // Avoid 'b not used' warning - -#elif defined(_MSC_VER) && defined(__INTEL_COMPILER) - - return _mm_popcnt_u64(b); - -#elif defined(_MSC_VER) - - return (int)__popcnt64(b); - -#else // Assumed gcc or compatible compiler - - return __builtin_popcountll(b); - -#endif -} - -#endif // #ifndef BITCOUNT_H_INCLUDED diff --git a/DroidFish/jni/stockfish/endgame.cpp b/DroidFish/jni/stockfish/endgame.cpp index cbca34b..ca17f6a 100644 --- a/DroidFish/jni/stockfish/endgame.cpp +++ b/DroidFish/jni/stockfish/endgame.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -83,26 +83,6 @@ namespace { return sq; } - // Get the material key of Position out of the given endgame key code - // like "KBPKN". The trick here is to first forge an ad-hoc FEN string - // and then let a Position object do the work for us. - Key key(const string& code, Color c) { - - assert(code.length() > 0 && code.length() < 8); - assert(code[0] == 'K'); - - string sides[] = { code.substr(code.find('K', 1)), // Weak - code.substr(0, code.find('K', 1)) }; // Strong - - std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower); - - string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/" - + sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10"; - - StateInfo st; - return Position().set(fen, false, &st, nullptr).material_key(); - } - } // namespace @@ -130,13 +110,6 @@ Endgames::Endgames() { } -template -void Endgames::add(const string& code) { - map()[key(code, WHITE)] = std::unique_ptr>(new Endgame(WHITE)); - map()[key(code, BLACK)] = std::unique_ptr>(new Endgame(BLACK)); -} - - /// Mate with KX vs K. This function is used to evaluate positions with /// king and plenty of material vs a lone king. It simply gives the /// attacking side a bonus for driving the defending king towards the edge @@ -162,8 +135,8 @@ Value Endgame::operator()(const Position& pos) const { if ( pos.count(strongSide) || pos.count(strongSide) ||(pos.count(strongSide) && pos.count(strongSide)) - ||(pos.count(strongSide) > 1 && opposite_colors(pos.squares(strongSide)[0], - pos.squares(strongSide)[1]))) + || ( (pos.pieces(strongSide, BISHOP) & ~DarkSquares) + && (pos.pieces(strongSide, BISHOP) & DarkSquares))) result = std::min(result + VALUE_KNOWN_WIN, VALUE_MATE_IN_MAX_PLY - 1); return strongSide == pos.side_to_move() ? result : -result; @@ -625,7 +598,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // If all pawns are ahead of the king, on a single rook file and // the king is within one file of the pawns, it's a draw. - if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq))) + if ( !(pawns & ~forward_ranks_bb(weakSide, ksq)) && !((pawns & ~FileABB) && (pawns & ~FileHBB)) && distance(ksq, lsb(pawns)) <= 1) return SCALE_FACTOR_DRAW; @@ -671,17 +644,15 @@ ScaleFactor Endgame::operator()(const Position& pos) const { if (relative_rank(strongSide, pawnSq) <= RANK_5) return SCALE_FACTOR_DRAW; - else - { - Bitboard path = forward_bb(strongSide, pawnSq); - if (path & pos.pieces(weakSide, KING)) - return SCALE_FACTOR_DRAW; + Bitboard path = forward_file_bb(strongSide, pawnSq); - if ( (pos.attacks_from(weakBishopSq) & path) - && distance(weakBishopSq, pawnSq) >= 3) - return SCALE_FACTOR_DRAW; - } + if (path & pos.pieces(weakSide, KING)) + return SCALE_FACTOR_DRAW; + + if ( (pos.attacks_from(weakBishopSq) & path) + && distance(weakBishopSq, pawnSq) >= 3) + return SCALE_FACTOR_DRAW; } return SCALE_FACTOR_NONE; } @@ -809,7 +780,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // King needs to get close to promoting pawn to prevent knight from blocking. // Rules for this are very tricky, so just approximate. - if (forward_bb(strongSide, pawnSq) & pos.attacks_from(bishopSq)) + if (forward_file_bb(strongSide, pawnSq) & pos.attacks_from(bishopSq)) return ScaleFactor(distance(weakKingSq, pawnSq)); return SCALE_FACTOR_NONE; diff --git a/DroidFish/jni/stockfish/endgame.h b/DroidFish/jni/stockfish/endgame.h index 5f6b4bb..3d61207 100644 --- a/DroidFish/jni/stockfish/endgame.h +++ b/DroidFish/jni/stockfish/endgame.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,12 +31,11 @@ #include "types.h" -/// EndgameType lists all supported endgames +/// EndgameCode lists all supported endgame functions by corresponding codes -enum EndgameType { - - // Evaluation functions +enum EndgameCode { + EVALUATION_FUNCTIONS, KNNK, // KNN vs K KXK, // Generic "mate lone king" eval KBNK, // KBN vs K @@ -47,10 +46,7 @@ enum EndgameType { KQKP, // KQ vs KP KQKR, // KQ vs KR - - // Scaling functions SCALING_FUNCTIONS, - KBPsK, // KB and pawns vs K KQKRPs, // KQ vs KR and pawns KRPKR, // KRP vs KR @@ -68,30 +64,28 @@ enum EndgameType { /// Endgame functions can be of two types depending on whether they return a /// Value or a ScaleFactor. -template using +template using eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; -/// Base and derived templates for endgame evaluation and scaling functions +/// Base and derived functors for endgame evaluation and scaling functions template struct EndgameBase { + explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {} virtual ~EndgameBase() = default; - virtual Color strong_side() const = 0; virtual T operator()(const Position&) const = 0; + + const Color strongSide, weakSide; }; -template> +template> struct Endgame : public EndgameBase { - explicit Endgame(Color c) : strongSide(c), weakSide(~c) {} - Color strong_side() const { return strongSide; } - T operator()(const Position&) const; - -private: - Color strongSide, weakSide; + explicit Endgame(Color c) : EndgameBase(c) {} + T operator()(const Position&) const override; }; @@ -101,16 +95,22 @@ private: class Endgames { - template using Map = std::map>>; - - template> - void add(const std::string& code); + template using Ptr = std::unique_ptr>; + template using Map = std::map>; template Map& map() { return std::get::value>(maps); } + template, typename P = Ptr> + void add(const std::string& code) { + + StateInfo st; + map()[Position().set(code, WHITE, &st).material_key()] = P(new Endgame(WHITE)); + map()[Position().set(code, BLACK, &st).material_key()] = P(new Endgame(BLACK)); + } + std::pair, Map> maps; public: diff --git a/DroidFish/jni/stockfish/evaluate.cpp b/DroidFish/jni/stockfish/evaluate.cpp index 434ebd6..7ee3652 100644 --- a/DroidFish/jni/stockfish/evaluate.cpp +++ b/DroidFish/jni/stockfish/evaluate.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -33,6 +33,8 @@ namespace { namespace Trace { + enum Tracing {NO_TRACE, TRACE}; + enum Term { // The first 8 entries are for PieceType MATERIAL = 8, IMBALANCE, MOBILITY, THREAT, PASSED, SPACE, TOTAL, TERM_NB }; @@ -69,9 +71,35 @@ namespace { using namespace Trace; - // Struct EvalInfo contains various information computed and collected + // Evaluation class contains various information computed and collected // by the evaluation functions. - struct EvalInfo { + template + class Evaluation { + + public: + Evaluation() = delete; + Evaluation(const Position& p) : pos(p) {} + Evaluation& operator=(const Evaluation&) = delete; + + Value value(); + + private: + // Evaluation helpers (used when calling value()) + template void initialize(); + template Score evaluate_king(); + template Score evaluate_threats(); + template Score evaluate_passed_pawns(); + template Score evaluate_space(); + template Score evaluate_pieces(); + ScaleFactor evaluate_scale_factor(Value eg); + Score evaluate_initiative(Value eg); + + // Data members + const Position& pos; + Material::Entry* me; + Pawns::Entry* pe; + Bitboard mobilityArea[COLOR_NB]; + Score mobility[COLOR_NB] = { SCORE_ZERO, SCORE_ZERO }; // attackedBy[color][piece type] is a bitboard representing all squares // attacked by a given color and piece type (can be also ALL_PIECES). @@ -84,7 +112,7 @@ namespace { // kingRing[color] is the zone around the king which is considered // by the king safety evaluation. This consists of the squares directly - // adjacent to the king, and the three (or two, for a king on an edge file) + // adjacent to the king, and (only for a king on its first rank) the // squares two ranks in front of the king. For instance, if black's king // is on g8, kingRing[BLACK] is a bitboard containing the squares f8, h8, // f7, g7, h7, f6, g6 and h6. @@ -106,70 +134,56 @@ namespace { // a white knight on g5 and black's king is on g8, this white knight adds 2 // to kingAdjacentZoneAttacksCount[WHITE]. int kingAdjacentZoneAttacksCount[COLOR_NB]; - - Bitboard pinnedPieces[COLOR_NB]; - Material::Entry* me; - Pawns::Entry* pi; }; #define V(v) Value(v) #define S(mg, eg) make_score(mg, eg) - // MobilityBonus[PieceType][attacked] contains bonuses for middle and end - // game, indexed by piece type and number of attacked squares in the MobilityArea. + // MobilityBonus[PieceType-2][attacked] contains bonuses for middle and end game, + // indexed by piece type and number of attacked squares in the mobility area. const Score MobilityBonus[][32] = { - {}, {}, - { S(-75,-76), S(-56,-54), S( -9,-26), S( -2,-10), S( 6, 5), S( 15, 11), // Knights - S( 22, 26), S( 30, 28), S( 36, 29) }, - { S(-48,-58), S(-21,-19), S( 16, -2), S( 26, 12), S( 37, 22), S( 51, 42), // Bishops - S( 54, 54), S( 63, 58), S( 65, 63), S( 71, 70), S( 79, 74), S( 81, 86), - S( 92, 90), S( 97, 94) }, - { S(-56,-78), S(-25,-18), S(-11, 26), S( -5, 55), S( -4, 70), S( -1, 81), // Rooks - S( 8,109), S( 14,120), S( 21,128), S( 23,143), S( 31,154), S( 32,160), - S( 43,165), S( 49,168), S( 59,169) }, - { S(-40,-35), S(-25,-12), S( 2, 7), S( 4, 19), S( 14, 37), S( 24, 55), // Queens - S( 25, 62), S( 40, 76), S( 43, 79), S( 47, 87), S( 54, 94), S( 56,102), - S( 60,111), S( 70,116), S( 72,118), S( 73,122), S( 75,128), S( 77,130), - S( 85,133), S( 94,136), S( 99,140), S(108,157), S(112,158), S(113,161), - S(118,174), S(119,177), S(123,191), S(128,199) } + { S(-75,-76), S(-57,-54), S( -9,-28), S( -2,-10), S( 6, 5), S( 14, 12), // Knights + S( 22, 26), S( 29, 29), S( 36, 29) }, + { S(-48,-59), S(-20,-23), S( 16, -3), S( 26, 13), S( 38, 24), S( 51, 42), // Bishops + S( 55, 54), S( 63, 57), S( 63, 65), S( 68, 73), S( 81, 78), S( 81, 86), + S( 91, 88), S( 98, 97) }, + { S(-58,-76), S(-27,-18), S(-15, 28), S(-10, 55), S( -5, 69), S( -2, 82), // Rooks + S( 9,112), S( 16,118), S( 30,132), S( 29,142), S( 32,155), S( 38,165), + S( 46,166), S( 48,169), S( 58,171) }, + { S(-39,-36), S(-21,-15), S( 3, 8), S( 3, 18), S( 14, 34), S( 22, 54), // Queens + S( 28, 61), S( 41, 73), S( 43, 79), S( 48, 92), S( 56, 94), S( 60,104), + S( 60,113), S( 66,120), S( 67,123), S( 70,126), S( 71,133), S( 73,136), + S( 79,140), S( 88,143), S( 88,148), S( 99,166), S(102,170), S(102,175), + S(106,184), S(109,191), S(113,206), S(116,212) } }; - // Outpost[knight/bishop][supported by pawn] contains bonuses for knights and - // bishops outposts, bigger if outpost piece is supported by a pawn. + // Outpost[knight/bishop][supported by pawn] contains bonuses for minor + // pieces if they can reach an outpost square, bigger if that square is + // supported by a pawn. If the minor piece occupies an outpost square + // then score is doubled. const Score Outpost[][2] = { - { S(43,11), S(65,20) }, // Knights - { S(20, 3), S(29, 8) } // Bishops - }; - - // ReachableOutpost[knight/bishop][supported by pawn] contains bonuses for - // knights and bishops which can reach an outpost square in one move, bigger - // if outpost square is supported by a pawn. - const Score ReachableOutpost[][2] = { - { S(21, 5), S(35, 8) }, // Knights - { S( 8, 0), S(14, 4) } // Bishops + { S(22, 6), S(33, 9) }, // Knight + { S( 9, 2), S(14, 4) } // Bishop }; // RookOnFile[semiopen/open] contains bonuses for each rook when there is no // friendly pawn on the rook file. - const Score RookOnFile[2] = { S(20, 7), S(45, 20) }; + const Score RookOnFile[] = { S(20, 7), S(45, 20) }; - // ThreatBySafePawn[PieceType] contains bonuses according to which piece - // type is attacked by a pawn which is protected or is not attacked. - const Score ThreatBySafePawn[PIECE_TYPE_NB] = { - S(0, 0), S(0, 0), S(176, 139), S(131, 127), S(217, 218), S(203, 215) + // ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to + // which piece type attacks which one. Attacks on lesser pieces which are + // pawn-defended are not considered. + const Score ThreatByMinor[PIECE_TYPE_NB] = { + S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72, 107), S(48, 118) }; - // Threat[by minor/by rook][attacked PieceType] contains - // bonuses according to which piece type attacks which one. - // Attacks on lesser pieces which are pawn-defended are not considered. - const Score Threat[][PIECE_TYPE_NB] = { - { S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72,107), S(48,118) }, // by Minor - { S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48) } // by Rook + const Score ThreatByRook[PIECE_TYPE_NB] = { + S(0, 0), S(0, 25), S(40, 62), S(40, 59), S(0, 34), S(35, 48) }; - // ThreatByKing[on one/on many] contains bonuses for King attacks on + // ThreatByKing[on one/on many] contains bonuses for king attacks on // pawns or pieces which are not pawn-defended. - const Score ThreatByKing[2] = { S(3, 62), S(9, 138) }; + const Score ThreatByKing[] = { S(3, 62), S(9, 138) }; // Passed[mg/eg][Rank] contains midgame and endgame bonuses for passed pawns. // We don't use a Score because we process the two components independently. @@ -181,30 +195,28 @@ namespace { // PassedFile[File] contains a bonus according to the file of a passed pawn const Score PassedFile[FILE_NB] = { S( 9, 10), S( 2, 10), S( 1, -8), S(-20,-12), - S(-20,-12), S( 1, -8), S( 2, 10), S( 9, 10) + S(-20,-12), S( 1, -8), S( 2, 10), S( 9, 10) }; - // Assorted bonuses and penalties used by evaluation - const Score MinorBehindPawn = S(16, 0); - const Score BishopPawns = S( 8, 12); - const Score RookOnPawn = S( 8, 24); - const Score TrappedRook = S(92, 0); - const Score CloseEnemies = S( 7, 0); - const Score SafeCheck = S(20, 20); - const Score OtherCheck = S(10, 10); - const Score ThreatByHangingPawn = S(71, 61); - const Score LooseEnemies = S( 0, 25); - const Score WeakQueen = S(35, 0); - const Score Hanging = S(48, 27); - const Score ThreatByPawnPush = S(38, 22); - const Score Unstoppable = S( 0, 20); - const Score PawnlessFlank = S(20, 80); - const Score HinderPassedPawn = S( 7, 0); + // KingProtector[PieceType-2] contains a bonus according to distance from king + const Score KingProtector[] = { S(-3, -5), S(-4, -3), S(-3, 0), S(-1, 1) }; - // Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by - // a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only - // happen in Chess960 games. - const Score TrappedBishopA1H1 = S(50, 50); + // Assorted bonuses and penalties used by evaluation + const Score MinorBehindPawn = S( 16, 0); + const Score BishopPawns = S( 8, 12); + const Score RookOnPawn = S( 8, 24); + const Score TrappedRook = S( 92, 0); + const Score WeakQueen = S( 50, 10); + const Score OtherCheck = S( 10, 10); + const Score CloseEnemies = S( 7, 0); + const Score PawnlessFlank = S( 20, 80); + const Score ThreatByHangingPawn = S( 71, 61); + const Score ThreatBySafePawn = S(182,175); + const Score ThreatByRank = S( 16, 3); + const Score Hanging = S( 48, 27); + const Score ThreatByPawnPush = S( 38, 22); + const Score HinderPassedPawn = S( 7, 0); + const Score TrappedBishopA1H1 = S( 50, 50); #undef S #undef V @@ -213,58 +225,72 @@ namespace { const int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 78, 56, 45, 11 }; // Penalties for enemy's safe checks - const int QueenContactCheck = 997; - const int QueenCheck = 695; - const int RookCheck = 638; - const int BishopCheck = 538; - const int KnightCheck = 874; + const int QueenCheck = 780; + const int RookCheck = 880; + const int BishopCheck = 435; + const int KnightCheck = 790; + + // Threshold for lazy and space evaluation + const Value LazyThreshold = Value(1500); + const Value SpaceThreshold = Value(12222); - // eval_init() initializes king and attack bitboards for a given color - // adding pawn attacks. To be done at the beginning of the evaluation. + // initialize() computes king and pawn attacks, and the king ring bitboard + // for a given color. This is done at the beginning of the evaluation. - template - void eval_init(const Position& pos, EvalInfo& ei) { + template template + void Evaluation::initialize() { const Color Them = (Us == WHITE ? BLACK : WHITE); + const Square Up = (Us == WHITE ? NORTH : SOUTH); const Square Down = (Us == WHITE ? SOUTH : NORTH); + const Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB: Rank7BB | Rank6BB); - ei.pinnedPieces[Us] = pos.pinned_pieces(Us); - Bitboard b = ei.attackedBy[Them][KING]; - ei.attackedBy[Them][ALL_PIECES] |= b; - ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us); - ei.attackedBy2[Us] = ei.attackedBy[Us][PAWN] & ei.attackedBy[Us][KING]; + // Find our pawns on the first two ranks, and those which are blocked + Bitboard b = pos.pieces(Us, PAWN) & (shift(pos.pieces()) | LowRanks); - // Init king safety tables only if we are going to use them - if (pos.non_pawn_material(Us) >= QueenValueMg) + // Squares occupied by those pawns, by our king, or controlled by enemy pawns + // are excluded from the mobility area. + mobilityArea[Us] = ~(b | pos.square(Us) | pe->pawn_attacks(Them)); + + // Initialise the attack bitboards with the king and pawn information + b = attackedBy[Us][KING] = pos.attacks_from(pos.square(Us)); + attackedBy[Us][PAWN] = pe->pawn_attacks(Us); + + attackedBy2[Us] = b & attackedBy[Us][PAWN]; + attackedBy[Us][ALL_PIECES] = b | attackedBy[Us][PAWN]; + + // Init our king safety tables only if we are going to use them + if (pos.non_pawn_material(Them) >= RookValueMg + KnightValueMg) { - ei.kingRing[Them] = b | shift(b); - b &= ei.attackedBy[Us][PAWN]; - ei.kingAttackersCount[Us] = popcount(b); - ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0; + kingRing[Us] = b; + if (relative_rank(Us, pos.square(Us)) == RANK_1) + kingRing[Us] |= shift(b); + + kingAttackersCount[Them] = popcount(b & pe->pawn_attacks(Them)); + kingAdjacentZoneAttacksCount[Them] = kingAttackersWeight[Them] = 0; } else - ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0; + kingRing[Us] = kingAttackersCount[Them] = 0; } // evaluate_pieces() assigns bonuses and penalties to the pieces of a given // color and type. - template - Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score* mobility, - const Bitboard* mobilityArea) { - Bitboard b, bb; - Square s; - Score score = SCORE_ZERO; + template template + Score Evaluation::evaluate_pieces() { - const PieceType NextPt = (Us == WHITE ? Pt : PieceType(Pt + 1)); const Color Them = (Us == WHITE ? BLACK : WHITE); const Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB : Rank5BB | Rank4BB | Rank3BB); const Square* pl = pos.squares(Us); - ei.attackedBy[Us][Pt] = 0; + Bitboard b, bb; + Square s; + Score score = SCORE_ZERO; + + attackedBy[Us][Pt] = 0; while ((s = *pl++) != SQ_NONE) { @@ -273,39 +299,37 @@ namespace { : Pt == ROOK ? attacks_bb< ROOK>(s, pos.pieces() ^ pos.pieces(Us, ROOK, QUEEN)) : pos.attacks_from(s); - if (ei.pinnedPieces[Us] & s) + if (pos.pinned_pieces(Us) & s) b &= LineBB[pos.square(Us)][s]; - ei.attackedBy2[Us] |= ei.attackedBy[Us][ALL_PIECES] & b; - ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][Pt] |= b; + attackedBy2[Us] |= attackedBy[Us][ALL_PIECES] & b; + attackedBy[Us][ALL_PIECES] |= attackedBy[Us][Pt] |= b; - if (b & ei.kingRing[Them]) + if (b & kingRing[Them]) { - ei.kingAttackersCount[Us]++; - ei.kingAttackersWeight[Us] += KingAttackWeights[Pt]; - ei.kingAdjacentZoneAttacksCount[Us] += popcount(b & ei.attackedBy[Them][KING]); + kingAttackersCount[Us]++; + kingAttackersWeight[Us] += KingAttackWeights[Pt]; + kingAdjacentZoneAttacksCount[Us] += popcount(b & attackedBy[Them][KING]); } - if (Pt == QUEEN) - b &= ~( ei.attackedBy[Them][KNIGHT] - | ei.attackedBy[Them][BISHOP] - | ei.attackedBy[Them][ROOK]); - int mob = popcount(b & mobilityArea[Us]); - mobility[Us] += MobilityBonus[Pt][mob]; + mobility[Us] += MobilityBonus[Pt - 2][mob]; + + // Bonus for this piece as a king protector + score += KingProtector[Pt - 2] * distance(s, pos.square(Us)); if (Pt == BISHOP || Pt == KNIGHT) { // Bonus for outpost squares - bb = OutpostRanks & ~ei.pi->pawn_attacks_span(Them); + bb = OutpostRanks & ~pe->pawn_attacks_span(Them); if (bb & s) - score += Outpost[Pt == BISHOP][!!(ei.attackedBy[Us][PAWN] & s)]; + score += Outpost[Pt == BISHOP][!!(attackedBy[Us][PAWN] & s)] * 2; else { bb &= b & ~pos.pieces(Us); if (bb) - score += ReachableOutpost[Pt == BISHOP][!!(ei.attackedBy[Us][PAWN] & bb)]; + score += Outpost[Pt == BISHOP][!!(attackedBy[Us][PAWN] & bb)]; } // Bonus when behind a pawn @@ -315,7 +339,7 @@ namespace { // Penalty for pawns on the same color square as the bishop if (Pt == BISHOP) - score -= BishopPawns * ei.pi->pawns_on_same_color_squares(Us, s); + score -= BishopPawns * pe->pawns_on_same_color_squares(Us, s); // An important Chess960 pattern: A cornered bishop blocked by a friendly // pawn diagonally in front of it is a very serious problem, especially @@ -339,17 +363,16 @@ namespace { score += RookOnPawn * popcount(pos.pieces(Them, PAWN) & PseudoAttacks[ROOK][s]); // Bonus when on an open or semi-open file - if (ei.pi->semiopen_file(Us, file_of(s))) - score += RookOnFile[!!ei.pi->semiopen_file(Them, file_of(s))]; + if (pe->semiopen_file(Us, file_of(s))) + score += RookOnFile[!!pe->semiopen_file(Them, file_of(s))]; - // Penalize when trapped by the king, even more if the king cannot castle + // Penalty when trapped by the king, even more if the king cannot castle else if (mob <= 3) { Square ksq = pos.square(Us); if ( ((file_of(ksq) < FILE_E) == (file_of(s) < file_of(ksq))) - && (rank_of(ksq) == rank_of(s) || relative_rank(Us, ksq) == RANK_1) - && !ei.pi->semiopen_side(Us, file_of(ksq), file_of(s) < file_of(ksq))) + && !pe->semiopen_side(Us, file_of(ksq), file_of(s) < file_of(ksq))) score -= (TrappedRook - make_score(mob * 22, 0)) * (1 + !pos.can_castle(Us)); } } @@ -363,129 +386,118 @@ namespace { } } - if (DoTrace) + if (T) Trace::add(Pt, Us, score); - // Recursively call evaluate_pieces() of next piece type until KING is excluded - return score - evaluate_pieces(pos, ei, mobility, mobilityArea); + return score; } - template<> - Score evaluate_pieces(const Position&, EvalInfo&, Score*, const Bitboard*) { return SCORE_ZERO; } - template<> - Score evaluate_pieces< true, WHITE, KING>(const Position&, EvalInfo&, Score*, const Bitboard*) { return SCORE_ZERO; } - // evaluate_king() assigns bonuses and penalties to a king of a given color - const Bitboard WhiteCamp = Rank1BB | Rank2BB | Rank3BB | Rank4BB | Rank5BB; - const Bitboard BlackCamp = Rank8BB | Rank7BB | Rank6BB | Rank5BB | Rank4BB; const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB; const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB; const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB; - const Bitboard KingFlank[COLOR_NB][FILE_NB] = { - { QueenSide & WhiteCamp, QueenSide & WhiteCamp, QueenSide & WhiteCamp, CenterFiles & WhiteCamp, - CenterFiles & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp }, - { QueenSide & BlackCamp, QueenSide & BlackCamp, QueenSide & BlackCamp, CenterFiles & BlackCamp, - CenterFiles & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp }, + const Bitboard KingFlank[FILE_NB] = { + QueenSide, QueenSide, QueenSide, CenterFiles, CenterFiles, KingSide, KingSide, KingSide }; - template - Score evaluate_king(const Position& pos, const EvalInfo& ei) { + template template + Score Evaluation::evaluate_king() { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Square Up = (Us == WHITE ? NORTH : SOUTH); + const Color Them = (Us == WHITE ? BLACK : WHITE); + const Square Up = (Us == WHITE ? NORTH : SOUTH); + const Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB + : AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB); - Bitboard undefended, b, b1, b2, safe, other; - int kingDanger; const Square ksq = pos.square(Us); + Bitboard kingOnlyDefended, undefended, b, b1, b2, safe, other; + int kingDanger; // King shelter and enemy pawns storm - Score score = ei.pi->king_safety(pos, ksq); + Score score = pe->king_safety(pos, ksq); // Main king safety evaluation - if (ei.kingAttackersCount[Them]) + if (kingAttackersCount[Them] > (1 - pos.count(Them))) { - // Find the attacked squares which are defended only by the king... - undefended = ei.attackedBy[Them][ALL_PIECES] - & ei.attackedBy[Us][KING] - & ~ei.attackedBy2[Us]; + // Find the attacked squares which are defended only by our king... + kingOnlyDefended = attackedBy[Them][ALL_PIECES] + & attackedBy[Us][KING] + & ~attackedBy2[Us]; // ... and those which are not defended at all in the larger king ring - b = ei.attackedBy[Them][ALL_PIECES] & ~ei.attackedBy[Us][ALL_PIECES] - & ei.kingRing[Us] & ~pos.pieces(Them); + undefended = attackedBy[Them][ALL_PIECES] + & ~attackedBy[Us][ALL_PIECES] + & kingRing[Us] + & ~pos.pieces(Them); // Initialize the 'kingDanger' variable, which will be transformed // later into a king danger score. The initial value is based on the // number and types of the enemy's attacking pieces, the number of - // attacked and undefended squares around our king and the quality of - // the pawn shelter (current 'score' value). - kingDanger = std::min(807, ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) - + 101 * ei.kingAdjacentZoneAttacksCount[Them] - + 235 * popcount(undefended) - + 134 * (popcount(b) + !!ei.pinnedPieces[Us]) - - 717 * !pos.count(Them) - - 7 * mg_value(score) / 5 - 5; + // attacked and weak squares around our king, the absence of queen and + // the quality of the pawn shelter (current 'score' value). + kingDanger = kingAttackersCount[Them] * kingAttackersWeight[Them] + + 102 * kingAdjacentZoneAttacksCount[Them] + + 191 * popcount(kingOnlyDefended | undefended) + + 143 * !!pos.pinned_pieces(Us) + - 848 * !pos.count(Them) + - 9 * mg_value(score) / 8 + + 40; - // Analyse the enemy's safe queen contact checks. Firstly, find the - // undefended squares around the king reachable by the enemy queen... - b = undefended & ei.attackedBy[Them][QUEEN] & ~pos.pieces(Them); + // Analyse the safe enemy's checks which are possible on next move + safe = ~pos.pieces(Them); + safe &= ~attackedBy[Us][ALL_PIECES] | (kingOnlyDefended & attackedBy2[Them]); - // ...and keep squares supported by another enemy piece - kingDanger += QueenContactCheck * popcount(b & ei.attackedBy2[Them]); - - // Analyse the safe enemy's checks which are possible on next move... - safe = ~(ei.attackedBy[Us][ALL_PIECES] | pos.pieces(Them)); - - // ... and some other potential checks, only requiring the square to be - // safe from pawn-attacks, and not being occupied by a blocked pawn. - other = ~( ei.attackedBy[Us][PAWN] - | (pos.pieces(Them, PAWN) & shift(pos.pieces(PAWN)))); - - b1 = pos.attacks_from(ksq); + b1 = pos.attacks_from< ROOK>(ksq); b2 = pos.attacks_from(ksq); // Enemy queen safe checks - if ((b1 | b2) & ei.attackedBy[Them][QUEEN] & safe) - kingDanger += QueenCheck, score -= SafeCheck; + if ((b1 | b2) & attackedBy[Them][QUEEN] & safe) + kingDanger += QueenCheck; - // For other pieces, also consider the square safe if attacked twice, - // and only defended by a queen. - safe |= ei.attackedBy2[Them] - & ~(ei.attackedBy2[Us] | pos.pieces(Them)) - & ei.attackedBy[Us][QUEEN]; + // For minors and rooks, also consider the square safe if attacked twice, + // and only defended by our queen. + safe |= attackedBy2[Them] + & ~(attackedBy2[Us] | pos.pieces(Them)) + & attackedBy[Us][QUEEN]; + + // Some other potential checks are also analysed, even from squares + // currently occupied by the opponent own pieces, as long as the square + // is not attacked by our pawns, and is not occupied by a blocked pawn. + other = ~( attackedBy[Us][PAWN] + | (pos.pieces(Them, PAWN) & shift(pos.pieces(PAWN)))); // Enemy rooks safe and other checks - if (b1 & ei.attackedBy[Them][ROOK] & safe) - kingDanger += RookCheck, score -= SafeCheck; + if (b1 & attackedBy[Them][ROOK] & safe) + kingDanger += RookCheck; - else if (b1 & ei.attackedBy[Them][ROOK] & other) + else if (b1 & attackedBy[Them][ROOK] & other) score -= OtherCheck; // Enemy bishops safe and other checks - if (b2 & ei.attackedBy[Them][BISHOP] & safe) - kingDanger += BishopCheck, score -= SafeCheck; + if (b2 & attackedBy[Them][BISHOP] & safe) + kingDanger += BishopCheck; - else if (b2 & ei.attackedBy[Them][BISHOP] & other) + else if (b2 & attackedBy[Them][BISHOP] & other) score -= OtherCheck; // Enemy knights safe and other checks - b = pos.attacks_from(ksq) & ei.attackedBy[Them][KNIGHT]; + b = pos.attacks_from(ksq) & attackedBy[Them][KNIGHT]; if (b & safe) - kingDanger += KnightCheck, score -= SafeCheck; + kingDanger += KnightCheck; else if (b & other) score -= OtherCheck; - // Compute the king danger score and subtract it from the evaluation + // Transform the kingDanger units into a Score, and substract it from the evaluation if (kingDanger > 0) - score -= make_score(std::min(kingDanger * kingDanger / 4096, 2 * int(BishopValueMg)), 0); + score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16); } // King tropism: firstly, find squares that opponent attacks in our king flank File kf = file_of(ksq); - b = ei.attackedBy[Them][ALL_PIECES] & KingFlank[Us][kf]; + b = attackedBy[Them][ALL_PIECES] & KingFlank[kf] & Camp; assert(((Us == WHITE ? b << 4 : b >> 4) & b) == 0); assert(popcount(Us == WHITE ? b << 4 : b >> 4) == popcount(b)); @@ -493,15 +505,15 @@ namespace { // Secondly, add the squares which are attacked twice in that flank and // which are not defended by our pawns. b = (Us == WHITE ? b << 4 : b >> 4) - | (b & ei.attackedBy2[Them] & ~ei.attackedBy[Us][PAWN]); + | (b & attackedBy2[Them] & ~attackedBy[Us][PAWN]); score -= CloseEnemies * popcount(b); // Penalty when our king is on a pawnless flank - if (!(pos.pieces(PAWN) & (KingFlank[WHITE][kf] | KingFlank[BLACK][kf]))) + if (!(pos.pieces(PAWN) & KingFlank[kf])) score -= PawnlessFlank; - if (DoTrace) + if (T) Trace::add(KING, Us, score); return score; @@ -511,110 +523,119 @@ namespace { // evaluate_threats() assigns bonuses according to the types of the attacking // and the attacked pieces. - template - Score evaluate_threats(const Position& pos, const EvalInfo& ei) { + template template + Score Evaluation::evaluate_threats() { const Color Them = (Us == WHITE ? BLACK : WHITE); const Square Up = (Us == WHITE ? NORTH : SOUTH); const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST); const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST); - const Bitboard TRank2BB = (Us == WHITE ? Rank2BB : Rank7BB); - const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB); + const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); - enum { Minor, Rook }; - - Bitboard b, weak, defended, safeThreats; + Bitboard b, weak, defended, stronglyProtected, safeThreats; Score score = SCORE_ZERO; - // Small bonus if the opponent has loose pawns or pieces - if ( (pos.pieces(Them) ^ pos.pieces(Them, QUEEN, KING)) - & ~(ei.attackedBy[Us][ALL_PIECES] | ei.attackedBy[Them][ALL_PIECES])) - score += LooseEnemies; - // Non-pawn enemies attacked by a pawn - weak = (pos.pieces(Them) ^ pos.pieces(Them, PAWN)) & ei.attackedBy[Us][PAWN]; + weak = (pos.pieces(Them) ^ pos.pieces(Them, PAWN)) & attackedBy[Us][PAWN]; if (weak) { - b = pos.pieces(Us, PAWN) & ( ~ei.attackedBy[Them][ALL_PIECES] - | ei.attackedBy[Us][ALL_PIECES]); + b = pos.pieces(Us, PAWN) & ( ~attackedBy[Them][ALL_PIECES] + | attackedBy[Us][ALL_PIECES]); safeThreats = (shift(b) | shift(b)) & weak; + score += ThreatBySafePawn * popcount(safeThreats); + if (weak ^ safeThreats) score += ThreatByHangingPawn; - - while (safeThreats) - score += ThreatBySafePawn[type_of(pos.piece_on(pop_lsb(&safeThreats)))]; } - // Non-pawn enemies defended by a pawn - defended = (pos.pieces(Them) ^ pos.pieces(Them, PAWN)) & ei.attackedBy[Them][PAWN]; + // Squares strongly protected by the opponent, either because they attack the + // square with a pawn, or because they attack the square twice and we don't. + stronglyProtected = attackedBy[Them][PAWN] + | (attackedBy2[Them] & ~attackedBy2[Us]); - // Enemies not defended by a pawn and under our attack + // Non-pawn enemies, strongly protected + defended = (pos.pieces(Them) ^ pos.pieces(Them, PAWN)) + & stronglyProtected; + + // Enemies not strongly protected and under our attack weak = pos.pieces(Them) - & ~ei.attackedBy[Them][PAWN] - & ei.attackedBy[Us][ALL_PIECES]; + & ~stronglyProtected + & attackedBy[Us][ALL_PIECES]; // Add a bonus according to the kind of attacking pieces if (defended | weak) { - b = (defended | weak) & (ei.attackedBy[Us][KNIGHT] | ei.attackedBy[Us][BISHOP]); + b = (defended | weak) & (attackedBy[Us][KNIGHT] | attackedBy[Us][BISHOP]); while (b) - score += Threat[Minor][type_of(pos.piece_on(pop_lsb(&b)))]; + { + Square s = pop_lsb(&b); + score += ThreatByMinor[type_of(pos.piece_on(s))]; + if (type_of(pos.piece_on(s)) != PAWN) + score += ThreatByRank * (int)relative_rank(Them, s); + } - b = (pos.pieces(Them, QUEEN) | weak) & ei.attackedBy[Us][ROOK]; + b = (pos.pieces(Them, QUEEN) | weak) & attackedBy[Us][ROOK]; while (b) - score += Threat[Rook ][type_of(pos.piece_on(pop_lsb(&b)))]; + { + Square s = pop_lsb(&b); + score += ThreatByRook[type_of(pos.piece_on(s))]; + if (type_of(pos.piece_on(s)) != PAWN) + score += ThreatByRank * (int)relative_rank(Them, s); + } - score += Hanging * popcount(weak & ~ei.attackedBy[Them][ALL_PIECES]); + score += Hanging * popcount(weak & ~attackedBy[Them][ALL_PIECES]); - b = weak & ei.attackedBy[Us][KING]; + b = weak & attackedBy[Us][KING]; if (b) score += ThreatByKing[more_than_one(b)]; } - // Bonus if some pawns can safely push and attack an enemy piece - b = pos.pieces(Us, PAWN) & ~TRank7BB; - b = shift(b | (shift(b & TRank2BB) & ~pos.pieces())); + // Find squares where our pawns can push on the next move + b = shift(pos.pieces(Us, PAWN)) & ~pos.pieces(); + b |= shift(b & TRank3BB) & ~pos.pieces(); - b &= ~pos.pieces() - & ~ei.attackedBy[Them][PAWN] - & (ei.attackedBy[Us][ALL_PIECES] | ~ei.attackedBy[Them][ALL_PIECES]); + // Keep only the squares which are not completely unsafe + b &= ~attackedBy[Them][PAWN] + & (attackedBy[Us][ALL_PIECES] | ~attackedBy[Them][ALL_PIECES]); + // Add a bonus for each new pawn threats from those squares b = (shift(b) | shift(b)) & pos.pieces(Them) - & ~ei.attackedBy[Us][PAWN]; + & ~attackedBy[Us][PAWN]; score += ThreatByPawnPush * popcount(b); - if (DoTrace) + if (T) Trace::add(THREAT, Us, score); return score; } - // evaluate_passed_pawns() evaluates the passed pawns of the given color + // evaluate_passed_pawns() evaluates the passed pawns and candidate passed + // pawns of the given color. - template - Score evaluate_passed_pawns(const Position& pos, const EvalInfo& ei) { + template template + Score Evaluation::evaluate_passed_pawns() { const Color Them = (Us == WHITE ? BLACK : WHITE); + const Square Up = (Us == WHITE ? NORTH : SOUTH); Bitboard b, bb, squaresToQueen, defendedSquares, unsafeSquares; Score score = SCORE_ZERO; - b = ei.pi->passed_pawns(Us); + b = pe->passed_pawns(Us); while (b) { Square s = pop_lsb(&b); - assert(pos.pawn_passed(Us, s)); - assert(!(pos.pieces(PAWN) & forward_bb(Us, s))); + assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up))); - bb = forward_bb(Us, s) & (ei.attackedBy[Them][ALL_PIECES] | pos.pieces(Them)); + bb = forward_file_bb(Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces(Them)); score -= HinderPassedPawn * popcount(bb); int r = relative_rank(Us, s) - RANK_2; @@ -624,15 +645,15 @@ namespace { if (rr) { - Square blockSq = s + pawn_push(Us); + Square blockSq = s + Up; // Adjust bonus based on the king's proximity ebonus += distance(pos.square(Them), blockSq) * 5 * rr - - distance(pos.square(Us ), blockSq) * 2 * rr; + - distance(pos.square( Us), blockSq) * 2 * rr; // If blockSq is not the queening square then consider also a second push if (relative_rank(Us, blockSq) != RANK_8) - ebonus -= distance(pos.square(Us), blockSq + pawn_push(Us)) * rr; + ebonus -= distance(pos.square(Us), blockSq + Up) * rr; // If the pawn is free to advance, then increase the bonus if (pos.empty(blockSq)) @@ -640,15 +661,15 @@ namespace { // If there is a rook or queen attacking/defending the pawn from behind, // consider all the squaresToQueen. Otherwise consider only the squares // in the pawn's path attacked or occupied by the enemy. - defendedSquares = unsafeSquares = squaresToQueen = forward_bb(Us, s); + defendedSquares = unsafeSquares = squaresToQueen = forward_file_bb(Us, s); - bb = forward_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from(s); + bb = forward_file_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from(s); if (!(pos.pieces(Us) & bb)) - defendedSquares &= ei.attackedBy[Us][ALL_PIECES]; + defendedSquares &= attackedBy[Us][ALL_PIECES]; if (!(pos.pieces(Them) & bb)) - unsafeSquares &= ei.attackedBy[Them][ALL_PIECES] | pos.pieces(Them); + unsafeSquares &= attackedBy[Them][ALL_PIECES] | pos.pieces(Them); // If there aren't any enemy attacks, assign a big bonus. Otherwise // assign a smaller bonus if the block square isn't attacked. @@ -668,13 +689,17 @@ namespace { mbonus += rr + r * 2, ebonus += rr + r * 2; } // rr != 0 + // Scale down bonus for candidate passers which need more than one + // pawn push to become passed or have a pawn in front of them. + if (!pos.pawn_passed(Us, s + Up) || (pos.pieces(PAWN) & forward_file_bb(Us, s))) + mbonus /= 2, ebonus /= 2; + score += make_score(mbonus, ebonus) + PassedFile[file_of(s)]; } - if (DoTrace) + if (T) Trace::add(PASSED, Us, score); - // Add the scores to the middlegame and endgame eval return score; } @@ -685,21 +710,22 @@ namespace { // squares one, two or three squares behind a friendly pawn are counted // twice. Finally, the space bonus is multiplied by a weight. The aim is to // improve play on game opening. - template - Score evaluate_space(const Position& pos, const EvalInfo& ei) { + + template template + Score Evaluation::evaluate_space() { const Color Them = (Us == WHITE ? BLACK : WHITE); const Bitboard SpaceMask = - Us == WHITE ? (FileCBB | FileDBB | FileEBB | FileFBB) & (Rank2BB | Rank3BB | Rank4BB) - : (FileCBB | FileDBB | FileEBB | FileFBB) & (Rank7BB | Rank6BB | Rank5BB); + Us == WHITE ? CenterFiles & (Rank2BB | Rank3BB | Rank4BB) + : CenterFiles & (Rank7BB | Rank6BB | Rank5BB); // Find the safe squares for our pieces inside the area defined by // SpaceMask. A square is unsafe if it is attacked by an enemy // pawn, or if it is undefended and attacked by an enemy piece. Bitboard safe = SpaceMask & ~pos.pieces(Us, PAWN) - & ~ei.attackedBy[Them][PAWN] - & (ei.attackedBy[Us][ALL_PIECES] | ~ei.attackedBy[Them][ALL_PIECES]); + & ~attackedBy[Them][PAWN] + & (attackedBy[Us][ALL_PIECES] | ~attackedBy[Them][ALL_PIECES]); // Find all squares which are at most three squares behind some friendly pawn Bitboard behind = pos.pieces(Us, PAWN); @@ -709,46 +735,48 @@ namespace { // Since SpaceMask[Us] is fully on our half of the board... assert(unsigned(safe >> (Us == WHITE ? 32 : 0)) == 0); - // ...count safe + (behind & safe) with a single popcount + // ...count safe + (behind & safe) with a single popcount. int bonus = popcount((Us == WHITE ? safe << 32 : safe >> 32) | (behind & safe)); - bonus = std::min(16, bonus); - int weight = pos.count(Us) - 2 * ei.pi->open_files(); + int weight = pos.count(Us) - 2 * pe->open_files(); - return make_score(bonus * weight * weight / 18, 0); + return make_score(bonus * weight * weight / 16, 0); } // evaluate_initiative() computes the initiative correction value for the // position, i.e., second order bonus/malus based on the known attacking/defending // status of the players. - Score evaluate_initiative(const Position& pos, int asymmetry, Value eg) { + + template + Score Evaluation::evaluate_initiative(Value eg) { int kingDistance = distance(pos.square(WHITE), pos.square(BLACK)) - distance(pos.square(WHITE), pos.square(BLACK)); - int pawns = pos.count(WHITE) + pos.count(BLACK); + bool bothFlanks = (pos.pieces(PAWN) & QueenSide) && (pos.pieces(PAWN) & KingSide); // Compute the initiative bonus for the attacking side - int initiative = 8 * (asymmetry + kingDistance - 15) + 12 * pawns; + int initiative = 8 * (pe->pawn_asymmetry() + kingDistance - 17) + 12 * pos.count() + 16 * bothFlanks; // Now apply the bonus: note that we find the attacking side by extracting // the sign of the endgame value, and that we carefully cap the bonus so - // that the endgame score will never be divided by more than two. - int value = ((eg > 0) - (eg < 0)) * std::max(initiative, -abs(eg / 2)); + // that the endgame score will never change sign after the bonus. + int v = ((eg > 0) - (eg < 0)) * std::max(initiative, -abs(eg)); - return make_score(0, value); + return make_score(0, v); } // evaluate_scale_factor() computes the scale factor for the winning side - ScaleFactor evaluate_scale_factor(const Position& pos, const EvalInfo& ei, Value eg) { + + template + ScaleFactor Evaluation::evaluate_scale_factor(Value eg) { Color strongSide = eg > VALUE_DRAW ? WHITE : BLACK; - ScaleFactor sf = ei.me->scale_factor(pos, strongSide); + ScaleFactor sf = me->scale_factor(pos, strongSide); // If we don't already have an unusual scale factor, check for certain // types of endgames, and use a lower scale for those. - if ( ei.me->game_phase() < PHASE_MIDGAME - && (sf == SCALE_FACTOR_NORMAL || sf == SCALE_FACTOR_ONEPAWN)) + if (sf == SCALE_FACTOR_NORMAL || sf == SCALE_FACTOR_ONEPAWN) { if (pos.opposite_bishops()) { @@ -756,140 +784,116 @@ namespace { // is almost a draw, in case of KBP vs KB, it is even more a draw. if ( pos.non_pawn_material(WHITE) == BishopValueMg && pos.non_pawn_material(BLACK) == BishopValueMg) - sf = more_than_one(pos.pieces(PAWN)) ? ScaleFactor(31) : ScaleFactor(9); + return more_than_one(pos.pieces(PAWN)) ? ScaleFactor(31) : ScaleFactor(9); // Endgame with opposite-colored bishops, but also other pieces. Still // a bit drawish, but not as drawish as with only the two bishops. - else - sf = ScaleFactor(46); + return ScaleFactor(46); } // Endings where weaker side can place his king in front of the opponent's // pawns are drawish. else if ( abs(eg) <= BishopValueEg && pos.count(strongSide) <= 2 && !pos.pawn_passed(~strongSide, pos.square(~strongSide))) - sf = ScaleFactor(37 + 7 * pos.count(strongSide)); + return ScaleFactor(37 + 7 * pos.count(strongSide)); } return sf; } + + // value() is the main function of the class. It computes the various parts of + // the evaluation and returns the value of the position from the point of view + // of the side to move. + + template + Value Evaluation::value() { + + assert(!pos.checkers()); + + // Probe the material hash table + me = Material::probe(pos); + + // If we have a specialized evaluation function for the current material + // configuration, call it and return. + if (me->specialized_eval_exists()) + return me->evaluate(pos); + + // Initialize score by reading the incrementally updated scores included in + // the position object (material + piece square tables) and the material + // imbalance. Score is computed internally from the white point of view. + Score score = pos.psq_score() + me->imbalance(); + + // Probe the pawn hash table + pe = Pawns::probe(pos); + score += pe->pawns_score(); + + // Early exit if score is high + Value v = (mg_value(score) + eg_value(score)) / 2; + if (abs(v) > LazyThreshold) + return pos.side_to_move() == WHITE ? v : -v; + + // Main evaluation begins here + + initialize(); + initialize(); + + score += evaluate_pieces() - evaluate_pieces(); + score += evaluate_pieces() - evaluate_pieces(); + score += evaluate_pieces() - evaluate_pieces(); + score += evaluate_pieces() - evaluate_pieces(); + + score += mobility[WHITE] - mobility[BLACK]; + + score += evaluate_king() + - evaluate_king(); + + score += evaluate_threats() + - evaluate_threats(); + + score += evaluate_passed_pawns() + - evaluate_passed_pawns(); + + if (pos.non_pawn_material() >= SpaceThreshold) + score += evaluate_space() + - evaluate_space(); + + score += evaluate_initiative(eg_value(score)); + + // Interpolate between a middlegame and a (scaled by 'sf') endgame score + ScaleFactor sf = evaluate_scale_factor(eg_value(score)); + v = mg_value(score) * int(me->game_phase()) + + eg_value(score) * int(PHASE_MIDGAME - me->game_phase()) * sf / SCALE_FACTOR_NORMAL; + + v /= int(PHASE_MIDGAME); + + // In case of tracing add all remaining individual evaluation terms + if (T) + { + Trace::add(MATERIAL, pos.psq_score()); + Trace::add(IMBALANCE, me->imbalance()); + Trace::add(PAWN, pe->pawns_score()); + Trace::add(MOBILITY, mobility[WHITE], mobility[BLACK]); + if (pos.non_pawn_material() >= SpaceThreshold) + Trace::add(SPACE, evaluate_space() + , evaluate_space()); + Trace::add(TOTAL, score); + } + + return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view + } + } // namespace -/// evaluate() is the main evaluation function. It returns a static evaluation +/// evaluate() is the evaluator for the outer world. It returns a static evaluation /// of the position from the point of view of the side to move. -template -Value Eval::evaluate(const Position& pos) { - - assert(!pos.checkers()); - - Score mobility[COLOR_NB] = { SCORE_ZERO, SCORE_ZERO }; - EvalInfo ei; - - // Probe the material hash table - ei.me = Material::probe(pos); - - // If we have a specialized evaluation function for the current material - // configuration, call it and return. - if (ei.me->specialized_eval_exists()) - return ei.me->evaluate(pos); - - // Initialize score by reading the incrementally updated scores included in - // the position object (material + piece square tables) and the material - // imbalance. Score is computed internally from the white point of view. - Score score = pos.psq_score() + ei.me->imbalance(); - - // Probe the pawn hash table - ei.pi = Pawns::probe(pos); - score += ei.pi->pawns_score(); - - // Initialize attack and king safety bitboards - ei.attackedBy[WHITE][ALL_PIECES] = ei.attackedBy[BLACK][ALL_PIECES] = 0; - ei.attackedBy[WHITE][KING] = pos.attacks_from(pos.square(WHITE)); - ei.attackedBy[BLACK][KING] = pos.attacks_from(pos.square(BLACK)); - eval_init(pos, ei); - eval_init(pos, ei); - - // Pawns blocked or on ranks 2 and 3 will be excluded from the mobility area - Bitboard blockedPawns[] = { - pos.pieces(WHITE, PAWN) & (shift(pos.pieces()) | Rank2BB | Rank3BB), - pos.pieces(BLACK, PAWN) & (shift(pos.pieces()) | Rank7BB | Rank6BB) - }; - - // Do not include in mobility area squares protected by enemy pawns, or occupied - // by our blocked pawns or king. - Bitboard mobilityArea[] = { - ~(ei.attackedBy[BLACK][PAWN] | blockedPawns[WHITE] | pos.square(WHITE)), - ~(ei.attackedBy[WHITE][PAWN] | blockedPawns[BLACK] | pos.square(BLACK)) - }; - - // Evaluate all pieces but king and pawns - score += evaluate_pieces(pos, ei, mobility, mobilityArea); - score += mobility[WHITE] - mobility[BLACK]; - - // Evaluate kings after all other pieces because we need full attack - // information when computing the king safety evaluation. - score += evaluate_king(pos, ei) - - evaluate_king(pos, ei); - - // Evaluate tactical threats, we need full attack information including king - score += evaluate_threats(pos, ei) - - evaluate_threats(pos, ei); - - // Evaluate passed pawns, we need full attack information including king - score += evaluate_passed_pawns(pos, ei) - - evaluate_passed_pawns(pos, ei); - - // If both sides have only pawns, score for potential unstoppable pawns - if (!pos.non_pawn_material(WHITE) && !pos.non_pawn_material(BLACK)) - { - Bitboard b; - if ((b = ei.pi->passed_pawns(WHITE)) != 0) - score += Unstoppable * int(relative_rank(WHITE, frontmost_sq(WHITE, b))); - - if ((b = ei.pi->passed_pawns(BLACK)) != 0) - score -= Unstoppable * int(relative_rank(BLACK, frontmost_sq(BLACK, b))); - } - - // Evaluate space for both sides, only during opening - if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 12222) - score += evaluate_space(pos, ei) - - evaluate_space(pos, ei); - - // Evaluate position potential for the winning side - score += evaluate_initiative(pos, ei.pi->pawn_asymmetry(), eg_value(score)); - - // Evaluate scale factor for the winning side - ScaleFactor sf = evaluate_scale_factor(pos, ei, eg_value(score)); - - // Interpolate between a middlegame and a (scaled by 'sf') endgame score - Value v = mg_value(score) * int(ei.me->game_phase()) - + eg_value(score) * int(PHASE_MIDGAME - ei.me->game_phase()) * sf / SCALE_FACTOR_NORMAL; - - v /= int(PHASE_MIDGAME); - - // In case of tracing add all remaining individual evaluation terms - if (DoTrace) - { - Trace::add(MATERIAL, pos.psq_score()); - Trace::add(IMBALANCE, ei.me->imbalance()); - Trace::add(PAWN, ei.pi->pawns_score()); - Trace::add(MOBILITY, mobility[WHITE], mobility[BLACK]); - Trace::add(SPACE, evaluate_space(pos, ei) - , evaluate_space(pos, ei)); - Trace::add(TOTAL, score); - } - - return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view +Value Eval::evaluate(const Position& pos) +{ + return Evaluation<>(pos).value(); } -// Explicit template instantiations -template Value Eval::evaluate(const Position&); -template Value Eval::evaluate(const Position&); - - /// trace() is like evaluate(), but instead of returning a value, it returns /// a string (suitable for outputting to stdout) that contains the detailed /// descriptions and values of each evaluation term. Useful for debugging. @@ -898,7 +902,7 @@ std::string Eval::trace(const Position& pos) { std::memset(scores, 0, sizeof(scores)); - Value v = evaluate(pos); + Value v = Evaluation(pos).value(); v = pos.side_to_move() == WHITE ? v : -v; // White's point of view std::stringstream ss; @@ -910,7 +914,7 @@ std::string Eval::trace(const Position& pos) { << " Imbalance | " << Term(IMBALANCE) << " Pawns | " << Term(PAWN) << " Knights | " << Term(KNIGHT) - << " Bishop | " << Term(BISHOP) + << " Bishops | " << Term(BISHOP) << " Rooks | " << Term(ROOK) << " Queens | " << Term(QUEEN) << " Mobility | " << Term(MOBILITY) diff --git a/DroidFish/jni/stockfish/evaluate.h b/DroidFish/jni/stockfish/evaluate.h index 7f655f6..95a1f19 100644 --- a/DroidFish/jni/stockfish/evaluate.h +++ b/DroidFish/jni/stockfish/evaluate.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -33,7 +33,6 @@ const Value Tempo = Value(20); // Must be visible to search std::string trace(const Position& pos); -template Value evaluate(const Position& pos); } diff --git a/DroidFish/jni/stockfish/main.cpp b/DroidFish/jni/stockfish/main.cpp index 7187d30..065fe8e 100644 --- a/DroidFish/jni/stockfish/main.cpp +++ b/DroidFish/jni/stockfish/main.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -43,9 +43,10 @@ int main(int argc, char* argv[]) { Bitbases::init(); Search::init(); Pawns::init(); - Threads.init(); Tablebases::init(Options["SyzygyPath"]); TT.resize(Options["Hash"]); + Threads.init(Options["Threads"]); + Search::clear(); // After threads are up UCI::loop(argc, argv); diff --git a/DroidFish/jni/stockfish/material.cpp b/DroidFish/jni/stockfish/material.cpp index f6c4e2d..d67b95c 100644 --- a/DroidFish/jni/stockfish/material.cpp +++ b/DroidFish/jni/stockfish/material.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ namespace { // OUR PIECES // pair pawn knight bishop rook queen {1667 }, // Bishop pair - { 40, 2 }, // Pawn + { 40, 0 }, // Pawn { 32, 255, -3 }, // Knight OUR PIECES { 0, 104, 4, 0 }, // Bishop { -26, -2, 47, 105, -149 }, // Rook @@ -53,6 +53,17 @@ namespace { { 101, 100, -37, 141, 268, 0 } // Queen }; + // PawnSet[pawn count] contains a bonus/malus indexed by number of pawns + const int PawnSet[] = { + 24, -32, 107, -51, 117, -9, -126, -21, 31 + }; + + // QueenMinorsImbalance[opp_minor_count] is applied when only one side has a queen. + // It contains a bonus/malus for the side with the queen. + const int QueenMinorsImbalance[13] = { + 31, -8, -15, -25, -5 + }; + // Endgame evaluation and scaling functions are accessed directly and not through // the function maps because they correspond to more than one material hash key. Endgame EvaluateKXK[] = { Endgame(WHITE), Endgame(BLACK) }; @@ -89,7 +100,7 @@ namespace { const Color Them = (Us == WHITE ? BLACK : WHITE); - int bonus = 0; + int bonus = PawnSet[pieceCount[Us][PAWN]]; // Second-degree polynomial material imbalance by Tord Romstad for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1) @@ -106,6 +117,10 @@ namespace { bonus += pieceCount[Us][pt1] * v; } + // Special handling of Queen vs. Minors + if (pieceCount[Us][QUEEN] == 1 && pieceCount[Them][QUEEN] == 0) + bonus += QueenMinorsImbalance[pieceCount[Them][KNIGHT] + pieceCount[Them][BISHOP]]; + return bonus; } @@ -129,7 +144,13 @@ Entry* probe(const Position& pos) { std::memset(e, 0, sizeof(Entry)); e->key = key; e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL; - e->gamePhase = pos.game_phase(); + + Value npm_w = pos.non_pawn_material(WHITE); + Value npm_b = pos.non_pawn_material(BLACK); + Value npm = std::max(EndgameLimit, std::min(npm_w + npm_b, MidgameLimit)); + + // Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME] + e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit)); // Let's look if we have a specialized evaluation function for this particular // material configuration. Firstly we look for a fixed configuration one, then @@ -150,7 +171,7 @@ Entry* probe(const Position& pos) { if ((sf = pos.this_thread()->endgames.probe(key)) != nullptr) { - e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned + e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned return e; } @@ -166,9 +187,6 @@ Entry* probe(const Position& pos) { e->scalingFunction[c] = &ScaleKQKRPs[c]; } - Value npm_w = pos.non_pawn_material(WHITE); - Value npm_b = pos.non_pawn_material(BLACK); - if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board { if (!pos.count(BLACK)) diff --git a/DroidFish/jni/stockfish/material.h b/DroidFish/jni/stockfish/material.h index bec2d66..ccf97b7 100644 --- a/DroidFish/jni/stockfish/material.h +++ b/DroidFish/jni/stockfish/material.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -56,11 +56,11 @@ struct Entry { } Key key; - int16_t value; - uint8_t factor[COLOR_NB]; EndgameBase* evaluationFunction; EndgameBase* scalingFunction[COLOR_NB]; // Could be one for each // side (e.g. KPKP, KBPsKs) + int16_t value; + uint8_t factor[COLOR_NB]; Phase gamePhase; }; diff --git a/DroidFish/jni/stockfish/misc.cpp b/DroidFish/jni/stockfish/misc.cpp index 4c6c254..0136d4c 100644 --- a/DroidFish/jni/stockfish/misc.cpp +++ b/DroidFish/jni/stockfish/misc.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,10 +18,29 @@ along with this program. If not, see . */ +#ifdef _WIN32 +#if _WIN32_WINNT < 0x0601 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0601 // Force to include needed API prototypes +#endif +#include +// The needed Windows API for processor groups could be missed from old Windows +// versions, so instead of calling them directly (forcing the linker to resolve +// the calls at compile time), try to load them at runtime. To do this we need +// first to define the corresponding function pointers. +extern "C" { +typedef bool(*fun1_t)(LOGICAL_PROCESSOR_RELATIONSHIP, + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD); +typedef bool(*fun2_t)(USHORT, PGROUP_AFFINITY); +typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY); +} +#endif + #include #include #include #include +#include #include "misc.h" #include "thread.h" @@ -32,7 +51,7 @@ namespace { /// Version number. If Version is left empty, then compile date in the format /// DD-MM-YY and show in engine_info. -const string Version = "8"; +const string Version = "2017-09-06"; /// Our fancy logging facility. The trick here is to replace cin.rdbuf() and /// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We @@ -44,10 +63,10 @@ struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {} - int sync() { return logBuf->pubsync(), buf->pubsync(); } - int overflow(int c) { return log(buf->sputc((char)c), "<< "); } - int underflow() { return buf->sgetc(); } - int uflow() { return log(buf->sbumpc(), ">> "); } + int sync() override { return logBuf->pubsync(), buf->pubsync(); } + int overflow(int c) override { return log(buf->sputc((char)c), "<< "); } + int underflow() override { return buf->sgetc(); } + int uflow() override { return log(buf->sbumpc(), ">> "); } streambuf *buf, *logBuf; @@ -185,3 +204,122 @@ void prefetch(void* addr) { } #endif + +void prefetch2(void* addr) { + + prefetch(addr); + prefetch((uint8_t*)addr + 64); +} + +namespace WinProcGroup { + +#ifndef _WIN32 + +void bindThisThread(size_t) {} + +#else + +/// get_group() retrieves logical processor information using Windows specific +/// API and returns the best group id for the thread with index idx. Original +/// code from Texel by Peter Österlund. + +int get_group(size_t idx) { + + int threads = 0; + int nodes = 0; + int cores = 0; + DWORD returnLength = 0; + DWORD byteOffset = 0; + + // Early exit if the needed API is not available at runtime + HMODULE k32 = GetModuleHandle("Kernel32.dll"); + auto fun1 = (fun1_t)GetProcAddress(k32, "GetLogicalProcessorInformationEx"); + if (!fun1) + return -1; + + // First call to get returnLength. We expect it to fail due to null buffer + if (fun1(RelationAll, nullptr, &returnLength)) + return -1; + + // Once we know returnLength, allocate the buffer + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer, *ptr; + ptr = buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(returnLength); + + // Second call, now we expect to succeed + if (!fun1(RelationAll, buffer, &returnLength)) + { + free(buffer); + return -1; + } + + while (ptr->Size > 0 && byteOffset + ptr->Size <= returnLength) + { + if (ptr->Relationship == RelationNumaNode) + nodes++; + + else if (ptr->Relationship == RelationProcessorCore) + { + cores++; + threads += (ptr->Processor.Flags == LTP_PC_SMT) ? 2 : 1; + } + + byteOffset += ptr->Size; + ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)(((char*)ptr) + ptr->Size); + } + + free(buffer); + + std::vector groups; + + // Run as many threads as possible on the same node until core limit is + // reached, then move on filling the next node. + for (int n = 0; n < nodes; n++) + for (int i = 0; i < cores / nodes; i++) + groups.push_back(n); + + // In case a core has more than one logical processor (we assume 2) and we + // have still threads to allocate, then spread them evenly across available + // nodes. + for (int t = 0; t < threads - cores; t++) + groups.push_back(t % nodes); + + // If we still have more threads than the total number of logical processors + // then return -1 and let the OS to decide what to do. + return idx < groups.size() ? groups[idx] : -1; +} + + +/// bindThisThread() set the group affinity of the current thread + +void bindThisThread(size_t idx) { + + // If OS already scheduled us on a different group than 0 then don't overwrite + // the choice, eventually we are one of many one-threaded processes running on + // some Windows NUMA hardware, for instance in fishtest. To make it simple, + // just check if running threads are below a threshold, in this case all this + // NUMA machinery is not needed. + if (Threads.size() < 8) + return; + + // Use only local variables to be thread-safe + int group = get_group(idx); + + if (group == -1) + return; + + // Early exit if the needed API are not available at runtime + HMODULE k32 = GetModuleHandle("Kernel32.dll"); + auto fun2 = (fun2_t)GetProcAddress(k32, "GetNumaNodeProcessorMaskEx"); + auto fun3 = (fun3_t)GetProcAddress(k32, "SetThreadGroupAffinity"); + + if (!fun2 || !fun3) + return; + + GROUP_AFFINITY affinity; + if (fun2(group, &affinity)) + fun3(GetCurrentThread(), &affinity, nullptr); +} + +#endif + +} // namespace WinProcGroup diff --git a/DroidFish/jni/stockfish/misc.h b/DroidFish/jni/stockfish/misc.h index 2889252..8283c63 100644 --- a/DroidFish/jni/stockfish/misc.h +++ b/DroidFish/jni/stockfish/misc.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,6 +32,7 @@ const std::string engine_info(bool to_uci = false); void prefetch(void* addr); +void prefetch2(void* addr); void start_logger(const std::string& fname); void dbg_hit_on(bool b); @@ -98,6 +99,17 @@ public: { return T(rand64() & rand64() & rand64()); } }; + +/// Under Windows it is not possible for a process to run on more than one +/// logical processor group. This usually means to be limited to use max 64 +/// cores. To overcome this, some special platform specific API should be +/// called to set group affinity for each thread. Original code from Texel by +/// Peter Österlund. + +namespace WinProcGroup { + void bindThisThread(size_t idx); +} + inline int stoi(const std::string& s) { std::stringstream ss(s); int result = 0; diff --git a/DroidFish/jni/stockfish/movegen.cpp b/DroidFish/jni/stockfish/movegen.cpp index 6a3cdc2..465d616 100644 --- a/DroidFish/jni/stockfish/movegen.cpp +++ b/DroidFish/jni/stockfish/movegen.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -80,7 +80,7 @@ namespace { // Knight promotion is the only promotion that can give a direct check // that's not already included in the queen promotion. - if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ksq)) + if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq)) *moveList++ = make(to - D, to, KNIGHT); else (void)ksq; // Silence a warning under MSVC @@ -346,7 +346,7 @@ ExtMove* generate(const Position& pos, ExtMove* moveList) { if (pt == PAWN) continue; // Will be generated together with direct checks - Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces(); + Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces(); if (pt == KING) b &= ~PseudoAttacks[QUEEN][pos.square(~us)]; diff --git a/DroidFish/jni/stockfish/movegen.h b/DroidFish/jni/stockfish/movegen.h index 2721f15..8256057 100644 --- a/DroidFish/jni/stockfish/movegen.h +++ b/DroidFish/jni/stockfish/movegen.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,6 +21,8 @@ #ifndef MOVEGEN_H_INCLUDED #define MOVEGEN_H_INCLUDED +#include + #include "types.h" class Position; @@ -36,10 +38,14 @@ enum GenType { struct ExtMove { Move move; - Value value; + int value; operator Move() const { return move; } void operator=(Move m) { move = m; } + + // Inhibit unwanted implicit conversions to Move + // with an ambiguity that yields to a compile error. + operator float() const; }; inline bool operator<(const ExtMove& f, const ExtMove& s) { @@ -59,8 +65,7 @@ struct MoveList { const ExtMove* end() const { return last; } size_t size() const { return last - moveList; } bool contains(Move move) const { - for (const auto& m : *this) if (m == move) return true; - return false; + return std::find(begin(), end(), move) != end(); } private: diff --git a/DroidFish/jni/stockfish/movepick.cpp b/DroidFish/jni/stockfish/movepick.cpp index c187c3a..1c0de69 100644 --- a/DroidFish/jni/stockfish/movepick.cpp +++ b/DroidFish/jni/stockfish/movepick.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,7 +21,6 @@ #include #include "movepick.h" -#include "thread.h" namespace { @@ -34,27 +33,28 @@ namespace { QSEARCH_RECAPTURES, QRECAPTURES }; - // Our insertion sort, which is guaranteed to be stable, as it should be - void insertion_sort(ExtMove* begin, ExtMove* end) - { - ExtMove tmp, *p, *q; + // partial_insertion_sort() sorts moves in descending order up to and including + // a given limit. The order of moves smaller than the limit is left unspecified. + void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) { - for (p = begin + 1; p < end; ++p) - { - tmp = *p; - for (q = p; q != begin && *(q-1) < tmp; --q) - *q = *(q-1); - *q = tmp; - } + for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p) + if (p->value >= limit) + { + ExtMove tmp = *p, *q; + *p = *++sortedEnd; + for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q) + *q = *(q - 1); + *q = tmp; + } } // pick_best() finds the best move in the range (begin, end) and moves it to // the front. It's faster than sorting all the moves in advance when there // are few moves, e.g., the possible captures. - Move pick_best(ExtMove* begin, ExtMove* end) - { - std::swap(*begin, *std::max_element(begin, end)); - return *begin; + Move pick_best(ExtMove* begin, ExtMove* end) { + + std::swap(*begin, *std::max_element(begin, end)); + return *begin; } } // namespace @@ -66,21 +66,22 @@ namespace { /// search captures, promotions, and some checks) and how important good move /// ordering is at the current node. -MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Search::Stack* s) - : pos(p), ss(s), depth(d) { +/// MovePicker constructor for the main search +MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, + const PieceToHistory** ch, Move cm, Move* killers_p) + : pos(p), mainHistory(mh), contHistory(ch), countermove(cm), + killers{killers_p[0], killers_p[1]}, depth(d){ assert(d > DEPTH_ZERO); - Square prevSq = to_sq((ss-1)->currentMove); - countermove = pos.this_thread()->counterMoves[pos.piece_on(prevSq)][prevSq]; - stage = pos.checkers() ? EVASION : MAIN_SEARCH; ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE; stage += (ttMove == MOVE_NONE); } -MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Square s) - : pos(p) { +/// MovePicker constructor for quiescence search +MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, Square s) + : pos(p), mainHistory(mh) { assert(d <= DEPTH_ZERO); @@ -104,81 +105,57 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Square s) stage += (ttMove == MOVE_NONE); } +/// MovePicker constructor for ProbCut: we generate captures with SEE higher +/// than or equal to the given threshold. MovePicker::MovePicker(const Position& p, Move ttm, Value th) : pos(p), threshold(th) { assert(!pos.checkers()); stage = PROBCUT; - - // In ProbCut we generate captures with SEE higher than the given threshold ttMove = ttm && pos.pseudo_legal(ttm) && pos.capture(ttm) - && pos.see_ge(ttm, threshold + 1)? ttm : MOVE_NONE; + && pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE; stage += (ttMove == MOVE_NONE); } +/// score() assigns a numerical value to each move in a list, used for sorting. +/// Captures are ordered by Most Valuable Victim (MVV), preferring captures +/// near our home rank. Quiets are ordered using the histories. +template +void MovePicker::score() { -/// score() assigns a numerical value to each move in a move list. The moves with -/// highest values will be picked first. -template<> -void MovePicker::score() { - // Winning and equal captures in the main search are ordered by MVV, preferring - // captures near our home rank. Surprisingly, this appears to perform slightly - // better than SEE-based move ordering: exchanging big pieces before capturing - // a hanging piece probably helps to reduce the subtree size. - // In the main search we want to push captures with negative SEE values to the - // badCaptures[] array, but instead of doing it now we delay until the move - // has been picked up, saving some SEE calls in case we get a cutoff. - for (auto& m : *this) - m.value = PieceValue[MG][pos.piece_on(to_sq(m))] - - Value(200 * relative_rank(pos.side_to_move(), to_sq(m))); -} - -template<> -void MovePicker::score() { - - const HistoryStats& history = pos.this_thread()->history; - const FromToStats& fromTo = pos.this_thread()->fromTo; - - const CounterMoveStats* cm = (ss-1)->counterMoves; - const CounterMoveStats* fm = (ss-2)->counterMoves; - const CounterMoveStats* f2 = (ss-4)->counterMoves; - - Color c = pos.side_to_move(); + static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type"); for (auto& m : *this) - m.value = history[pos.moved_piece(m)][to_sq(m)] - + (cm ? (*cm)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) - + (fm ? (*fm)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) - + (f2 ? (*f2)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) - + fromTo.get(c, m); -} - -template<> -void MovePicker::score() { - // Try captures ordered by MVV/LVA, then non-captures ordered by history value - const HistoryStats& history = pos.this_thread()->history; - const FromToStats& fromTo = pos.this_thread()->fromTo; - Color c = pos.side_to_move(); - - for (auto& m : *this) - if (pos.capture(m)) + if (Type == CAPTURES) m.value = PieceValue[MG][pos.piece_on(to_sq(m))] - - Value(type_of(pos.moved_piece(m))) + HistoryStats::Max; - else - m.value = history[pos.moved_piece(m)][to_sq(m)] + fromTo.get(c, m); -} + - Value(200 * relative_rank(pos.side_to_move(), to_sq(m))); + else if (Type == QUIETS) + m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] + + (*contHistory[0])[pos.moved_piece(m)][to_sq(m)] + + (*contHistory[1])[pos.moved_piece(m)][to_sq(m)] + + (*contHistory[3])[pos.moved_piece(m)][to_sq(m)]; + + else // Type == EVASIONS + { + if (pos.capture(m)) + m.value = PieceValue[MG][pos.piece_on(to_sq(m))] + - Value(type_of(pos.moved_piece(m))); + else + m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28); + } +} /// next_move() is the most important method of the MovePicker class. It returns /// a new pseudo legal move every time it is called, until there are no more moves /// left. It picks the move with the biggest value from a list of generated moves /// taking care not to return the ttMove if it has already been searched. -Move MovePicker::next_move() { +Move MovePicker::next_move(bool skipQuiets) { Move move; @@ -194,6 +171,7 @@ Move MovePicker::next_move() { endMoves = generate(pos, cur); score(); ++stage; + /* fallthrough */ case GOOD_CAPTURES: while (cur < endMoves) @@ -201,7 +179,7 @@ Move MovePicker::next_move() { move = pick_best(cur++, endMoves); if (move != ttMove) { - if (pos.see_ge(move, VALUE_ZERO)) + if (pos.see_ge(move)) return move; // Losing capture, move it to the beginning of the array @@ -210,58 +188,59 @@ Move MovePicker::next_move() { } ++stage; - move = ss->killers[0]; // First killer move + move = killers[0]; // First killer move if ( move != MOVE_NONE && move != ttMove && pos.pseudo_legal(move) && !pos.capture(move)) return move; + /* fallthrough */ case KILLERS: ++stage; - move = ss->killers[1]; // Second killer move + move = killers[1]; // Second killer move if ( move != MOVE_NONE && move != ttMove && pos.pseudo_legal(move) && !pos.capture(move)) return move; + /* fallthrough */ case COUNTERMOVE: ++stage; move = countermove; if ( move != MOVE_NONE && move != ttMove - && move != ss->killers[0] - && move != ss->killers[1] + && move != killers[0] + && move != killers[1] && pos.pseudo_legal(move) && !pos.capture(move)) return move; + /* fallthrough */ case QUIET_INIT: cur = endBadCaptures; endMoves = generate(pos, cur); score(); - if (depth < 3 * ONE_PLY) - { - ExtMove* goodQuiet = std::partition(cur, endMoves, [](const ExtMove& m) - { return m.value > VALUE_ZERO; }); - insertion_sort(cur, goodQuiet); - } else - insertion_sort(cur, endMoves); + partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY); ++stage; + /* fallthrough */ case QUIET: - while (cur < endMoves) + while ( cur < endMoves + && (!skipQuiets || cur->value >= VALUE_ZERO)) { move = *cur++; + if ( move != ttMove - && move != ss->killers[0] - && move != ss->killers[1] + && move != killers[0] + && move != killers[1] && move != countermove) return move; } ++stage; cur = moves; // Point to beginning of bad captures + /* fallthrough */ case BAD_CAPTURES: if (cur < endBadCaptures) @@ -273,6 +252,7 @@ Move MovePicker::next_move() { endMoves = generate(pos, cur); score(); ++stage; + /* fallthrough */ case ALL_EVASIONS: while (cur < endMoves) @@ -288,13 +268,14 @@ Move MovePicker::next_move() { endMoves = generate(pos, cur); score(); ++stage; + /* fallthrough */ case PROBCUT_CAPTURES: while (cur < endMoves) { move = pick_best(cur++, endMoves); if ( move != ttMove - && pos.see_ge(move, threshold + 1)) + && pos.see_ge(move, threshold)) return move; } break; @@ -304,6 +285,7 @@ Move MovePicker::next_move() { endMoves = generate(pos, cur); score(); ++stage; + /* fallthrough */ case QCAPTURES_1: case QCAPTURES_2: while (cur < endMoves) @@ -317,6 +299,7 @@ Move MovePicker::next_move() { cur = moves; endMoves = generate(pos, cur); ++stage; + /* fallthrough */ case QCHECKS: while (cur < endMoves) @@ -332,6 +315,7 @@ Move MovePicker::next_move() { endMoves = generate(pos, cur); score(); ++stage; + /* fallthrough */ case QRECAPTURES: while (cur < endMoves) diff --git a/DroidFish/jni/stockfish/movepick.h b/DroidFish/jni/stockfish/movepick.h index 6fbd8be..66fc1bd 100644 --- a/DroidFish/jni/stockfish/movepick.h +++ b/DroidFish/jni/stockfish/movepick.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,68 +21,66 @@ #ifndef MOVEPICK_H_INCLUDED #define MOVEPICK_H_INCLUDED -#include // For std::max -#include // For std::memset +#include #include "movegen.h" #include "position.h" #include "types.h" +/// StatBoards is a generic 2-dimensional array used to store various statistics +template +struct StatBoards : public std::array, Size1> { -/// The Stats struct stores moves statistics. According to the template parameter -/// the class can store History and Countermoves. History records how often -/// different moves have been successful or unsuccessful during the current search -/// and is used for reduction and move ordering decisions. -/// Countermoves store the move that refute a previous one. Entries are stored -/// using only the moving piece and destination square, hence two moves with -/// different origin but same destination and piece will be considered identical. -template -struct Stats { - - static const Value Max = Value(1 << 28); - - const T* operator[](Piece pc) const { return table[pc]; } - T* operator[](Piece pc) { return table[pc]; } - void clear() { std::memset(table, 0, sizeof(table)); } - void update(Piece pc, Square to, Move m) { table[pc][to] = m; } - void update(Piece pc, Square to, Value v) { - - if (abs(int(v)) >= 324) - return; - - table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 936 : 324); - table[pc][to] += int(v) * 32; + void fill(const T& v) { + T* p = &(*this)[0][0]; + std::fill(p, p + sizeof(*this) / sizeof(*p), v); } -private: - T table[PIECE_NB][SQUARE_NB]; -}; + void update(T& entry, int bonus, const int D) { -typedef Stats MoveStats; -typedef Stats HistoryStats; -typedef Stats CounterMoveStats; -typedef Stats CounterMoveHistoryStats; + assert(abs(bonus) <= D); // Ensure range is [-32 * D, 32 * D] + assert(abs(32 * D) < INT16_MAX); // Ensure we don't overflow -struct FromToStats { + entry += bonus * 32 - entry * abs(bonus) / D; - Value get(Color c, Move m) const { return table[c][from_sq(m)][to_sq(m)]; } - void clear() { std::memset(table, 0, sizeof(table)); } - void update(Color c, Move m, Value v) { - - if (abs(int(v)) >= 324) - return; - - Square from = from_sq(m); - Square to = to_sq(m); - - table[c][from][to] -= table[c][from][to] * abs(int(v)) / 324; - table[c][from][to] += int(v) * 32; + assert(abs(entry) <= 32 * D); } - -private: - Value table[COLOR_NB][SQUARE_NB][SQUARE_NB]; }; +/// ButterflyBoards are 2 tables (one for each color) indexed by the move's from +/// and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards +typedef StatBoards ButterflyBoards; + +/// PieceToBoards are addressed by a move's [piece][to] information +typedef StatBoards PieceToBoards; + +/// ButterflyHistory records how often quiet moves have been successful or +/// unsuccessful during the current search, and is used for reduction and move +/// ordering decisions. It uses ButterflyBoards as backing store. +struct ButterflyHistory : public ButterflyBoards { + + void update(Color c, Move m, int bonus) { + StatBoards::update((*this)[c][from_to(m)], bonus, 324); + } +}; + +/// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards +struct PieceToHistory : public PieceToBoards { + + void update(Piece pc, Square to, int bonus) { + StatBoards::update((*this)[pc][to], bonus, 936); + } +}; + +/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous +/// move, see chessprogramming.wikispaces.com/Countermove+Heuristic +typedef StatBoards CounterMoveHistory; + +/// ContinuationHistory is the history of a given pair of moves, usually the +/// current one given a previous one. History table is based on PieceToBoards +/// instead of ButterflyBoards. +typedef StatBoards ContinuationHistory; + /// MovePicker class is used to pick one pseudo legal move at a time from the /// current position. The most important method is next_move(), which returns a @@ -90,18 +88,15 @@ private: /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha /// beta algorithm, MovePicker attempts to return the moves which are most likely /// to get a cut-off first. -namespace Search { struct Stack; } class MovePicker { public: MovePicker(const MovePicker&) = delete; MovePicker& operator=(const MovePicker&) = delete; - MovePicker(const Position&, Move, Value); - MovePicker(const Position&, Move, Depth, Square); - MovePicker(const Position&, Move, Depth, Search::Stack*); - - Move next_move(); + MovePicker(const Position&, Move, Depth, const ButterflyHistory*, Square); + MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const PieceToHistory**, Move, Move*); + Move next_move(bool skipQuiets = false); private: template void score(); @@ -109,14 +104,14 @@ private: ExtMove* end() { return endMoves; } const Position& pos; - const Search::Stack* ss; - Move countermove; - Depth depth; - Move ttMove; + const ButterflyHistory* mainHistory; + const PieceToHistory** contHistory; + Move ttMove, countermove, killers[2]; + ExtMove *cur, *endMoves, *endBadCaptures; + int stage; Square recaptureSquare; Value threshold; - int stage; - ExtMove *cur, *endMoves, *endBadCaptures; + Depth depth; ExtMove moves[MAX_MOVES]; }; diff --git a/DroidFish/jni/stockfish/pawns.cpp b/DroidFish/jni/stockfish/pawns.cpp index 80f5e49..aa36b1e 100644 --- a/DroidFish/jni/stockfish/pawns.cpp +++ b/DroidFish/jni/stockfish/pawns.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,19 +32,16 @@ namespace { #define S(mg, eg) make_score(mg, eg) // Isolated pawn penalty by opposed flag - const Score Isolated[2] = { S(45, 40), S(30, 27) }; + const Score Isolated[] = { S(27, 30), S(13, 18) }; // Backward pawn penalty by opposed flag - const Score Backward[2] = { S(56, 33), S(41, 19) }; + const Score Backward[] = { S(40, 26), S(24, 12) }; - // Unsupported pawn penalty for pawns which are neither isolated or backward - const Score Unsupported = S(17, 8); - - // Connected pawn bonus by opposed, phalanx, twice supported and rank - Score Connected[2][2][2][RANK_NB]; + // Connected pawn bonus by opposed, phalanx, #support and rank + Score Connected[2][2][3][RANK_NB]; // Doubled pawn penalty - const Score Doubled = S(18,38); + const Score Doubled = S(18, 38); // Lever bonus by rank const Score Lever[RANK_NB] = { @@ -52,32 +49,39 @@ namespace { S(17, 16), S(33, 32), S(0, 0), S(0, 0) }; - // Weakness of our pawn shelter in front of the king by [distance from edge][rank] - const Value ShelterWeakness[][RANK_NB] = { - { V( 97), V(21), V(26), V(51), V(87), V( 89), V( 99) }, - { V(120), V( 0), V(28), V(76), V(88), V(103), V(104) }, - { V(101), V( 7), V(54), V(78), V(77), V( 92), V(101) }, - { V( 80), V(11), V(44), V(68), V(87), V( 90), V(119) } + // Weakness of our pawn shelter in front of the king by [isKingFile][distance from edge][rank]. + // RANK_1 = 0 is used for files where we have no pawns or our pawn is behind our king. + const Value ShelterWeakness[][int(FILE_NB) / 2][RANK_NB] = { + { { V( 97), V(17), V( 9), V(44), V( 84), V( 87), V( 99) }, // Not On King file + { V(106), V( 6), V(33), V(86), V( 87), V(104), V(112) }, + { V(101), V( 2), V(65), V(98), V( 58), V( 89), V(115) }, + { V( 73), V( 7), V(54), V(73), V( 84), V( 83), V(111) } }, + { { V(104), V(20), V( 6), V(27), V( 86), V( 93), V( 82) }, // On King file + { V(123), V( 9), V(34), V(96), V(112), V( 88), V( 75) }, + { V(120), V(25), V(65), V(91), V( 66), V( 78), V(117) }, + { V( 81), V( 2), V(47), V(63), V( 94), V( 93), V(104) } } }; - // Danger of enemy pawns moving toward our king by [type][distance from edge][rank] + // Danger of enemy pawns moving toward our king by [type][distance from edge][rank]. + // For the unopposed and unblocked cases, RANK_1 = 0 is used when opponent has + // no pawn on the given file, or their pawn is behind our king. const Value StormDanger[][4][RANK_NB] = { - { { V( 0), V( 67), V( 134), V(38), V(32) }, - { V( 0), V( 57), V( 139), V(37), V(22) }, - { V( 0), V( 43), V( 115), V(43), V(27) }, - { V( 0), V( 68), V( 124), V(57), V(32) } }, - { { V(20), V( 43), V( 100), V(56), V(20) }, - { V(23), V( 20), V( 98), V(40), V(15) }, - { V(23), V( 39), V( 103), V(36), V(18) }, - { V(28), V( 19), V( 108), V(42), V(26) } }, - { { V( 0), V( 0), V( 75), V(14), V( 2) }, - { V( 0), V( 0), V( 150), V(30), V( 4) }, - { V( 0), V( 0), V( 160), V(22), V( 5) }, - { V( 0), V( 0), V( 166), V(24), V(13) } }, - { { V( 0), V(-283), V(-281), V(57), V(31) }, - { V( 0), V( 58), V( 141), V(39), V(18) }, - { V( 0), V( 65), V( 142), V(48), V(32) }, - { V( 0), V( 60), V( 126), V(51), V(19) } } + { { V( 0), V(-290), V(-274), V(57), V(41) }, // BlockedByKing + { V( 0), V( 60), V( 144), V(39), V(13) }, + { V( 0), V( 65), V( 141), V(41), V(34) }, + { V( 0), V( 53), V( 127), V(56), V(14) } }, + { { V( 4), V( 73), V( 132), V(46), V(31) }, // Unopposed + { V( 1), V( 64), V( 143), V(26), V(13) }, + { V( 1), V( 47), V( 110), V(44), V(24) }, + { V( 0), V( 72), V( 127), V(50), V(31) } }, + { { V( 0), V( 0), V( 79), V(23), V( 1) }, // BlockedByPawn + { V( 0), V( 0), V( 148), V(27), V( 2) }, + { V( 0), V( 0), V( 161), V(16), V( 1) }, + { V( 0), V( 0), V( 171), V(22), V(15) } }, + { { V(22), V( 45), V( 104), V(62), V( 6) }, // Unblocked + { V(31), V( 30), V( 99), V(39), V(19) }, + { V(23), V( 29), V( 96), V(41), V(15) }, + { V(21), V( 23), V( 116), V(41), V(15) } } }; // Max bonus for king safety. Corresponds to start position with all the pawns @@ -96,13 +100,13 @@ namespace { const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST); Bitboard b, neighbours, stoppers, doubled, supported, phalanx; + Bitboard lever, leverPush; Square s; - bool opposed, lever, connected, backward; + bool opposed, backward; Score score = SCORE_ZERO; const Square* pl = pos.squares(Us); - const Bitboard* pawnAttacksBB = StepAttacksBB[make_piece(Us, PAWN)]; - Bitboard ourPawns = pos.pieces(Us , PAWN); + Bitboard ourPawns = pos.pieces( Us, PAWN); Bitboard theirPawns = pos.pieces(Them, PAWN); e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0; @@ -123,14 +127,14 @@ namespace { e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s); // Flag the pawn - opposed = theirPawns & forward_bb(Us, s); + opposed = theirPawns & forward_file_bb(Us, s); stoppers = theirPawns & passed_pawn_mask(Us, s); - lever = theirPawns & pawnAttacksBB[s]; - doubled = ourPawns & (s + Up); + lever = theirPawns & PawnAttacks[Us][s]; + leverPush = theirPawns & PawnAttacks[Us][s + Up]; + doubled = ourPawns & (s - Up); neighbours = ourPawns & adjacent_files_bb(f); phalanx = neighbours & rank_bb(s); supported = neighbours & rank_bb(s - Up); - connected = supported | phalanx; // A pawn is backward when it is behind all pawns of the same color on the // adjacent files and cannot be safely advanced. @@ -146,28 +150,39 @@ namespace { // stopper on adjacent file which controls the way to that rank. backward = (b | shift(b & adjacent_files_bb(f))) & stoppers; - assert(!backward || !(pawn_attack_span(Them, s + Up) & neighbours)); + assert(!(backward && (forward_ranks_bb(Them, s + Up) & neighbours))); } // Passed pawns will be properly scored in evaluation because we need - // full attack info to evaluate them. - if (!stoppers && !(ourPawns & forward_bb(Us, s))) + // full attack info to evaluate them. Include also not passed pawns + // which could become passed after one or two pawn pushes when are + // not attacked more times than defended. + if ( !(stoppers ^ lever ^ leverPush) + && !(ourPawns & forward_file_bb(Us, s)) + && popcount(supported) >= popcount(lever) + && popcount(phalanx) >= popcount(leverPush)) e->passedPawns[Us] |= s; + else if ( stoppers == SquareBB[s + Up] + && relative_rank(Us, s) >= RANK_5) + { + b = shift(supported) & ~theirPawns; + while (b) + if (!more_than_one(theirPawns & PawnAttacks[Us][pop_lsb(&b)])) + e->passedPawns[Us] |= s; + } + // Score this pawn - if (!neighbours) + if (supported | phalanx) + score += Connected[opposed][!!phalanx][popcount(supported)][relative_rank(Us, s)]; + + else if (!neighbours) score -= Isolated[opposed]; else if (backward) score -= Backward[opposed]; - else if (!supported) - score -= Unsupported; - - if (connected) - score += Connected[opposed][!!phalanx][more_than_one(supported)][relative_rank(Us, s)]; - - if (doubled) + if (doubled && !supported) score -= Doubled; if (lever) @@ -187,16 +202,17 @@ namespace Pawns { void init() { - static const int Seed[RANK_NB] = { 0, 8, 19, 13, 71, 94, 169, 324 }; + static const int Seed[RANK_NB] = { 0, 13, 24, 18, 76, 100, 175, 330 }; for (int opposed = 0; opposed <= 1; ++opposed) for (int phalanx = 0; phalanx <= 1; ++phalanx) - for (int apex = 0; apex <= 1; ++apex) + for (int support = 0; support <= 2; ++support) for (Rank r = RANK_2; r < RANK_8; ++r) { - int v = (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed; - v += (apex ? v / 2 : 0); - Connected[opposed][phalanx][apex][r] = make_score(v, v * 5 / 8); + int v = 17 * support; + v += (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed; + + Connected[opposed][phalanx][support][r] = make_score(v, v * (r - 2) / 4); } } @@ -223,16 +239,16 @@ Entry* probe(const Position& pos) { /// Entry::shelter_storm() calculates shelter and storm penalties for the file -/// the king is on, as well as the two adjacent files. +/// the king is on, as well as the two closest files. template Value Entry::shelter_storm(const Position& pos, Square ksq) { const Color Them = (Us == WHITE ? BLACK : WHITE); - enum { NoFriendlyPawn, Unblocked, BlockedByPawn, BlockedByKing }; + enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked }; - Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq)); + Bitboard b = pos.pieces(PAWN) & (forward_ranks_bb(Us, ksq) | rank_bb(ksq)); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); Value safety = MaxSafetyBonus; @@ -243,15 +259,16 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) { b = ourPawns & file_bb(f); Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1; - b = theirPawns & file_bb(f); + b = theirPawns & file_bb(f); Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1; - safety -= ShelterWeakness[std::min(f, FILE_H - f)][rkUs] + int d = std::min(f, FILE_H - f); + safety -= ShelterWeakness[f == file_of(ksq)][d][rkUs] + StormDanger [f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing : - rkUs == RANK_1 ? NoFriendlyPawn : + rkUs == RANK_1 ? Unopposed : rkThem == rkUs + 1 ? BlockedByPawn : Unblocked] - [std::min(f, FILE_H - f)][rkThem]; + [d][rkThem]; } return safety; diff --git a/DroidFish/jni/stockfish/pawns.h b/DroidFish/jni/stockfish/pawns.h index e26ae67..15b0b77 100644 --- a/DroidFish/jni/stockfish/pawns.h +++ b/DroidFish/jni/stockfish/pawns.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/DroidFish/jni/stockfish/position.cpp b/DroidFish/jni/stockfish/position.cpp index c09a953..b6dc31b 100644 --- a/DroidFish/jni/stockfish/position.cpp +++ b/DroidFish/jni/stockfish/position.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,6 +32,7 @@ #include "thread.h" #include "tt.h" #include "uci.h" +#include "syzygy/tbprobe.h" using std::string; @@ -44,14 +45,17 @@ namespace Zobrist { Key psq[PIECE_NB][SQUARE_NB]; Key enpassant[FILE_NB]; Key castling[CASTLING_RIGHT_NB]; - Key side; + Key side, noPawns; } namespace { const string PieceToChar(" PNBRQK pnbrqk"); -// min_attacker() is a helper function used by see() to locate the least +const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, + B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING }; + +// min_attacker() is a helper function used by see_ge() to locate the least // valuable attacker for the side to move, remove the attacker we just found // from the bitboards and scan for new X-ray attacks behind it. @@ -61,7 +65,7 @@ PieceType min_attacker(const Bitboard* bb, Square to, Bitboard stmAttackers, Bitboard b = stmAttackers & bb[Pt]; if (!b) - return min_attacker(bb, to, stmAttackers, occupied, attackers); + return min_attacker(bb, to, stmAttackers, occupied, attackers); occupied ^= b & ~(b - 1); @@ -98,11 +102,25 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) { } os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase - << std::setfill('0') << std::setw(16) << pos.key() << std::dec << "\nCheckers: "; + << std::setfill('0') << std::setw(16) << pos.key() + << std::setfill(' ') << std::dec << "\nCheckers: "; for (Bitboard b = pos.checkers(); b; ) os << UCI::square(pop_lsb(&b)) << " "; + if ( int(Tablebases::MaxCardinality) >= popcount(pos.pieces()) + && !pos.can_castle(ANY_CASTLING)) + { + StateInfo st; + Position p; + p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread()); + Tablebases::ProbeState s1, s2; + Tablebases::WDLScore wdl = Tablebases::probe_wdl(p, &s1); + int dtz = Tablebases::probe_dtz(p, &s2); + os << "\nTablebases WDL: " << std::setw(4) << wdl << " (" << s1 << ")" + << "\nTablebases DTZ: " << std::setw(4) << dtz << " (" << s2 << ")"; + } + return os; } @@ -133,6 +151,7 @@ void Position::init() { } Zobrist::side = rng.rand(); + Zobrist::noPawns = rng.rand(); } @@ -164,8 +183,9 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th 4) En passant target square (in algebraic notation). If there's no en passant target square, this is "-". If a pawn has just made a 2-square move, this - is the position "behind" the pawn. This is recorded regardless of whether - there is a pawn in position to make an en passant capture. + is the position "behind" the pawn. This is recorded only if there is a pawn + in position to make an en passant capture, and if there really is a pawn + that might have advanced two squares. 5) Halfmove clock. This is the number of halfmoves since the last pawn advance or capture. This is used to determine if a draw can be claimed under the @@ -242,7 +262,8 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th { st->epSquare = make_square(File(col - 'a'), Rank(row - '1')); - if (!(attackers_to(st->epSquare) & pieces(sideToMove, PAWN))) + if ( !(attackers_to(st->epSquare) & pieces(sideToMove, PAWN)) + || !(pieces(~sideToMove, PAWN) & (st->epSquare + pawn_push(~sideToMove)))) st->epSquare = SQ_NONE; } else @@ -317,7 +338,8 @@ void Position::set_check_info(StateInfo* si) const { void Position::set_state(StateInfo* si) const { - si->key = si->pawnKey = si->materialKey = 0; + si->key = si->materialKey = 0; + si->pawnKey = Zobrist::noPawns; si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO; si->psq = SCORE_ZERO; si->checkersBB = attackers_to(square(sideToMove)) & pieces(~sideToMove); @@ -357,6 +379,27 @@ void Position::set_state(StateInfo* si) const { } +/// Position::set() is an overload to initialize the position object with +/// the given endgame code string like "KBPKN". It is mainly a helper to +/// get the material key out of an endgame code. + +Position& Position::set(const string& code, Color c, StateInfo* si) { + + assert(code.length() > 0 && code.length() < 8); + assert(code[0] == 'K'); + + string sides[] = { code.substr(code.find('K', 1)), // Weak + code.substr(0, code.find('K', 1)) }; // Strong + + std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower); + + string fenStr = "8/" + sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/" + + sides[1] + char(8 - sides[1].length() + '0') + "/8 w - - 0 10"; + + return set(fenStr, false, si, nullptr); +} + + /// Position::fen() returns a FEN representation of the position. In case of /// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function. @@ -407,19 +450,6 @@ const string Position::fen() const { } -/// Position::game_phase() calculates the game phase interpolating total non-pawn -/// material between endgame and midgame limits. - -Phase Position::game_phase() const { - - Value npm = st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK]; - - npm = std::max(EndgameLimit, std::min(npm, MidgameLimit)); - - return Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit)); -} - - /// Position::slider_blockers() returns a bitboard of all the pieces (both colors) /// that are blocking attacks on the square 's' from 'sliders'. A piece blocks a /// slider if removing that piece from the board would result in a position where @@ -433,7 +463,7 @@ Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners pinners = 0; // Snipers are sliders that attack 's' when a piece is removed - Bitboard snipers = ( (PseudoAttacks[ROOK ][s] & pieces(QUEEN, ROOK)) + Bitboard snipers = ( (PseudoAttacks[ ROOK][s] & pieces(QUEEN, ROOK)) | (PseudoAttacks[BISHOP][s] & pieces(QUEEN, BISHOP))) & sliders; while (snipers) @@ -460,7 +490,7 @@ Bitboard Position::attackers_to(Square s, Bitboard occupied) const { return (attacks_from(s, BLACK) & pieces(WHITE, PAWN)) | (attacks_from(s, WHITE) & pieces(BLACK, PAWN)) | (attacks_from(s) & pieces(KNIGHT)) - | (attacks_bb(s, occupied) & pieces(ROOK, QUEEN)) + | (attacks_bb< ROOK>(s, occupied) & pieces( ROOK, QUEEN)) | (attacks_bb(s, occupied) & pieces(BISHOP, QUEEN)) | (attacks_from(s) & pieces(KING)); } @@ -554,7 +584,7 @@ bool Position::pseudo_legal(const Move m) const { && empty(to - pawn_push(us)))) return false; } - else if (!(attacks_from(pc, from) & to)) + else if (!(attacks_from(type_of(pc), from) & to)) return false; // Evasions generator already takes care to avoid some kind of illegal moves @@ -607,7 +637,7 @@ bool Position::gives_check(Move m) const { return false; case PROMOTION: - return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & square(~sideToMove); + return attacks_bb(promotion_type(m), to, pieces() ^ from) & square(~sideToMove); // En passant capture with check? We have already handled the case // of direct checks and ordinary discovered check, so the only case we @@ -647,7 +677,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { assert(is_ok(m)); assert(&newSt != st); - ++nodes; + thisThread->nodes.fetch_add(1, std::memory_order_relaxed); Key k = st->key ^ Zobrist::side; // Copy some fields of the old state to our new StateInfo object except the @@ -786,7 +816,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { // Update pawn hash key and prefetch access to pawnsTable st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to]; - prefetch(thisThread->pawnsTable[st->pawnKey]); + prefetch2(thisThread->pawnsTable[st->pawnKey]); // Reset rule 50 draw counter st->rule50 = 0; @@ -956,18 +986,16 @@ Key Position::key_after(Move m) const { /// Position::see_ge (Static Exchange Evaluation Greater or Equal) tests if the -/// SEE value of move is greater or equal to the given value. We'll use an +/// SEE value of move is greater or equal to the given threshold. We'll use an /// algorithm similar to alpha-beta pruning with a null window. -bool Position::see_ge(Move m, Value v) const { +bool Position::see_ge(Move m, Value threshold) const { assert(is_ok(m)); - // Castling moves are implemented as king capturing the rook so cannot be - // handled correctly. Simply assume the SEE value is VALUE_ZERO that is always - // correct unless in the rare case the rook ends up under attack. - if (type_of(m) == CASTLING) - return VALUE_ZERO >= v; + // Only deal with normal moves, assume others pass a simple see + if (type_of(m) != NORMAL) + return VALUE_ZERO >= threshold; Square from = from_sq(m), to = to_sq(m); PieceType nextVictim = type_of(piece_on(from)); @@ -975,30 +1003,18 @@ bool Position::see_ge(Move m, Value v) const { Value balance; // Values of the pieces taken by us minus opponent's ones Bitboard occupied, stmAttackers; - if (type_of(m) == ENPASSANT) - { - occupied = SquareBB[to - pawn_push(~stm)]; // Remove the captured pawn - balance = PieceValue[MG][PAWN]; - } - else - { - balance = PieceValue[MG][piece_on(to)]; - occupied = 0; - } + balance = PieceValue[MG][piece_on(to)]; - if (balance < v) + if (balance < threshold) return false; - if (nextVictim == KING) - return true; - balance -= PieceValue[MG][nextVictim]; - if (balance >= v) + if (balance >= threshold) // Always true if nextVictim == KING return true; bool relativeStm = true; // True if the opponent is to move - occupied ^= pieces() ^ from ^ to; + occupied = pieces() ^ from ^ to; // Find all attackers to the destination square, with the moving piece removed, // but possibly an X-ray attacker added behind it. @@ -1027,7 +1043,7 @@ bool Position::see_ge(Move m, Value v) const { relativeStm = !relativeStm; - if (relativeStm == (balance >= v)) + if (relativeStm == (balance >= threshold)) return relativeStm; stm = ~stm; @@ -1038,18 +1054,29 @@ bool Position::see_ge(Move m, Value v) const { /// Position::is_draw() tests whether the position is drawn by 50-move rule /// or by repetition. It does not detect stalemates. -bool Position::is_draw() const { +bool Position::is_draw(int ply) const { if (st->rule50 > 99 && (!checkers() || MoveList(*this).size())) return true; - StateInfo* stp = st; - for (int i = 2, e = std::min(st->rule50, st->pliesFromNull); i <= e; i += 2) + int end = std::min(st->rule50, st->pliesFromNull); + + if (end < 4) + return false; + + StateInfo* stp = st->previous->previous; + int cnt = 0; + + for (int i = 4; i <= end; i += 2) { stp = stp->previous->previous; - if (stp->key == st->key) - return true; // Draw at first repetition + // At root position ply is 1, so return a draw score if a position + // repeats once earlier but strictly after the root, or repeats twice + // before or at the root. + if ( stp->key == st->key + && ++cnt + (ply - 1 > i) == 2) + return true; } return false; @@ -1091,78 +1118,72 @@ void Position::flip() { } -/// Position::pos_is_ok() performs some consistency checks for the position object. +/// Position::pos_is_ok() performs some consistency checks for the +/// position object and raises an asserts if something wrong is detected. /// This is meant to be helpful when debugging. -bool Position::pos_is_ok(int* failedStep) const { +bool Position::pos_is_ok() const { const bool Fast = true; // Quick (default) or full check? - enum { Default, King, Bitboards, State, Lists, Castling }; + if ( (sideToMove != WHITE && sideToMove != BLACK) + || piece_on(square(WHITE)) != W_KING + || piece_on(square(BLACK)) != B_KING + || ( ep_square() != SQ_NONE + && relative_rank(sideToMove, ep_square()) != RANK_6)) + assert(0 && "pos_is_ok: Default"); - for (int step = Default; step <= (Fast ? Default : Castling); step++) + if (Fast) + return true; + + if ( pieceCount[W_KING] != 1 + || pieceCount[B_KING] != 1 + || attackers_to(square(~sideToMove)) & pieces(sideToMove)) + assert(0 && "pos_is_ok: Kings"); + + if ( (pieces(PAWN) & (Rank1BB | Rank8BB)) + || pieceCount[W_PAWN] > 8 + || pieceCount[B_PAWN] > 8) + assert(0 && "pos_is_ok: Pawns"); + + if ( (pieces(WHITE) & pieces(BLACK)) + || (pieces(WHITE) | pieces(BLACK)) != pieces() + || popcount(pieces(WHITE)) > 16 + || popcount(pieces(BLACK)) > 16) + assert(0 && "pos_is_ok: Bitboards"); + + for (PieceType p1 = PAWN; p1 <= KING; ++p1) + for (PieceType p2 = PAWN; p2 <= KING; ++p2) + if (p1 != p2 && (pieces(p1) & pieces(p2))) + assert(0 && "pos_is_ok: Bitboards"); + + StateInfo si = *st; + set_state(&si); + if (std::memcmp(&si, st, sizeof(StateInfo))) + assert(0 && "pos_is_ok: State"); + + for (Piece pc : Pieces) { - if (failedStep) - *failedStep = step; + if ( pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc))) + || pieceCount[pc] != std::count(board, board + SQUARE_NB, pc)) + assert(0 && "pos_is_ok: Pieces"); - if (step == Default) - if ( (sideToMove != WHITE && sideToMove != BLACK) - || piece_on(square(WHITE)) != W_KING - || piece_on(square(BLACK)) != B_KING - || ( ep_square() != SQ_NONE - && relative_rank(sideToMove, ep_square()) != RANK_6)) - return false; - - if (step == King) - if ( std::count(board, board + SQUARE_NB, W_KING) != 1 - || std::count(board, board + SQUARE_NB, B_KING) != 1 - || attackers_to(square(~sideToMove)) & pieces(sideToMove)) - return false; - - if (step == Bitboards) - { - if ( (pieces(WHITE) & pieces(BLACK)) - ||(pieces(WHITE) | pieces(BLACK)) != pieces()) - return false; - - for (PieceType p1 = PAWN; p1 <= KING; ++p1) - for (PieceType p2 = PAWN; p2 <= KING; ++p2) - if (p1 != p2 && (pieces(p1) & pieces(p2))) - return false; - } - - if (step == State) - { - StateInfo si = *st; - set_state(&si); - if (std::memcmp(&si, st, sizeof(StateInfo))) - return false; - } - - if (step == Lists) - for (Piece pc : Pieces) - { - if (pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc)))) - return false; - - for (int i = 0; i < pieceCount[pc]; ++i) - if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i) - return false; - } - - if (step == Castling) - for (Color c = WHITE; c <= BLACK; ++c) - for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1)) - { - if (!can_castle(c | s)) - continue; - - if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK) - || castlingRightsMask[castlingRookSquare[c | s]] != (c | s) - ||(castlingRightsMask[square(c)] & (c | s)) != (c | s)) - return false; - } + for (int i = 0; i < pieceCount[pc]; ++i) + if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i) + assert(0 && "pos_is_ok: Index"); } + for (Color c = WHITE; c <= BLACK; ++c) + for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1)) + { + if (!can_castle(c | s)) + continue; + + if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK) + || castlingRightsMask[castlingRookSquare[c | s]] != (c | s) + || (castlingRightsMask[square(c)] & (c | s)) != (c | s)) + assert(0 && "pos_is_ok: Castling"); + } + return true; } diff --git a/DroidFish/jni/stockfish/position.h b/DroidFish/jni/stockfish/position.h index 9aa4c44..fa812ef 100644 --- a/DroidFish/jni/stockfish/position.h +++ b/DroidFish/jni/stockfish/position.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -56,7 +56,10 @@ struct StateInfo { Bitboard checkSquares[PIECE_TYPE_NB]; }; -// In a std::deque references to elements are unaffected upon resizing +/// A list to keep track of the position states along the setup moves (from the +/// start position to the position just before the search starts). Needed by +/// 'draw by repetition' detection. Use a std::deque because pointers to +/// elements are not invalidated upon list resizing. typedef std::unique_ptr> StateListPtr; @@ -76,6 +79,7 @@ public: // FEN string input/output Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th); + Position& set(const std::string& code, Color c, StateInfo* si); const std::string fen() const; // Position representation @@ -89,6 +93,7 @@ public: Square ep_square() const; bool empty(Square s) const; template int count(Color c) const; + template int count() const; template const Square* squares(Color c) const; template Square square(Color c) const; @@ -107,7 +112,7 @@ public: // Attacks to/from a given square Bitboard attackers_to(Square s) const; Bitboard attackers_to(Square s, Bitboard occupied) const; - Bitboard attacks_from(Piece pc, Square s) const; + Bitboard attacks_from(PieceType pt, Square s) const; template Bitboard attacks_from(Square s) const; template Bitboard attacks_from(Square s, Color c) const; Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const; @@ -127,13 +132,14 @@ public: bool opposite_bishops() const; // Doing and undoing moves - void do_move(Move m, StateInfo& st, bool givesCheck); + void do_move(Move m, StateInfo& newSt); + void do_move(Move m, StateInfo& newSt, bool givesCheck); void undo_move(Move m); - void do_null_move(StateInfo& st); + void do_null_move(StateInfo& newSt); void undo_null_move(); // Static Exchange Evaluation - bool see_ge(Move m, Value value) const; + bool see_ge(Move m, Value threshold = VALUE_ZERO) const; // Accessing hash keys Key key() const; @@ -143,18 +149,17 @@ public: // Other properties of the position Color side_to_move() const; - Phase game_phase() const; int game_ply() const; bool is_chess960() const; Thread* this_thread() const; - uint64_t nodes_searched() const; - bool is_draw() const; + bool is_draw(int ply) const; int rule50_count() const; Score psq_score() const; Value non_pawn_material(Color c) const; + Value non_pawn_material() const; // Position consistency check, for debugging - bool pos_is_ok(int* failedStep = nullptr) const; + bool pos_is_ok() const; void flip(); private: @@ -180,7 +185,6 @@ private: int castlingRightsMask[SQUARE_NB]; Square castlingRookSquare[CASTLING_RIGHT_NB]; Bitboard castlingPath[CASTLING_RIGHT_NB]; - uint64_t nodes; int gamePly; Color sideToMove; Thread* thisThread; @@ -234,6 +238,10 @@ template inline int Position::count(Color c) const { return pieceCount[make_piece(c, Pt)]; } +template inline int Position::count() const { + return pieceCount[make_piece(WHITE, Pt)] + pieceCount[make_piece(BLACK, Pt)]; +} + template inline const Square* Position::squares(Color c) const { return pieceList[make_piece(c, Pt)]; } @@ -265,18 +273,19 @@ inline Square Position::castling_rook_square(CastlingRight cr) const { template inline Bitboard Position::attacks_from(Square s) const { + assert(Pt != PAWN); return Pt == BISHOP || Pt == ROOK ? attacks_bb(s, byTypeBB[ALL_PIECES]) : Pt == QUEEN ? attacks_from(s) | attacks_from(s) - : StepAttacksBB[Pt][s]; + : PseudoAttacks[Pt][s]; } template<> inline Bitboard Position::attacks_from(Square s, Color c) const { - return StepAttacksBB[make_piece(c, PAWN)][s]; + return PawnAttacks[c][s]; } -inline Bitboard Position::attacks_from(Piece pc, Square s) const { - return attacks_bb(pc, s, byTypeBB[ALL_PIECES]); +inline Bitboard Position::attacks_from(PieceType pt, Square s) const { + return attacks_bb(pt, s, byTypeBB[ALL_PIECES]); } inline Bitboard Position::attackers_to(Square s) const { @@ -328,6 +337,10 @@ inline Value Position::non_pawn_material(Color c) const { return st->nonPawnMaterial[c]; } +inline Value Position::non_pawn_material() const { + return st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK]; +} + inline int Position::game_ply() const { return gamePly; } @@ -336,10 +349,6 @@ inline int Position::rule50_count() const { return st->rule50; } -inline uint64_t Position::nodes_searched() const { - return nodes; -} - inline bool Position::opposite_bishops() const { return pieceCount[W_BISHOP] == 1 && pieceCount[B_BISHOP] == 1 @@ -411,4 +420,8 @@ inline void Position::move_piece(Piece pc, Square from, Square to) { pieceList[pc][index[to]] = to; } +inline void Position::do_move(Move m, StateInfo& newSt) { + do_move(m, newSt, gives_check(m)); +} + #endif // #ifndef POSITION_H_INCLUDED diff --git a/DroidFish/jni/stockfish/psqt.cpp b/DroidFish/jni/stockfish/psqt.cpp index 41a9b49..0d16b1c 100644 --- a/DroidFish/jni/stockfish/psqt.cpp +++ b/DroidFish/jni/stockfish/psqt.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -39,32 +39,32 @@ const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = { { }, { // Pawn { S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) }, - { S(-16, 7), S( 1,-4), S( 7, 8), S( 3,-2) }, - { S(-23,-4), S( -7,-5), S( 19, 5), S(24, 4) }, - { S(-22, 3), S(-14, 3), S( 20,-8), S(35,-3) }, - { S(-11, 8), S( 0, 9), S( 3, 7), S(21,-6) }, - { S(-11, 8), S(-13,-5), S( -6, 2), S(-2, 4) }, - { S( -9, 3), S( 15,-9), S( -8, 1), S(-4,18) } + { S(-11, 7), S( 6,-4), S( 7, 8), S( 3,-2) }, + { S(-18,-4), S( -2,-5), S( 19, 5), S(24, 4) }, + { S(-17, 3), S( -9, 3), S( 20,-8), S(35,-3) }, + { S( -6, 8), S( 5, 9), S( 3, 7), S(21,-6) }, + { S( -6, 8), S( -8,-5), S( -6, 2), S(-2, 4) }, + { S( -4, 3), S( 20,-9), S( -8, 1), S(-4,18) } }, { // Knight - { S(-143, -97), S(-96,-82), S(-80,-46), S(-73,-14) }, - { S( -83, -69), S(-43,-55), S(-21,-17), S(-10, 9) }, - { S( -71, -50), S(-22,-39), S( 0, -8), S( 9, 28) }, - { S( -25, -41), S( 18,-25), S( 43, 7), S( 47, 38) }, - { S( -26, -46), S( 16,-25), S( 38, 2), S( 50, 41) }, - { S( -11, -55), S( 37,-38), S( 56, -8), S( 71, 27) }, - { S( -62, -64), S(-17,-50), S( 5,-24), S( 14, 13) }, - { S(-195,-110), S(-66,-90), S(-42,-50), S(-29,-13) } + { S(-161,-105), S(-96,-82), S(-80,-46), S(-73,-14) }, + { S( -83, -69), S(-43,-54), S(-21,-17), S(-10, 9) }, + { S( -71, -50), S(-22,-39), S( 0, -7), S( 9, 28) }, + { S( -25, -41), S( 18,-25), S( 43, 6), S( 47, 38) }, + { S( -26, -46), S( 16,-25), S( 38, 3), S( 50, 40) }, + { S( -11, -54), S( 37,-38), S( 56, -7), S( 65, 27) }, + { S( -63, -65), S(-19,-50), S( 5,-24), S( 14, 13) }, + { S(-195,-109), S(-67,-89), S(-42,-50), S(-29,-13) } }, { // Bishop - { S(-54,-68), S(-23,-40), S(-35,-46), S(-44,-28) }, - { S(-30,-43), S( 10,-17), S( 2,-23), S( -9, -5) }, - { S(-19,-32), S( 17, -9), S( 11,-13), S( 1, 8) }, - { S(-21,-36), S( 18,-13), S( 11,-15), S( 0, 7) }, - { S(-21,-36), S( 14,-14), S( 6,-17), S( -1, 3) }, - { S(-27,-35), S( 6,-13), S( 2,-10), S( -8, 1) }, - { S(-33,-44), S( 7,-21), S( -4,-22), S(-12, -4) }, - { S(-45,-65), S(-21,-42), S(-29,-46), S(-39,-27) } + { S(-44,-58), S(-13,-31), S(-25,-37), S(-34,-19) }, + { S(-20,-34), S( 20, -9), S( 12,-14), S( 1, 4) }, + { S( -9,-23), S( 27, 0), S( 21, -3), S( 11, 16) }, + { S(-11,-26), S( 28, -3), S( 21, -5), S( 10, 16) }, + { S(-11,-26), S( 27, -4), S( 16, -7), S( 9, 14) }, + { S(-17,-24), S( 16, -2), S( 12, 0), S( 2, 13) }, + { S(-23,-34), S( 17,-10), S( 6,-12), S( -2, 6) }, + { S(-35,-55), S(-11,-32), S(-19,-36), S(-29,-17) } }, { // Rook { S(-25, 0), S(-16, 0), S(-16, 0), S(-9, 0) }, @@ -77,24 +77,24 @@ const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = { { S(-23, 0), S(-15, 0), S(-11, 0), S(-5, 0) } }, { // Queen - { S( 0,-70), S(-3,-57), S(-4,-41), S(-1,-29) }, - { S(-4,-58), S( 6,-30), S( 9,-21), S( 8, -4) }, - { S(-2,-39), S( 6,-17), S( 9, -7), S( 9, 5) }, - { S(-1,-29), S( 8, -5), S(10, 9), S( 7, 17) }, - { S(-3,-27), S( 9, -5), S( 8, 10), S( 7, 23) }, - { S(-2,-40), S( 6,-16), S( 8,-11), S(10, 3) }, - { S(-2,-54), S( 7,-30), S( 7,-21), S( 6, -7) }, - { S(-1,-75), S(-4,-54), S(-1,-44), S( 0,-30) } + { S( 0,-71), S(-4,-56), S(-3,-42), S(-1,-29) }, + { S(-4,-56), S( 6,-30), S( 9,-21), S( 8, -5) }, + { S(-2,-39), S( 6,-17), S( 9, -8), S( 9, 5) }, + { S(-1,-29), S( 8, -5), S(10, 9), S( 7, 19) }, + { S(-3,-27), S( 9, -5), S( 8, 10), S( 7, 21) }, + { S(-2,-40), S( 6,-16), S( 8,-10), S(10, 3) }, + { S(-2,-55), S( 7,-30), S( 7,-21), S( 6, -6) }, + { S(-1,-74), S(-4,-55), S(-1,-43), S( 0,-30) } }, { // King - { S(291, 28), S(344, 76), S(294,103), S(219,112) }, - { S(289, 70), S(329,119), S(263,170), S(205,159) }, - { S(226,109), S(271,164), S(202,195), S(136,191) }, - { S(204,131), S(212,194), S(175,194), S(137,204) }, - { S(177,132), S(205,187), S(143,224), S( 94,227) }, - { S(147,118), S(188,178), S(113,199), S( 70,197) }, - { S(116, 72), S(158,121), S( 93,142), S( 48,161) }, - { S( 94, 30), S(120, 76), S( 78,101), S( 31,111) } + { S(267, 0), S(320, 48), S(270, 75), S(195, 84) }, + { S(264, 43), S(304, 92), S(238,143), S(180,132) }, + { S(200, 83), S(245,138), S(176,167), S(110,165) }, + { S(177,106), S(185,169), S(148,169), S(110,179) }, + { S(149,108), S(177,163), S(115,200), S( 66,203) }, + { S(118, 95), S(159,155), S( 84,176), S( 41,174) }, + { S( 87, 50), S(128, 99), S( 63,122), S( 20,139) }, + { S( 63, 9), S( 88, 55), S( 47, 80), S( 0, 90) } } }; diff --git a/DroidFish/jni/stockfish/search.cpp b/DroidFish/jni/stockfish/search.cpp index 5dfdd7b..9f6b511 100644 --- a/DroidFish/jni/stockfish/search.cpp +++ b/DroidFish/jni/stockfish/search.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -39,7 +39,6 @@ namespace Search { - SignalsType Signals; LimitsType Limits; } @@ -63,8 +62,13 @@ namespace { // Different node types, used as a template parameter enum NodeType { NonPV, PV }; + // Sizes and phases of the skip-blocks, used for distributing search depths across the threads + const int skipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; + const int skipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 }; + // Razoring and futility margin based on depth - const int razor_margin[4] = { 483, 570, 603, 554 }; + // razor_margin[0] is unused as long as depth >= ONE_PLY in search + const int razor_margin[] = { 0, 570, 603, 554 }; Value futility_margin(Depth d) { return Value(150 * d / ONE_PLY); } // Futility and reductions lookup tables, initialized at startup @@ -75,6 +79,12 @@ namespace { return Reductions[PvNode][i][std::min(d / ONE_PLY, 63)][std::min(mn, 63)] * ONE_PLY; } + // History and stats update bonus, based on depth + int stat_bonus(Depth depth) { + int d = depth / ONE_PLY; + return d > 17 ? 0 : d * d + 2 * d - 2; + } + // Skill structure is used to implement strength limit struct Skill { Skill(int l) : level(l) {} @@ -87,8 +97,8 @@ namespace { Move best = MOVE_NONE; }; - // EasyMoveManager structure is used to detect an 'easy move'. When the PV is - // stable across multiple search iterations, we can quickly return the best move. + // EasyMoveManager structure is used to detect an 'easy move'. When the PV is stable + // across multiple search iterations, we can quickly return the best move. struct EasyMoveManager { void clear() { @@ -113,63 +123,59 @@ namespace { 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]); } } - int stableCnt; Key expectedPosKey; + int stableCnt; Move pv[3]; }; - // Set of rows with half bits set to 1 and half to 0. It is used to allocate - // the search depths across the threads. - typedef std::vector Row; - - const Row HalfDensity[] = { - {0, 1}, - {1, 0}, - {0, 0, 1, 1}, - {0, 1, 1, 0}, - {1, 1, 0, 0}, - {1, 0, 0, 1}, - {0, 0, 0, 1, 1, 1}, - {0, 0, 1, 1, 1, 0}, - {0, 1, 1, 1, 0, 0}, - {1, 1, 1, 0, 0, 0}, - {1, 1, 0, 0, 0, 1}, - {1, 0, 0, 0, 1, 1}, - {0, 0, 0, 0, 1, 1, 1, 1}, - {0, 0, 0, 1, 1, 1, 1, 0}, - {0, 0, 1, 1, 1, 1, 0 ,0}, - {0, 1, 1, 1, 1, 0, 0 ,0}, - {1, 1, 1, 1, 0, 0, 0 ,0}, - {1, 1, 1, 0, 0, 0, 0 ,1}, - {1, 1, 0, 0, 0, 0, 1 ,1}, - {1, 0, 0, 0, 0, 1, 1 ,1}, - }; - - const size_t HalfDensitySize = std::extent::value; - EasyMoveManager EasyMove; Value DrawValue[COLOR_NB]; template - Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode); + Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode, bool skipEarlyPruning); template - Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth); + Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth = DEPTH_ZERO); Value value_to_tt(Value v, int ply); Value value_from_tt(Value v, int ply); void update_pv(Move* pv, Move move, Move* childPv); - void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus); - void update_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, Value bonus); - void check_time(); + void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus); + void update_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, int bonus); + + // perft() is our utility to verify move generation. All the leaf nodes up + // to the given depth are generated and counted, and the sum is returned. + template + uint64_t perft(Position& pos, Depth depth) { + + StateInfo st; + uint64_t cnt, nodes = 0; + const bool leaf = (depth == 2 * ONE_PLY); + + for (const auto& m : MoveList(pos)) + { + if (Root && depth <= ONE_PLY) + cnt = 1, nodes++; + else + { + pos.do_move(m, st); + cnt = leaf ? MoveList(pos).size() : perft(pos, depth - ONE_PLY); + nodes += cnt; + pos.undo_move(m); + } + if (Root) + sync_cout << UCI::move(m, pos.is_chess960()) << ": " << cnt << sync_endl; + } + return nodes; + } } // namespace @@ -182,9 +188,7 @@ void Search::init() { for (int d = 1; d < 64; ++d) for (int mc = 1; mc < 64; ++mc) { - double r = log(d) * log(mc) / 2; - if (r < 0.80) - continue; + double r = log(d) * log(mc) / 1.95; Reductions[NonPV][imp][d][mc] = int(round(r)); Reductions[PV][imp][d][mc] = std::max(Reductions[NonPV][imp][d][mc] - 1, 0); @@ -196,66 +200,44 @@ void Search::init() { for (int d = 0; d < 16; ++d) { - FutilityMoveCounts[0][d] = int(2.4 + 0.773 * pow(d + 0.00, 1.8)); - FutilityMoveCounts[1][d] = int(2.9 + 1.045 * pow(d + 0.49, 1.8)); + FutilityMoveCounts[0][d] = int(2.4 + 0.74 * pow(d, 1.78)); + FutilityMoveCounts[1][d] = int(5.0 + 1.00 * pow(d, 2.00)); } } -/// Search::clear() resets search state to zero, to obtain reproducible results +/// Search::clear() resets search state to its initial value void Search::clear() { + Threads.main()->wait_for_search_finished(); + + Time.availableNodes = 0; TT.clear(); for (Thread* th : Threads) - { - th->history.clear(); - th->counterMoves.clear(); - th->fromTo.clear(); - th->counterMoveHistory.clear(); - } + th->clear(); + Threads.main()->callsCnt = 0; Threads.main()->previousScore = VALUE_INFINITE; } -/// Search::perft() is our utility to verify move generation. All the leaf nodes -/// up to the given depth are generated and counted, and the sum is returned. -template -uint64_t Search::perft(Position& pos, Depth depth) { - - StateInfo st; - uint64_t cnt, nodes = 0; - const bool leaf = (depth == 2 * ONE_PLY); - - for (const auto& m : MoveList(pos)) - { - if (Root && depth <= ONE_PLY) - cnt = 1, nodes++; - else - { - pos.do_move(m, st, pos.gives_check(m)); - cnt = leaf ? MoveList(pos).size() : perft(pos, depth - ONE_PLY); - nodes += cnt; - pos.undo_move(m); - } - if (Root) - sync_cout << UCI::move(m, pos.is_chess960()) << ": " << cnt << sync_endl; - } - return nodes; -} - -template uint64_t Search::perft(Position&, Depth); - - /// MainThread::search() is called by the main thread when the program receives /// the UCI 'go' command. It searches from the root position and outputs the "bestmove". void MainThread::search() { + if (Limits.perft) + { + nodes = perft(rootPos, Limits.perft * ONE_PLY); + sync_cout << "\nNodes searched: " << nodes << "\n" << sync_endl; + return; + } + Color us = rootPos.side_to_move(); Time.init(Limits, us, rootPos.game_ply()); + TT.new_search(); int contempt = Options["Contempt"] * PawnValueEg / 100; // From centipawns DrawValue[ us] = VALUE_DRAW - Value(contempt); @@ -263,7 +245,7 @@ void MainThread::search() { if (rootMoves.empty()) { - rootMoves.push_back(RootMove(MOVE_NONE)); + rootMoves.emplace_back(MOVE_NONE); sync_cout << "info depth 0 score " << UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW) << sync_endl; @@ -283,18 +265,18 @@ void MainThread::search() { Time.availableNodes += Limits.inc[us] - Threads.nodes_searched(); // When we reach the maximum depth, we can arrive here without a raise of - // Signals.stop. However, if we are pondering or in an infinite search, + // Threads.stop. However, if we are pondering or in an infinite search, // the UCI protocol states that we shouldn't print the best move before the // GUI sends a "stop" or "ponderhit" command. We therefore simply wait here - // until the GUI sends one of those commands (which also raises Signals.stop). - if (!Signals.stop && (Limits.ponder || Limits.infinite)) - { - Signals.stopOnPonderhit = true; - wait(Signals.stop); - } + // until the GUI sends one of those commands (which also raises Threads.stop). + Threads.stopOnPonderhit = true; - // Stop the threads if not already stopped - Signals.stop = true; + while (!Threads.stop && (Threads.ponder || Limits.infinite)) + {} // Busy wait for a stop or a ponder reset + + // Stop the threads if not already stopped (also raise the stop if + // "ponderhit" just reset Threads.ponder). + Threads.stop = true; // Wait until all threads have finished for (Thread* th : Threads) @@ -310,9 +292,15 @@ void MainThread::search() { && rootMoves[0].pv[0] != MOVE_NONE) { for (Thread* th : Threads) - if ( th->completedDepth > bestThread->completedDepth - && th->rootMoves[0].score > bestThread->rootMoves[0].score) + { + Depth depthDiff = th->completedDepth - bestThread->completedDepth; + Value scoreDiff = th->rootMoves[0].score - bestThread->rootMoves[0].score; + + // Select the thread with the best score, always if it is a mate + if ( scoreDiff > 0 + && (depthDiff >= 0 || th->rootMoves[0].score >= VALUE_MATE_IN_MAX_PLY)) bestThread = th; + } } previousScore = bestThread->rootMoves[0].score; @@ -330,22 +318,23 @@ void MainThread::search() { } -// Thread::search() is the main iterative deepening loop. It calls search() -// repeatedly with increasing depth until the allocated thinking time has been -// consumed, the user stops the search, or the maximum search depth is reached. +/// Thread::search() is the main iterative deepening loop. It calls search() +/// repeatedly with increasing depth until the allocated thinking time has been +/// consumed, the user stops the search, or the maximum search depth is reached. 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)); + for (int i = 4; i > 0; i--) + (ss-i)->contHistory = &this->contHistory[NO_PIECE][0]; // Use as sentinel bestValue = delta = alpha = -VALUE_INFINITE; beta = VALUE_INFINITE; - completedDepth = DEPTH_ZERO; if (mainThread) { @@ -353,7 +342,6 @@ void Thread::search() { EasyMove.clear(); mainThread->easyMovePlayed = mainThread->failedLow = false; mainThread->bestMoveChanges = 0; - TT.new_search(); } size_t multiPV = Options["MultiPV"]; @@ -368,16 +356,15 @@ void Thread::search() { // Iterative deepening loop until requested to stop or the target depth is reached while ( (rootDepth += ONE_PLY) < DEPTH_MAX - && !Signals.stop - && (!Limits.depth || Threads.main()->rootDepth / ONE_PLY <= Limits.depth)) + && !Threads.stop + && !(Limits.depth && mainThread && rootDepth / ONE_PLY > Limits.depth)) { - // Set up the new depths for the helper threads skipping on average every - // 2nd ply (using a half-density matrix). - if (!mainThread) + // Distribute search depths across the threads + if (idx) { - const Row& row = HalfDensity[(idx - 1) % HalfDensitySize]; - if (row[(rootDepth / ONE_PLY + rootPos.game_ply()) % row.size()]) - continue; + int i = (idx - 1) % 20; + if (((rootDepth / ONE_PLY + rootPos.game_ply() + skipPhase[i]) / skipSize[i]) % 2) + continue; } // Age out PV variability metric @@ -390,8 +377,11 @@ void Thread::search() { rm.previousScore = rm.score; // MultiPV loop. We perform a full root search for each PV line - for (PVIdx = 0; PVIdx < multiPV && !Signals.stop; ++PVIdx) + for (PVIdx = 0; PVIdx < multiPV && !Threads.stop; ++PVIdx) { + // Reset UCI info selDepth for each depth and each PV line + selDepth = 0; + // Reset aspiration window starting size if (rootDepth >= 5 * ONE_PLY) { @@ -405,7 +395,7 @@ void Thread::search() { // high/low anymore. while (true) { - bestValue = ::search(rootPos, ss, alpha, beta, rootDepth, false); + bestValue = ::search(rootPos, ss, alpha, beta, rootDepth, false, false); // Bring the best move to the front. It is critical that sorting // is done with a stable algorithm because all the values but the @@ -415,10 +405,10 @@ void Thread::search() { // search the already searched PV lines are preserved. std::stable_sort(rootMoves.begin() + PVIdx, rootMoves.end()); - // If search has been stopped, break immediately. Sorting and + // If search has been stopped, we break immediately. Sorting and // writing PV back to TT is safe because RootMoves is still // valid, although it refers to the previous iteration. - if (Signals.stop) + if (Threads.stop) break; // When failing high/low give some update (without cluttering @@ -439,14 +429,11 @@ void Thread::search() { if (mainThread) { mainThread->failedLow = true; - Signals.stopOnPonderhit = false; + Threads.stopOnPonderhit = false; } } else if (bestValue >= beta) - { - alpha = (alpha + beta) / 2; beta = std::min(bestValue + delta, VALUE_INFINITE); - } else break; @@ -458,16 +445,20 @@ void Thread::search() { // Sort the PV lines searched so far and update the GUI std::stable_sort(rootMoves.begin(), rootMoves.begin() + PVIdx + 1); - if (!mainThread) - continue; - - if (Signals.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000) + if ( mainThread + && (Threads.stop || PVIdx + 1 == multiPV || Time.elapsed() > 3000)) sync_cout << UCI::pv(rootPos, rootDepth, alpha, beta) << sync_endl; } - if (!Signals.stop) + if (!Threads.stop) completedDepth = rootDepth; + // Have we found a "mate in x"? + if ( Limits.mate + && bestValue >= VALUE_MATE_IN_MAX_PLY + && VALUE_MATE - bestValue <= 2 * Limits.mate) + Threads.stop = true; + if (!mainThread) continue; @@ -475,16 +466,10 @@ void Thread::search() { if (skill.enabled() && skill.time_to_pick(rootDepth)) skill.pick_best(multiPV); - // Have we found a "mate in x"? - if ( Limits.mate - && bestValue >= VALUE_MATE_IN_MAX_PLY - && VALUE_MATE - bestValue <= 2 * Limits.mate) - Signals.stop = true; - // Do we have time for the next iteration? Can we stop searching now? if (Limits.use_time_management()) { - if (!Signals.stop && !Signals.stopOnPonderhit) + if (!Threads.stop && !Threads.stopOnPonderhit) { // Stop the search if only one legal move is available, or if all // of the available time has been used, or if we matched an easyMove @@ -497,7 +482,7 @@ void Thread::search() { bool doEasyMove = rootMoves[0].pv[0] == easyMove && mainThread->bestMoveChanges < 0.03 - && Time.elapsed() > Time.optimum() * 5 / 42; + && Time.elapsed() > Time.optimum() * 5 / 44; if ( rootMoves.size() == 1 || Time.elapsed() > Time.optimum() * unstablePvFactor * improvingFactor / 628 @@ -505,10 +490,10 @@ void Thread::search() { { // If we are allowed to ponder do not stop the search now but // keep pondering until the GUI sends "ponderhit" or "stop". - if (Limits.ponder) - Signals.stopOnPonderhit = true; + if (Threads.ponder) + Threads.stopOnPonderhit = true; else - Signals.stop = true; + Threads.stop = true; } } @@ -539,7 +524,7 @@ namespace { // search<>() is the main search function for both PV and non-PV nodes template - Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode) { + Value search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth, bool cutNode, bool skipEarlyPruning) { const bool PvNode = NT == PV; const bool rootNode = PvNode && (ss-1)->ply == 0; @@ -556,42 +541,32 @@ namespace { Key posKey; Move ttMove, move, excludedMove, bestMove; Depth extension, newDepth; - Value bestValue, value, ttValue, eval, nullValue; + Value bestValue, value, ttValue, eval; bool ttHit, inCheck, givesCheck, singularExtensionNode, improving; - bool captureOrPromotion, doFullDepthSearch, moveCountPruning; - Piece moved_piece; + bool captureOrPromotion, doFullDepthSearch, moveCountPruning, skipQuiets, ttCapture; + Piece movedPiece; int moveCount, quietCount; // Step 1. Initialize node Thread* thisThread = pos.this_thread(); inCheck = pos.checkers(); - moveCount = quietCount = ss->moveCount = 0; - ss->history = VALUE_ZERO; + moveCount = quietCount = ss->moveCount = 0; + ss->statScore = 0; bestValue = -VALUE_INFINITE; ss->ply = (ss-1)->ply + 1; // Check for the available remaining time - if (thisThread->resetCalls.load(std::memory_order_relaxed)) - { - thisThread->resetCalls = false; - thisThread->callsCnt = 0; - } - if (++thisThread->callsCnt > 4096) - { - for (Thread* th : Threads) - th->resetCalls = true; - - check_time(); - } + if (thisThread == Threads.main()) + static_cast(thisThread)->check_time(); // Used to send selDepth info to GUI - if (PvNode && thisThread->maxPly < ss->ply) - thisThread->maxPly = ss->ply; + if (PvNode && thisThread->selDepth < ss->ply) + thisThread->selDepth = ss->ply; if (!rootNode) { // Step 2. Check for aborted search and immediate draw - if (Signals.stop.load(std::memory_order_relaxed) || pos.is_draw() || ss->ply >= MAX_PLY) + if (Threads.stop.load(std::memory_order_relaxed) || pos.is_draw(ss->ply) || ss->ply >= MAX_PLY) return ss->ply >= MAX_PLY && !inCheck ? evaluate(pos) : DrawValue[pos.side_to_move()]; @@ -610,9 +585,9 @@ namespace { assert(0 <= ss->ply && ss->ply < MAX_PLY); ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE; - ss->counterMoves = nullptr; - (ss+1)->skipEarlyPruning = false; + ss->contHistory = &thisThread->contHistory[NO_PIECE][0]; (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 @@ -632,23 +607,24 @@ namespace { && (ttValue >= beta ? (tte->bound() & BOUND_LOWER) : (tte->bound() & BOUND_UPPER))) { - // If ttMove is quiet, update killers, history, counter move on TT hit - if (ttValue >= beta && ttMove) + // If ttMove is quiet, update move sorting heuristics on TT hit + if (ttMove) { - int d = depth / ONE_PLY; - - if (!pos.capture_or_promotion(ttMove)) + if (ttValue >= beta) { - Value bonus = Value(d * d + 2 * d - 2); - update_stats(pos, ss, ttMove, nullptr, 0, bonus); + if (!pos.capture_or_promotion(ttMove)) + update_stats(pos, ss, ttMove, nullptr, 0, stat_bonus(depth)); + + // Extra penalty for a quiet TT move in previous ply when it gets refuted + if ((ss-1)->moveCount == 1 && !pos.captured_piece()) + update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY)); } - - // Extra penalty for a quiet TT move in previous ply when it gets refuted - if ((ss-1)->moveCount == 1 && !pos.captured_piece()) + // Penalty for a quiet ttMove that fails low + else if (!pos.capture_or_promotion(ttMove)) { - 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); + int penalty = -stat_bonus(depth); + thisThread->mainHistory.update(pos.side_to_move(), ttMove, penalty); + update_continuation_histories(ss, pos.moved_piece(ttMove), to_sq(ttMove), penalty); } } return ttValue; @@ -657,18 +633,19 @@ namespace { // Step 4a. Tablebase probe if (!rootNode && TB::Cardinality) { - int piecesCnt = pos.count(WHITE) + pos.count(BLACK); + int piecesCount = pos.count(); - if ( piecesCnt <= TB::Cardinality - && (piecesCnt < TB::Cardinality || depth >= TB::ProbeDepth) + if ( piecesCount <= TB::Cardinality + && (piecesCount < TB::Cardinality || depth >= TB::ProbeDepth) && pos.rule50_count() == 0 && !pos.can_castle(ANY_CASTLING)) { - int found, v = Tablebases::probe_wdl(pos, &found); + TB::ProbeState err; + TB::WDLScore v = Tablebases::probe_wdl(pos, &err); - if (found) + if (err != TB::ProbeState::FAIL) { - thisThread->tbHits++; + thisThread->tbHits.fetch_add(1, std::memory_order_relaxed); int drawScore = TB::UseRule50 ? 1 : 0; @@ -699,9 +676,9 @@ namespace { eval = ss->staticEval = evaluate(pos); // Can ttValue be used as a better position evaluation? - if (ttValue != VALUE_NONE) - if (tte->bound() & (ttValue > eval ? BOUND_LOWER : BOUND_UPPER)) - eval = ttValue; + if ( ttValue != VALUE_NONE + && (tte->bound() & (ttValue > eval ? BOUND_LOWER : BOUND_UPPER))) + eval = ttValue; } else { @@ -713,20 +690,19 @@ namespace { ss->staticEval, TT.generation()); } - if (ss->skipEarlyPruning) + if (skipEarlyPruning) goto moves_loop; // Step 6. Razoring (skipped when in check) if ( !PvNode && depth < 4 * ONE_PLY - && ttMove == MOVE_NONE && eval + razor_margin[depth / ONE_PLY] <= alpha) { if (depth <= ONE_PLY) - return qsearch(pos, ss, alpha, beta, DEPTH_ZERO); + return qsearch(pos, ss, alpha, alpha+1); Value ralpha = alpha - razor_margin[depth / ONE_PLY]; - Value v = qsearch(pos, ss, ralpha, ralpha+1, DEPTH_ZERO); + Value v = qsearch(pos, ss, ralpha, ralpha+1); if (v <= ralpha) return v; } @@ -745,19 +721,18 @@ namespace { && (ss->staticEval >= beta - 35 * (depth / ONE_PLY - 6) || depth >= 13 * ONE_PLY) && pos.non_pawn_material(pos.side_to_move())) { - ss->currentMove = MOVE_NULL; - ss->counterMoves = nullptr; assert(eval - beta >= 0); // Null move dynamic reduction based on depth and value Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY; + ss->currentMove = MOVE_NULL; + ss->contHistory = &thisThread->contHistory[NO_PIECE][0]; + pos.do_null_move(st); - (ss+1)->skipEarlyPruning = true; - nullValue = depth-R < ONE_PLY ? -qsearch(pos, ss+1, -beta, -beta+1, DEPTH_ZERO) - : - search(pos, ss+1, -beta, -beta+1, depth-R, !cutNode); - (ss+1)->skipEarlyPruning = false; + Value nullValue = depth-R < ONE_PLY ? -qsearch(pos, ss+1, -beta, -beta+1) + : - search(pos, ss+1, -beta, -beta+1, depth-R, !cutNode, true); pos.undo_null_move(); if (nullValue >= beta) @@ -770,10 +745,8 @@ namespace { return nullValue; // Do verification search at high depths - ss->skipEarlyPruning = true; - Value v = depth-R < ONE_PLY ? qsearch(pos, ss, beta-1, beta, DEPTH_ZERO) - : search(pos, ss, beta-1, beta, depth-R, false); - ss->skipEarlyPruning = false; + Value v = depth-R < ONE_PLY ? qsearch(pos, ss, beta-1, beta) + : search(pos, ss, beta-1, beta, depth-R, false, true); if (v >= beta) return nullValue; @@ -788,11 +761,8 @@ namespace { && abs(beta) < VALUE_MATE_IN_MAX_PLY) { Value rbeta = std::min(beta + 200, VALUE_INFINITE); - Depth rdepth = depth - 4 * ONE_PLY; - assert(rdepth >= ONE_PLY); - assert((ss-1)->currentMove != MOVE_NONE); - assert((ss-1)->currentMove != MOVE_NULL); + assert(is_ok((ss-1)->currentMove)); MovePicker mp(pos, ttMove, rbeta - ss->staticEval); @@ -800,9 +770,11 @@ namespace { if (pos.legal(move)) { ss->currentMove = move; - ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)]; - pos.do_move(move, st, pos.gives_check(move)); - value = -search(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode); + ss->contHistory = &thisThread->contHistory[pos.moved_piece(move)][to_sq(move)]; + + assert(depth >= 5 * ONE_PLY); + pos.do_move(move, st); + value = -search(pos, ss+1, -rbeta, -rbeta+1, depth - 4 * ONE_PLY, !cutNode, false); pos.undo_move(move); if (value >= rbeta) return value; @@ -815,9 +787,7 @@ namespace { && (PvNode || ss->staticEval + 256 >= beta)) { Depth d = (3 * depth / (4 * ONE_PLY) - 2) * ONE_PLY; - ss->skipEarlyPruning = true; - search(pos, ss, alpha, beta, d, cutNode); - ss->skipEarlyPruning = false; + search(pos, ss, alpha, beta, d, cutNode, true); tte = TT.probe(posKey, ttHit); ttMove = ttHit ? tte->move() : MOVE_NONE; @@ -825,11 +795,10 @@ namespace { moves_loop: // When in check search starts from here - const CounterMoveStats* cmh = (ss-1)->counterMoves; - const CounterMoveStats* fmh = (ss-2)->counterMoves; - const CounterMoveStats* fmh2 = (ss-4)->counterMoves; + const PieceToHistory* contHist[] = { (ss-1)->contHistory, (ss-2)->contHistory, nullptr, (ss-4)->contHistory }; + Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq]; - MovePicker mp(pos, ttMove, depth, ss); + MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory, contHist, countermove, ss->killers); value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc improving = ss->staticEval >= (ss-2)->staticEval /* || ss->staticEval == VALUE_NONE Already implicit in the previous condition */ @@ -842,10 +811,12 @@ moves_loop: // When in check search starts from here && !excludedMove // Recursive singular search is not allowed && (tte->bound() & BOUND_LOWER) && tte->depth() >= depth - 3 * ONE_PLY; + skipQuiets = false; + ttCapture = false; // Step 11. Loop through moves // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs - while ((move = mp.next_move()) != MOVE_NONE) + while ((move = mp.next_move(skipQuiets)) != MOVE_NONE) { assert(is_ok(move)); @@ -871,7 +842,7 @@ moves_loop: // When in check search starts from here extension = DEPTH_ZERO; captureOrPromotion = pos.capture_or_promotion(move); - moved_piece = pos.moved_piece(move); + movedPiece = pos.moved_piece(move); givesCheck = type_of(move) == NORMAL && !pos.discovered_check_candidates() ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move) @@ -880,57 +851,57 @@ moves_loop: // When in check search starts from here moveCountPruning = depth < 16 * ONE_PLY && moveCount >= FutilityMoveCounts[improving][depth / ONE_PLY]; - // Step 12. Extend checks - if ( givesCheck - && !moveCountPruning - && pos.see_ge(move, VALUE_ZERO)) - extension = ONE_PLY; + // Step 12. Singular and Gives Check Extensions // Singular extension search. If all moves but one fail low on a search of // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move // is singular and should be extended. To verify this we do a reduced search // on all the other moves but the ttMove and if the result is lower than - // ttValue minus a margin then we extend the ttMove. + // ttValue minus a margin then we will extend the ttMove. if ( singularExtensionNode && move == ttMove - && !extension && pos.legal(move)) { Value rBeta = std::max(ttValue - 2 * depth / ONE_PLY, -VALUE_MATE); Depth d = (depth / (2 * ONE_PLY)) * ONE_PLY; ss->excludedMove = move; - ss->skipEarlyPruning = true; - value = search(pos, ss, rBeta - 1, rBeta, d, cutNode); - ss->skipEarlyPruning = false; + value = search(pos, ss, rBeta - 1, rBeta, d, cutNode, true); ss->excludedMove = MOVE_NONE; if (value < rBeta) extension = ONE_PLY; } + else if ( givesCheck + && !moveCountPruning + && pos.see_ge(move)) + extension = ONE_PLY; - // Update the current move (this must be done after singular extension search) + // Calculate new depth for this move newDepth = depth - ONE_PLY + extension; // Step 13. Pruning at shallow depth if ( !rootNode + && pos.non_pawn_material(pos.side_to_move()) && bestValue > VALUE_MATED_IN_MAX_PLY) { if ( !captureOrPromotion && !givesCheck - && !pos.advanced_pawn_push(move)) + && (!pos.advanced_pawn_push(move) || pos.non_pawn_material() >= Value(5000))) { // Move count based pruning if (moveCountPruning) + { + skipQuiets = true; continue; + } // Reduced depth of the next LMR search int lmrDepth = std::max(newDepth - reduction(improving, depth, moveCount), DEPTH_ZERO) / ONE_PLY; // Countermoves based pruning if ( lmrDepth < 3 - && (!cmh || (*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO) - && (!fmh || (*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO) - && (!fmh2 || (*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO || (cmh && fmh))) + && (*contHist[0])[movedPiece][to_sq(move)] < CounterMovePruneThreshold + && (*contHist[1])[movedPiece][to_sq(move)] < CounterMovePruneThreshold) continue; // Futility pruning: parent node @@ -944,9 +915,9 @@ moves_loop: // When in check search starts from here && !pos.see_ge(move, Value(-35 * lmrDepth * lmrDepth))) continue; } - else if ( depth < 7 * ONE_PLY + else if ( depth < 7 * ONE_PLY && !extension - && !pos.see_ge(move, Value(-35 * depth / ONE_PLY * depth / ONE_PLY))) + && !pos.see_ge(move, -PawnValueEg * (depth / ONE_PLY))) continue; } @@ -960,8 +931,12 @@ moves_loop: // When in check search starts from here continue; } + if (move == ttMove && captureOrPromotion) + ttCapture = true; + + // Update the current move (this must be done after singular extension search) ss->currentMove = move; - ss->counterMoves = &thisThread->counterMoveHistory[moved_piece][to_sq(move)]; + ss->contHistory = &thisThread->contHistory[movedPiece][to_sq(move)]; // Step 14. Make the move pos.do_move(move, st, givesCheck); @@ -978,40 +953,45 @@ moves_loop: // When in check search starts from here r -= r ? ONE_PLY : DEPTH_ZERO; else { + // Decrease reduction if opponent's move count is high + if ((ss-1)->moveCount > 15) + r -= ONE_PLY; + + // Increase reduction if ttMove is a capture + if (ttCapture) + r += ONE_PLY; + // Increase reduction for cut nodes if (cutNode) r += 2 * ONE_PLY; // 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. - 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)) + // hence break make_move(). + else if ( type_of(move) == NORMAL + && !pos.see_ge(make_move(to_sq(move), from_sq(move)))) r -= 2 * ONE_PLY; - ss->history = thisThread->history[moved_piece][to_sq(move)] - + (cmh ? (*cmh )[moved_piece][to_sq(move)] : VALUE_ZERO) - + (fmh ? (*fmh )[moved_piece][to_sq(move)] : VALUE_ZERO) - + (fmh2 ? (*fmh2)[moved_piece][to_sq(move)] : VALUE_ZERO) - + thisThread->fromTo.get(~pos.side_to_move(), move) - - 8000; // Correction factor + ss->statScore = thisThread->mainHistory[~pos.side_to_move()][from_to(move)] + + (*contHist[0])[movedPiece][to_sq(move)] + + (*contHist[1])[movedPiece][to_sq(move)] + + (*contHist[3])[movedPiece][to_sq(move)] + - 4000; // Decrease/increase reduction by comparing opponent's stat score - if (ss->history > VALUE_ZERO && (ss-1)->history < VALUE_ZERO) + if (ss->statScore > 0 && (ss-1)->statScore < 0) r -= ONE_PLY; - else if (ss->history < VALUE_ZERO && (ss-1)->history > VALUE_ZERO) + else if (ss->statScore < 0 && (ss-1)->statScore > 0) r += ONE_PLY; // Decrease/increase reduction for moves with a good/bad history - r = std::max(DEPTH_ZERO, (r / ONE_PLY - ss->history / 20000) * ONE_PLY); + r = std::max(DEPTH_ZERO, (r / ONE_PLY - ss->statScore / 20000) * ONE_PLY); } Depth d = std::max(newDepth - r, ONE_PLY); - value = -search(pos, ss+1, -(alpha+1), -alpha, d, true); + value = -search(pos, ss+1, -(alpha+1), -alpha, d, true, false); doFullDepthSearch = (value > alpha && d != newDepth); } @@ -1021,9 +1001,9 @@ moves_loop: // When in check search starts from here // Step 16. Full depth search when LMR is skipped or fails high if (doFullDepthSearch) value = newDepth < ONE_PLY ? - givesCheck ? -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO) - : -qsearch(pos, ss+1, -(alpha+1), -alpha, DEPTH_ZERO) - : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode); + givesCheck ? -qsearch(pos, ss+1, -(alpha+1), -alpha) + : -qsearch(pos, ss+1, -(alpha+1), -alpha) + : - search(pos, ss+1, -(alpha+1), -alpha, newDepth, !cutNode, false); // For PV nodes only, do a full PV search on the first move or after a fail // high (in the latter case search only if value < beta), otherwise let the @@ -1034,9 +1014,9 @@ moves_loop: // When in check search starts from here (ss+1)->pv[0] = MOVE_NONE; value = newDepth < ONE_PLY ? - givesCheck ? -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO) - : -qsearch(pos, ss+1, -beta, -alpha, DEPTH_ZERO) - : - search(pos, ss+1, -beta, -alpha, newDepth, false); + givesCheck ? -qsearch(pos, ss+1, -beta, -alpha) + : -qsearch(pos, ss+1, -beta, -alpha) + : - search(pos, ss+1, -beta, -alpha, newDepth, false, false); } // Step 17. Undo move @@ -1048,7 +1028,7 @@ moves_loop: // When in check search starts from here // Finished searching the move. If a stop occurred, the return value of // the search cannot be trusted, and we return immediately without // updating best move, PV and TT. - if (Signals.stop.load(std::memory_order_relaxed)) + if (Threads.stop.load(std::memory_order_relaxed)) return VALUE_ZERO; if (rootNode) @@ -1060,6 +1040,7 @@ moves_loop: // When in check search starts from here if (moveCount == 1 || value > alpha) { rm.score = value; + rm.selDepth = thisThread->selDepth; rm.pv.resize(1); assert((ss+1)->pv); @@ -1074,8 +1055,8 @@ moves_loop: // When in check search starts from here ++static_cast(thisThread)->bestMoveChanges; } else - // All other moves but the PV are set to the lowest value: this is - // not a problem when sorting because the sort is stable and the + // All other moves but the PV are set to the lowest value: this + // is not a problem when sorting because the sort is stable and the // move position in the list is preserved - just the PV is pushed up. rm.score = -VALUE_INFINITE; } @@ -1086,13 +1067,6 @@ moves_loop: // When in check search starts from here if (value > alpha) { - // If there is an easy move for this position, clear it if unstable - if ( PvNode - && thisThread == Threads.main() - && EasyMove.get(pos.key()) - && (move != EasyMove.get(pos.key()) || moveCount > 1)) - EasyMove.clear(); - bestMove = move; if (PvNode && !rootNode) // Update pv even in fail-high case @@ -1116,7 +1090,7 @@ moves_loop: // When in check search starts from here // completed. But in this case bestValue is valid because we have fully // searched our subtree, and we can anyhow save the result in TT. /* - if (Signals.stop) + if (Threads.stop) return VALUE_DRAW; */ @@ -1132,38 +1106,25 @@ moves_loop: // When in check search starts from here : 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 + // Quiet best move: update move sorting heuristics 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, stat_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_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + ONE_PLY)); } // 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_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth)); - tte->save(posKey, value_to_tt(bestValue, ss->ply), - bestValue >= beta ? BOUND_LOWER : - PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER, - depth, bestMove, ss->staticEval, TT.generation()); + if (!excludedMove) + tte->save(posKey, value_to_tt(bestValue, ss->ply), + bestValue >= beta ? BOUND_LOWER : + PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER, + depth, bestMove, ss->staticEval, TT.generation()); assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE); @@ -1172,8 +1133,7 @@ moves_loop: // When in check search starts from here // qsearch() is the quiescence search function, which is called by the main - // search function when the remaining depth is zero (or, to be more precise, - // less than ONE_PLY). + // search function with depth zero, or recursively with depth less than ONE_PLY. template Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) { @@ -1194,6 +1154,7 @@ moves_loop: // When in check search starts from here Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha; bool ttHit, givesCheck, evasionPrunable; Depth ttDepth; + int moveCount; if (PvNode) { @@ -1204,9 +1165,10 @@ moves_loop: // When in check search starts from here ss->currentMove = bestMove = MOVE_NONE; ss->ply = (ss-1)->ply + 1; + moveCount = 0; // Check for an instant draw or if the maximum ply has been reached - if (pos.is_draw() || ss->ply >= MAX_PLY) + if (pos.is_draw(ss->ply) || ss->ply >= MAX_PLY) return ss->ply >= MAX_PLY && !InCheck ? evaluate(pos) : DrawValue[pos.side_to_move()]; @@ -1247,9 +1209,9 @@ moves_loop: // When in check search starts from here ss->staticEval = bestValue = evaluate(pos); // Can ttValue be used as a better position evaluation? - if (ttValue != VALUE_NONE) - if (tte->bound() & (ttValue > bestValue ? BOUND_LOWER : BOUND_UPPER)) - bestValue = ttValue; + if ( ttValue != VALUE_NONE + && (tte->bound() & (ttValue > bestValue ? BOUND_LOWER : BOUND_UPPER))) + bestValue = ttValue; } else ss->staticEval = bestValue = @@ -1276,7 +1238,7 @@ moves_loop: // When in check search starts from here // to search the moves. Because the depth is <= 0 here, only captures, // queen promotions and checks (only if depth >= DEPTH_QS_CHECKS) will // be generated. - MovePicker mp(pos, ttMove, depth, to_sq((ss-1)->currentMove)); + MovePicker mp(pos, ttMove, depth, &pos.this_thread()->mainHistory, to_sq((ss-1)->currentMove)); // Loop through the moves until no moves remain or a beta cutoff occurs while ((move = mp.next_move()) != MOVE_NONE) @@ -1287,6 +1249,8 @@ moves_loop: // When in check search starts from here ? pos.check_squares(type_of(pos.piece_on(from_sq(move)))) & to_sq(move) : pos.gives_check(move); + moveCount++; + // Futility pruning if ( !InCheck && !givesCheck @@ -1312,13 +1276,14 @@ moves_loop: // When in check search starts from here // Detect non-capture evasions that are candidates to be pruned evasionPrunable = InCheck + && (depth != DEPTH_ZERO || moveCount > 2) && bestValue > VALUE_MATED_IN_MAX_PLY && !pos.capture(move); // Don't search moves with negative SEE values if ( (!InCheck || evasionPrunable) && type_of(move) != PROMOTION - && !pos.see_ge(move, VALUE_ZERO)) + && !pos.see_ge(move)) continue; // Speculative prefetch as early as possible @@ -1326,7 +1291,10 @@ moves_loop: // When in check search starts from here // Check for legality just before making the move if (!pos.legal(move)) + { + moveCount--; continue; + } ss->currentMove = move; @@ -1414,30 +1382,21 @@ moves_loop: // When in check search starts from here } - // update_cm_stats() updates countermove and follow-up move history + // update_continuation_histories() updates histories of the move pairs formed + // by moves at ply -1, -2, and -4 with current move. - void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus) { + void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) { - CounterMoveStats* cmh = (ss-1)->counterMoves; - CounterMoveStats* fmh1 = (ss-2)->counterMoves; - CounterMoveStats* fmh2 = (ss-4)->counterMoves; - - if (cmh) - cmh->update(pc, s, bonus); - - if (fmh1) - fmh1->update(pc, s, bonus); - - if (fmh2) - fmh2->update(pc, s, bonus); + for (int i : {1, 2, 4}) + if (is_ok((ss-i)->currentMove)) + (ss-i)->contHistory->update(pc, to, bonus); } - // update_stats() updates killers, history, countermove and countermove plus - // follow-up move history when a new quiet best move is found. + // update_stats() updates move sorting heuristics when a new quiet best move is found void update_stats(const Position& pos, Stack* ss, Move move, - Move* quiets, int quietsCnt, Value bonus) { + Move* quiets, int quietsCnt, int bonus) { if (ss->killers[0] != move) { @@ -1447,22 +1406,20 @@ moves_loop: // When in check search starts from here Color c = pos.side_to_move(); Thread* thisThread = pos.this_thread(); - thisThread->fromTo.update(c, move, bonus); - thisThread->history.update(pos.moved_piece(move), to_sq(move), bonus); - update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus); + thisThread->mainHistory.update(c, move, bonus); + update_continuation_histories(ss, pos.moved_piece(move), to_sq(move), bonus); - if ((ss-1)->counterMoves) + if (is_ok((ss-1)->currentMove)) { Square prevSq = to_sq((ss-1)->currentMove); - thisThread->counterMoves.update(pos.piece_on(prevSq), prevSq, move); + thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] = move; } // Decrease all the other played quiet moves for (int i = 0; i < quietsCnt; ++i) { - thisThread->fromTo.update(c, quiets[i], -bonus); - thisThread->history.update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus); - update_cm_stats(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus); + thisThread->mainHistory.update(c, quiets[i], -bonus); + update_continuation_histories(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus); } } @@ -1500,11 +1457,19 @@ moves_loop: // When in check search starts from here return best; } +} // namespace // check_time() is used to print debug info and, more importantly, to detect // when we are out of available time and thus stop the search. - void check_time() { + void MainThread::check_time() { + + if (--callsCnt > 0) + return; + + // At low node count increase the checking rate to about 0.1% of nodes + // otherwise use a default value. + callsCnt = Limits.nodes ? std::min(4096, int(Limits.nodes / 1024)) : 4096; static TimePoint lastInfoTime = now(); @@ -1518,17 +1483,15 @@ moves_loop: // When in check search starts from here } // An engine may not stop pondering until told so by the GUI - if (Limits.ponder) + if (Threads.ponder) return; - if ( (Limits.use_time_management() && elapsed > Time.maximum() - 10) + if ( (Limits.use_time_management() && elapsed > Time.maximum()) || (Limits.movetime && elapsed >= Limits.movetime) || (Limits.nodes && Threads.nodes_searched() >= (uint64_t)Limits.nodes)) - Signals.stop = true; + Threads.stop = true; } -} // namespace - /// UCI::pv() formats PV information according to the UCI protocol. UCI requires /// that all (if any) unsearched PV lines are sent using a previous search score. @@ -1545,7 +1508,7 @@ string UCI::pv(const Position& pos, Depth depth, Value alpha, Value beta) { for (size_t i = 0; i < multiPV; ++i) { - bool updated = (i <= PVIdx); + bool updated = (i <= PVIdx && rootMoves[i].score != -VALUE_INFINITE); if (depth == ONE_PLY && !updated) continue; @@ -1561,7 +1524,7 @@ string UCI::pv(const Position& pos, Depth depth, Value alpha, Value beta) { ss << "info" << " depth " << d / ONE_PLY - << " seldepth " << pos.this_thread()->maxPly + << " seldepth " << rootMoves[i].selDepth << " multipv " << i + 1 << " score " << UCI::value(v); @@ -1601,7 +1564,7 @@ bool RootMove::extract_ponder_from_tt(Position& pos) { 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) diff --git a/DroidFish/jni/stockfish/search.h b/DroidFish/jni/stockfish/search.h index d8051ec..694dd64 100644 --- a/DroidFish/jni/stockfish/search.h +++ b/DroidFish/jni/stockfish/search.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,7 +21,6 @@ #ifndef SEARCH_H_INCLUDED #define SEARCH_H_INCLUDED -#include #include #include "misc.h" @@ -32,21 +31,24 @@ class Position; namespace Search { +/// Threshold used for countermoves based pruning +const int CounterMovePruneThreshold = 0; + + /// Stack struct keeps track of the information we need to remember from nodes /// shallower and deeper in the tree during the search. Each search thread has /// its own array of Stack objects, indexed by the current ply. struct Stack { Move* pv; + PieceToHistory* contHistory; int ply; Move currentMove; Move excludedMove; Move killers[2]; Value staticEval; - Value history; - bool skipEarlyPruning; + int statScore; int moveCount; - CounterMoveStats* counterMoves; }; @@ -57,13 +59,16 @@ struct Stack { struct RootMove { explicit RootMove(Move m) : pv(1, m) {} - - bool operator<(const RootMove& m) const { return m.score < score; } // Descending sort - bool operator==(const Move& m) const { return pv[0] == m; } bool extract_ponder_from_tt(Position& pos); + bool operator==(const Move& m) const { return pv[0] == m; } + bool operator<(const RootMove& m) const { // Sort in descending order + return m.score != score ? m.score < score + : m.previousScore < previousScore; + } Value score = -VALUE_INFINITE; Value previousScore = -VALUE_INFINITE; + int selDepth = 0; std::vector pv; }; @@ -71,40 +76,30 @@ typedef std::vector RootMoves; /// LimitsType struct stores information sent by GUI about available time to -/// search the current move, maximum depth/time, if we are in analysis mode or -/// if we have to ponder while it's our opponent's turn to move. +/// search the current move, maximum depth/time, or if we are in analysis mode. struct LimitsType { LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = - npmsec = movestogo = depth = movetime = mate = infinite = ponder = 0; + npmsec = movestogo = depth = movetime = mate = perft = infinite = 0; } bool use_time_management() const { - return !(mate | movetime | depth | nodes | infinite); + return !(mate | movetime | depth | nodes | perft | infinite); } std::vector searchmoves; - int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder; + int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, + movetime, mate, perft, infinite; int64_t nodes; TimePoint startTime; }; - -/// SignalsType struct stores atomic flags updated during the search, typically -/// in an async fashion e.g. to stop the search by the GUI. - -struct SignalsType { - std::atomic_bool stop, stopOnPonderhit; -}; - -extern SignalsType Signals; extern LimitsType Limits; void init(); void clear(); -template uint64_t perft(Position& pos, Depth depth); } // namespace Search diff --git a/DroidFish/jni/stockfish/syzygy/tbcore.cpp b/DroidFish/jni/stockfish/syzygy/tbcore.cpp deleted file mode 100644 index d37559e..0000000 --- a/DroidFish/jni/stockfish/syzygy/tbcore.cpp +++ /dev/null @@ -1,1380 +0,0 @@ -/* - Copyright (c) 2011-2013 Ronald de Man - This file may be redistributed and/or modified without restrictions. - - tbcore.c contains engine-independent routines of the tablebase probing code. - This file should not need too much adaptation to add tablebase probing to - a particular engine, provided the engine is written in C or C++. -*/ - -#include -#include -#include -#include -#include -#include -#ifndef _WIN32 -#include -#include -#endif -#include "tbcore.h" - -#define TBMAX_PIECE 254 -#define TBMAX_PAWN 256 -#define HSHMAX 5 - -#define Swap(a,b) {int tmp=a;a=b;b=tmp;} - -#define TB_PAWN 1 -#define TB_KNIGHT 2 -#define TB_BISHOP 3 -#define TB_ROOK 4 -#define TB_QUEEN 5 -#define TB_KING 6 - -#define TB_WPAWN TB_PAWN -#define TB_BPAWN (TB_PAWN | 8) - -static LOCK_T TB_mutex; - -static bool initialized = false; -static int num_paths = 0; -static char *path_string = NULL; -static char **paths = NULL; - -static int TBnum_piece, TBnum_pawn; -static struct TBEntry_piece TB_piece[TBMAX_PIECE]; -static struct TBEntry_pawn TB_pawn[TBMAX_PAWN]; - -static struct TBHashEntry TB_hash[1 << TBHASHBITS][HSHMAX]; - -#define DTZ_ENTRIES 64 - -static struct DTZTableEntry DTZ_table[DTZ_ENTRIES]; - -static void init_indices(void); -static uint64 calc_key_from_pcs(int *pcs, int mirror); -static void free_wdl_entry(struct TBEntry *entry); -static void free_dtz_entry(struct TBEntry *entry); - -static FD open_tb(const char *str, const char *suffix) -{ - int i; - FD fd; - char file[256]; - - for (i = 0; i < num_paths; i++) { - strcpy(file, paths[i]); - strcat(file, "/"); - strcat(file, str); - strcat(file, suffix); -#ifndef _WIN32 - fd = open(file, O_RDONLY); -#else - fd = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#endif - if (fd != FD_ERR) return fd; - } - return FD_ERR; -} - -static void close_tb(FD fd) -{ -#ifndef _WIN32 - close(fd); -#else - CloseHandle(fd); -#endif -} - -static char *map_file(const char *name, const char *suffix, uint64 *mapping) -{ - FD fd = open_tb(name, suffix); - if (fd == FD_ERR) - return NULL; -#ifndef _WIN32 - struct stat statbuf; - fstat(fd, &statbuf); - *mapping = statbuf.st_size; - char *data = (char *)mmap(NULL, statbuf.st_size, PROT_READ, - MAP_SHARED, fd, 0); - if (data == (char *)(-1)) { - printf("Could not mmap() %s.\n", name); - exit(1); - } -#else - DWORD size_low, size_high; - size_low = GetFileSize(fd, &size_high); -// *size = ((uint64)size_high) << 32 | ((uint64)size_low); - HANDLE map = CreateFileMapping(fd, NULL, PAGE_READONLY, size_high, size_low, - NULL); - if (map == NULL) { - printf("CreateFileMapping() failed.\n"); - exit(1); - } - *mapping = (uint64)map; - char *data = (char *)MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); - if (data == NULL) { - printf("MapViewOfFile() failed, name = %s%s, error = %lu.\n", name, suffix, GetLastError()); - exit(1); - } -#endif - close_tb(fd); - return data; -} - -#ifndef _WIN32 -static void unmap_file(char *data, uint64 size) -{ - if (!data) return; - munmap(data, size); -} -#else -static void unmap_file(char *data, uint64 mapping) -{ - if (!data) return; - UnmapViewOfFile(data); - CloseHandle((HANDLE)mapping); -} -#endif - -static void add_to_hash(struct TBEntry *ptr, uint64 key) -{ - int i, hshidx; - - hshidx = key >> (64 - TBHASHBITS); - i = 0; - while (i < HSHMAX && TB_hash[hshidx][i].ptr) - i++; - if (i == HSHMAX) { - printf("HSHMAX too low!\n"); - exit(1); - } else { - TB_hash[hshidx][i].key = key; - TB_hash[hshidx][i].ptr = ptr; - } -} - -static char pchr[] = {'K', 'Q', 'R', 'B', 'N', 'P'}; - -static void init_tb(char *str) -{ - FD fd; - struct TBEntry *entry; - int i, j, pcs[16]; - uint64 key, key2; - int color; - char *s; - - fd = open_tb(str, WDLSUFFIX); - if (fd == FD_ERR) return; - close_tb(fd); - - for (i = 0; i < 16; i++) - pcs[i] = 0; - color = 0; - for (s = str; *s; s++) - switch (*s) { - case 'P': - pcs[TB_PAWN | color]++; - break; - case 'N': - pcs[TB_KNIGHT | color]++; - break; - case 'B': - pcs[TB_BISHOP | color]++; - break; - case 'R': - pcs[TB_ROOK | color]++; - break; - case 'Q': - pcs[TB_QUEEN | color]++; - break; - case 'K': - pcs[TB_KING | color]++; - break; - case 'v': - color = 0x08; - break; - } - for (i = 0; i < 8; i++) - if (pcs[i] != pcs[i+8]) - break; - key = calc_key_from_pcs(pcs, 0); - key2 = calc_key_from_pcs(pcs, 1); - if (pcs[TB_WPAWN] + pcs[TB_BPAWN] == 0) { - if (TBnum_piece == TBMAX_PIECE) { - printf("TBMAX_PIECE limit too low!\n"); - exit(1); - } - entry = (struct TBEntry *)&TB_piece[TBnum_piece++]; - } else { - if (TBnum_pawn == TBMAX_PAWN) { - printf("TBMAX_PAWN limit too low!\n"); - exit(1); - } - entry = (struct TBEntry *)&TB_pawn[TBnum_pawn++]; - } - entry->key = key; - entry->ready = 0; - entry->num = 0; - for (i = 0; i < 16; i++) - entry->num += (ubyte)pcs[i]; - entry->symmetric = (key == key2); - entry->has_pawns = (pcs[TB_WPAWN] + pcs[TB_BPAWN] > 0); - if (entry->num > Tablebases::MaxCardinality) - Tablebases::MaxCardinality = entry->num; - - if (entry->has_pawns) { - struct TBEntry_pawn *ptr = (struct TBEntry_pawn *)entry; - ptr->pawns[0] = (ubyte)pcs[TB_WPAWN]; - ptr->pawns[1] = (ubyte)pcs[TB_BPAWN]; - if (pcs[TB_BPAWN] > 0 - && (pcs[TB_WPAWN] == 0 || pcs[TB_BPAWN] < pcs[TB_WPAWN])) { - ptr->pawns[0] = (ubyte)pcs[TB_BPAWN]; - ptr->pawns[1] = (ubyte)pcs[TB_WPAWN]; - } - } else { - struct TBEntry_piece *ptr = (struct TBEntry_piece *)entry; - for (i = 0, j = 0; i < 16; i++) - if (pcs[i] == 1) j++; - if (j >= 3) ptr->enc_type = 0; - else if (j == 2) ptr->enc_type = 2; - else { /* only for suicide */ - j = 16; - for (i = 0; i < 16; i++) { - if (pcs[i] < j && pcs[i] > 1) j = pcs[i]; - ptr->enc_type = ubyte(1 + j); - } - } - } - add_to_hash(entry, key); - if (key2 != key) add_to_hash(entry, key2); -} - -void Tablebases::init(const std::string& path) -{ - char str[16]; - int i, j, k, l; - - if (initialized) { - free(path_string); - free(paths); - struct TBEntry *entry; - for (i = 0; i < TBnum_piece; i++) { - entry = (struct TBEntry *)&TB_piece[i]; - free_wdl_entry(entry); - } - for (i = 0; i < TBnum_pawn; i++) { - entry = (struct TBEntry *)&TB_pawn[i]; - free_wdl_entry(entry); - } - for (i = 0; i < DTZ_ENTRIES; i++) - if (DTZ_table[i].entry) - free_dtz_entry(DTZ_table[i].entry); - } else { - init_indices(); - initialized = true; - } - - const char *p = path.c_str(); - if (strlen(p) == 0 || !strcmp(p, "")) return; - path_string = (char *)malloc(strlen(p) + 1); - strcpy(path_string, p); - num_paths = 0; - for (i = 0;; i++) { - if (path_string[i] != SEP_CHAR) - num_paths++; - while (path_string[i] && path_string[i] != SEP_CHAR) - i++; - if (!path_string[i]) break; - path_string[i] = 0; - } - paths = (char **)malloc(num_paths * sizeof(char *)); - for (i = j = 0; i < num_paths; i++) { - while (!path_string[j]) j++; - paths[i] = &path_string[j]; - while (path_string[j]) j++; - } - - LOCK_INIT(TB_mutex); - - TBnum_piece = TBnum_pawn = 0; - MaxCardinality = 0; - - for (i = 0; i < (1 << TBHASHBITS); i++) - for (j = 0; j < HSHMAX; j++) { - TB_hash[i][j].key = 0ULL; - TB_hash[i][j].ptr = NULL; - } - - for (i = 0; i < DTZ_ENTRIES; i++) - DTZ_table[i].entry = NULL; - - for (i = 1; i < 6; i++) { - sprintf(str, "K%cvK", pchr[i]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) { - sprintf(str, "K%cvK%c", pchr[i], pchr[j]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) { - sprintf(str, "K%c%cvK", pchr[i], pchr[j]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) - for (k = 1; k < 6; k++) { - sprintf(str, "K%c%cvK%c", pchr[i], pchr[j], pchr[k]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) - for (k = j; k < 6; k++) { - sprintf(str, "K%c%c%cvK", pchr[i], pchr[j], pchr[k]); - init_tb(str); - } - - if (sizeof(char*) >= 8) { - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) - for (k = i; k < 6; k++) - for (l = (i == k) ? j : k; l < 6; l++) { - sprintf(str, "K%c%cvK%c%c", pchr[i], pchr[j], pchr[k], pchr[l]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) - for (k = j; k < 6; k++) - for (l = 1; l < 6; l++) { - sprintf(str, "K%c%c%cvK%c", pchr[i], pchr[j], pchr[k], pchr[l]); - init_tb(str); - } - - for (i = 1; i < 6; i++) - for (j = i; j < 6; j++) - for (k = j; k < 6; k++) - for (l = k; l < 6; l++) { - sprintf(str, "K%c%c%c%cvK", pchr[i], pchr[j], pchr[k], pchr[l]); - init_tb(str); - } - } - - printf("info string Found %d tablebases.\n", TBnum_piece + TBnum_pawn); -} - -static const signed char offdiag[] = { - 0,-1,-1,-1,-1,-1,-1,-1, - 1, 0,-1,-1,-1,-1,-1,-1, - 1, 1, 0,-1,-1,-1,-1,-1, - 1, 1, 1, 0,-1,-1,-1,-1, - 1, 1, 1, 1, 0,-1,-1,-1, - 1, 1, 1, 1, 1, 0,-1,-1, - 1, 1, 1, 1, 1, 1, 0,-1, - 1, 1, 1, 1, 1, 1, 1, 0 -}; - -static const ubyte triangle[] = { - 6, 0, 1, 2, 2, 1, 0, 6, - 0, 7, 3, 4, 4, 3, 7, 0, - 1, 3, 8, 5, 5, 8, 3, 1, - 2, 4, 5, 9, 9, 5, 4, 2, - 2, 4, 5, 9, 9, 5, 4, 2, - 1, 3, 8, 5, 5, 8, 3, 1, - 0, 7, 3, 4, 4, 3, 7, 0, - 6, 0, 1, 2, 2, 1, 0, 6 -}; - -static const ubyte invtriangle[] = { - 1, 2, 3, 10, 11, 19, 0, 9, 18, 27 -}; - -static const ubyte invdiag[] = { - 0, 9, 18, 27, 36, 45, 54, 63, - 7, 14, 21, 28, 35, 42, 49, 56 -}; - -static const ubyte flipdiag[] = { - 0, 8, 16, 24, 32, 40, 48, 56, - 1, 9, 17, 25, 33, 41, 49, 57, - 2, 10, 18, 26, 34, 42, 50, 58, - 3, 11, 19, 27, 35, 43, 51, 59, - 4, 12, 20, 28, 36, 44, 52, 60, - 5, 13, 21, 29, 37, 45, 53, 61, - 6, 14, 22, 30, 38, 46, 54, 62, - 7, 15, 23, 31, 39, 47, 55, 63 -}; - -static const ubyte lower[] = { - 28, 0, 1, 2, 3, 4, 5, 6, - 0, 29, 7, 8, 9, 10, 11, 12, - 1, 7, 30, 13, 14, 15, 16, 17, - 2, 8, 13, 31, 18, 19, 20, 21, - 3, 9, 14, 18, 32, 22, 23, 24, - 4, 10, 15, 19, 22, 33, 25, 26, - 5, 11, 16, 20, 23, 25, 34, 27, - 6, 12, 17, 21, 24, 26, 27, 35 -}; - -static const ubyte diag[] = { - 0, 0, 0, 0, 0, 0, 0, 8, - 0, 1, 0, 0, 0, 0, 9, 0, - 0, 0, 2, 0, 0, 10, 0, 0, - 0, 0, 0, 3, 11, 0, 0, 0, - 0, 0, 0, 12, 4, 0, 0, 0, - 0, 0, 13, 0, 0, 5, 0, 0, - 0, 14, 0, 0, 0, 0, 6, 0, - 15, 0, 0, 0, 0, 0, 0, 7 -}; - -static const ubyte flap[] = { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 6, 12, 18, 18, 12, 6, 0, - 1, 7, 13, 19, 19, 13, 7, 1, - 2, 8, 14, 20, 20, 14, 8, 2, - 3, 9, 15, 21, 21, 15, 9, 3, - 4, 10, 16, 22, 22, 16, 10, 4, - 5, 11, 17, 23, 23, 17, 11, 5, - 0, 0, 0, 0, 0, 0, 0, 0 -}; - -static const ubyte ptwist[] = { - 0, 0, 0, 0, 0, 0, 0, 0, - 47, 35, 23, 11, 10, 22, 34, 46, - 45, 33, 21, 9, 8, 20, 32, 44, - 43, 31, 19, 7, 6, 18, 30, 42, - 41, 29, 17, 5, 4, 16, 28, 40, - 39, 27, 15, 3, 2, 14, 26, 38, - 37, 25, 13, 1, 0, 12, 24, 36, - 0, 0, 0, 0, 0, 0, 0, 0 -}; - -static const ubyte invflap[] = { - 8, 16, 24, 32, 40, 48, - 9, 17, 25, 33, 41, 49, - 10, 18, 26, 34, 42, 50, - 11, 19, 27, 35, 43, 51 -}; - -static const ubyte invptwist[] = { - 52, 51, 44, 43, 36, 35, 28, 27, 20, 19, 12, 11, - 53, 50, 45, 42, 37, 34, 29, 26, 21, 18, 13, 10, - 54, 49, 46, 41, 38, 33, 30, 25, 22, 17, 14, 9, - 55, 48, 47, 40, 39, 32, 31, 24, 23, 16, 15, 8 -}; - -static const ubyte file_to_file[] = { - 0, 1, 2, 3, 3, 2, 1, 0 -}; - -static const short KK_idx[10][64] = { - { -1, -1, -1, 0, 1, 2, 3, 4, - -1, -1, -1, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, - 18, 19, 20, 21, 22, 23, 24, 25, - 26, 27, 28, 29, 30, 31, 32, 33, - 34, 35, 36, 37, 38, 39, 40, 41, - 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 57 }, - { 58, -1, -1, -1, 59, 60, 61, 62, - 63, -1, -1, -1, 64, 65, 66, 67, - 68, 69, 70, 71, 72, 73, 74, 75, - 76, 77, 78, 79, 80, 81, 82, 83, - 84, 85, 86, 87, 88, 89, 90, 91, - 92, 93, 94, 95, 96, 97, 98, 99, - 100,101,102,103,104,105,106,107, - 108,109,110,111,112,113,114,115}, - {116,117, -1, -1, -1,118,119,120, - 121,122, -1, -1, -1,123,124,125, - 126,127,128,129,130,131,132,133, - 134,135,136,137,138,139,140,141, - 142,143,144,145,146,147,148,149, - 150,151,152,153,154,155,156,157, - 158,159,160,161,162,163,164,165, - 166,167,168,169,170,171,172,173 }, - {174, -1, -1, -1,175,176,177,178, - 179, -1, -1, -1,180,181,182,183, - 184, -1, -1, -1,185,186,187,188, - 189,190,191,192,193,194,195,196, - 197,198,199,200,201,202,203,204, - 205,206,207,208,209,210,211,212, - 213,214,215,216,217,218,219,220, - 221,222,223,224,225,226,227,228 }, - {229,230, -1, -1, -1,231,232,233, - 234,235, -1, -1, -1,236,237,238, - 239,240, -1, -1, -1,241,242,243, - 244,245,246,247,248,249,250,251, - 252,253,254,255,256,257,258,259, - 260,261,262,263,264,265,266,267, - 268,269,270,271,272,273,274,275, - 276,277,278,279,280,281,282,283 }, - {284,285,286,287,288,289,290,291, - 292,293, -1, -1, -1,294,295,296, - 297,298, -1, -1, -1,299,300,301, - 302,303, -1, -1, -1,304,305,306, - 307,308,309,310,311,312,313,314, - 315,316,317,318,319,320,321,322, - 323,324,325,326,327,328,329,330, - 331,332,333,334,335,336,337,338 }, - { -1, -1,339,340,341,342,343,344, - -1, -1,345,346,347,348,349,350, - -1, -1,441,351,352,353,354,355, - -1, -1, -1,442,356,357,358,359, - -1, -1, -1, -1,443,360,361,362, - -1, -1, -1, -1, -1,444,363,364, - -1, -1, -1, -1, -1, -1,445,365, - -1, -1, -1, -1, -1, -1, -1,446 }, - { -1, -1, -1,366,367,368,369,370, - -1, -1, -1,371,372,373,374,375, - -1, -1, -1,376,377,378,379,380, - -1, -1, -1,447,381,382,383,384, - -1, -1, -1, -1,448,385,386,387, - -1, -1, -1, -1, -1,449,388,389, - -1, -1, -1, -1, -1, -1,450,390, - -1, -1, -1, -1, -1, -1, -1,451 }, - {452,391,392,393,394,395,396,397, - -1, -1, -1, -1,398,399,400,401, - -1, -1, -1, -1,402,403,404,405, - -1, -1, -1, -1,406,407,408,409, - -1, -1, -1, -1,453,410,411,412, - -1, -1, -1, -1, -1,454,413,414, - -1, -1, -1, -1, -1, -1,455,415, - -1, -1, -1, -1, -1, -1, -1,456 }, - {457,416,417,418,419,420,421,422, - -1,458,423,424,425,426,427,428, - -1, -1, -1, -1, -1,429,430,431, - -1, -1, -1, -1, -1,432,433,434, - -1, -1, -1, -1, -1,435,436,437, - -1, -1, -1, -1, -1,459,438,439, - -1, -1, -1, -1, -1, -1,460,440, - -1, -1, -1, -1, -1, -1, -1,461 } -}; - -static int binomial[5][64]; -static int pawnidx[5][24]; -static int pfactor[5][4]; - -static void init_indices(void) -{ - int i, j, k; - -// binomial[k-1][n] = Bin(n, k) - for (i = 0; i < 5; i++) - for (j = 0; j < 64; j++) { - int f = j; - int l = 1; - for (k = 1; k <= i; k++) { - f *= (j - k); - l *= (k + 1); - } - binomial[i][j] = f / l; - } - - for (i = 0; i < 5; i++) { - int s = 0; - for (j = 0; j < 6; j++) { - pawnidx[i][j] = s; - s += (i == 0) ? 1 : binomial[i - 1][ptwist[invflap[j]]]; - } - pfactor[i][0] = s; - s = 0; - for (; j < 12; j++) { - pawnidx[i][j] = s; - s += (i == 0) ? 1 : binomial[i - 1][ptwist[invflap[j]]]; - } - pfactor[i][1] = s; - s = 0; - for (; j < 18; j++) { - pawnidx[i][j] = s; - s += (i == 0) ? 1 : binomial[i - 1][ptwist[invflap[j]]]; - } - pfactor[i][2] = s; - s = 0; - for (; j < 24; j++) { - pawnidx[i][j] = s; - s += (i == 0) ? 1 : binomial[i - 1][ptwist[invflap[j]]]; - } - pfactor[i][3] = s; - } -} - -static uint64 encode_piece(struct TBEntry_piece *ptr, ubyte *norm, int *pos, int *factor) -{ - uint64 idx; - int i, j, k, m, l, p; - int n = ptr->num; - - if (pos[0] & 0x04) { - for (i = 0; i < n; i++) - pos[i] ^= 0x07; - } - if (pos[0] & 0x20) { - for (i = 0; i < n; i++) - pos[i] ^= 0x38; - } - - for (i = 0; i < n; i++) - if (offdiag[pos[i]]) break; - if (i < (ptr->enc_type == 0 ? 3 : 2) && offdiag[pos[i]] > 0) - for (i = 0; i < n; i++) - pos[i] = flipdiag[pos[i]]; - - switch (ptr->enc_type) { - - case 0: /* 111 */ - i = (pos[1] > pos[0]); - j = (pos[2] > pos[0]) + (pos[2] > pos[1]); - - if (offdiag[pos[0]]) - idx = triangle[pos[0]] * 63*62 + (pos[1] - i) * 62 + (pos[2] - j); - else if (offdiag[pos[1]]) - idx = 6*63*62 + diag[pos[0]] * 28*62 + lower[pos[1]] * 62 + pos[2] - j; - else if (offdiag[pos[2]]) - idx = 6*63*62 + 4*28*62 + (diag[pos[0]]) * 7*28 + (diag[pos[1]] - i) * 28 + lower[pos[2]]; - else - idx = 6*63*62 + 4*28*62 + 4*7*28 + (diag[pos[0]] * 7*6) + (diag[pos[1]] - i) * 6 + (diag[pos[2]] - j); - i = 3; - break; - - case 1: /* K3 */ - j = (pos[2] > pos[0]) + (pos[2] > pos[1]); - - idx = KK_idx[triangle[pos[0]]][pos[1]]; - if (idx < 441) - idx = idx + 441 * (pos[2] - j); - else { - idx = 441*62 + (idx - 441) + 21 * lower[pos[2]]; - if (!offdiag[pos[2]]) - idx -= j * 21; - } - i = 3; - break; - - default: /* K2 */ - idx = KK_idx[triangle[pos[0]]][pos[1]]; - i = 2; - break; - } - idx *= factor[0]; - - for (; i < n;) { - int t = norm[i]; - for (j = i; j < i + t; j++) - for (k = j + 1; k < i + t; k++) - if (pos[j] > pos[k]) Swap(pos[j], pos[k]); - int s = 0; - for (m = i; m < i + t; m++) { - p = pos[m]; - for (l = 0, j = 0; l < i; l++) - j += (p > pos[l]); - s += binomial[m - i][p - j]; - } - idx += ((uint64)s) * ((uint64)factor[i]); - i += t; - } - - return idx; -} - -// determine file of leftmost pawn and sort pawns -static int pawn_file(struct TBEntry_pawn *ptr, int *pos) -{ - int i; - - for (i = 1; i < ptr->pawns[0]; i++) - if (flap[pos[0]] > flap[pos[i]]) - Swap(pos[0], pos[i]); - - return file_to_file[pos[0] & 0x07]; -} - -static uint64 encode_pawn(struct TBEntry_pawn *ptr, ubyte *norm, int *pos, int *factor) -{ - uint64 idx; - int i, j, k, m, s, t; - int n = ptr->num; - - if (pos[0] & 0x04) - for (i = 0; i < n; i++) - pos[i] ^= 0x07; - - for (i = 1; i < ptr->pawns[0]; i++) - for (j = i + 1; j < ptr->pawns[0]; j++) - if (ptwist[pos[i]] < ptwist[pos[j]]) - Swap(pos[i], pos[j]); - - t = ptr->pawns[0] - 1; - idx = pawnidx[t][flap[pos[0]]]; - for (i = t; i > 0; i--) - idx += binomial[t - i][ptwist[pos[i]]]; - idx *= factor[0]; - -// remaining pawns - i = ptr->pawns[0]; - t = i + ptr->pawns[1]; - if (t > i) { - for (j = i; j < t; j++) - for (k = j + 1; k < t; k++) - if (pos[j] > pos[k]) Swap(pos[j], pos[k]); - s = 0; - for (m = i; m < t; m++) { - int p = pos[m]; - for (k = 0, j = 0; k < i; k++) - j += (p > pos[k]); - s += binomial[m - i][p - j - 8]; - } - idx += ((uint64)s) * ((uint64)factor[i]); - i = t; - } - - for (; i < n;) { - t = norm[i]; - for (j = i; j < i + t; j++) - for (k = j + 1; k < i + t; k++) - if (pos[j] > pos[k]) Swap(pos[j], pos[k]); - s = 0; - for (m = i; m < i + t; m++) { - int p = pos[m]; - for (k = 0, j = 0; k < i; k++) - j += (p > pos[k]); - s += binomial[m - i][p - j]; - } - idx += ((uint64)s) * ((uint64)factor[i]); - i += t; - } - - return idx; -} - -// place k like pieces on n squares -static int subfactor(int k, int n) -{ - int i, f, l; - - f = n; - l = 1; - for (i = 1; i < k; i++) { - f *= n - i; - l *= i + 1; - } - - return f / l; -} - -static uint64 calc_factors_piece(int *factor, int num, int order, ubyte *norm, ubyte enc_type) -{ - int i, k, n; - uint64 f; - static int pivfac[] = { 31332, 28056, 462 }; - - n = 64 - norm[0]; - - f = 1; - for (i = norm[0], k = 0; i < num || k == order; k++) { - if (k == order) { - factor[0] = static_cast(f); - f *= pivfac[enc_type]; - } else { - factor[i] = static_cast(f); - f *= subfactor(norm[i], n); - n -= norm[i]; - i += norm[i]; - } - } - - return f; -} - -static uint64 calc_factors_pawn(int *factor, int num, int order, int order2, ubyte *norm, int file) -{ - int i, k, n; - uint64 f; - - i = norm[0]; - if (order2 < 0x0f) i += norm[i]; - n = 64 - i; - - f = 1; - for (k = 0; i < num || k == order || k == order2; k++) { - if (k == order) { - factor[0] = static_cast(f); - f *= pfactor[norm[0] - 1][file]; - } else if (k == order2) { - factor[norm[0]] = static_cast(f); - f *= subfactor(norm[norm[0]], 48 - norm[0]); - } else { - factor[i] = static_cast(f); - f *= subfactor(norm[i], n); - n -= norm[i]; - i += norm[i]; - } - } - - return f; -} - -static void set_norm_piece(struct TBEntry_piece *ptr, ubyte *norm, ubyte *pieces) -{ - int i, j; - - for (i = 0; i < ptr->num; i++) - norm[i] = 0; - - switch (ptr->enc_type) { - case 0: - norm[0] = 3; - break; - case 2: - norm[0] = 2; - break; - default: - norm[0] = ubyte(ptr->enc_type - 1); - break; - } - - for (i = norm[0]; i < ptr->num; i += norm[i]) - for (j = i; j < ptr->num && pieces[j] == pieces[i]; j++) - norm[i]++; -} - -static void set_norm_pawn(struct TBEntry_pawn *ptr, ubyte *norm, ubyte *pieces) -{ - int i, j; - - for (i = 0; i < ptr->num; i++) - norm[i] = 0; - - norm[0] = ptr->pawns[0]; - if (ptr->pawns[1]) norm[ptr->pawns[0]] = ptr->pawns[1]; - - for (i = ptr->pawns[0] + ptr->pawns[1]; i < ptr->num; i += norm[i]) - for (j = i; j < ptr->num && pieces[j] == pieces[i]; j++) - norm[i]++; -} - -static void setup_pieces_piece(struct TBEntry_piece *ptr, unsigned char *data, uint64 *tb_size) -{ - int i; - int order; - - for (i = 0; i < ptr->num; i++) - ptr->pieces[0][i] = ubyte(data[i + 1] & 0x0f); - order = data[0] & 0x0f; - set_norm_piece(ptr, ptr->norm[0], ptr->pieces[0]); - tb_size[0] = calc_factors_piece(ptr->factor[0], ptr->num, order, ptr->norm[0], ptr->enc_type); - - for (i = 0; i < ptr->num; i++) - ptr->pieces[1][i] = ubyte(data[i + 1] >> 4); - order = data[0] >> 4; - set_norm_piece(ptr, ptr->norm[1], ptr->pieces[1]); - tb_size[1] = calc_factors_piece(ptr->factor[1], ptr->num, order, ptr->norm[1], ptr->enc_type); -} - -static void setup_pieces_piece_dtz(struct DTZEntry_piece *ptr, unsigned char *data, uint64 *tb_size) -{ - int i; - int order; - - for (i = 0; i < ptr->num; i++) - ptr->pieces[i] = ubyte(data[i + 1] & 0x0f); - order = data[0] & 0x0f; - set_norm_piece((struct TBEntry_piece *)ptr, ptr->norm, ptr->pieces); - tb_size[0] = calc_factors_piece(ptr->factor, ptr->num, order, ptr->norm, ptr->enc_type); -} - -static void setup_pieces_pawn(struct TBEntry_pawn *ptr, unsigned char *data, uint64 *tb_size, int f) -{ - int i, j; - int order, order2; - - j = 1 + (ptr->pawns[1] > 0); - order = data[0] & 0x0f; - order2 = ptr->pawns[1] ? (data[1] & 0x0f) : 0x0f; - for (i = 0; i < ptr->num; i++) - ptr->file[f].pieces[0][i] = ubyte(data[i + j] & 0x0f); - set_norm_pawn(ptr, ptr->file[f].norm[0], ptr->file[f].pieces[0]); - tb_size[0] = calc_factors_pawn(ptr->file[f].factor[0], ptr->num, order, order2, ptr->file[f].norm[0], f); - - order = data[0] >> 4; - order2 = ptr->pawns[1] ? (data[1] >> 4) : 0x0f; - for (i = 0; i < ptr->num; i++) - ptr->file[f].pieces[1][i] = ubyte(data[i + j] >> 4); - set_norm_pawn(ptr, ptr->file[f].norm[1], ptr->file[f].pieces[1]); - tb_size[1] = calc_factors_pawn(ptr->file[f].factor[1], ptr->num, order, order2, ptr->file[f].norm[1], f); -} - -static void setup_pieces_pawn_dtz(struct DTZEntry_pawn *ptr, unsigned char *data, uint64 *tb_size, int f) -{ - int i, j; - int order, order2; - - j = 1 + (ptr->pawns[1] > 0); - order = data[0] & 0x0f; - order2 = ptr->pawns[1] ? (data[1] & 0x0f) : 0x0f; - for (i = 0; i < ptr->num; i++) - ptr->file[f].pieces[i] = ubyte(data[i + j] & 0x0f); - set_norm_pawn((struct TBEntry_pawn *)ptr, ptr->file[f].norm, ptr->file[f].pieces); - tb_size[0] = calc_factors_pawn(ptr->file[f].factor, ptr->num, order, order2, ptr->file[f].norm, f); -} - -static void calc_symlen(struct PairsData *d, int s, char *tmp) -{ - int s1, s2; - - ubyte* w = d->sympat + 3 * s; - s2 = (w[2] << 4) | (w[1] >> 4); - if (s2 == 0x0fff) - d->symlen[s] = 0; - else { - s1 = ((w[1] & 0xf) << 8) | w[0]; - if (!tmp[s1]) calc_symlen(d, s1, tmp); - if (!tmp[s2]) calc_symlen(d, s2, tmp); - d->symlen[s] = ubyte(d->symlen[s1] + d->symlen[s2] + 1); - } - tmp[s] = 1; -} - -ushort ReadUshort(ubyte* d) { - return ushort(d[0] | (d[1] << 8)); -} - -uint32 ReadUint32(ubyte* d) { - return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24); -} - -static struct PairsData *setup_pairs(unsigned char *data, uint64 tb_size, uint64 *size, unsigned char **next, ubyte *flags, int wdl) -{ - struct PairsData *d; - int i; - - *flags = data[0]; - if (data[0] & 0x80) { - d = (struct PairsData *)malloc(sizeof(struct PairsData)); - d->idxbits = 0; - if (wdl) - d->min_len = data[1]; - else - d->min_len = 0; - *next = data + 2; - size[0] = size[1] = size[2] = 0; - return d; - } - - int blocksize = data[1]; - int idxbits = data[2]; - int real_num_blocks = ReadUint32(&data[4]); - int num_blocks = real_num_blocks + *(ubyte *)(&data[3]); - int max_len = data[8]; - int min_len = data[9]; - int h = max_len - min_len + 1; - int num_syms = ReadUshort(&data[10 + 2 * h]); - d = (struct PairsData *)malloc(sizeof(struct PairsData) + (h - 1) * sizeof(base_t) + num_syms); - d->blocksize = blocksize; - d->idxbits = idxbits; - d->offset = (ushort*)(&data[10]); - d->symlen = ((ubyte *)d) + sizeof(struct PairsData) + (h - 1) * sizeof(base_t); - d->sympat = &data[12 + 2 * h]; - d->min_len = min_len; - *next = &data[12 + 2 * h + 3 * num_syms + (num_syms & 1)]; - - uint64 num_indices = (tb_size + (1ULL << idxbits) - 1) >> idxbits; - size[0] = 6ULL * num_indices; - size[1] = 2ULL * num_blocks; - size[2] = (1ULL << blocksize) * real_num_blocks; - - // char tmp[num_syms]; - char tmp[4096]; - for (i = 0; i < num_syms; i++) - tmp[i] = 0; - for (i = 0; i < num_syms; i++) - if (!tmp[i]) - calc_symlen(d, i, tmp); - - d->base[h - 1] = 0; - for (i = h - 2; i >= 0; i--) - d->base[i] = (d->base[i + 1] + ReadUshort((ubyte*)(d->offset + i)) - ReadUshort((ubyte*)(d->offset + i + 1))) / 2; - for (i = 0; i < h; i++) - d->base[i] <<= 64 - (min_len + i); - - d->offset -= d->min_len; - - return d; -} - -static int init_table_wdl(struct TBEntry *entry, char *str) -{ - ubyte *next; - int f, s; - uint64 tb_size[8]; - uint64 size[8 * 3]; - ubyte flags; - - // first mmap the table into memory - - entry->data = map_file(str, WDLSUFFIX, &entry->mapping); - if (!entry->data) { - printf("Could not find %s" WDLSUFFIX, str); - return 0; - } - - ubyte *data = (ubyte *)entry->data; - if (data[0] != WDL_MAGIC[0] || - data[1] != WDL_MAGIC[1] || - data[2] != WDL_MAGIC[2] || - data[3] != WDL_MAGIC[3]) { - printf("Corrupted table.\n"); - unmap_file(entry->data, entry->mapping); - entry->data = 0; - return 0; - } - - int split = data[4] & 0x01; - int files = data[4] & 0x02 ? 4 : 1; - - data += 5; - - if (!entry->has_pawns) { - struct TBEntry_piece *ptr = (struct TBEntry_piece *)entry; - setup_pieces_piece(ptr, data, &tb_size[0]); - data += ptr->num + 1; - data += ((uintptr_t)data) & 0x01; - - ptr->precomp[0] = setup_pairs(data, tb_size[0], &size[0], &next, &flags, 1); - data = next; - if (split) { - ptr->precomp[1] = setup_pairs(data, tb_size[1], &size[3], &next, &flags, 1); - data = next; - } else - ptr->precomp[1] = NULL; - - ptr->precomp[0]->indextable = (char *)data; - data += size[0]; - if (split) { - ptr->precomp[1]->indextable = (char *)data; - data += size[3]; - } - - ptr->precomp[0]->sizetable = (ushort *)data; - data += size[1]; - if (split) { - ptr->precomp[1]->sizetable = (ushort *)data; - data += size[4]; - } - - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->precomp[0]->data = data; - data += size[2]; - if (split) { - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->precomp[1]->data = data; - } - } else { - struct TBEntry_pawn *ptr = (struct TBEntry_pawn *)entry; - s = 1 + (ptr->pawns[1] > 0); - for (f = 0; f < 4; f++) { - setup_pieces_pawn((struct TBEntry_pawn *)ptr, data, &tb_size[2 * f], f); - data += ptr->num + s; - } - data += ((uintptr_t)data) & 0x01; - - for (f = 0; f < files; f++) { - ptr->file[f].precomp[0] = setup_pairs(data, tb_size[2 * f], &size[6 * f], &next, &flags, 1); - data = next; - if (split) { - ptr->file[f].precomp[1] = setup_pairs(data, tb_size[2 * f + 1], &size[6 * f + 3], &next, &flags, 1); - data = next; - } else - ptr->file[f].precomp[1] = NULL; - } - - for (f = 0; f < files; f++) { - ptr->file[f].precomp[0]->indextable = (char *)data; - data += size[6 * f]; - if (split) { - ptr->file[f].precomp[1]->indextable = (char *)data; - data += size[6 * f + 3]; - } - } - - for (f = 0; f < files; f++) { - ptr->file[f].precomp[0]->sizetable = (ushort *)data; - data += size[6 * f + 1]; - if (split) { - ptr->file[f].precomp[1]->sizetable = (ushort *)data; - data += size[6 * f + 4]; - } - } - - for (f = 0; f < files; f++) { - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->file[f].precomp[0]->data = data; - data += size[6 * f + 2]; - if (split) { - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->file[f].precomp[1]->data = data; - data += size[6 * f + 5]; - } - } - } - - return 1; -} - -static int init_table_dtz(struct TBEntry *entry) -{ - ubyte *data = (ubyte *)entry->data; - ubyte *next; - int f, s; - uint64 tb_size[4]; - uint64 size[4 * 3]; - - if (!data) - return 0; - - if (data[0] != DTZ_MAGIC[0] || - data[1] != DTZ_MAGIC[1] || - data[2] != DTZ_MAGIC[2] || - data[3] != DTZ_MAGIC[3]) { - printf("Corrupted table.\n"); - return 0; - } - - int files = data[4] & 0x02 ? 4 : 1; - - data += 5; - - if (!entry->has_pawns) { - struct DTZEntry_piece *ptr = (struct DTZEntry_piece *)entry; - setup_pieces_piece_dtz(ptr, data, &tb_size[0]); - data += ptr->num + 1; - data += ((uintptr_t)data) & 0x01; - - ptr->precomp = setup_pairs(data, tb_size[0], &size[0], &next, &(ptr->flags), 0); - data = next; - - ptr->map = data; - if (ptr->flags & 2) { - int i; - for (i = 0; i < 4; i++) { - ptr->map_idx[i] = static_cast(data + 1 - ptr->map); - data += 1 + data[0]; - } - data += ((uintptr_t)data) & 0x01; - } - - ptr->precomp->indextable = (char *)data; - data += size[0]; - - ptr->precomp->sizetable = (ushort *)data; - data += size[1]; - - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->precomp->data = data; - data += size[2]; - } else { - struct DTZEntry_pawn *ptr = (struct DTZEntry_pawn *)entry; - s = 1 + (ptr->pawns[1] > 0); - for (f = 0; f < 4; f++) { - setup_pieces_pawn_dtz(ptr, data, &tb_size[f], f); - data += ptr->num + s; - } - data += ((uintptr_t)data) & 0x01; - - for (f = 0; f < files; f++) { - ptr->file[f].precomp = setup_pairs(data, tb_size[f], &size[3 * f], &next, &(ptr->flags[f]), 0); - data = next; - } - - ptr->map = data; - for (f = 0; f < files; f++) { - if (ptr->flags[f] & 2) { - int i; - for (i = 0; i < 4; i++) { - ptr->map_idx[f][i] = static_cast(data + 1 - ptr->map); - data += 1 + data[0]; - } - } - } - data += ((uintptr_t)data) & 0x01; - - for (f = 0; f < files; f++) { - ptr->file[f].precomp->indextable = (char *)data; - data += size[3 * f]; - } - - for (f = 0; f < files; f++) { - ptr->file[f].precomp->sizetable = (ushort *)data; - data += size[3 * f + 1]; - } - - for (f = 0; f < files; f++) { - data = (ubyte *)((((uintptr_t)data) + 0x3f) & ~0x3f); - ptr->file[f].precomp->data = data; - data += size[3 * f + 2]; - } - } - - return 1; -} - -template -static ubyte decompress_pairs(struct PairsData *d, uint64 idx) -{ - if (!d->idxbits) - return ubyte(d->min_len); - - uint32 mainidx = static_cast(idx >> d->idxbits); - int litidx = (idx & ((1ULL << d->idxbits) - 1)) - (1ULL << (d->idxbits - 1)); - uint32 block = *(uint32 *)(d->indextable + 6 * mainidx); - if (!LittleEndian) - block = BSWAP32(block); - - ushort idxOffset = *(ushort *)(d->indextable + 6 * mainidx + 4); - if (!LittleEndian) - idxOffset = ushort((idxOffset << 8) | (idxOffset >> 8)); - litidx += idxOffset; - - if (litidx < 0) { - do { - litidx += d->sizetable[--block] + 1; - } while (litidx < 0); - } else { - while (litidx > d->sizetable[block]) - litidx -= d->sizetable[block++] + 1; - } - - uint32 *ptr = (uint32 *)(d->data + (block << d->blocksize)); - - int m = d->min_len; - ushort *offset = d->offset; - base_t *base = d->base - m; - ubyte *symlen = d->symlen; - int sym, bitcnt; - - uint64 code = *((uint64 *)ptr); - if (LittleEndian) - code = BSWAP64(code); - - ptr += 2; - bitcnt = 0; // number of "empty bits" in code - for (;;) { - int l = m; - while (code < base[l]) l++; - sym = offset[l]; - if (!LittleEndian) - sym = ((sym & 0xff) << 8) | (sym >> 8); - sym += static_cast((code - base[l]) >> (64 - l)); - if (litidx < (int)symlen[sym] + 1) break; - litidx -= (int)symlen[sym] + 1; - code <<= l; - bitcnt += l; - if (bitcnt >= 32) { - bitcnt -= 32; - uint32 tmp = *ptr++; - if (LittleEndian) - tmp = BSWAP32(tmp); - code |= ((uint64)tmp) << bitcnt; - } - } - - ubyte *sympat = d->sympat; - while (symlen[sym] != 0) { - ubyte* w = sympat + (3 * sym); - int s1 = ((w[1] & 0xf) << 8) | w[0]; - if (litidx < (int)symlen[s1] + 1) - sym = s1; - else { - litidx -= (int)symlen[s1] + 1; - sym = (w[2] << 4) | (w[1] >> 4); - } - } - - return sympat[3 * sym]; -} - -void load_dtz_table(char *str, uint64 key1, uint64 key2) -{ - int i; - struct TBEntry *ptr, *ptr3; - struct TBHashEntry *ptr2; - - DTZ_table[0].key1 = key1; - DTZ_table[0].key2 = key2; - DTZ_table[0].entry = NULL; - - // find corresponding WDL entry - ptr2 = TB_hash[key1 >> (64 - TBHASHBITS)]; - for (i = 0; i < HSHMAX; i++) - if (ptr2[i].key == key1) break; - if (i == HSHMAX) return; - ptr = ptr2[i].ptr; - - ptr3 = (struct TBEntry *)malloc(ptr->has_pawns - ? sizeof(struct DTZEntry_pawn) - : sizeof(struct DTZEntry_piece)); - - ptr3->data = map_file(str, DTZSUFFIX, &ptr3->mapping); - ptr3->key = ptr->key; - ptr3->num = ptr->num; - ptr3->symmetric = ptr->symmetric; - ptr3->has_pawns = ptr->has_pawns; - if (ptr3->has_pawns) { - struct DTZEntry_pawn *entry = (struct DTZEntry_pawn *)ptr3; - entry->pawns[0] = ((struct TBEntry_pawn *)ptr)->pawns[0]; - entry->pawns[1] = ((struct TBEntry_pawn *)ptr)->pawns[1]; - } else { - struct DTZEntry_piece *entry = (struct DTZEntry_piece *)ptr3; - entry->enc_type = ((struct TBEntry_piece *)ptr)->enc_type; - } - if (!init_table_dtz(ptr3)) - free(ptr3); - else - DTZ_table[0].entry = ptr3; -} - -static void free_wdl_entry(struct TBEntry *entry) -{ - unmap_file(entry->data, entry->mapping); - if (!entry->has_pawns) { - struct TBEntry_piece *ptr = (struct TBEntry_piece *)entry; - free(ptr->precomp[0]); - if (ptr->precomp[1]) - free(ptr->precomp[1]); - } else { - struct TBEntry_pawn *ptr = (struct TBEntry_pawn *)entry; - int f; - for (f = 0; f < 4; f++) { - free(ptr->file[f].precomp[0]); - if (ptr->file[f].precomp[1]) - free(ptr->file[f].precomp[1]); - } - } -} - -static void free_dtz_entry(struct TBEntry *entry) -{ - unmap_file(entry->data, entry->mapping); - if (!entry->has_pawns) { - struct DTZEntry_piece *ptr = (struct DTZEntry_piece *)entry; - free(ptr->precomp); - } else { - struct DTZEntry_pawn *ptr = (struct DTZEntry_pawn *)entry; - int f; - for (f = 0; f < 4; f++) - free(ptr->file[f].precomp); - } - free(entry); -} - -static int wdl_to_map[5] = { 1, 3, 0, 2, 0 }; -static ubyte pa_flags[5] = { 8, 0, 0, 0, 4 }; - diff --git a/DroidFish/jni/stockfish/syzygy/tbcore.h b/DroidFish/jni/stockfish/syzygy/tbcore.h deleted file mode 100644 index cdaf2ac..0000000 --- a/DroidFish/jni/stockfish/syzygy/tbcore.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - Copyright (c) 2011-2013 Ronald de Man -*/ - -#ifndef TBCORE_H -#define TBCORE_H - -#ifndef _WIN32 -#include -#define SEP_CHAR ':' -#define FD int -#define FD_ERR -1 -#else -#include -#define SEP_CHAR ';' -#define FD HANDLE -#define FD_ERR INVALID_HANDLE_VALUE -#endif - -#ifndef _WIN32 -#define LOCK_T pthread_mutex_t -#define LOCK_INIT(x) pthread_mutex_init(&(x), NULL) -#define LOCK(x) pthread_mutex_lock(&(x)) -#define UNLOCK(x) pthread_mutex_unlock(&(x)) -#else -#define LOCK_T HANDLE -#define LOCK_INIT(x) do { x = CreateMutex(NULL, FALSE, NULL); } while (0) -#define LOCK(x) WaitForSingleObject(x, INFINITE) -#define UNLOCK(x) ReleaseMutex(x) -#endif - -#ifndef _MSC_VER -#define BSWAP32(v) __builtin_bswap32(v) -#define BSWAP64(v) __builtin_bswap64(v) -#else -#define BSWAP32(v) _byteswap_ulong(v) -#define BSWAP64(v) _byteswap_uint64(v) -#endif - -#define WDLSUFFIX ".rtbw" -#define DTZSUFFIX ".rtbz" -#define WDLDIR "RTBWDIR" -#define DTZDIR "RTBZDIR" -#define TBPIECES 6 - -typedef unsigned long long uint64; -typedef unsigned int uint32; -typedef unsigned char ubyte; -typedef unsigned short ushort; - -const ubyte WDL_MAGIC[4] = { 0x71, 0xe8, 0x23, 0x5d }; -const ubyte DTZ_MAGIC[4] = { 0xd7, 0x66, 0x0c, 0xa5 }; - -#define TBHASHBITS 10 - -struct TBHashEntry; - -typedef uint64 base_t; - -struct PairsData { - char *indextable; - ushort *sizetable; - ubyte *data; - ushort *offset; - ubyte *symlen; - ubyte *sympat; - int blocksize; - int idxbits; - int min_len; - base_t base[1]; // C++ complains about base[]... -}; - -struct TBEntry { - char *data; - uint64 key; - uint64 mapping; - ubyte ready; - ubyte num; - ubyte symmetric; - ubyte has_pawns; -} -#ifndef _WIN32 -__attribute__((__may_alias__)) -#endif -; - -struct TBEntry_piece { - char *data; - uint64 key; - uint64 mapping; - ubyte ready; - ubyte num; - ubyte symmetric; - ubyte has_pawns; - ubyte enc_type; - struct PairsData *precomp[2]; - int factor[2][TBPIECES]; - ubyte pieces[2][TBPIECES]; - ubyte norm[2][TBPIECES]; -}; - -struct TBEntry_pawn { - char *data; - uint64 key; - uint64 mapping; - ubyte ready; - ubyte num; - ubyte symmetric; - ubyte has_pawns; - ubyte pawns[2]; - struct { - struct PairsData *precomp[2]; - int factor[2][TBPIECES]; - ubyte pieces[2][TBPIECES]; - ubyte norm[2][TBPIECES]; - } file[4]; -}; - -struct DTZEntry_piece { - char *data; - uint64 key; - uint64 mapping; - ubyte ready; - ubyte num; - ubyte symmetric; - ubyte has_pawns; - ubyte enc_type; - struct PairsData *precomp; - int factor[TBPIECES]; - ubyte pieces[TBPIECES]; - ubyte norm[TBPIECES]; - ubyte flags; // accurate, mapped, side - ushort map_idx[4]; - ubyte *map; -}; - -struct DTZEntry_pawn { - char *data; - uint64 key; - uint64 mapping; - ubyte ready; - ubyte num; - ubyte symmetric; - ubyte has_pawns; - ubyte pawns[2]; - struct { - struct PairsData *precomp; - int factor[TBPIECES]; - ubyte pieces[TBPIECES]; - ubyte norm[TBPIECES]; - } file[4]; - ubyte flags[4]; - ushort map_idx[4][4]; - ubyte *map; -}; - -struct TBHashEntry { - uint64 key; - struct TBEntry *ptr; -}; - -struct DTZTableEntry { - uint64 key1; - uint64 key2; - struct TBEntry *entry; -}; - -#endif - diff --git a/DroidFish/jni/stockfish/syzygy/tbprobe.cpp b/DroidFish/jni/stockfish/syzygy/tbprobe.cpp index 0281ccc..9aa603c 100644 --- a/DroidFish/jni/stockfish/syzygy/tbprobe.cpp +++ b/DroidFish/jni/stockfish/syzygy/tbprobe.cpp @@ -1,560 +1,1417 @@ /* + Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (c) 2013 Ronald de Man - This file may be redistributed and/or modified without restrictions. + Copyright (C) 2016-2017 Marco Costalba, Lucas Braesch - tbprobe.cpp contains the Stockfish-specific routines of the - tablebase probing code. It should be relatively easy to adapt - this code to other chess engines. + Stockfish is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Stockfish is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ -#define NOMINMAX - #include +#include +#include +#include // For std::memset +#include +#include +#include +#include +#include +#include -#include "../position.h" -#include "../movegen.h" #include "../bitboard.h" +#include "../movegen.h" +#include "../position.h" #include "../search.h" +#include "../thread_win32.h" +#include "../types.h" #include "tbprobe.h" -#include "tbcore.h" -#include "tbcore.cpp" - -namespace Zobrist { - extern Key psq[PIECE_NB][SQUARE_NB]; -} - -int Tablebases::MaxCardinality = 0; - -// Given a position with 6 or fewer pieces, produce a text string -// of the form KQPvKRP, where "KQP" represents the white pieces if -// mirror == 0 and the black pieces if mirror == 1. -static void prt_str(Position& pos, char *str, int mirror) -{ - Color color; - PieceType pt; - int i; - - color = !mirror ? WHITE : BLACK; - for (pt = KING; pt >= PAWN; --pt) - for (i = popcount(pos.pieces(color, pt)); i > 0; i--) - *str++ = pchr[6 - pt]; - *str++ = 'v'; - color = ~color; - for (pt = KING; pt >= PAWN; --pt) - for (i = popcount(pos.pieces(color, pt)); i > 0; i--) - *str++ = pchr[6 - pt]; - *str++ = 0; -} - -// Given a position, produce a 64-bit material signature key. -// If the engine supports such a key, it should equal the engine's key. -static uint64 calc_key(Position& pos, int mirror) -{ - Color color; - PieceType pt; - int i; - uint64 key = 0; - - color = !mirror ? WHITE : BLACK; - for (pt = PAWN; pt <= KING; ++pt) - for (i = popcount(pos.pieces(color, pt)); i > 0; i--) - key ^= Zobrist::psq[make_piece(WHITE, pt)][i - 1]; - color = ~color; - for (pt = PAWN; pt <= KING; ++pt) - for (i = popcount(pos.pieces(color, pt)); i > 0; i--) - key ^= Zobrist::psq[make_piece(BLACK, pt)][i - 1]; - - return key; -} - -// Produce a 64-bit material key corresponding to the material combination -// defined by pcs[16], where pcs[1], ..., pcs[6] is the number of white -// pawns, ..., kings and pcs[9], ..., pcs[14] is the number of black -// pawns, ..., kings. -static uint64 calc_key_from_pcs(int *pcs, int mirror) -{ - int color; - PieceType pt; - int i; - uint64 key = 0; - - color = !mirror ? 0 : 8; - for (pt = PAWN; pt <= KING; ++pt) - for (i = 0; i < pcs[color + pt]; i++) - key ^= Zobrist::psq[make_piece(WHITE, pt)][i]; - color ^= 8; - for (pt = PAWN; pt <= KING; ++pt) - for (i = 0; i < pcs[color + pt]; i++) - key ^= Zobrist::psq[make_piece(BLACK, pt)][i]; - - return key; -} - -bool is_little_endian() { - union { - int i; - char c[sizeof(int)]; - } x; - x.i = 1; - return x.c[0] == 1; -} - -static ubyte decompress_pairs(struct PairsData *d, uint64 idx) -{ - static const bool isLittleEndian = is_little_endian(); - return isLittleEndian ? decompress_pairs(d, idx) - : decompress_pairs(d, idx); -} - -// probe_wdl_table and probe_dtz_table require similar adaptations. -static int probe_wdl_table(Position& pos, int *success) -{ - struct TBEntry *ptr; - struct TBHashEntry *ptr2; - uint64 idx; - uint64 key; - int i; - ubyte res; - int p[TBPIECES]; - - // Obtain the position's material signature key. - key = pos.material_key(); - - // Test for KvK. - if (key == (Zobrist::psq[W_KING][0] ^ Zobrist::psq[B_KING][0])) - return 0; - - ptr2 = TB_hash[key >> (64 - TBHASHBITS)]; - for (i = 0; i < HSHMAX; i++) - if (ptr2[i].key == key) break; - if (i == HSHMAX) { - *success = 0; - return 0; - } - - ptr = ptr2[i].ptr; - if (!ptr->ready) { - LOCK(TB_mutex); - if (!ptr->ready) { - char str[16]; - prt_str(pos, str, ptr->key != key); - if (!init_table_wdl(ptr, str)) { - ptr2[i].key = 0ULL; - *success = 0; - UNLOCK(TB_mutex); - return 0; - } - // Memory barrier to ensure ptr->ready = 1 is not reordered. -#ifdef _MSC_VER - _ReadWriteBarrier(); +#ifndef _WIN32 +#include +#include +#include +#include #else - __asm__ __volatile__ ("" ::: "memory"); +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include #endif - ptr->ready = 1; - } - UNLOCK(TB_mutex); - } - int bside, mirror, cmirror; - if (!ptr->symmetric) { - if (key != ptr->key) { - cmirror = 8; - mirror = 0x38; - bside = (pos.side_to_move() == WHITE); - } else { - cmirror = mirror = 0; - bside = !(pos.side_to_move() == WHITE); - } - } else { - cmirror = pos.side_to_move() == WHITE ? 0 : 8; - mirror = pos.side_to_move() == WHITE ? 0 : 0x38; - bside = 0; - } +using namespace Tablebases; - // p[i] is to contain the square 0-63 (A1-H8) for a piece of type - // pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king. - // Pieces of the same type are guaranteed to be consecutive. - if (!ptr->has_pawns) { - struct TBEntry_piece *entry = (struct TBEntry_piece *)ptr; - ubyte *pc = entry->pieces[bside]; - for (i = 0; i < entry->num;) { - Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), - (PieceType)(pc[i] & 0x07)); - do { - p[i++] = pop_lsb(&bb); - } while (bb); - } - idx = encode_piece(entry, entry->norm[bside], p, entry->factor[bside]); - res = decompress_pairs(entry->precomp[bside], idx); - } else { - struct TBEntry_pawn *entry = (struct TBEntry_pawn *)ptr; - int k = entry->file[0].pieces[0][0] ^ cmirror; - Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07)); - i = 0; - do { - p[i++] = pop_lsb(&bb) ^ mirror; - } while (bb); - int f = pawn_file(entry, p); - ubyte *pc = entry->file[f].pieces[bside]; - for (; i < entry->num;) { - bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), - (PieceType)(pc[i] & 0x07)); - do { - p[i++] = pop_lsb(&bb) ^ mirror; - } while (bb); - } - idx = encode_pawn(entry, entry->file[f].norm[bside], p, entry->file[f].factor[bside]); - res = decompress_pairs(entry->file[f].precomp[bside], idx); - } +int Tablebases::MaxCardinality; - return ((int)res) - 2; +namespace { + +// Each table has a set of flags: all of them refer to DTZ tables, the last one to WDL tables +enum TBFlag { STM = 1, Mapped = 2, WinPlies = 4, LossPlies = 8, SingleValue = 128 }; + +inline WDLScore operator-(WDLScore d) { return WDLScore(-int(d)); } +inline Square operator^=(Square& s, int i) { return s = Square(int(s) ^ i); } +inline Square operator^(Square s, int i) { return Square(int(s) ^ i); } + +// DTZ tables don't store valid scores for moves that reset the rule50 counter +// like captures and pawn moves but we can easily recover the correct dtz of the +// previous move if we know the position's WDL score. +int dtz_before_zeroing(WDLScore wdl) { + return wdl == WDLWin ? 1 : + wdl == WDLCursedWin ? 101 : + wdl == WDLBlessedLoss ? -101 : + wdl == WDLLoss ? -1 : 0; } -static int probe_dtz_table(Position& pos, int wdl, int *success) -{ - struct TBEntry *ptr; - uint64 idx; - int i, res; - int p[TBPIECES]; - - // Obtain the position's material signature key. - uint64 key = pos.material_key(); - - if (DTZ_table[0].key1 != key && DTZ_table[0].key2 != key) { - for (i = 1; i < DTZ_ENTRIES; i++) - if (DTZ_table[i].key1 == key) break; - if (i < DTZ_ENTRIES) { - struct DTZTableEntry table_entry = DTZ_table[i]; - for (; i > 0; i--) - DTZ_table[i] = DTZ_table[i - 1]; - DTZ_table[0] = table_entry; - } else { - struct TBHashEntry *ptr2 = TB_hash[key >> (64 - TBHASHBITS)]; - for (i = 0; i < HSHMAX; i++) - if (ptr2[i].key == key) break; - if (i == HSHMAX) { - *success = 0; - return 0; - } - ptr = ptr2[i].ptr; - char str[16]; - int mirror = (ptr->key != key); - prt_str(pos, str, mirror); - if (DTZ_table[DTZ_ENTRIES - 1].entry) - free_dtz_entry(DTZ_table[DTZ_ENTRIES-1].entry); - for (i = DTZ_ENTRIES - 1; i > 0; i--) - DTZ_table[i] = DTZ_table[i - 1]; - load_dtz_table(str, calc_key(pos, mirror), calc_key(pos, !mirror)); - } - } - - ptr = DTZ_table[0].entry; - if (!ptr) { - *success = 0; - return 0; - } - - int bside, mirror, cmirror; - if (!ptr->symmetric) { - if (key != ptr->key) { - cmirror = 8; - mirror = 0x38; - bside = (pos.side_to_move() == WHITE); - } else { - cmirror = mirror = 0; - bside = !(pos.side_to_move() == WHITE); - } - } else { - cmirror = pos.side_to_move() == WHITE ? 0 : 8; - mirror = pos.side_to_move() == WHITE ? 0 : 0x38; - bside = 0; - } - - if (!ptr->has_pawns) { - struct DTZEntry_piece *entry = (struct DTZEntry_piece *)ptr; - if ((entry->flags & 1) != bside && !entry->symmetric) { - *success = -1; - return 0; - } - ubyte *pc = entry->pieces; - for (i = 0; i < entry->num;) { - Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), - (PieceType)(pc[i] & 0x07)); - do { - p[i++] = pop_lsb(&bb); - } while (bb); - } - idx = encode_piece((struct TBEntry_piece *)entry, entry->norm, p, entry->factor); - res = decompress_pairs(entry->precomp, idx); - - if (entry->flags & 2) - res = entry->map[entry->map_idx[wdl_to_map[wdl + 2]] + res]; - - if (!(entry->flags & pa_flags[wdl + 2]) || (wdl & 1)) - res *= 2; - } else { - struct DTZEntry_pawn *entry = (struct DTZEntry_pawn *)ptr; - int k = entry->file[0].pieces[0] ^ cmirror; - Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07)); - i = 0; - do { - p[i++] = pop_lsb(&bb) ^ mirror; - } while (bb); - int f = pawn_file((struct TBEntry_pawn *)entry, p); - if ((entry->flags[f] & 1) != bside) { - *success = -1; - return 0; - } - ubyte *pc = entry->file[f].pieces; - for (; i < entry->num;) { - bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), - (PieceType)(pc[i] & 0x07)); - do { - p[i++] = pop_lsb(&bb) ^ mirror; - } while (bb); - } - idx = encode_pawn((struct TBEntry_pawn *)entry, entry->file[f].norm, p, entry->file[f].factor); - res = decompress_pairs(entry->file[f].precomp, idx); - - if (entry->flags[f] & 2) - res = entry->map[entry->map_idx[f][wdl_to_map[wdl + 2]] + res]; - - if (!(entry->flags[f] & pa_flags[wdl + 2]) || (wdl & 1)) - res *= 2; - } - - return res; +// Return the sign of a number (-1, 0, 1) +template int sign_of(T val) { + return (T(0) < val) - (val < T(0)); } -// Add underpromotion captures to list of captures. -static ExtMove *add_underprom_caps(Position& pos, ExtMove *stack, ExtMove *end) -{ - ExtMove *moves, *extra = end; +// Numbers in little endian used by sparseIndex[] to point into blockLength[] +struct SparseEntry { + char block[4]; // Number of block + char offset[2]; // Offset within the block +}; - for (moves = stack; moves < end; moves++) { - Move move = moves->move; - if (type_of(move) == PROMOTION && !pos.empty(to_sq(move))) { - (*extra++).move = (Move)(move - (1 << 12)); - (*extra++).move = (Move)(move - (2 << 12)); - (*extra++).move = (Move)(move - (3 << 12)); +static_assert(sizeof(SparseEntry) == 6, "SparseEntry must be 6 bytes"); + +typedef uint16_t Sym; // Huffman symbol + +struct LR { + enum Side { Left, Right, Value }; + + uint8_t lr[3]; // The first 12 bits is the left-hand symbol, the second 12 + // bits is the right-hand symbol. If symbol has length 1, + // then the first byte is the stored value. + template + Sym get() { + return S == Left ? ((lr[1] & 0xF) << 8) | lr[0] : + S == Right ? (lr[2] << 4) | (lr[1] >> 4) : + S == Value ? lr[0] : (assert(false), Sym(-1)); } - } +}; - return extra; +static_assert(sizeof(LR) == 3, "LR tree entry must be 3 bytes"); + +const int TBPIECES = 6; + +struct PairsData { + int flags; + size_t sizeofBlock; // Block size in bytes + size_t span; // About every span values there is a SparseIndex[] entry + int blocksNum; // Number of blocks in the TB file + int maxSymLen; // Maximum length in bits of the Huffman symbols + int minSymLen; // Minimum length in bits of the Huffman symbols + Sym* lowestSym; // lowestSym[l] is the symbol of length l with the lowest value + LR* btree; // btree[sym] stores the left and right symbols that expand sym + uint16_t* blockLength; // Number of stored positions (minus one) for each block: 1..65536 + int blockLengthSize; // Size of blockLength[] table: padded so it's bigger than blocksNum + SparseEntry* sparseIndex; // Partial indices into blockLength[] + size_t sparseIndexSize; // Size of SparseIndex[] table + uint8_t* data; // Start of Huffman compressed data + std::vector base64; // base64[l - min_sym_len] is the 64bit-padded lowest symbol of length l + std::vector symlen; // Number of values (-1) represented by a given Huffman symbol: 1..256 + Piece pieces[TBPIECES]; // Position pieces: the order of pieces defines the groups + uint64_t groupIdx[TBPIECES+1]; // Start index used for the encoding of the group's pieces + int groupLen[TBPIECES+1]; // Number of pieces in a given group: KRKN -> (3, 1) +}; + +// Helper struct to avoid manually defining entry copy constructor as we +// should because the default one is not compatible with std::atomic_bool. +struct Atomic { + Atomic() = default; + Atomic(const Atomic& e) { ready = e.ready.load(); } // MSVC 2013 wants assignment within body + std::atomic_bool ready; +}; + +// We define types for the different parts of the WLDEntry and DTZEntry with +// corresponding specializations for pieces or pawns. + +struct WLDEntryPiece { + PairsData* precomp; +}; + +struct WDLEntryPawn { + uint8_t pawnCount[2]; // [Lead color / other color] + WLDEntryPiece file[2][4]; // [wtm / btm][FILE_A..FILE_D] +}; + +struct DTZEntryPiece { + PairsData* precomp; + uint16_t map_idx[4]; // WDLWin, WDLLoss, WDLCursedWin, WDLBlessedLoss + uint8_t* map; +}; + +struct DTZEntryPawn { + uint8_t pawnCount[2]; + DTZEntryPiece file[4]; + uint8_t* map; +}; + +struct TBEntry : public Atomic { + void* baseAddress; + uint64_t mapping; + Key key; + Key key2; + int pieceCount; + bool hasPawns; + bool hasUniquePieces; +}; + +// Now the main types: WDLEntry and DTZEntry +struct WDLEntry : public TBEntry { + WDLEntry(const std::string& code); + ~WDLEntry(); + union { + WLDEntryPiece pieceTable[2]; // [wtm / btm] + WDLEntryPawn pawnTable; + }; +}; + +struct DTZEntry : public TBEntry { + DTZEntry(const WDLEntry& wdl); + ~DTZEntry(); + union { + DTZEntryPiece pieceTable; + DTZEntryPawn pawnTable; + }; +}; + +typedef decltype(WDLEntry::pieceTable) WDLPieceTable; +typedef decltype(DTZEntry::pieceTable) DTZPieceTable; +typedef decltype(WDLEntry::pawnTable ) WDLPawnTable; +typedef decltype(DTZEntry::pawnTable ) DTZPawnTable; + +auto item(WDLPieceTable& e, int stm, int ) -> decltype(e[stm])& { return e[stm]; } +auto item(DTZPieceTable& e, int , int ) -> decltype(e)& { return e; } +auto item(WDLPawnTable& e, int stm, int f) -> decltype(e.file[stm][f])& { return e.file[stm][f]; } +auto item(DTZPawnTable& e, int , int f) -> decltype(e.file[f])& { return e.file[f]; } + +template struct Ret { typedef int type; }; +template<> struct Ret { typedef WDLScore type; }; + +int MapPawns[SQUARE_NB]; +int MapB1H1H7[SQUARE_NB]; +int MapA1D1D4[SQUARE_NB]; +int MapKK[10][SQUARE_NB]; // [MapA1D1D4][SQUARE_NB] + +// Comparison function to sort leading pawns in ascending MapPawns[] order +bool pawns_comp(Square i, Square j) { return MapPawns[i] < MapPawns[j]; } +int off_A1H8(Square sq) { return int(rank_of(sq)) - file_of(sq); } + +const Value WDL_to_value[] = { + -VALUE_MATE + MAX_PLY + 1, + VALUE_DRAW - 2, + VALUE_DRAW, + VALUE_DRAW + 2, + VALUE_MATE - MAX_PLY - 1 +}; + +const std::string PieceToChar = " PNBRQK pnbrqk"; + +int Binomial[6][SQUARE_NB]; // [k][n] k elements from a set of n elements +int LeadPawnIdx[5][SQUARE_NB]; // [leadPawnsCnt][SQUARE_NB] +int LeadPawnsSize[5][4]; // [leadPawnsCnt][FILE_A..FILE_D] + +enum { BigEndian, LittleEndian }; + +template +inline void swap_byte(T& x) +{ + char tmp, *c = (char*)&x; + for (int i = 0; i < Half; ++i) + tmp = c[i], c[i] = c[End - i], c[End - i] = tmp; } +template<> inline void swap_byte(uint8_t&) {} -static int probe_ab(Position& pos, int alpha, int beta, int *success) +template T number(void* addr) { - int v; - ExtMove stack[64]; - ExtMove *moves, *end; - StateInfo st; + const union { uint32_t i; char c[4]; } Le = { 0x01020304 }; + const bool IsLittleEndian = (Le.c[0] == 4); - // Generate (at least) all legal non-ep captures including (under)promotions. - // It is OK to generate more, as long as they are filtered out below. - if (!pos.checkers()) { - end = generate(pos, stack); - // Since underpromotion captures are not included, we need to add them. - end = add_underprom_caps(pos, stack, end); - } else - end = generate(pos, stack); + T v; - for (moves = stack; moves < end; moves++) { - Move capture = moves->move; - if (!pos.capture(capture) || type_of(capture) == ENPASSANT - || !pos.legal(capture)) - continue; - pos.do_move(capture, st, pos.gives_check(capture)); - v = -probe_ab(pos, -beta, -alpha, success); - pos.undo_move(capture); - if (*success == 0) return 0; - if (v > alpha) { - if (v >= beta) { - *success = 2; - return v; - } - alpha = v; - } - } + if ((uintptr_t)addr & (alignof(T) - 1)) // Unaligned pointer (very rare) + std::memcpy(&v, addr, sizeof(T)); + else + v = *((T*)addr); - v = probe_wdl_table(pos, success); - if (*success == 0) return 0; - if (alpha >= v) { - *success = 1 + (alpha > 0); - return alpha; - } else { - *success = 1; + if (LE != IsLittleEndian) + swap_byte(v); return v; +} + +class HashTable { + + typedef std::pair EntryPair; + typedef std::pair Entry; + + static const int TBHASHBITS = 10; + static const int HSHMAX = 5; + + Entry hashTable[1 << TBHASHBITS][HSHMAX]; + + std::deque wdlTable; + std::deque dtzTable; + + void insert(Key key, WDLEntry* wdl, DTZEntry* dtz) { + Entry* entry = hashTable[key >> (64 - TBHASHBITS)]; + + for (int i = 0; i < HSHMAX; ++i, ++entry) + if (!entry->second.first || entry->first == key) { + *entry = std::make_pair(key, std::make_pair(wdl, dtz)); + return; + } + + std::cerr << "HSHMAX too low!" << std::endl; + exit(1); + } + +public: + template::value ? 0 : 1> + E* get(Key key) { + Entry* entry = hashTable[key >> (64 - TBHASHBITS)]; + + for (int i = 0; i < HSHMAX; ++i, ++entry) + if (entry->first == key) + return std::get(entry->second); + + return nullptr; } + + void clear() { + std::memset(hashTable, 0, sizeof(hashTable)); + wdlTable.clear(); + dtzTable.clear(); + } + size_t size() const { return wdlTable.size(); } + void insert(const std::vector& pieces); +}; + +HashTable EntryTable; + +class TBFile : public std::ifstream { + + std::string fname; + +public: + // Look for and open the file among the Paths directories where the .rtbw + // and .rtbz files can be found. Multiple directories are separated by ";" + // on Windows and by ":" on Unix-based operating systems. + // + // Example: + // C:\tb\wdl345;C:\tb\wdl6;D:\tb\dtz345;D:\tb\dtz6 + static std::string Paths; + + TBFile(const std::string& f) { + +#ifndef _WIN32 + const char SepChar = ':'; +#else + const char SepChar = ';'; +#endif + std::stringstream ss(Paths); + std::string path; + + while (std::getline(ss, path, SepChar)) { + fname = path + "/" + f; + std::ifstream::open(fname); + if (is_open()) + return; + } + } + + // Memory map the file and check it. File should be already open and will be + // closed after mapping. + uint8_t* map(void** baseAddress, uint64_t* mapping, const uint8_t* TB_MAGIC) { + + assert(is_open()); + + close(); // Need to re-open to get native file descriptor + +#ifndef _WIN32 + struct stat statbuf; + int fd = ::open(fname.c_str(), O_RDONLY); + + if (fd == -1) + return *baseAddress = nullptr, nullptr; + + fstat(fd, &statbuf); + *mapping = statbuf.st_size; + *baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); + ::close(fd); + + if (*baseAddress == MAP_FAILED) { + std::cerr << "Could not mmap() " << fname << std::endl; + exit(1); + } +#else + HANDLE fd = CreateFile(fname.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + + if (fd == INVALID_HANDLE_VALUE) + return *baseAddress = nullptr, nullptr; + + DWORD size_high; + DWORD size_low = GetFileSize(fd, &size_high); + HANDLE mmap = CreateFileMapping(fd, nullptr, PAGE_READONLY, size_high, size_low, nullptr); + CloseHandle(fd); + + if (!mmap) { + std::cerr << "CreateFileMapping() failed" << std::endl; + exit(1); + } + + *mapping = (uint64_t)mmap; + *baseAddress = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); + + if (!*baseAddress) { + std::cerr << "MapViewOfFile() failed, name = " << fname + << ", error = " << GetLastError() << std::endl; + exit(1); + } +#endif + uint8_t* data = (uint8_t*)*baseAddress; + + if ( *data++ != *TB_MAGIC++ + || *data++ != *TB_MAGIC++ + || *data++ != *TB_MAGIC++ + || *data++ != *TB_MAGIC) { + std::cerr << "Corrupted table in file " << fname << std::endl; + unmap(*baseAddress, *mapping); + return *baseAddress = nullptr, nullptr; + } + + return data; + } + + static void unmap(void* baseAddress, uint64_t mapping) { + +#ifndef _WIN32 + munmap(baseAddress, mapping); +#else + UnmapViewOfFile(baseAddress); + CloseHandle((HANDLE)mapping); +#endif + } +}; + +std::string TBFile::Paths; + +WDLEntry::WDLEntry(const std::string& code) { + + StateInfo st; + Position pos; + + memset(this, 0, sizeof(WDLEntry)); + + ready = false; + key = pos.set(code, WHITE, &st).material_key(); + pieceCount = popcount(pos.pieces()); + hasPawns = pos.pieces(PAWN); + + for (Color c = WHITE; c <= BLACK; ++c) + for (PieceType pt = PAWN; pt < KING; ++pt) + if (popcount(pos.pieces(c, pt)) == 1) + hasUniquePieces = true; + + if (hasPawns) { + // Set the leading color. In case both sides have pawns the leading color + // is the side with less pawns because this leads to better compression. + bool c = !pos.count(BLACK) + || ( pos.count(WHITE) + && pos.count(BLACK) >= pos.count(WHITE)); + + pawnTable.pawnCount[0] = pos.count(c ? WHITE : BLACK); + pawnTable.pawnCount[1] = pos.count(c ? BLACK : WHITE); + } + + key2 = pos.set(code, BLACK, &st).material_key(); +} + +WDLEntry::~WDLEntry() { + + if (baseAddress) + TBFile::unmap(baseAddress, mapping); + + for (int i = 0; i < 2; ++i) + if (hasPawns) + for (File f = FILE_A; f <= FILE_D; ++f) + delete pawnTable.file[i][f].precomp; + else + delete pieceTable[i].precomp; +} + +DTZEntry::DTZEntry(const WDLEntry& wdl) { + + memset(this, 0, sizeof(DTZEntry)); + + ready = false; + key = wdl.key; + key2 = wdl.key2; + pieceCount = wdl.pieceCount; + hasPawns = wdl.hasPawns; + hasUniquePieces = wdl.hasUniquePieces; + + if (hasPawns) { + pawnTable.pawnCount[0] = wdl.pawnTable.pawnCount[0]; + pawnTable.pawnCount[1] = wdl.pawnTable.pawnCount[1]; + } +} + +DTZEntry::~DTZEntry() { + + if (baseAddress) + TBFile::unmap(baseAddress, mapping); + + if (hasPawns) + for (File f = FILE_A; f <= FILE_D; ++f) + delete pawnTable.file[f].precomp; + else + delete pieceTable.precomp; +} + +void HashTable::insert(const std::vector& pieces) { + + std::string code; + + for (PieceType pt : pieces) + code += PieceToChar[pt]; + + TBFile file(code.insert(code.find('K', 1), "v") + ".rtbw"); // KRK -> KRvK + + if (!file.is_open()) // Only WDL file is checked + return; + + file.close(); + + MaxCardinality = std::max((int)pieces.size(), MaxCardinality); + + wdlTable.emplace_back(code); + dtzTable.emplace_back(wdlTable.back()); + + insert(wdlTable.back().key , &wdlTable.back(), &dtzTable.back()); + insert(wdlTable.back().key2, &wdlTable.back(), &dtzTable.back()); +} + +// TB tables are compressed with canonical Huffman code. The compressed data is divided into +// blocks of size d->sizeofBlock, and each block stores a variable number of symbols. +// Each symbol represents either a WDL or a (remapped) DTZ value, or a pair of other symbols +// (recursively). If you keep expanding the symbols in a block, you end up with up to 65536 +// WDL or DTZ values. Each symbol represents up to 256 values and will correspond after +// Huffman coding to at least 1 bit. So a block of 32 bytes corresponds to at most +// 32 x 8 x 256 = 65536 values. This maximum is only reached for tables that consist mostly +// of draws or mostly of wins, but such tables are actually quite common. In principle, the +// blocks in WDL tables are 64 bytes long (and will be aligned on cache lines). But for +// mostly-draw or mostly-win tables this can leave many 64-byte blocks only half-filled, so +// in such cases blocks are 32 bytes long. The blocks of DTZ tables are up to 1024 bytes long. +// The generator picks the size that leads to the smallest table. The "book" of symbols and +// Huffman codes is the same for all blocks in the table. A non-symmetric pawnless TB file +// will have one table for wtm and one for btm, a TB file with pawns will have tables per +// file a,b,c,d also in this case one set for wtm and one for btm. +int decompress_pairs(PairsData* d, uint64_t idx) { + + // Special case where all table positions store the same value + if (d->flags & TBFlag::SingleValue) + return d->minSymLen; + + // First we need to locate the right block that stores the value at index "idx". + // Because each block n stores blockLength[n] + 1 values, the index i of the block + // that contains the value at position idx is: + // + // for (i = -1, sum = 0; sum <= idx; i++) + // sum += blockLength[i + 1] + 1; + // + // This can be slow, so we use SparseIndex[] populated with a set of SparseEntry that + // point to known indices into blockLength[]. Namely SparseIndex[k] is a SparseEntry + // that stores the blockLength[] index and the offset within that block of the value + // with index I(k), where: + // + // I(k) = k * d->span + d->span / 2 (1) + + // First step is to get the 'k' of the I(k) nearest to our idx, using definition (1) + uint32_t k = idx / d->span; + + // Then we read the corresponding SparseIndex[] entry + uint32_t block = number(&d->sparseIndex[k].block); + int offset = number(&d->sparseIndex[k].offset); + + // Now compute the difference idx - I(k). From definition of k we know that + // + // idx = k * d->span + idx % d->span (2) + // + // So from (1) and (2) we can compute idx - I(K): + int diff = idx % d->span - d->span / 2; + + // Sum the above to offset to find the offset corresponding to our idx + offset += diff; + + // Move to previous/next block, until we reach the correct block that contains idx, + // that is when 0 <= offset <= d->blockLength[block] + while (offset < 0) + offset += d->blockLength[--block] + 1; + + while (offset > d->blockLength[block]) + offset -= d->blockLength[block++] + 1; + + // Finally, we find the start address of our block of canonical Huffman symbols + uint32_t* ptr = (uint32_t*)(d->data + block * d->sizeofBlock); + + // Read the first 64 bits in our block, this is a (truncated) sequence of + // unknown number of symbols of unknown length but we know the first one + // is at the beginning of this 64 bits sequence. + uint64_t buf64 = number(ptr); ptr += 2; + int buf64Size = 64; + Sym sym; + + while (true) { + int len = 0; // This is the symbol length - d->min_sym_len + + // Now get the symbol length. For any symbol s64 of length l right-padded + // to 64 bits we know that d->base64[l-1] >= s64 >= d->base64[l] so we + // can find the symbol length iterating through base64[]. + while (buf64 < d->base64[len]) + ++len; + + // All the symbols of a given length are consecutive integers (numerical + // sequence property), so we can compute the offset of our symbol of + // length len, stored at the beginning of buf64. + sym = (buf64 - d->base64[len]) >> (64 - len - d->minSymLen); + + // Now add the value of the lowest symbol of length len to get our symbol + sym += number(&d->lowestSym[len]); + + // If our offset is within the number of values represented by symbol sym + // we are done... + if (offset < d->symlen[sym] + 1) + break; + + // ...otherwise update the offset and continue to iterate + offset -= d->symlen[sym] + 1; + len += d->minSymLen; // Get the real length + buf64 <<= len; // Consume the just processed symbol + buf64Size -= len; + + if (buf64Size <= 32) { // Refill the buffer + buf64Size += 32; + buf64 |= (uint64_t)number(ptr++) << (64 - buf64Size); + } + } + + // Ok, now we have our symbol that expands into d->symlen[sym] + 1 symbols. + // We binary-search for our value recursively expanding into the left and + // right child symbols until we reach a leaf node where symlen[sym] + 1 == 1 + // that will store the value we need. + while (d->symlen[sym]) { + + Sym left = d->btree[sym].get(); + + // If a symbol contains 36 sub-symbols (d->symlen[sym] + 1 = 36) and + // expands in a pair (d->symlen[left] = 23, d->symlen[right] = 11), then + // we know that, for instance the ten-th value (offset = 10) will be on + // the left side because in Recursive Pairing child symbols are adjacent. + if (offset < d->symlen[left] + 1) + sym = left; + else { + offset -= d->symlen[left] + 1; + sym = d->btree[sym].get(); + } + } + + return d->btree[sym].get(); +} + +bool check_dtz_stm(WDLEntry*, int, File) { return true; } + +bool check_dtz_stm(DTZEntry* entry, int stm, File f) { + + int flags = entry->hasPawns ? entry->pawnTable.file[f].precomp->flags + : entry->pieceTable.precomp->flags; + + return (flags & TBFlag::STM) == stm + || ((entry->key == entry->key2) && !entry->hasPawns); +} + +// DTZ scores are sorted by frequency of occurrence and then assigned the +// values 0, 1, 2, ... in order of decreasing frequency. This is done for each +// of the four WDLScore values. The mapping information necessary to reconstruct +// the original values is stored in the TB file and read during map[] init. +WDLScore map_score(WDLEntry*, File, int value, WDLScore) { return WDLScore(value - 2); } + +int map_score(DTZEntry* entry, File f, int value, WDLScore wdl) { + + const int WDLMap[] = { 1, 3, 0, 2, 0 }; + + int flags = entry->hasPawns ? entry->pawnTable.file[f].precomp->flags + : entry->pieceTable.precomp->flags; + + uint8_t* map = entry->hasPawns ? entry->pawnTable.map + : entry->pieceTable.map; + + uint16_t* idx = entry->hasPawns ? entry->pawnTable.file[f].map_idx + : entry->pieceTable.map_idx; + if (flags & TBFlag::Mapped) + value = map[idx[WDLMap[wdl + 2]] + value]; + + // DTZ tables store distance to zero in number of moves or plies. We + // want to return plies, so we have convert to plies when needed. + if ( (wdl == WDLWin && !(flags & TBFlag::WinPlies)) + || (wdl == WDLLoss && !(flags & TBFlag::LossPlies)) + || wdl == WDLCursedWin + || wdl == WDLBlessedLoss) + value *= 2; + + return value + 1; +} + +// Compute a unique index out of a position and use it to probe the TB file. To +// encode k pieces of same type and color, first sort the pieces by square in +// ascending order s1 <= s2 <= ... <= sk then compute the unique index as: +// +// idx = Binomial[1][s1] + Binomial[2][s2] + ... + Binomial[k][sk] +// +template::type> +T do_probe_table(const Position& pos, Entry* entry, WDLScore wdl, ProbeState* result) { + + const bool IsWDL = std::is_same::value; + + Square squares[TBPIECES]; + Piece pieces[TBPIECES]; + uint64_t idx; + int next = 0, size = 0, leadPawnsCnt = 0; + PairsData* d; + Bitboard b, leadPawns = 0; + File tbFile = FILE_A; + + // A given TB entry like KRK has associated two material keys: KRvk and Kvkr. + // If both sides have the same pieces keys are equal. In this case TB tables + // only store the 'white to move' case, so if the position to lookup has black + // to move, we need to switch the color and flip the squares before to lookup. + bool symmetricBlackToMove = (entry->key == entry->key2 && pos.side_to_move()); + + // TB files are calculated for white as stronger side. For instance we have + // KRvK, not KvKR. A position where stronger side is white will have its + // material key == entry->key, otherwise we have to switch the color and + // flip the squares before to lookup. + bool blackStronger = (pos.material_key() != entry->key); + + int flipColor = (symmetricBlackToMove || blackStronger) * 8; + int flipSquares = (symmetricBlackToMove || blackStronger) * 070; + int stm = (symmetricBlackToMove || blackStronger) ^ pos.side_to_move(); + + // For pawns, TB files store 4 separate tables according if leading pawn is on + // file a, b, c or d after reordering. The leading pawn is the one with maximum + // MapPawns[] value, that is the one most toward the edges and with lowest rank. + if (entry->hasPawns) { + + // In all the 4 tables, pawns are at the beginning of the piece sequence and + // their color is the reference one. So we just pick the first one. + Piece pc = Piece(item(entry->pawnTable, 0, 0).precomp->pieces[0] ^ flipColor); + + assert(type_of(pc) == PAWN); + + leadPawns = b = pos.pieces(color_of(pc), PAWN); + do + squares[size++] = pop_lsb(&b) ^ flipSquares; + while (b); + + leadPawnsCnt = size; + + std::swap(squares[0], *std::max_element(squares, squares + leadPawnsCnt, pawns_comp)); + + tbFile = file_of(squares[0]); + if (tbFile > FILE_D) + tbFile = file_of(squares[0] ^ 7); // Horizontal flip: SQ_H1 -> SQ_A1 + + d = item(entry->pawnTable , stm, tbFile).precomp; + } else + d = item(entry->pieceTable, stm, tbFile).precomp; + + // DTZ tables are one-sided, i.e. they store positions only for white to + // move or only for black to move, so check for side to move to be stm, + // early exit otherwise. + if (!IsWDL && !check_dtz_stm(entry, stm, tbFile)) + return *result = CHANGE_STM, T(); + + // Now we are ready to get all the position pieces (but the lead pawns) and + // directly map them to the correct color and square. + b = pos.pieces() ^ leadPawns; + do { + Square s = pop_lsb(&b); + squares[size] = s ^ flipSquares; + pieces[size++] = Piece(pos.piece_on(s) ^ flipColor); + } while (b); + + assert(size >= 2); + + // Then we reorder the pieces to have the same sequence as the one stored + // in precomp->pieces[i]: the sequence that ensures the best compression. + for (int i = leadPawnsCnt; i < size; ++i) + for (int j = i; j < size; ++j) + if (d->pieces[i] == pieces[j]) + { + std::swap(pieces[i], pieces[j]); + std::swap(squares[i], squares[j]); + break; + } + + // Now we map again the squares so that the square of the lead piece is in + // the triangle A1-D1-D4. + if (file_of(squares[0]) > FILE_D) + for (int i = 0; i < size; ++i) + squares[i] ^= 7; // Horizontal flip: SQ_H1 -> SQ_A1 + + // Encode leading pawns starting with the one with minimum MapPawns[] and + // proceeding in ascending order. + if (entry->hasPawns) { + idx = LeadPawnIdx[leadPawnsCnt][squares[0]]; + + std::sort(squares + 1, squares + leadPawnsCnt, pawns_comp); + + for (int i = 1; i < leadPawnsCnt; ++i) + idx += Binomial[i][MapPawns[squares[i]]]; + + goto encode_remaining; // With pawns we have finished special treatments + } + + // In positions withouth pawns, we further flip the squares to ensure leading + // piece is below RANK_5. + if (rank_of(squares[0]) > RANK_4) + for (int i = 0; i < size; ++i) + squares[i] ^= 070; // Vertical flip: SQ_A8 -> SQ_A1 + + // Look for the first piece of the leading group not on the A1-D4 diagonal + // and ensure it is mapped below the diagonal. + for (int i = 0; i < d->groupLen[0]; ++i) { + if (!off_A1H8(squares[i])) + continue; + + if (off_A1H8(squares[i]) > 0) // A1-H8 diagonal flip: SQ_A3 -> SQ_C3 + for (int j = i; j < size; ++j) + squares[j] = Square(((squares[j] >> 3) | (squares[j] << 3)) & 63); + break; + } + + // Encode the leading group. + // + // Suppose we have KRvK. Let's say the pieces are on square numbers wK, wR + // and bK (each 0...63). The simplest way to map this position to an index + // is like this: + // + // index = wK * 64 * 64 + wR * 64 + bK; + // + // But this way the TB is going to have 64*64*64 = 262144 positions, with + // lots of positions being equivalent (because they are mirrors of each + // other) and lots of positions being invalid (two pieces on one square, + // adjacent kings, etc.). + // Usually the first step is to take the wK and bK together. There are just + // 462 ways legal and not-mirrored ways to place the wK and bK on the board. + // Once we have placed the wK and bK, there are 62 squares left for the wR + // Mapping its square from 0..63 to available squares 0..61 can be done like: + // + // wR -= (wR > wK) + (wR > bK); + // + // In words: if wR "comes later" than wK, we deduct 1, and the same if wR + // "comes later" than bK. In case of two same pieces like KRRvK we want to + // place the two Rs "together". If we have 62 squares left, we can place two + // Rs "together" in 62 * 61 / 2 ways (we divide by 2 because rooks can be + // swapped and still get the same position.) + // + // In case we have at least 3 unique pieces (inlcuded kings) we encode them + // together. + if (entry->hasUniquePieces) { + + int adjust1 = squares[1] > squares[0]; + int adjust2 = (squares[2] > squares[0]) + (squares[2] > squares[1]); + + // First piece is below a1-h8 diagonal. MapA1D1D4[] maps the b1-d1-d3 + // triangle to 0...5. There are 63 squares for second piece and and 62 + // (mapped to 0...61) for the third. + if (off_A1H8(squares[0])) + idx = ( MapA1D1D4[squares[0]] * 63 + + (squares[1] - adjust1)) * 62 + + squares[2] - adjust2; + + // First piece is on a1-h8 diagonal, second below: map this occurence to + // 6 to differentiate from the above case, rank_of() maps a1-d4 diagonal + // to 0...3 and finally MapB1H1H7[] maps the b1-h1-h7 triangle to 0..27. + else if (off_A1H8(squares[1])) + idx = ( 6 * 63 + rank_of(squares[0]) * 28 + + MapB1H1H7[squares[1]]) * 62 + + squares[2] - adjust2; + + // First two pieces are on a1-h8 diagonal, third below + else if (off_A1H8(squares[2])) + idx = 6 * 63 * 62 + 4 * 28 * 62 + + rank_of(squares[0]) * 7 * 28 + + (rank_of(squares[1]) - adjust1) * 28 + + MapB1H1H7[squares[2]]; + + // All 3 pieces on the diagonal a1-h8 + else + idx = 6 * 63 * 62 + 4 * 28 * 62 + 4 * 7 * 28 + + rank_of(squares[0]) * 7 * 6 + + (rank_of(squares[1]) - adjust1) * 6 + + (rank_of(squares[2]) - adjust2); + } else + // We don't have at least 3 unique pieces, like in KRRvKBB, just map + // the kings. + idx = MapKK[MapA1D1D4[squares[0]]][squares[1]]; + +encode_remaining: + idx *= d->groupIdx[0]; + Square* groupSq = squares + d->groupLen[0]; + + // Encode remainig pawns then pieces according to square, in ascending order + bool remainingPawns = entry->hasPawns && entry->pawnTable.pawnCount[1]; + + while (d->groupLen[++next]) + { + std::sort(groupSq, groupSq + d->groupLen[next]); + uint64_t n = 0; + + // Map down a square if "comes later" than a square in the previous + // groups (similar to what done earlier for leading group pieces). + for (int i = 0; i < d->groupLen[next]; ++i) + { + auto f = [&](Square s) { return groupSq[i] > s; }; + auto adjust = std::count_if(squares, groupSq, f); + n += Binomial[i + 1][groupSq[i] - adjust - 8 * remainingPawns]; + } + + remainingPawns = false; + idx += n * d->groupIdx[next]; + groupSq += d->groupLen[next]; + } + + // Now that we have the index, decompress the pair and get the score + return map_score(entry, tbFile, decompress_pairs(d, idx), wdl); +} + +// Group together pieces that will be encoded together. The general rule is that +// a group contains pieces of same type and color. The exception is the leading +// group that, in case of positions withouth pawns, can be formed by 3 different +// pieces (default) or by the king pair when there is not a unique piece apart +// from the kings. When there are pawns, pawns are always first in pieces[]. +// +// As example KRKN -> KRK + N, KNNK -> KK + NN, KPPKP -> P + PP + K + K +// +// The actual grouping depends on the TB generator and can be inferred from the +// sequence of pieces in piece[] array. +template +void set_groups(T& e, PairsData* d, int order[], File f) { + + int n = 0, firstLen = e.hasPawns ? 0 : e.hasUniquePieces ? 3 : 2; + d->groupLen[n] = 1; + + // Number of pieces per group is stored in groupLen[], for instance in KRKN + // the encoder will default on '111', so groupLen[] will be (3, 1). + for (int i = 1; i < e.pieceCount; ++i) + if (--firstLen > 0 || d->pieces[i] == d->pieces[i - 1]) + d->groupLen[n]++; + else + d->groupLen[++n] = 1; + + d->groupLen[++n] = 0; // Zero-terminated + + // The sequence in pieces[] defines the groups, but not the order in which + // they are encoded. If the pieces in a group g can be combined on the board + // in N(g) different ways, then the position encoding will be of the form: + // + // g1 * N(g2) * N(g3) + g2 * N(g3) + g3 + // + // This ensures unique encoding for the whole position. The order of the + // groups is a per-table parameter and could not follow the canonical leading + // pawns/pieces -> remainig pawns -> remaining pieces. In particular the + // first group is at order[0] position and the remaining pawns, when present, + // are at order[1] position. + bool pp = e.hasPawns && e.pawnTable.pawnCount[1]; // Pawns on both sides + int next = pp ? 2 : 1; + int freeSquares = 64 - d->groupLen[0] - (pp ? d->groupLen[1] : 0); + uint64_t idx = 1; + + for (int k = 0; next < n || k == order[0] || k == order[1]; ++k) + if (k == order[0]) // Leading pawns or pieces + { + d->groupIdx[0] = idx; + idx *= e.hasPawns ? LeadPawnsSize[d->groupLen[0]][f] + : e.hasUniquePieces ? 31332 : 462; + } + else if (k == order[1]) // Remaining pawns + { + d->groupIdx[1] = idx; + idx *= Binomial[d->groupLen[1]][48 - d->groupLen[0]]; + } + else // Remainig pieces + { + d->groupIdx[next] = idx; + idx *= Binomial[d->groupLen[next]][freeSquares]; + freeSquares -= d->groupLen[next++]; + } + + d->groupIdx[n] = idx; +} + +// In Recursive Pairing each symbol represents a pair of childern symbols. So +// read d->btree[] symbols data and expand each one in his left and right child +// symbol until reaching the leafs that represent the symbol value. +uint8_t set_symlen(PairsData* d, Sym s, std::vector& visited) { + + visited[s] = true; // We can set it now because tree is acyclic + Sym sr = d->btree[s].get(); + + if (sr == 0xFFF) + return 0; + + Sym sl = d->btree[s].get(); + + if (!visited[sl]) + d->symlen[sl] = set_symlen(d, sl, visited); + + if (!visited[sr]) + d->symlen[sr] = set_symlen(d, sr, visited); + + return d->symlen[sl] + d->symlen[sr] + 1; +} + +uint8_t* set_sizes(PairsData* d, uint8_t* data) { + + d->flags = *data++; + + if (d->flags & TBFlag::SingleValue) { + d->blocksNum = d->blockLengthSize = 0; + d->span = d->sparseIndexSize = 0; // Broken MSVC zero-init + d->minSymLen = *data++; // Here we store the single value + return data; + } + + // groupLen[] is a zero-terminated list of group lengths, the last groupIdx[] + // element stores the biggest index that is the tb size. + uint64_t tbSize = d->groupIdx[std::find(d->groupLen, d->groupLen + 7, 0) - d->groupLen]; + + d->sizeofBlock = 1ULL << *data++; + d->span = 1ULL << *data++; + d->sparseIndexSize = (tbSize + d->span - 1) / d->span; // Round up + int padding = number(data++); + d->blocksNum = number(data); data += sizeof(uint32_t); + d->blockLengthSize = d->blocksNum + padding; // Padded to ensure SparseIndex[] + // does not point out of range. + d->maxSymLen = *data++; + d->minSymLen = *data++; + d->lowestSym = (Sym*)data; + d->base64.resize(d->maxSymLen - d->minSymLen + 1); + + // The canonical code is ordered such that longer symbols (in terms of + // the number of bits of their Huffman code) have lower numeric value, + // so that d->lowestSym[i] >= d->lowestSym[i+1] (when read as LittleEndian). + // Starting from this we compute a base64[] table indexed by symbol length + // and containing 64 bit values so that d->base64[i] >= d->base64[i+1]. + // See http://www.eecs.harvard.edu/~michaelm/E210/huffman.pdf + for (int i = d->base64.size() - 2; i >= 0; --i) { + d->base64[i] = (d->base64[i + 1] + number(&d->lowestSym[i]) + - number(&d->lowestSym[i + 1])) / 2; + + assert(d->base64[i] * 2 >= d->base64[i+1]); + } + + // Now left-shift by an amount so that d->base64[i] gets shifted 1 bit more + // than d->base64[i+1] and given the above assert condition, we ensure that + // d->base64[i] >= d->base64[i+1]. Moreover for any symbol s64 of length i + // and right-padded to 64 bits holds d->base64[i-1] >= s64 >= d->base64[i]. + for (size_t i = 0; i < d->base64.size(); ++i) + d->base64[i] <<= 64 - i - d->minSymLen; // Right-padding to 64 bits + + data += d->base64.size() * sizeof(Sym); + d->symlen.resize(number(data)); data += sizeof(uint16_t); + d->btree = (LR*)data; + + // The comrpession scheme used is "Recursive Pairing", that replaces the most + // frequent adjacent pair of symbols in the source message by a new symbol, + // reevaluating the frequencies of all of the symbol pairs with respect to + // the extended alphabet, and then repeating the process. + // See http://www.larsson.dogma.net/dcc99.pdf + std::vector visited(d->symlen.size()); + + for (Sym sym = 0; sym < d->symlen.size(); ++sym) + if (!visited[sym]) + d->symlen[sym] = set_symlen(d, sym, visited); + + return data + d->symlen.size() * sizeof(LR) + (d->symlen.size() & 1); +} + +template +uint8_t* set_dtz_map(WDLEntry&, T&, uint8_t*, File) { return nullptr; } + +template +uint8_t* set_dtz_map(DTZEntry&, T& p, uint8_t* data, File maxFile) { + + p.map = data; + + for (File f = FILE_A; f <= maxFile; ++f) { + if (item(p, 0, f).precomp->flags & TBFlag::Mapped) + for (int i = 0; i < 4; ++i) { // Sequence like 3,x,x,x,1,x,0,2,x,x + item(p, 0, f).map_idx[i] = (uint16_t)(data - p.map + 1); + data += *data + 1; + } + } + + return data += (uintptr_t)data & 1; // Word alignment +} + +template +void do_init(Entry& e, T& p, uint8_t* data) { + + const bool IsWDL = std::is_same::value; + + PairsData* d; + + enum { Split = 1, HasPawns = 2 }; + + assert(e.hasPawns == !!(*data & HasPawns)); + assert((e.key != e.key2) == !!(*data & Split)); + + data++; // First byte stores flags + + const int Sides = IsWDL && (e.key != e.key2) ? 2 : 1; + const File MaxFile = e.hasPawns ? FILE_D : FILE_A; + + bool pp = e.hasPawns && e.pawnTable.pawnCount[1]; // Pawns on both sides + + assert(!pp || e.pawnTable.pawnCount[0]); + + for (File f = FILE_A; f <= MaxFile; ++f) { + + for (int i = 0; i < Sides; i++) + item(p, i, f).precomp = new PairsData(); + + int order[][2] = { { *data & 0xF, pp ? *(data + 1) & 0xF : 0xF }, + { *data >> 4, pp ? *(data + 1) >> 4 : 0xF } }; + data += 1 + pp; + + for (int k = 0; k < e.pieceCount; ++k, ++data) + for (int i = 0; i < Sides; i++) + item(p, i, f).precomp->pieces[k] = Piece(i ? *data >> 4 : *data & 0xF); + + for (int i = 0; i < Sides; ++i) + set_groups(e, item(p, i, f).precomp, order[i], f); + } + + data += (uintptr_t)data & 1; // Word alignment + + for (File f = FILE_A; f <= MaxFile; ++f) + for (int i = 0; i < Sides; i++) + data = set_sizes(item(p, i, f).precomp, data); + + if (!IsWDL) + data = set_dtz_map(e, p, data, MaxFile); + + for (File f = FILE_A; f <= MaxFile; ++f) + for (int i = 0; i < Sides; i++) { + (d = item(p, i, f).precomp)->sparseIndex = (SparseEntry*)data; + data += d->sparseIndexSize * sizeof(SparseEntry); + } + + for (File f = FILE_A; f <= MaxFile; ++f) + for (int i = 0; i < Sides; i++) { + (d = item(p, i, f).precomp)->blockLength = (uint16_t*)data; + data += d->blockLengthSize * sizeof(uint16_t); + } + + for (File f = FILE_A; f <= MaxFile; ++f) + for (int i = 0; i < Sides; i++) { + data = (uint8_t*)(((uintptr_t)data + 0x3F) & ~0x3F); // 64 byte alignment + (d = item(p, i, f).precomp)->data = data; + data += d->blocksNum * d->sizeofBlock; + } +} + +template +void* init(Entry& e, const Position& pos) { + + const bool IsWDL = std::is_same::value; + + static Mutex mutex; + + // Avoid a thread reads 'ready' == true while another is still in do_init(), + // this could happen due to compiler reordering. + if (e.ready.load(std::memory_order_acquire)) + return e.baseAddress; + + std::unique_lock lk(mutex); + + if (e.ready.load(std::memory_order_relaxed)) // Recheck under lock + return e.baseAddress; + + // Pieces strings in decreasing order for each color, like ("KPP","KR") + std::string fname, w, b; + for (PieceType pt = KING; pt >= PAWN; --pt) { + w += std::string(popcount(pos.pieces(WHITE, pt)), PieceToChar[pt]); + b += std::string(popcount(pos.pieces(BLACK, pt)), PieceToChar[pt]); + } + + const uint8_t TB_MAGIC[][4] = { { 0xD7, 0x66, 0x0C, 0xA5 }, + { 0x71, 0xE8, 0x23, 0x5D } }; + + fname = (e.key == pos.material_key() ? w + 'v' + b : b + 'v' + w) + + (IsWDL ? ".rtbw" : ".rtbz"); + + uint8_t* data = TBFile(fname).map(&e.baseAddress, &e.mapping, TB_MAGIC[IsWDL]); + if (data) + e.hasPawns ? do_init(e, e.pawnTable, data) : do_init(e, e.pieceTable, data); + + e.ready.store(true, std::memory_order_release); + return e.baseAddress; +} + +template::type> +T probe_table(const Position& pos, ProbeState* result, WDLScore wdl = WDLDraw) { + + if (!(pos.pieces() ^ pos.pieces(KING))) + return T(WDLDraw); // KvK + + E* entry = EntryTable.get(pos.material_key()); + + if (!entry || !init(*entry, pos)) + return *result = FAIL, T(); + + return do_probe_table(pos, entry, wdl, result); +} + +// For a position where the side to move has a winning capture it is not necessary +// to store a winning value so the generator treats such positions as "don't cares" +// and tries to assign to it a value that improves the compression ratio. Similarly, +// if the side to move has a drawing capture, then the position is at least drawn. +// If the position is won, then the TB needs to store a win value. But if the +// position is drawn, the TB may store a loss value if that is better for compression. +// All of this means that during probing, the engine must look at captures and probe +// their results and must probe the position itself. The "best" result of these +// probes is the correct result for the position. +// DTZ table don't store values when a following move is a zeroing winning move +// (winning capture or winning pawn move). Also DTZ store wrong values for positions +// where the best move is an ep-move (even if losing). So in all these cases set +// the state to ZEROING_BEST_MOVE. +template +WDLScore search(Position& pos, ProbeState* result) { + + WDLScore value, bestValue = WDLLoss; + StateInfo st; + + auto moveList = MoveList(pos); + size_t totalCount = moveList.size(), moveCount = 0; + + for (const Move& move : moveList) + { + if ( !pos.capture(move) + && (!CheckZeroingMoves || type_of(pos.moved_piece(move)) != PAWN)) + continue; + + moveCount++; + + pos.do_move(move, st); + value = -search(pos, result); + pos.undo_move(move); + + if (*result == FAIL) + return WDLDraw; + + if (value > bestValue) + { + bestValue = value; + + if (value >= WDLWin) + { + *result = ZEROING_BEST_MOVE; // Winning DTZ-zeroing move + return value; + } + } + } + + // In case we have already searched all the legal moves we don't have to probe + // the TB because the stored score could be wrong. For instance TB tables + // do not contain information on position with ep rights, so in this case + // the result of probe_wdl_table is wrong. Also in case of only capture + // moves, for instance here 4K3/4q3/6p1/2k5/6p1/8/8/8 w - - 0 7, we have to + // return with ZEROING_BEST_MOVE set. + bool noMoreMoves = (moveCount && moveCount == totalCount); + + if (noMoreMoves) + value = bestValue; + else + { + value = probe_table(pos, result); + + if (*result == FAIL) + return WDLDraw; + } + + // DTZ stores a "don't care" value if bestValue is a win + if (bestValue >= value) + return *result = ( bestValue > WDLDraw + || noMoreMoves ? ZEROING_BEST_MOVE : OK), bestValue; + + return *result = OK, value; +} + +} // namespace + +void Tablebases::init(const std::string& paths) { + + EntryTable.clear(); + MaxCardinality = 0; + TBFile::Paths = paths; + + if (paths.empty() || paths == "") + return; + + // MapB1H1H7[] encodes a square below a1-h8 diagonal to 0..27 + int code = 0; + for (Square s = SQ_A1; s <= SQ_H8; ++s) + if (off_A1H8(s) < 0) + MapB1H1H7[s] = code++; + + // MapA1D1D4[] encodes a square in the a1-d1-d4 triangle to 0..9 + std::vector diagonal; + code = 0; + for (Square s = SQ_A1; s <= SQ_D4; ++s) + if (off_A1H8(s) < 0 && file_of(s) <= FILE_D) + MapA1D1D4[s] = code++; + + else if (!off_A1H8(s) && file_of(s) <= FILE_D) + diagonal.push_back(s); + + // Diagonal squares are encoded as last ones + for (auto s : diagonal) + MapA1D1D4[s] = code++; + + // MapKK[] encodes all the 461 possible legal positions of two kings where + // the first is in the a1-d1-d4 triangle. If the first king is on the a1-d4 + // diagonal, the other one shall not to be above the a1-h8 diagonal. + std::vector> bothOnDiagonal; + code = 0; + for (int idx = 0; idx < 10; idx++) + for (Square s1 = SQ_A1; s1 <= SQ_D4; ++s1) + if (MapA1D1D4[s1] == idx && (idx || s1 == SQ_B1)) // SQ_B1 is mapped to 0 + { + for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) + if ((PseudoAttacks[KING][s1] | s1) & s2) + continue; // Illegal position + + else if (!off_A1H8(s1) && off_A1H8(s2) > 0) + continue; // First on diagonal, second above + + else if (!off_A1H8(s1) && !off_A1H8(s2)) + bothOnDiagonal.push_back(std::make_pair(idx, s2)); + + else + MapKK[idx][s2] = code++; + } + + // Legal positions with both kings on diagonal are encoded as last ones + for (auto p : bothOnDiagonal) + MapKK[p.first][p.second] = code++; + + // Binomial[] stores the Binomial Coefficents using Pascal rule. There + // are Binomial[k][n] ways to choose k elements from a set of n elements. + Binomial[0][0] = 1; + + for (int n = 1; n < 64; n++) // Squares + for (int k = 0; k < 6 && k <= n; ++k) // Pieces + Binomial[k][n] = (k > 0 ? Binomial[k - 1][n - 1] : 0) + + (k < n ? Binomial[k ][n - 1] : 0); + + // MapPawns[s] encodes squares a2-h7 to 0..47. This is the number of possible + // available squares when the leading one is in 's'. Moreover the pawn with + // highest MapPawns[] is the leading pawn, the one nearest the edge and, + // among pawns with same file, the one with lowest rank. + int availableSquares = 47; // Available squares when lead pawn is in a2 + + // Init the tables for the encoding of leading pawns group: with 6-men TB we + // can have up to 4 leading pawns (KPPPPK). + for (int leadPawnsCnt = 1; leadPawnsCnt <= 4; ++leadPawnsCnt) + for (File f = FILE_A; f <= FILE_D; ++f) + { + // Restart the index at every file because TB table is splitted + // by file, so we can reuse the same index for different files. + int idx = 0; + + // Sum all possible combinations for a given file, starting with + // the leading pawn on rank 2 and increasing the rank. + for (Rank r = RANK_2; r <= RANK_7; ++r) + { + Square sq = make_square(f, r); + + // Compute MapPawns[] at first pass. + // If sq is the leading pawn square, any other pawn cannot be + // below or more toward the edge of sq. There are 47 available + // squares when sq = a2 and reduced by 2 for any rank increase + // due to mirroring: sq == a3 -> no a2, h2, so MapPawns[a3] = 45 + if (leadPawnsCnt == 1) + { + MapPawns[sq] = availableSquares--; + MapPawns[sq ^ 7] = availableSquares--; // Horizontal flip + } + LeadPawnIdx[leadPawnsCnt][sq] = idx; + idx += Binomial[leadPawnsCnt - 1][MapPawns[sq]]; + } + // After a file is traversed, store the cumulated per-file index + LeadPawnsSize[leadPawnsCnt][f] = idx; + } + + for (PieceType p1 = PAWN; p1 < KING; ++p1) { + EntryTable.insert({KING, p1, KING}); + + for (PieceType p2 = PAWN; p2 <= p1; ++p2) { + EntryTable.insert({KING, p1, p2, KING}); + EntryTable.insert({KING, p1, KING, p2}); + + for (PieceType p3 = PAWN; p3 < KING; ++p3) + EntryTable.insert({KING, p1, p2, KING, p3}); + + for (PieceType p3 = PAWN; p3 <= p2; ++p3) { + EntryTable.insert({KING, p1, p2, p3, KING}); + + if (sizeof(char*) >= 8) + for (PieceType p4 = PAWN; p4 <= p3; ++p4) + EntryTable.insert({KING, p1, p2, p3, p4, KING}); + + for (PieceType p4 = PAWN; p4 < KING; ++p4) + EntryTable.insert({KING, p1, p2, p3, KING, p4}); + } + + for (PieceType p3 = PAWN; p3 <= p1; ++p3) + for (PieceType p4 = PAWN; p4 <= (p1 == p3 ? p2 : p3); ++p4) + EntryTable.insert({KING, p1, p2, KING, p3, p4}); + } + } + + sync_cout << "info string Found " << EntryTable.size() << " tablebases" << sync_endl; } // Probe the WDL table for a particular position. -// If *success != 0, the probe was successful. +// If *result != FAIL, the probe was successful. // The return value is from the point of view of the side to move: // -2 : loss // -1 : loss, but draw under 50-move rule // 0 : draw // 1 : win, but draw under 50-move rule // 2 : win -int Tablebases::probe_wdl(Position& pos, int *success) -{ - int v; +WDLScore Tablebases::probe_wdl(Position& pos, ProbeState* result) { - *success = 1; - v = probe_ab(pos, -2, 2, success); - - // If en passant is not possible, we are done. - if (pos.ep_square() == SQ_NONE) - return v; - if (!(*success)) return 0; - - // Now handle en passant. - int v1 = -3; - // Generate (at least) all legal en passant captures. - ExtMove stack[192]; - ExtMove *moves, *end; - StateInfo st; - - if (!pos.checkers()) - end = generate(pos, stack); - else - end = generate(pos, stack); - - for (moves = stack; moves < end; moves++) { - Move capture = moves->move; - if (type_of(capture) != ENPASSANT - || !pos.legal(capture)) - continue; - pos.do_move(capture, st, pos.gives_check(capture)); - int v0 = -probe_ab(pos, -2, 2, success); - pos.undo_move(capture); - if (*success == 0) return 0; - if (v0 > v1) v1 = v0; - } - if (v1 > -3) { - if (v1 >= v) v = v1; - else if (v == 0) { - // Check whether there is at least one legal non-ep move. - for (moves = stack; moves < end; moves++) { - Move capture = moves->move; - if (type_of(capture) == ENPASSANT) continue; - if (pos.legal(capture)) break; - } - if (moves == end && !pos.checkers()) { - end = generate(pos, end); - for (; moves < end; moves++) { - Move move = moves->move; - if (pos.legal(move)) - break; - } - } - // If not, then we are forced to play the losing ep capture. - if (moves == end) - v = v1; - } - } - - return v; + *result = OK; + return search(pos, result); } -// This routine treats a position with en passant captures as one without. -static int probe_dtz_no_ep(Position& pos, int *success) -{ - int wdl, dtz; - - wdl = probe_ab(pos, -2, 2, success); - if (*success == 0) return 0; - - if (wdl == 0) return 0; - - if (*success == 2) - return wdl == 2 ? 1 : 101; - - ExtMove stack[192]; - ExtMove *moves, *end = NULL; - StateInfo st; - - if (wdl > 0) { - // Generate at least all legal non-capturing pawn moves - // including non-capturing promotions. - if (!pos.checkers()) - end = generate(pos, stack); - else - end = generate(pos, stack); - - for (moves = stack; moves < end; moves++) { - Move move = moves->move; - if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move) - || !pos.legal(move)) - continue; - pos.do_move(move, st, pos.gives_check(move)); - int v = -Tablebases::probe_wdl(pos, success); - pos.undo_move(move); - if (*success == 0) return 0; - if (v == wdl) - return v == 2 ? 1 : 101; - } - } - - dtz = 1 + probe_dtz_table(pos, wdl, success); - if (*success >= 0) { - if (wdl & 1) dtz += 100; - return wdl >= 0 ? dtz : -dtz; - } - - if (wdl > 0) { - int best = 0xffff; - for (moves = stack; moves < end; moves++) { - Move move = moves->move; - if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN - || !pos.legal(move)) - continue; - pos.do_move(move, st, pos.gives_check(move)); - int v = -Tablebases::probe_dtz(pos, success); - pos.undo_move(move); - if (*success == 0) return 0; - if (v > 0 && v + 1 < best) - best = v + 1; - } - return best; - } else { - int best = -1; - if (!pos.checkers()) - end = generate(pos, stack); - else - end = generate(pos, stack); - for (moves = stack; moves < end; moves++) { - int v; - Move move = moves->move; - if (!pos.legal(move)) - continue; - pos.do_move(move, st, pos.gives_check(move)); - if (st.rule50 == 0) { - if (wdl == -2) v = -1; - else { - v = probe_ab(pos, 1, 2, success); - v = (v == 2) ? 0 : -101; - } - } else { - v = -Tablebases::probe_dtz(pos, success) - 1; - } - pos.undo_move(move); - if (*success == 0) return 0; - if (v < best) - best = v; - } - return best; - } -} - -static int wdl_to_dtz[] = { - -1, -101, 0, 101, 1 -}; - // Probe the DTZ table for a particular position. -// If *success != 0, the probe was successful. +// If *result != FAIL, the probe was successful. // The return value is from the point of view of the side to move: // n < -100 : loss, but draw under 50-move rule // -100 <= n < -1 : loss in n ply (assuming 50-move counter == 0) @@ -578,103 +1435,90 @@ static int wdl_to_dtz[] = { // // In short, if a move is available resulting in dtz + 50-move-counter <= 99, // then do not accept moves leading to dtz + 50-move-counter == 100. -// -int Tablebases::probe_dtz(Position& pos, int *success) -{ - *success = 1; - int v = probe_dtz_no_ep(pos, success); +int Tablebases::probe_dtz(Position& pos, ProbeState* result) { - if (pos.ep_square() == SQ_NONE) - return v; - if (*success == 0) return 0; + *result = OK; + WDLScore wdl = search(pos, result); - // Now handle en passant. - int v1 = -3; + if (*result == FAIL || wdl == WDLDraw) // DTZ tables don't store draws + return 0; - ExtMove stack[192]; - ExtMove *moves, *end; - StateInfo st; + // DTZ stores a 'don't care' value in this case, or even a plain wrong + // one as in case the best move is a losing ep, so it cannot be probed. + if (*result == ZEROING_BEST_MOVE) + return dtz_before_zeroing(wdl); - if (!pos.checkers()) - end = generate(pos, stack); - else - end = generate(pos, stack); + int dtz = probe_table(pos, result, wdl); - for (moves = stack; moves < end; moves++) { - Move capture = moves->move; - if (type_of(capture) != ENPASSANT - || !pos.legal(capture)) - continue; - pos.do_move(capture, st, pos.gives_check(capture)); - int v0 = -probe_ab(pos, -2, 2, success); - pos.undo_move(capture); - if (*success == 0) return 0; - if (v0 > v1) v1 = v0; - } - if (v1 > -3) { - v1 = wdl_to_dtz[v1 + 2]; - if (v < -100) { - if (v1 >= 0) - v = v1; - } else if (v < 0) { - if (v1 >= 0 || v1 < -100) - v = v1; - } else if (v > 100) { - if (v1 > 0) - v = v1; - } else if (v > 0) { - if (v1 == 1) - v = v1; - } else if (v1 >= 0) { - v = v1; - } else { - for (moves = stack; moves < end; moves++) { - Move move = moves->move; - if (type_of(move) == ENPASSANT) continue; - if (pos.legal(move)) break; - } - if (moves == end && !pos.checkers()) { - end = generate(pos, end); - for (; moves < end; moves++) { - Move move = moves->move; - if (pos.legal(move)) - break; - } - } - if (moves == end) - v = v1; + if (*result == FAIL) + return 0; + + if (*result != CHANGE_STM) + return (dtz + 100 * (wdl == WDLBlessedLoss || wdl == WDLCursedWin)) * sign_of(wdl); + + // DTZ stores results for the other side, so we need to do a 1-ply search and + // find the winning move that minimizes DTZ. + StateInfo st; + int minDTZ = 0xFFFF; + + for (const Move& move : MoveList(pos)) + { + bool zeroing = pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN; + + pos.do_move(move, st); + + // For zeroing moves we want the dtz of the move _before_ doing it, + // otherwise we will get the dtz of the next move sequence. Search the + // position after the move to get the score sign (because even in a + // winning position we could make a losing capture or going for a draw). + dtz = zeroing ? -dtz_before_zeroing(search(pos, result)) + : -probe_dtz(pos, result); + + pos.undo_move(move); + + if (*result == FAIL) + return 0; + + // Convert result from 1-ply search. Zeroing moves are already accounted + // by dtz_before_zeroing() that returns the DTZ of the previous move. + if (!zeroing) + dtz += sign_of(dtz); + + // Skip the draws and if we are winning only pick positive dtz + if (dtz < minDTZ && sign_of(dtz) == sign_of(wdl)) + minDTZ = dtz; } - } - return v; + // Special handle a mate position, when there are no legal moves, in this + // case return value is somewhat arbitrary, so stick to the original TB code + // that returns -1 in this case. + return minDTZ == 0xFFFF ? -1 : minDTZ; } // Check whether there has been at least one repetition of positions // since the last capture or pawn move. static int has_repeated(StateInfo *st) { - while (1) { - int i = 4, e = std::min(st->rule50, st->pliesFromNull); - if (e < i) - return 0; - StateInfo *stp = st->previous->previous; - do { - stp = stp->previous->previous; - if (stp->key == st->key) - return 1; - i += 2; - } while (i <= e); - st = st->previous; - } -} + while (1) { + int i = 4, e = std::min(st->rule50, st->pliesFromNull); -static Value wdl_to_Value[5] = { - -VALUE_MATE + MAX_PLY + 1, - VALUE_DRAW - 2, - VALUE_DRAW, - VALUE_DRAW + 2, - VALUE_MATE - MAX_PLY - 1 -}; + if (e < i) + return 0; + + StateInfo *stp = st->previous->previous; + + do { + stp = stp->previous->previous; + + if (stp->key == st->key) + return 1; + + i += 2; + } while (i <= e); + + st = st->previous; + } +} // Use the DTZ tables to filter out moves that don't preserve the win or draw. // If the position is lost, but DTZ is fairly high, only keep moves that @@ -684,103 +1528,130 @@ static Value wdl_to_Value[5] = { // no moves were filtered out. bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score) { - int success; + assert(rootMoves.size()); - int dtz = probe_dtz(pos, &success); - if (!success) return false; + ProbeState result; + int dtz = probe_dtz(pos, &result); - StateInfo st; + if (result == FAIL) + return false; - // Probe each move. - for (size_t i = 0; i < rootMoves.size(); i++) { - Move move = rootMoves[i].pv[0]; - pos.do_move(move, st, pos.gives_check(move)); - int v = 0; - if (pos.checkers() && dtz > 0) { - ExtMove s[192]; - if (generate(pos, s) == s) - v = 1; + StateInfo st; + + // Probe each move + for (size_t i = 0; i < rootMoves.size(); ++i) { + Move move = rootMoves[i].pv[0]; + pos.do_move(move, st); + int v = 0; + + if (pos.checkers() && dtz > 0) { + ExtMove s[MAX_MOVES]; + + if (generate(pos, s) == s) + v = 1; + } + + if (!v) { + if (st.rule50 != 0) { + v = -probe_dtz(pos, &result); + + if (v > 0) + ++v; + else if (v < 0) + --v; + } else { + v = -probe_wdl(pos, &result); + v = dtz_before_zeroing(WDLScore(v)); + } + } + + pos.undo_move(move); + + if (result == FAIL) + return false; + + rootMoves[i].score = (Value)v; } - if (!v) { - if (st.rule50 != 0) { - v = -Tablebases::probe_dtz(pos, &success); - if (v > 0) v++; - else if (v < 0) v--; - } else { - v = -Tablebases::probe_wdl(pos, &success); - v = wdl_to_dtz[v + 2]; - } - } - pos.undo_move(move); - if (!success) return false; - rootMoves[i].score = (Value)v; - } - // Obtain 50-move counter for the root position. - // In Stockfish there seems to be no clean way, so we do it like this: - int cnt50 = st.previous->rule50; + // Obtain 50-move counter for the root position. + // In Stockfish there seems to be no clean way, so we do it like this: + int cnt50 = st.previous ? st.previous->rule50 : 0; - // Use 50-move counter to determine whether the root position is - // won, lost or drawn. - int wdl = 0; - if (dtz > 0) - wdl = (dtz + cnt50 <= 100) ? 2 : 1; - else if (dtz < 0) - wdl = (-dtz + cnt50 <= 100) ? -2 : -1; + // Use 50-move counter to determine whether the root position is + // won, lost or drawn. + WDLScore wdl = WDLDraw; - // Determine the score to report to the user. - score = wdl_to_Value[wdl + 2]; - // If the position is winning or losing, but too few moves left, adjust the - // score to show how close it is to winning or losing. - // NOTE: int(PawnValueEg) is used as scaling factor in score_to_uci(). - if (wdl == 1 && dtz <= 100) - score = (Value)(((200 - dtz - cnt50) * int(PawnValueEg)) / 200); - else if (wdl == -1 && dtz >= -100) - score = -(Value)(((200 + dtz - cnt50) * int(PawnValueEg)) / 200); + if (dtz > 0) + wdl = (dtz + cnt50 <= 100) ? WDLWin : WDLCursedWin; + else if (dtz < 0) + wdl = (-dtz + cnt50 <= 100) ? WDLLoss : WDLBlessedLoss; - // Now be a bit smart about filtering out moves. - size_t j = 0; - if (dtz > 0) { // winning (or 50-move rule draw) - int best = 0xffff; - for (size_t i = 0; i < rootMoves.size(); i++) { - int v = rootMoves[i].score; - if (v > 0 && v < best) - best = v; - } - int max = best; - // If the current phase has not seen repetitions, then try all moves - // that stay safely within the 50-move budget, if there are any. - if (!has_repeated(st.previous) && best + cnt50 <= 99) - max = 99 - cnt50; - for (size_t i = 0; i < rootMoves.size(); i++) { - int v = rootMoves[i].score; - if (v > 0 && v <= max) - rootMoves[j++] = rootMoves[i]; - } - } else if (dtz < 0) { // losing (or 50-move rule draw) - int best = 0; - for (size_t i = 0; i < rootMoves.size(); i++) { - int v = rootMoves[i].score; - if (v < best) - best = v; - } - // Try all moves, unless we approach or have a 50-move rule draw. - if (-best * 2 + cnt50 < 100) - return true; - for (size_t i = 0; i < rootMoves.size(); i++) { - if (rootMoves[i].score == best) - rootMoves[j++] = rootMoves[i]; - } - } else { // drawing - // Try all moves that preserve the draw. - for (size_t i = 0; i < rootMoves.size(); i++) { - if (rootMoves[i].score == 0) - rootMoves[j++] = rootMoves[i]; - } - } - rootMoves.resize(j, Search::RootMove(MOVE_NONE)); + // Determine the score to report to the user. + score = WDL_to_value[wdl + 2]; - return true; + // If the position is winning or losing, but too few moves left, adjust the + // score to show how close it is to winning or losing. + // NOTE: int(PawnValueEg) is used as scaling factor in score_to_uci(). + if (wdl == WDLCursedWin && dtz <= 100) + score = (Value)(((200 - dtz - cnt50) * int(PawnValueEg)) / 200); + else if (wdl == WDLBlessedLoss && dtz >= -100) + score = -(Value)(((200 + dtz - cnt50) * int(PawnValueEg)) / 200); + + // Now be a bit smart about filtering out moves. + size_t j = 0; + + if (dtz > 0) { // winning (or 50-move rule draw) + int best = 0xffff; + + for (size_t i = 0; i < rootMoves.size(); ++i) { + int v = rootMoves[i].score; + + if (v > 0 && v < best) + best = v; + } + + int max = best; + + // If the current phase has not seen repetitions, then try all moves + // that stay safely within the 50-move budget, if there are any. + if (!has_repeated(st.previous) && best + cnt50 <= 99) + max = 99 - cnt50; + + for (size_t i = 0; i < rootMoves.size(); ++i) { + int v = rootMoves[i].score; + + if (v > 0 && v <= max) + rootMoves[j++] = rootMoves[i]; + } + } else if (dtz < 0) { // losing (or 50-move rule draw) + int best = 0; + + for (size_t i = 0; i < rootMoves.size(); ++i) { + int v = rootMoves[i].score; + + if (v < best) + best = v; + } + + // Try all moves, unless we approach or have a 50-move rule draw. + if (-best * 2 + cnt50 < 100) + return true; + + for (size_t i = 0; i < rootMoves.size(); ++i) { + if (rootMoves[i].score == best) + rootMoves[j++] = rootMoves[i]; + } + } else { // drawing + // Try all moves that preserve the draw. + for (size_t i = 0; i < rootMoves.size(); ++i) { + if (rootMoves[i].score == 0) + rootMoves[j++] = rootMoves[i]; + } + } + + rootMoves.resize(j, Search::RootMove(MOVE_NONE)); + + return true; } // Use the WDL tables to filter out moves that don't preserve the win or draw. @@ -790,35 +1661,43 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves, Value& // no moves were filtered out. bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score) { - int success; + ProbeState result; - int wdl = Tablebases::probe_wdl(pos, &success); - if (!success) return false; - score = wdl_to_Value[wdl + 2]; + WDLScore wdl = Tablebases::probe_wdl(pos, &result); - StateInfo st; + if (result == FAIL) + return false; - int best = -2; + score = WDL_to_value[wdl + 2]; - // Probe each move. - for (size_t i = 0; i < rootMoves.size(); i++) { - Move move = rootMoves[i].pv[0]; - pos.do_move(move, st, pos.gives_check(move)); - int v = -Tablebases::probe_wdl(pos, &success); - pos.undo_move(move); - if (!success) return false; - rootMoves[i].score = (Value)v; - if (v > best) - best = v; - } + StateInfo st; - size_t j = 0; - for (size_t i = 0; i < rootMoves.size(); i++) { - if (rootMoves[i].score == best) - rootMoves[j++] = rootMoves[i]; - } - rootMoves.resize(j, Search::RootMove(MOVE_NONE)); + int best = WDLLoss; - return true; + // Probe each move + for (size_t i = 0; i < rootMoves.size(); ++i) { + Move move = rootMoves[i].pv[0]; + pos.do_move(move, st); + WDLScore v = -Tablebases::probe_wdl(pos, &result); + pos.undo_move(move); + + if (result == FAIL) + return false; + + rootMoves[i].score = (Value)v; + + if (v > best) + best = v; + } + + size_t j = 0; + + for (size_t i = 0; i < rootMoves.size(); ++i) { + if (rootMoves[i].score == best) + rootMoves[j++] = rootMoves[i]; + } + + rootMoves.resize(j, Search::RootMove(MOVE_NONE)); + + return true; } - diff --git a/DroidFish/jni/stockfish/syzygy/tbprobe.h b/DroidFish/jni/stockfish/syzygy/tbprobe.h index b23fdf6..f44674b 100644 --- a/DroidFish/jni/stockfish/syzygy/tbprobe.h +++ b/DroidFish/jni/stockfish/syzygy/tbprobe.h @@ -1,19 +1,79 @@ +/* + Stockfish, a UCI chess playing engine derived from Glaurung 2.1 + Copyright (c) 2013 Ronald de Man + Copyright (C) 2016-2017 Marco Costalba, Lucas Braesch + + Stockfish is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Stockfish is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + #ifndef TBPROBE_H #define TBPROBE_H +#include + #include "../search.h" namespace Tablebases { +enum WDLScore { + WDLLoss = -2, // Loss + WDLBlessedLoss = -1, // Loss, but draw under 50-move rule + WDLDraw = 0, // Draw + WDLCursedWin = 1, // Win, but draw under 50-move rule + WDLWin = 2, // Win + + WDLScoreNone = -1000 +}; + +// Possible states after a probing operation +enum ProbeState { + FAIL = 0, // Probe failed (missing file table) + OK = 1, // Probe succesful + CHANGE_STM = -1, // DTZ should check the other side + ZEROING_BEST_MOVE = 2 // Best move zeroes DTZ (capture or pawn move) +}; + extern int MaxCardinality; -void init(const std::string& path); -int probe_wdl(Position& pos, int *success); -int probe_dtz(Position& pos, int *success); +void init(const std::string& paths); +WDLScore probe_wdl(Position& pos, ProbeState* result); +int probe_dtz(Position& pos, ProbeState* result); bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score); bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score); void filter_root_moves(Position& pos, Search::RootMoves& rootMoves); +inline std::ostream& operator<<(std::ostream& os, const WDLScore v) { + + os << (v == WDLLoss ? "Loss" : + v == WDLBlessedLoss ? "Blessed loss" : + v == WDLDraw ? "Draw" : + v == WDLCursedWin ? "Cursed win" : + v == WDLWin ? "Win" : "None"); + + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const ProbeState v) { + + os << (v == FAIL ? "Failed" : + v == OK ? "Success" : + v == CHANGE_STM ? "Probed opponent side" : + v == ZEROING_BEST_MOVE ? "Best move zeroes DTZ" : "None"); + + return os; +} + } #endif diff --git a/DroidFish/jni/stockfish/thread.cpp b/DroidFish/jni/stockfish/thread.cpp index 1f1490a..d095aef 100644 --- a/DroidFish/jni/stockfish/thread.cpp +++ b/DroidFish/jni/stockfish/thread.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,177 +24,144 @@ #include "movegen.h" #include "search.h" #include "thread.h" -#include "uci.h" #include "syzygy/tbprobe.h" ThreadPool Threads; // Global object -/// Thread constructor launches the thread and then waits until it goes to sleep -/// in idle_loop(). -Thread::Thread() { +/// Thread constructor launches the thread and waits until it goes to sleep +/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set. - resetCalls = exit = false; - maxPly = callsCnt = 0; - tbHits = 0; - history.clear(); - counterMoves.clear(); - idx = Threads.size(); // Start from 0 +Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) { - std::unique_lock lk(mutex); - searching = true; - nativeThread = std::thread(&Thread::idle_loop, this); - sleepCondition.wait(lk, [&]{ return !searching; }); + wait_for_search_finished(); + clear(); // Zero-init histories (based on std::array) } -/// Thread destructor waits for thread termination before returning +/// Thread destructor wakes up the thread in idle_loop() and waits +/// for its termination. Thread should be already waiting. Thread::~Thread() { - mutex.lock(); + assert(!searching); + exit = true; - sleepCondition.notify_one(); - mutex.unlock(); - nativeThread.join(); + start_searching(); + stdThread.join(); } -/// Thread::wait_for_search_finished() waits on sleep condition -/// until not searching +/// Thread::clear() reset histories, usually before a new game + +void Thread::clear() { + + counterMoves.fill(MOVE_NONE); + mainHistory.fill(0); + + for (auto& to : contHistory) + for (auto& h : to) + h.fill(0); + + contHistory[NO_PIECE][0].fill(Search::CounterMovePruneThreshold - 1); +} + +/// Thread::start_searching() wakes up the thread that will start the search + +void Thread::start_searching() { + + std::lock_guard lk(mutex); + searching = true; + cv.notify_one(); // Wake up the thread in idle_loop() +} + + +/// Thread::wait_for_search_finished() blocks on the condition variable +/// until the thread has finished searching. void Thread::wait_for_search_finished() { std::unique_lock lk(mutex); - sleepCondition.wait(lk, [&]{ return !searching; }); + cv.wait(lk, [&]{ return !searching; }); } -/// Thread::wait() waits on sleep condition until condition is true - -void Thread::wait(std::atomic_bool& condition) { - - std::unique_lock lk(mutex); - sleepCondition.wait(lk, [&]{ return bool(condition); }); -} - - -/// Thread::start_searching() wakes up the thread that will start the search - -void Thread::start_searching(bool resume) { - - std::unique_lock lk(mutex); - - if (!resume) - searching = true; - - sleepCondition.notify_one(); -} - - -/// Thread::idle_loop() is where the thread is parked when it has no work to do +/// Thread::idle_loop() is where the thread is parked, blocked on the +/// condition variable, when it has no work to do. void Thread::idle_loop() { - while (!exit) + WinProcGroup::bindThisThread(idx); + + while (true) { std::unique_lock lk(mutex); - searching = false; + cv.notify_one(); // Wake up anyone waiting for search finished + cv.wait(lk, [&]{ return searching; }); - while (!searching && !exit) - { - sleepCondition.notify_one(); // Wake up any waiting thread - sleepCondition.wait(lk); - } + if (exit) + return; lk.unlock(); - if (!exit) - search(); + search(); } } -/// ThreadPool::init() creates and launches requested threads that will go -/// immediately to sleep. We cannot use a constructor because Threads is a -/// static object and we need a fully initialized engine at this point due to -/// allocation of Endgames in the Thread constructor. +/// ThreadPool::init() creates and launches the threads that will go +/// immediately to sleep in idle_loop. We cannot use the c'tor because +/// Threads is a static object and we need a fully initialized engine at +/// this point due to allocation of Endgames in the Thread constructor. -void ThreadPool::init() { +void ThreadPool::init(size_t requested) { - push_back(new MainThread); - read_uci_options(); + push_back(new MainThread(0)); + set(requested); } /// ThreadPool::exit() terminates threads before the program exits. Cannot be -/// done in destructor because threads must be terminated before deleting any -/// static objects while still in main(). +/// done in the destructor because threads must be terminated before deleting +/// any static object, so before main() returns. void ThreadPool::exit() { - while (size()) - delete back(), pop_back(); + main()->wait_for_search_finished(); + set(0); } -/// ThreadPool::read_uci_options() updates internal threads parameters from the -/// corresponding UCI options and creates/destroys threads to match requested -/// number. Thread objects are dynamically allocated. +/// ThreadPool::set() creates/destroys threads to match the requested number -void ThreadPool::read_uci_options() { - - size_t requested = Options["Threads"]; - - assert(requested > 0); +void ThreadPool::set(size_t requested) { while (size() < requested) - push_back(new Thread); + push_back(new Thread(size())); while (size() > requested) delete back(), pop_back(); } -/// ThreadPool::nodes_searched() returns the number of nodes searched - -uint64_t ThreadPool::nodes_searched() const { - - uint64_t nodes = 0; - for (Thread* th : *this) - nodes += th->rootPos.nodes_searched(); - return nodes; -} - - -/// ThreadPool::tb_hits() returns the number of TB hits - -uint64_t ThreadPool::tb_hits() const { - - uint64_t hits = 0; - for (Thread* th : *this) - hits += th->tbHits; - return hits; -} - - -/// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop() -/// and starts a new search, then returns immediately. +/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and +/// returns immediately. Main thread will wake up other threads and start the search. void ThreadPool::start_thinking(Position& pos, StateListPtr& states, - const Search::LimitsType& limits) { + const Search::LimitsType& limits, bool ponderMode) { main()->wait_for_search_finished(); - Search::Signals.stopOnPonderhit = Search::Signals.stop = false; + stopOnPonderhit = stop = false; + ponder = ponderMode; Search::Limits = limits; Search::RootMoves rootMoves; for (const auto& m : MoveList(pos)) if ( limits.searchmoves.empty() || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m)) - rootMoves.push_back(Search::RootMove(m)); + rootMoves.emplace_back(m); if (!rootMoves.empty()) Tablebases::filter_root_moves(pos, rootMoves); @@ -206,18 +173,22 @@ void ThreadPool::start_thinking(Position& pos, StateListPtr& states, if (states.get()) setupStates = std::move(states); // Ownership transfer, states is now empty + // We use Position::set() to set root position across threads. But there are + // some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot + // be deduced from a fen string, so set() clears them and to not lose the info + // we need to backup and later restore setupStates->back(). Note that setupStates + // is shared by threads but is accessed in read-only mode. StateInfo tmp = setupStates->back(); for (Thread* th : Threads) { - th->maxPly = 0; - th->tbHits = 0; - th->rootDepth = DEPTH_ZERO; + th->nodes = th->tbHits = 0; + th->rootDepth = th->completedDepth = DEPTH_ZERO; th->rootMoves = rootMoves; th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th); } - setupStates->back() = tmp; // Restore st->previous, cleared by Position::set() + setupStates->back() = tmp; main()->start_searching(); } diff --git a/DroidFish/jni/stockfish/thread.h b/DroidFish/jni/stockfish/thread.h index d1165bb..093b951 100644 --- a/DroidFish/jni/stockfish/thread.h +++ b/DroidFish/jni/stockfish/thread.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,7 +22,6 @@ #define THREAD_H_INCLUDED #include -#include #include #include #include @@ -36,74 +35,87 @@ #include "thread_win32.h" -/// Thread struct keeps together all the thread-related stuff. We also use -/// per-thread pawn and material hash tables so that once we get a pointer to an -/// entry its life time is unlimited and we don't have to care about someone -/// changing the entry under our feet. +/// Thread class keeps together all the thread-related stuff. We use +/// per-thread pawn and material hash tables so that once we get a +/// pointer to an entry its life time is unlimited and we don't have +/// to care about someone changing the entry under our feet. class Thread { - std::thread nativeThread; Mutex mutex; - ConditionVariable sleepCondition; - bool exit, searching; + ConditionVariable cv; + size_t idx; + bool exit = false, searching = true; // Set before starting std::thread + std::thread stdThread; public: - Thread(); + explicit Thread(size_t); virtual ~Thread(); virtual void search(); + void clear(); void idle_loop(); - void start_searching(bool resume = false); + void start_searching(); void wait_for_search_finished(); - void wait(std::atomic_bool& b); Pawns::Table pawnsTable; Material::Table materialTable; Endgames endgames; - size_t idx, PVIdx; - int maxPly, callsCnt; - uint64_t tbHits; + size_t PVIdx; + int selDepth; + std::atomic nodes, tbHits; Position rootPos; Search::RootMoves rootMoves; - Depth rootDepth; - Depth completedDepth; - std::atomic_bool resetCalls; - HistoryStats history; - MoveStats counterMoves; - FromToStats fromTo; - CounterMoveHistoryStats counterMoveHistory; + Depth rootDepth, completedDepth; + CounterMoveHistory counterMoves; + ButterflyHistory mainHistory; + ContinuationHistory contHistory; }; -/// MainThread is a derived class with a specific overload for the main thread +/// MainThread is a derived class specific for main thread struct MainThread : public Thread { - virtual void search(); + + using Thread::Thread; + + void search() override; + void check_time(); bool easyMovePlayed, failedLow; double bestMoveChanges; Value previousScore; + int callsCnt; }; /// ThreadPool struct handles all the threads-related stuff like init, starting, /// parking and, most importantly, launching a thread. All the access to threads -/// data is done through this class. +/// is done through this class. struct ThreadPool : public std::vector { - void init(); // No constructor and destructor, threads rely on globals that should - void exit(); // be initialized and valid during the whole thread lifetime. + void init(size_t); // No constructor and destructor, threads rely on globals that should + void exit(); // be initialized and valid during the whole thread lifetime. + void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false); + void set(size_t); - MainThread* main() { return static_cast(at(0)); } - void start_thinking(Position&, StateListPtr&, const Search::LimitsType&); - void read_uci_options(); - uint64_t nodes_searched() const; - uint64_t tb_hits() const; + MainThread* main() const { return static_cast(front()); } + uint64_t nodes_searched() const { return accumulate(&Thread::nodes); } + uint64_t tb_hits() const { return accumulate(&Thread::tbHits); } + + std::atomic_bool stop, ponder, stopOnPonderhit; private: StateListPtr setupStates; + + uint64_t accumulate(std::atomic Thread::* member) const { + + uint64_t sum = 0; + for (Thread* th : *this) + sum += (th->*member).load(std::memory_order_relaxed); + return sum; + } }; extern ThreadPool Threads; diff --git a/DroidFish/jni/stockfish/thread_win32.h b/DroidFish/jni/stockfish/thread_win32.h index 47516c6..917563a 100644 --- a/DroidFish/jni/stockfish/thread_win32.h +++ b/DroidFish/jni/stockfish/thread_win32.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/DroidFish/jni/stockfish/timeman.cpp b/DroidFish/jni/stockfish/timeman.cpp index 6d3b731..2612fb5 100644 --- a/DroidFish/jni/stockfish/timeman.cpp +++ b/DroidFish/jni/stockfish/timeman.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,8 +19,6 @@ */ #include -#include -#include #include "search.h" #include "timeman.h" @@ -32,41 +30,43 @@ namespace { enum TimeType { OptimumTime, MaxTime }; - const int MoveHorizon = 50; // Plan time management at most this many moves ahead - const double MaxRatio = 7.09; // When in trouble, we can step over reserved time with this ratio - const double StealRatio = 0.35; // However we must not steal time from remaining moves over this ratio + int remaining(int myTime, int myInc, int moveOverhead, int movesToGo, + int moveNum, bool ponder, TimeType type) { + if (myTime <= 0) + return 0; - // move_importance() is a skew-logistic function based on naive statistical - // analysis of "how many games are still undecided after n half-moves". Game - // is considered "undecided" as long as neither side has >275cp advantage. - // Data was extracted from the CCRL game database with some simple filtering criteria. + double ratio; // Which ratio of myTime we are going to use - double move_importance(int ply) { + // Usage of increment follows quadratic distribution with the maximum at move 25 + double inc = myInc * std::max(55.0, 120 - 0.12 * (moveNum - 25) * (moveNum - 25)); - const double XScale = 7.64; - const double XShift = 58.4; - const double Skew = 0.183; + // In moves-to-go we distribute time according to a quadratic function with + // the maximum around move 20 for 40 moves in y time case. + if (movesToGo) + { + ratio = (type == OptimumTime ? 1.0 : 6.0) / std::min(50, movesToGo); - return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero - } + if (moveNum <= 40) + ratio *= 1.1 - 0.001 * (moveNum - 20) * (moveNum - 20); + else + ratio *= 1.5; - template - int remaining(int myTime, int movesToGo, int ply, int slowMover) { + ratio *= 1 + inc / (myTime * 8.5); + } + // Otherwise we increase usage of remaining time as the game goes on + else + { + double k = 1 + 20 * moveNum / (500.0 + moveNum); + ratio = (type == OptimumTime ? 0.017 : 0.07) * (k + inc / myTime); + } - const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); - const double TStealRatio = (T == OptimumTime ? 0 : StealRatio); + int time = int(std::min(1.0, ratio) * std::max(0, myTime - moveOverhead)); - double moveImportance = (move_importance(ply) * slowMover) / 100; - double otherMovesImportance = 0; + if (type == OptimumTime && ponder) + time = 5 * time / 4; - for (int i = 1; i < movesToGo; ++i) - otherMovesImportance += move_importance(ply + 2 * i); - - double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance); - double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance); - - return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast + return time; } } // namespace @@ -81,12 +81,11 @@ namespace { /// inc > 0 && movestogo == 0 means: x basetime + z increment /// inc > 0 && movestogo != 0 means: x moves in y minutes + z increment -void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { - - int minThinkingTime = Options["Minimum Thinking Time"]; - int moveOverhead = Options["Move Overhead"]; - int slowMover = Options["Slow Mover"]; - int npmsec = Options["nodestime"]; +void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) +{ + int moveOverhead = Options["Move Overhead"]; + int npmsec = Options["nodestime"]; + bool ponder = Options["Ponder"]; // If we have to play in 'nodes as time' mode, then convert from time // to nodes, and use resulting values in time management formulas. @@ -103,30 +102,11 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { limits.npmsec = npmsec; } + int moveNum = (ply + 1) / 2; + startTime = limits.startTime; - optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime); - - const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon; - - // We calculate optimum time usage for different hypothetical "moves to go"-values - // and choose the minimum of calculated search time values. Usually the greatest - // hypMTG gives the minimum values. - for (int hypMTG = 1; hypMTG <= MaxMTG; ++hypMTG) - { - // Calculate thinking time for hypothetical "moves to go"-value - int hypMyTime = limits.time[us] - + limits.inc[us] * (hypMTG - 1) - - moveOverhead * (2 + std::min(hypMTG, 40)); - - hypMyTime = std::max(hypMyTime, 0); - - int t1 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); - int t2 = minThinkingTime + remaining(hypMyTime, hypMTG, ply, slowMover); - - optimumTime = std::min(t1, optimumTime); - maximumTime = std::min(t2, maximumTime); - } - - if (Options["Ponder"]) - optimumTime += optimumTime / 4; + optimumTime = remaining(limits.time[us], limits.inc[us], moveOverhead, + limits.movestogo, moveNum, ponder, OptimumTime); + maximumTime = remaining(limits.time[us], limits.inc[us], moveOverhead, + limits.movestogo, moveNum, ponder, MaxTime); } diff --git a/DroidFish/jni/stockfish/timeman.h b/DroidFish/jni/stockfish/timeman.h index 9930a4b..c22c8c2 100644 --- a/DroidFish/jni/stockfish/timeman.h +++ b/DroidFish/jni/stockfish/timeman.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/DroidFish/jni/stockfish/tt.cpp b/DroidFish/jni/stockfish/tt.cpp index f5b72ba..f283d0c 100644 --- a/DroidFish/jni/stockfish/tt.cpp +++ b/DroidFish/jni/stockfish/tt.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/DroidFish/jni/stockfish/tt.h b/DroidFish/jni/stockfish/tt.h index 677f38e..24045ed 100644 --- a/DroidFish/jni/stockfish/tt.h +++ b/DroidFish/jni/stockfish/tt.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/DroidFish/jni/stockfish/types.h b/DroidFish/jni/stockfish/types.h index 519f6af..acca874 100644 --- a/DroidFish/jni/stockfish/types.h +++ b/DroidFish/jni/stockfish/types.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -76,7 +76,7 @@ # include // Header for _pext_u64() intrinsic # define pext(b, m) _pext_u64(b, m) #else -# define pext(b, m) (0) +# define pext(b, m) 0 #endif #ifdef USE_POPCNT @@ -128,7 +128,7 @@ enum MoveType { }; enum Color { - WHITE, BLACK, NO_COLOR, COLOR_NB = 2 + WHITE, BLACK, COLOR_NB = 2 }; enum CastlingSide { @@ -183,11 +183,11 @@ enum Value : int { VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY, VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY, - PawnValueMg = 188, PawnValueEg = 248, - KnightValueMg = 753, KnightValueEg = 832, - BishopValueMg = 826, BishopValueEg = 897, - RookValueMg = 1285, RookValueEg = 1371, - QueenValueMg = 2513, QueenValueEg = 2650, + PawnValueMg = 171, PawnValueEg = 240, + KnightValueMg = 764, KnightValueEg = 848, + BishopValueMg = 826, BishopValueEg = 891, + RookValueMg = 1282, RookValueEg = 1373, + QueenValueMg = 2526, QueenValueEg = 2646, MidgameLimit = 15258, EndgameLimit = 3915 }; @@ -205,11 +205,9 @@ enum Piece { PIECE_NB = 16 }; -const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, - B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING }; extern Value PieceValue[PHASE_NB][PIECE_NB]; -enum Depth { +enum Depth : int { ONE_PLY = 1, @@ -239,8 +237,8 @@ enum Square { NORTH = 8, EAST = 1, - SOUTH = -8, - WEST = -1, + SOUTH = -NORTH, + WEST = -EAST, NORTH_EAST = NORTH + EAST, SOUTH_EAST = SOUTH + EAST, @@ -285,19 +283,19 @@ inline Value mg_value(Score s) { #define ENABLE_BASE_OPERATORS_ON(T) \ inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \ inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \ -inline T operator*(int i, T d) { return T(i * int(d)); } \ -inline T operator*(T d, int i) { return T(int(d) * i); } \ inline T operator-(T d) { return T(-int(d)); } \ inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \ inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \ -inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } #define ENABLE_FULL_OPERATORS_ON(T) \ ENABLE_BASE_OPERATORS_ON(T) \ +inline T operator*(int i, T d) { return T(i * int(d)); } \ +inline T operator*(T d, int i) { return T(int(d) * i); } \ inline T& operator++(T& d) { return d = T(int(d) + 1); } \ inline T& operator--(T& d) { return d = T(int(d) - 1); } \ inline T operator/(T d, int i) { return T(int(d) / i); } \ inline int operator/(T d1, T d2) { return int(d1) / int(d2); } \ +inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \ inline T& operator/=(T& d, int i) { return d = T(int(d) / i); } ENABLE_FULL_OPERATORS_ON(Value) @@ -329,6 +327,18 @@ inline Score operator/(Score s, int i) { return make_score(mg_value(s) / i, eg_value(s) / i); } +/// Multiplication of a Score by an integer. We check for overflow in debug mode. +inline Score operator*(Score s, int i) { + + Score result = Score(int(s) * i); + + assert(eg_value(result) == (i * eg_value(s))); + assert(mg_value(result) == (i * mg_value(s))); + assert((i == 0) || (result / i) == s ); + + return result; +} + inline Color operator~(Color c) { return Color(c ^ BLACK); // Toggle color } @@ -411,6 +421,10 @@ inline Square to_sq(Move m) { return Square(m & 0x3F); } +inline int from_to(Move m) { + return m & 0xFFF; +} + inline MoveType type_of(Move m) { return MoveType(m & (3 << 14)); } diff --git a/DroidFish/jni/stockfish/uci.cpp b/DroidFish/jni/stockfish/uci.cpp index b195b87..fc66f0b 100644 --- a/DroidFish/jni/stockfish/uci.cpp +++ b/DroidFish/jni/stockfish/uci.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include #include @@ -27,30 +28,27 @@ #include "position.h" #include "search.h" #include "thread.h" +#include "tt.h" #include "timeman.h" #include "uci.h" +#include "syzygy/tbprobe.h" using namespace std; -extern void benchmark(const Position& pos, istream& is); +extern vector setup_bench(const Position&, istream&); namespace { // FEN string of the initial position, normal chess const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; - // A list to keep track of the position states along the setup moves (from the - // start position to the position just before the search starts). Needed by - // 'draw by repetition' detection. - StateListPtr States(new std::deque(1)); - // position() is called when engine receives the "position" UCI command. // The function sets up the position described in the given FEN string ("fen") // or the starting position ("startpos") and then makes the moves given in the // following move list ("moves"). - void position(Position& pos, istringstream& is) { + void position(Position& pos, istringstream& is, StateListPtr& states) { Move m; string token, fen; @@ -68,14 +66,14 @@ namespace { else return; - States = StateListPtr(new std::deque(1)); - pos.set(fen, Options["UCI_Chess960"], &States->back(), Threads.main()); + states = StateListPtr(new std::deque(1)); // Drop old and create a new one + pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main()); // Parse move list (if any) while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE) { - States->push_back(StateInfo()); - pos.do_move(m, States->back(), pos.gives_check(m)); + states->emplace_back(); + pos.do_move(m, states->back()); } } @@ -108,10 +106,11 @@ namespace { // the thinking time and other parameters from the input string, then starts // the search. - void go(Position& pos, istringstream& is) { + void go(Position& pos, istringstream& is, StateListPtr& states) { Search::LimitsType limits; string token; + bool ponderMode = false; limits.startTime = now(); // As early as possible! @@ -129,10 +128,53 @@ namespace { else if (token == "nodes") is >> limits.nodes; else if (token == "movetime") is >> limits.movetime; else if (token == "mate") is >> limits.mate; + else if (token == "perft") is >> limits.perft; else if (token == "infinite") limits.infinite = 1; - else if (token == "ponder") limits.ponder = 1; + else if (token == "ponder") ponderMode = true; - Threads.start_thinking(pos, States, limits); + Threads.start_thinking(pos, states, limits, ponderMode); + } + + + // bench() is called when engine receives the "bench" command. Firstly + // a list of UCI commands is setup according to bench parameters, then + // it is run one by one printing a summary at the end. + + void bench(Position& pos, istream& args, StateListPtr& states) { + + string token; + uint64_t num, nodes = 0, cnt = 1; + + vector list = setup_bench(pos, args); + num = count_if(list.begin(), list.end(), [](string s) { return s.find("go ") == 0; }); + + TimePoint elapsed = now(); + + for (const auto& cmd : list) + { + istringstream is(cmd); + is >> skipws >> token; + + if (token == "go") + { + cerr << "\nPosition: " << cnt++ << '/' << num << endl; + go(pos, is, states); + Threads.main()->wait_for_search_finished(); + nodes += Threads.nodes_searched(); + } + else if (token == "setoption") setoption(is); + else if (token == "position") position(pos, is, states); + else if (token == "ucinewgame") Search::clear(); + } + + elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero' + + dbg_print(); // Just before exiting + + cerr << "\n===========================" + << "\nTotal time (ms) : " << elapsed + << "\nNodes searched : " << nodes + << "\nNodes/second : " << 1000 * nodes / elapsed << endl; } } // namespace @@ -148,8 +190,10 @@ void UCI::loop(int argc, char* argv[]) { Position pos; string token, cmd; + StateListPtr states(new std::deque(1)); + auto uiThread = std::make_shared(0); - pos.set(StartFEN, false, &States->back(), Threads.main()); + pos.set(StartFEN, false, &states->back(), uiThread.get()); for (int i = 1; i < argc; ++i) cmd += std::string(argv[i]) + " "; @@ -160,61 +204,42 @@ void UCI::loop(int argc, char* argv[]) { istringstream is(cmd); - token.clear(); // getline() could return empty or blank line + token.clear(); // Avoid a stale if getline() returns empty or blank line is >> skipws >> token; - // The GUI sends 'ponderhit' to tell us to ponder on the same move the - // opponent has played. In case Signals.stopOnPonderhit is set we are - // waiting for 'ponderhit' to stop the search (for instance because we - // already ran out of time), otherwise we should continue searching but - // switching from pondering to normal search. + // The GUI sends 'ponderhit' to tell us the user has played the expected move. + // So 'ponderhit' will be sent if we were told to ponder on the same move the + // user has played. We should continue searching but switch from pondering to + // normal search. In case Threads.stopOnPonderhit is set we are waiting for + // 'ponderhit' to stop the search, for instance if max search depth is reached. if ( token == "quit" || token == "stop" - || (token == "ponderhit" && Search::Signals.stopOnPonderhit)) - { - Search::Signals.stop = true; - Threads.main()->start_searching(true); // Could be sleeping - } + || (token == "ponderhit" && Threads.stopOnPonderhit)) + Threads.stop = true; + else if (token == "ponderhit") - Search::Limits.ponder = 0; // Switch to normal search + Threads.ponder = false; // Switch to normal search else if (token == "uci") sync_cout << "id name " << engine_info(true) << "\n" << Options << "\nuciok" << sync_endl; - else if (token == "ucinewgame") - { - Search::clear(); - Time.availableNodes = 0; - } - else if (token == "isready") sync_cout << "readyok" << sync_endl; - else if (token == "go") go(pos, is); - else if (token == "position") position(pos, is); else if (token == "setoption") setoption(is); + else if (token == "go") go(pos, is, states); + else if (token == "position") position(pos, is, states); + else if (token == "ucinewgame") Search::clear(); + else if (token == "isready") sync_cout << "readyok" << sync_endl; - // Additional custom non-UCI commands, useful for debugging - else if (token == "flip") pos.flip(); - else if (token == "bench") benchmark(pos, is); - else if (token == "d") sync_cout << pos << sync_endl; - else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl; - else if (token == "perft") - { - int depth; - stringstream ss; - - is >> depth; - ss << Options["Hash"] << " " - << Options["Threads"] << " " << depth << " current perft"; - - benchmark(pos, ss); - } + // Additional custom non-UCI commands, mainly for debugging + else if (token == "flip") pos.flip(); + else if (token == "bench") bench(pos, is, states); + else if (token == "d") sync_cout << pos << sync_endl; + else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl; else sync_cout << "Unknown command: " << cmd << sync_endl; - } while (token != "quit" && argc == 1); // Passed args have one-shot behaviour - - Threads.main()->wait_for_search_finished(); + } while (token != "quit" && argc == 1); // Command line args are one-shot } @@ -227,6 +252,8 @@ void UCI::loop(int argc, char* argv[]) { string UCI::value(Value v) { + assert(-VALUE_INFINITE < v && v < VALUE_INFINITE); + stringstream ss; if (abs(v) < VALUE_MATE - MAX_PLY) diff --git a/DroidFish/jni/stockfish/uci.h b/DroidFish/jni/stockfish/uci.h index 4697877..e6b31c5 100644 --- a/DroidFish/jni/stockfish/uci.h +++ b/DroidFish/jni/stockfish/uci.h @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -49,7 +49,7 @@ public: Option(OnChange = nullptr); Option(bool v, OnChange = nullptr); Option(const char* v, OnChange = nullptr); - Option(int v, int min, int max, OnChange = nullptr); + Option(int v, int minv, int maxv, OnChange = nullptr); Option& operator=(const std::string&); void operator<<(const Option&); diff --git a/DroidFish/jni/stockfish/ucioption.cpp b/DroidFish/jni/stockfish/ucioption.cpp index 99e5ea6..062b52d 100644 --- a/DroidFish/jni/stockfish/ucioption.cpp +++ b/DroidFish/jni/stockfish/ucioption.cpp @@ -2,7 +2,7 @@ Stockfish, a UCI chess playing engine derived from Glaurung 2.1 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad - Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad + Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad Stockfish is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -39,7 +39,7 @@ namespace UCI { void on_clear_hash(const Option&) { Search::clear(); } void on_hash_size(const Option& o) { TT.resize(o); } void on_logger(const Option& o) { start_logger(o); } -void on_threads(const Option&) { Threads.read_uci_options(); } +void on_threads(const Option& o) { Threads.set(o); } void on_tb_path(const Option& o) { Tablebases::init(o); } @@ -59,15 +59,13 @@ void init(OptionsMap& o) { o["Debug Log File"] << Option("", on_logger); o["Contempt"] << Option(0, -100, 100); - o["Threads"] << Option(1, 1, 128, on_threads); + o["Threads"] << Option(1, 1, 512, on_threads); o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size); o["Clear Hash"] << Option(on_clear_hash); o["Ponder"] << Option(false); o["MultiPV"] << Option(1, 1, 500); o["Skill Level"] << Option(20, 0, 20); - o["Move Overhead"] << Option(30, 0, 5000); - o["Minimum Thinking Time"] << Option(20, 0, 5000); - o["Slow Mover"] << Option(89, 10, 1000); + o["Move Overhead"] << Option(60, 0, 5000); o["nodestime"] << Option(0, 0, 10000); o["UCI_Chess960"] << Option(false); o["SyzygyPath"] << Option("", on_tb_path);