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/>.
34 inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
38 Result classify_leaf(int idx);
39 Result classify(int idx, Result db[]);
42 template<Color Us> Result classify(const Result db[]) const;
44 template<Color Us> Bitboard k_attacks() const {
45 return Us == WHITE ? StepAttacksBB[W_KING][wksq] : StepAttacksBB[B_KING][bksq];
48 Bitboard p_attacks() const { return StepAttacksBB[W_PAWN][psq]; }
49 void decode_index(int idx);
51 Square wksq, bksq, psq;
55 // The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
56 const int IndexMax = 2 * 24 * 64 * 64; // stm * wp_sq * wk_sq * bk_sq = 196608
58 // Each uint32_t stores results of 32 positions, one per bit
59 uint32_t KPKBitbase[IndexMax / 32];
61 int index(Square wksq, Square bksq, Square psq, Color stm);
65 uint32_t Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
67 int idx = index(wksq, bksq, wpsq, stm);
68 return KPKBitbase[idx / 32] & (1 << (idx & 31));
72 void Bitbases::init_kpk() {
76 int idx, bit, repeat = 1;
78 // Initialize table with known win / draw positions
79 for (idx = 0; idx < IndexMax; idx++)
80 db[idx] = pos.classify_leaf(idx);
82 // Iterate until all positions are classified (30 cycles needed)
84 for (repeat = idx = 0; idx < IndexMax; idx++)
85 if (db[idx] == UNKNOWN && (db[idx] = pos.classify(idx, db)) != UNKNOWN)
88 // Map 32 position results into one KPKBitbase[] entry
89 for (idx = 0; idx < IndexMax / 32; idx++)
90 for (bit = 0; bit < 32; bit++)
91 if (db[32 * idx + bit] == WIN)
92 KPKBitbase[idx] |= 1 << bit;
98 // A KPK bitbase index is an integer in [0, IndexMax] range
100 // Information is mapped in this way
102 // bit 0: side to move (WHITE or BLACK)
103 // bit 1- 6: black king square (from SQ_A1 to SQ_H8)
104 // bit 7-12: white king square (from SQ_A1 to SQ_H8)
105 // bit 13-14: white pawn file (from FILE_A to FILE_D)
106 // bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
108 int index(Square w, Square b, Square p, Color c) {
110 assert(file_of(p) <= FILE_D);
112 return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
115 void KPKPosition::decode_index(int idx) {
117 stm = Color(idx & 1);
118 bksq = Square((idx >> 1) & 63);
119 wksq = Square((idx >> 7) & 63);
120 psq = File((idx >> 13) & 3) | Rank((idx >> 15) + 1);
123 Result KPKPosition::classify_leaf(int idx) {
127 // Check if two pieces are on the same square or if a king can be captured
128 if ( wksq == psq || wksq == bksq || bksq == psq
129 || (k_attacks<WHITE>() & bksq)
130 || (stm == WHITE && (p_attacks() & bksq)))
133 // The position is an immediate win if it is white to move and the white
134 // pawn can be promoted without getting captured.
135 if ( rank_of(psq) == RANK_7
137 && wksq != psq + DELTA_N
138 && ( square_distance(bksq, psq + DELTA_N) > 1
139 ||(k_attacks<WHITE>() & (psq + DELTA_N))))
142 // Check for known draw positions
146 && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
149 // Case 2: King can capture undefended pawn
151 && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
154 // Case 3: Black king in front of white pawn
155 if ( bksq == psq + DELTA_N
156 && rank_of(psq) < RANK_7)
159 // Case 4: White king in front of pawn and black has opposition
161 && wksq == psq + DELTA_N
162 && bksq == wksq + DELTA_N + DELTA_N
163 && rank_of(psq) < RANK_5)
166 // Case 5: Stalemate with rook pawn
168 && file_of(psq) == FILE_A)
171 // Case 6: White king trapped on the rook file
172 if ( file_of(wksq) == FILE_A
173 && file_of(psq) == FILE_A
174 && rank_of(wksq) > rank_of(psq)
182 Result KPKPosition::classify(const Result db[]) const {
184 // White to Move: If one move leads to a position classified as RESULT_WIN,
185 // the result of the current position is RESULT_WIN. If all moves lead to
186 // positions classified as RESULT_DRAW, the current position is classified
187 // RESULT_DRAW otherwise the current position is classified as RESULT_UNKNOWN.
189 // Black to Move: If one move leads to a position classified as RESULT_DRAW,
190 // the result of the current position is RESULT_DRAW. If all moves lead to
191 // positions classified as RESULT_WIN, the position is classified RESULT_WIN.
192 // Otherwise, the current position is classified as RESULT_UNKNOWN.
195 Bitboard b = k_attacks<Us>();
199 r |= Us == WHITE ? db[index(pop_lsb(&b), bksq, psq, BLACK)]
200 : db[index(wksq, pop_lsb(&b), psq, WHITE)];
202 if (Us == WHITE && (r & WIN))
205 if (Us == BLACK && (r & DRAW))
209 if (Us == WHITE && rank_of(psq) < RANK_7)
211 Square s = psq + DELTA_N;
212 r |= db[index(wksq, bksq, s, BLACK)]; // Single push
214 if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
215 r |= db[index(wksq, bksq, s + DELTA_N, BLACK)]; // Double push
221 return r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
224 Result KPKPosition::classify(int idx, Result db[]) {
227 return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db);