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