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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 // The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
29 const unsigned IndexMax = 24*64*64*2; // wp_sq * wk_sq * bk_sq * stm = 196608
31 // Each uint32_t stores results of 32 positions, one per bit
32 uint32_t KPKBitbase[IndexMax / 32];
34 // A KPK bitbase index is an integer in [0, IndexMax] range
36 // Information is mapped in this way
38 // bit 0: side to move (WHITE or BLACK)
39 // bit 1- 6: black king square (from SQ_A1 to SQ_H8)
40 // bit 7-12: white king square (from SQ_A1 to SQ_H8)
41 // bit 13-14: white pawn file (from FILE_A to FILE_D)
42 // bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
43 unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
44 return stm + (bksq << 1) + (wksq << 7) + (file_of(psq) << 13) + ((rank_of(psq) - 1) << 15);
54 inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
58 void classify_leaf(unsigned idx);
60 Result classify(const std::vector<KPKPosition>& db)
61 { return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
63 operator Result() const { return res; }
66 template<Color Us> Bitboard k_attacks() const
67 { return StepAttacksBB[KING][Us == WHITE ? wksq : bksq]; }
69 template<Color Us> Result classify(const std::vector<KPKPosition>& db);
72 Square bksq, wksq, psq;
79 bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
81 assert(file_of(wpsq) <= FILE_D);
83 unsigned idx = index(stm, bksq, wksq, wpsq);
84 return KPKBitbase[idx / 32] & (1 << (idx & 31));
88 void Bitbases::init_kpk() {
90 unsigned idx, repeat = 1;
91 std::vector<KPKPosition> db(IndexMax);
93 // Initialize db with known win / draw positions
94 for (idx = 0; idx < IndexMax; idx++)
95 db[idx].classify_leaf(idx);
97 // Iterate until all positions are classified (30 cycles needed)
99 for (repeat = idx = 0; idx < IndexMax; idx++)
100 if (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN)
103 // Map 32 results into one KPKBitbase[] entry
104 for (idx = 0; idx < IndexMax; idx++)
106 KPKBitbase[idx / 32] |= 1 << (idx & 31);
112 void KPKPosition::classify_leaf(unsigned idx) {
114 stm = Color(idx & 1);
115 bksq = Square((idx >> 1) & 0x3F);
116 wksq = Square((idx >> 7) & 0x3F);
117 psq = File((idx >> 13) & 3) | Rank((idx >> 15) + 1);
119 // Check if two pieces are on the same square or if a king can be captured
120 if ( wksq == psq || wksq == bksq || bksq == psq
121 || (k_attacks<WHITE>() & bksq)
122 || (stm == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
125 // The position is an immediate win if it is white to move and the white
126 // pawn can be promoted without getting captured.
127 else if ( rank_of(psq) == RANK_7
129 && wksq != psq + DELTA_N
130 && ( square_distance(bksq, psq + DELTA_N) > 1
131 ||(k_attacks<WHITE>() & (psq + DELTA_N))))
134 // Check for known draw positions
137 else if ( stm == BLACK
138 && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | StepAttacksBB[PAWN][psq])))
141 // Case 2: King can capture undefended pawn
142 else if ( stm == BLACK
143 && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
146 // Case 3: Black king in front of white pawn
147 else if ( bksq == psq + DELTA_N
148 && rank_of(psq) < RANK_7)
151 // Case 4: White king in front of pawn and black has opposition
152 else if ( stm == WHITE
153 && wksq == psq + DELTA_N
154 && bksq == wksq + DELTA_N + DELTA_N
155 && rank_of(psq) < RANK_5)
158 // Case 5: Stalemate with rook pawn
159 else if ( bksq == SQ_A8
160 && file_of(psq) == FILE_A)
163 // Case 6: White king trapped on the rook file
164 else if ( file_of(wksq) == FILE_A
165 && file_of(psq) == FILE_A
166 && rank_of(wksq) > rank_of(psq)
175 Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
177 // White to Move: If one move leads to a position classified as RESULT_WIN,
178 // the result of the current position is RESULT_WIN. If all moves lead to
179 // positions classified as RESULT_DRAW, the current position is classified
180 // RESULT_DRAW otherwise the current position is classified as RESULT_UNKNOWN.
182 // Black to Move: If one move leads to a position classified as RESULT_DRAW,
183 // the result of the current position is RESULT_DRAW. If all moves lead to
184 // positions classified as RESULT_WIN, the position is classified RESULT_WIN.
185 // Otherwise, the current position is classified as RESULT_UNKNOWN.
188 Bitboard b = k_attacks<Us>();
192 r |= Us == WHITE ? db[index(BLACK, bksq, pop_lsb(&b), psq)]
193 : db[index(WHITE, pop_lsb(&b), wksq, psq)];
195 if (Us == WHITE && (r & WIN))
198 if (Us == BLACK && (r & DRAW))
202 if (Us == WHITE && rank_of(psq) < RANK_7)
204 Square s = psq + DELTA_N;
205 r |= db[index(BLACK, bksq, wksq, s)]; // Single push
207 if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
208 r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
214 return res = r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;