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-2013 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 = 2*24*64*64; // stm * psq * wksq * bksq = 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 a way that minimizes number of iterations:
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 6 - rank (from 6 - RANK_7 to 6 - RANK_2)
43 unsigned index(Color us, Square bksq, Square wksq, Square psq) {
44 return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((6 - rank_of(psq)) << 15);
54 inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
58 operator Result() const { return res; }
59 Result classify_leaf(unsigned idx);
60 Result classify(const std::vector<KPKPosition>& db)
61 { return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
64 template<Color Us> Result classify(const std::vector<KPKPosition>& db);
67 Square bksq, wksq, psq;
74 bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
76 assert(file_of(wpsq) <= FILE_D);
78 unsigned idx = index(us, bksq, wksq, wpsq);
79 return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
83 void Bitbases::init_kpk() {
85 unsigned idx, repeat = 1;
86 std::vector<KPKPosition> db(IndexMax);
88 // Initialize db with known win / draw positions
89 for (idx = 0; idx < IndexMax; idx++)
90 db[idx].classify_leaf(idx);
92 // Iterate through the positions until no more of the unknown positions can be
93 // changed to either wins or draws (15 cycles needed).
95 for (repeat = idx = 0; idx < IndexMax; idx++)
96 if (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN)
99 // Map 32 results into one KPKBitbase[] entry
100 for (idx = 0; idx < IndexMax; idx++)
102 KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
108 Result KPKPosition::classify_leaf(unsigned idx) {
110 wksq = Square((idx >> 0) & 0x3F);
111 bksq = Square((idx >> 6) & 0x3F);
112 us = Color((idx >> 12) & 0x01);
113 psq = File((idx >> 13) & 3) | Rank(6 - (idx >> 15));
115 // Check if two pieces are on the same square or if a king can be captured
116 if ( wksq == psq || wksq == bksq || bksq == psq
117 || (StepAttacksBB[KING][wksq] & bksq)
118 || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
119 return res = INVALID;
123 // Immediate win if pawn can be promoted without getting captured
124 if ( rank_of(psq) == RANK_7
125 && wksq != psq + DELTA_N
126 && ( square_distance(bksq, psq + DELTA_N) > 1
127 ||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
130 // Immediate draw if is stalemate or king captures undefended pawn
131 else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
132 || (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
135 return res = UNKNOWN;
139 Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
141 // White to Move: If one move leads to a position classified as WIN, the result
142 // of the current position is WIN. If all moves lead to positions classified
143 // as DRAW, the current position is classified DRAW otherwise the current
144 // position is classified as UNKNOWN.
146 // Black to Move: If one move leads to a position classified as DRAW, the result
147 // of the current position is DRAW. If all moves lead to positions classified
148 // as WIN, the position is classified WIN otherwise the current position is
149 // classified UNKNOWN.
152 Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
155 r |= Us == WHITE ? db[index(~Us, bksq, pop_lsb(&b), psq)]
156 : db[index(~Us, pop_lsb(&b), wksq, psq)];
158 if (Us == WHITE && rank_of(psq) < RANK_7)
160 Square s = psq + DELTA_N;
161 r |= db[index(BLACK, bksq, wksq, s)]; // Single push
163 if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
164 r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
168 return res = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
170 return res = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;