]> git.sesse.net Git - stockfish/blob - src/bitbase.cpp
Fix for incorrect VALUE_MATE_IN_MAX_PLY usage.
[stockfish] / src / bitbase.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cassert>
22 #include <vector>
23 #include <bitset>
24
25 #include "bitboard.h"
26 #include "types.h"
27
28 namespace {
29
30   // There are 24 possible pawn squares: files A to D and ranks from 2 to 7.
31   // Positions with the pawn on files E to H will be mirrored before probing.
32   constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
33
34   std::bitset<MAX_INDEX> KPKBitbase;
35
36   // A KPK bitbase index is an integer in [0, IndexMax] range
37   //
38   // Information is mapped in a way that minimizes the number of iterations:
39   //
40   // bit  0- 5: white king square (from SQ_A1 to SQ_H8)
41   // bit  6-11: black king square (from SQ_A1 to SQ_H8)
42   // bit    12: side to move (WHITE or BLACK)
43   // bit 13-14: white pawn file (from FILE_A to FILE_D)
44   // bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
45   unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
46     return int(wksq) | (bksq << 6) | (stm << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
47   }
48
49   enum Result {
50     INVALID = 0,
51     UNKNOWN = 1,
52     DRAW    = 2,
53     WIN     = 4
54   };
55
56   Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
57
58   struct KPKPosition {
59     KPKPosition() = default;
60     explicit KPKPosition(unsigned idx);
61     operator Result() const { return result; }
62     Result classify(const std::vector<KPKPosition>& db);
63
64     Color stm;
65     Square ksq[COLOR_NB], psq;
66     Result result;
67   };
68
69 } // namespace
70
71
72 bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
73
74   assert(file_of(wpsq) <= FILE_D);
75
76   return KPKBitbase[index(stm, bksq, wksq, wpsq)];
77 }
78
79
80 void Bitbases::init() {
81
82   std::vector<KPKPosition> db(MAX_INDEX);
83   unsigned idx, repeat = 1;
84
85   // Initialize db with known win / draw positions
86   for (idx = 0; idx < MAX_INDEX; ++idx)
87       db[idx] = KPKPosition(idx);
88
89   // Iterate through the positions until none of the unknown positions can be
90   // changed to either wins or draws (15 cycles needed).
91   while (repeat)
92       for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
93           repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
94
95   // Fill the bitbase with the decisive results
96   for (idx = 0; idx < MAX_INDEX; ++idx)
97       if (db[idx] == WIN)
98           KPKBitbase.set(idx);
99 }
100
101
102 namespace {
103
104   KPKPosition::KPKPosition(unsigned idx) {
105
106     ksq[WHITE] = Square((idx >>  0) & 0x3F);
107     ksq[BLACK] = Square((idx >>  6) & 0x3F);
108     stm        = Color ((idx >> 12) & 0x01);
109     psq        = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
110
111     // Check if two pieces are on the same square or if a king can be captured
112     if (   distance(ksq[WHITE], ksq[BLACK]) <= 1
113         || ksq[WHITE] == psq
114         || ksq[BLACK] == psq
115         || (stm == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
116         result = INVALID;
117
118     // Immediate win if a pawn can be promoted without getting captured
119     else if (   stm == WHITE
120              && rank_of(psq) == RANK_7
121              && ksq[stm] != psq + NORTH
122              && (    distance(ksq[~stm], psq + NORTH) > 1
123                  || (PseudoAttacks[KING][ksq[stm]] & (psq + NORTH))))
124         result = WIN;
125
126     // Immediate draw if it is a stalemate or a king captures undefended pawn
127     else if (   stm == BLACK
128              && (  !(PseudoAttacks[KING][ksq[stm]] & ~(PseudoAttacks[KING][ksq[~stm]] | PawnAttacks[~stm][psq]))
129                  || (PseudoAttacks[KING][ksq[stm]] & psq & ~PseudoAttacks[KING][ksq[~stm]])))
130         result = DRAW;
131
132     // Position will be classified later
133     else
134         result = UNKNOWN;
135   }
136
137   Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
138
139     // White to move: If one move leads to a position classified as WIN, the result
140     // of the current position is WIN. If all moves lead to positions classified
141     // as DRAW, the current position is classified as DRAW, otherwise the current
142     // position is classified as UNKNOWN.
143     //
144     // Black to move: If one move leads to a position classified as DRAW, the result
145     // of the current position is DRAW. If all moves lead to positions classified
146     // as WIN, the position is classified as WIN, otherwise the current position is
147     // classified as UNKNOWN.
148     const Result Good = (stm == WHITE ? WIN   : DRAW);
149     const Result Bad  = (stm == WHITE ? DRAW  : WIN);
150
151     Result r = INVALID;
152     Bitboard b = PseudoAttacks[KING][ksq[stm]];
153
154     while (b)
155         r |= stm == WHITE ? db[index(BLACK, ksq[BLACK] , pop_lsb(&b), psq)]
156                           : db[index(WHITE, pop_lsb(&b),  ksq[WHITE], psq)];
157
158     if (stm == WHITE)
159     {
160         if (rank_of(psq) < RANK_7)      // Single push
161             r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH)];
162
163         if (   rank_of(psq) == RANK_2   // Double push
164             && psq + NORTH != ksq[WHITE]
165             && psq + NORTH != ksq[BLACK])
166             r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH + NORTH)];
167     }
168
169     return result = r & Good  ? Good  : r & UNKNOWN ? UNKNOWN : Bad;
170   }
171
172 } // namespace