]> git.sesse.net Git - stockfish/blob - src/bitbase.cpp
Reformat check_is_dangerous()
[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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
5
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.
10
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.
15
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/>.
18 */
19
20 #include <cassert>
21
22 #include "bitboard.h"
23 #include "types.h"
24
25 namespace {
26
27   enum Result {
28     INVALID = 0,
29     UNKNOWN = 1,
30     DRAW    = 2,
31     WIN     = 4
32   };
33
34   inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
35
36   struct KPKPosition {
37
38     Result classify_leaf(int idx);
39     Result classify(int idx, Result db[]);
40
41   private:
42     template<Color Us> Result classify(const Result db[]) const;
43
44     template<Color Us> Bitboard k_attacks() const {
45       return Us == WHITE ? StepAttacksBB[W_KING][wksq] : StepAttacksBB[B_KING][bksq];
46     }
47
48     Bitboard p_attacks() const { return StepAttacksBB[W_PAWN][psq]; }
49     void decode_index(int idx);
50
51     Square wksq, bksq, psq;
52     Color stm;
53   };
54
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
57
58   // Each uint32_t stores results of 32 positions, one per bit
59   uint32_t KPKBitbase[IndexMax / 32];
60
61   int index(Square wksq, Square bksq, Square psq, Color stm);
62 }
63
64
65 uint32_t Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
66
67   int idx = index(wksq, bksq, wpsq, stm);
68   return KPKBitbase[idx / 32] & (1 << (idx & 31));
69 }
70
71
72 void Bitbases::init_kpk() {
73
74   Result* db = new Result[IndexMax]; // Avoid to hit stack limit on some platforms
75   KPKPosition pos;
76   int idx, bit, repeat = 1;
77
78   // Initialize table with known win / draw positions
79   for (idx = 0; idx < IndexMax; idx++)
80       db[idx] = pos.classify_leaf(idx);
81
82   // Iterate until all positions are classified (30 cycles needed)
83   while (repeat)
84       for (repeat = idx = 0; idx < IndexMax; idx++)
85           if (db[idx] == UNKNOWN && (db[idx] = pos.classify(idx, db)) != UNKNOWN)
86               repeat = 1;
87
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;
93
94   delete [] db;
95 }
96
97
98 namespace {
99
100   // A KPK bitbase index is an integer in [0, IndexMax] range
101   //
102   // Information is mapped in this way
103   //
104   // bit     0: side to move (WHITE or BLACK)
105   // bit  1- 6: black king square (from SQ_A1 to SQ_H8)
106   // bit  7-12: white king square (from SQ_A1 to SQ_H8)
107   // bit 13-14: white pawn file (from FILE_A to FILE_D)
108   // bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
109
110   int index(Square w, Square b, Square p, Color c) {
111
112     assert(file_of(p) <= FILE_D);
113
114     return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
115   }
116
117   void KPKPosition::decode_index(int idx) {
118
119     stm  = Color(idx & 1);
120     bksq = Square((idx >> 1) & 63);
121     wksq = Square((idx >> 7) & 63);
122     psq  = File((idx >> 13) & 3) | Rank((idx >> 15) + 1);
123   }
124
125   Result KPKPosition::classify_leaf(int idx) {
126
127     decode_index(idx);
128
129     // Check if two pieces are on the same square or if a king can be captured
130     if (   wksq == psq || wksq == bksq || bksq == psq
131         || (k_attacks<WHITE>() & bksq)
132         || (stm == WHITE && (p_attacks() & bksq)))
133         return INVALID;
134
135     // The position is an immediate win if it is white to move and the white
136     // pawn can be promoted without getting captured.
137     if (   rank_of(psq) == RANK_7
138         && stm == WHITE
139         && wksq != psq + DELTA_N
140         && (   square_distance(bksq, psq + DELTA_N) > 1
141             ||(k_attacks<WHITE>() & (psq + DELTA_N))))
142         return WIN;
143
144     // Check for known draw positions
145     //
146     // Case 1: Stalemate
147     if (   stm == BLACK
148         && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
149         return DRAW;
150
151     // Case 2: King can capture undefended pawn
152     if (   stm == BLACK
153         && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
154         return DRAW;
155
156     // Case 3: Black king in front of white pawn
157     if (   bksq == psq + DELTA_N
158         && rank_of(psq) < RANK_7)
159         return DRAW;
160
161     // Case 4: White king in front of pawn and black has opposition
162     if (   stm == WHITE
163         && wksq == psq + DELTA_N
164         && bksq == wksq + DELTA_N + DELTA_N
165         && rank_of(psq) < RANK_5)
166         return DRAW;
167
168     // Case 5: Stalemate with rook pawn
169     if (   bksq == SQ_A8
170         && file_of(psq) == FILE_A)
171         return DRAW;
172
173     // Case 6: White king trapped on the rook file
174     if (   file_of(wksq) == FILE_A
175         && file_of(psq) == FILE_A
176         && rank_of(wksq) > rank_of(psq)
177         && bksq == wksq + 2)
178         return DRAW;
179
180     return UNKNOWN;
181   }
182
183   template<Color Us>
184   Result KPKPosition::classify(const Result db[]) const {
185
186     // White to Move: If one move leads to a position classified as RESULT_WIN,
187     // the result of the current position is RESULT_WIN. If all moves lead to
188     // positions classified as RESULT_DRAW, the current position is classified
189     // RESULT_DRAW otherwise the current position is classified as RESULT_UNKNOWN.
190     //
191     // Black to Move: If one move leads to a position classified as RESULT_DRAW,
192     // the result of the current position is RESULT_DRAW. If all moves lead to
193     // positions classified as RESULT_WIN, the position is classified RESULT_WIN.
194     // Otherwise, the current position is classified as RESULT_UNKNOWN.
195
196     Result r = INVALID;
197     Bitboard b = k_attacks<Us>();
198
199     while (b)
200     {
201         r |= Us == WHITE ? db[index(pop_lsb(&b), bksq, psq, BLACK)]
202                          : db[index(wksq, pop_lsb(&b), psq, WHITE)];
203
204         if (Us == WHITE && (r & WIN))
205             return WIN;
206
207         if (Us == BLACK && (r & DRAW))
208             return DRAW;
209     }
210
211     if (Us == WHITE && rank_of(psq) < RANK_7)
212     {
213         Square s = psq + DELTA_N;
214         r |= db[index(wksq, bksq, s, BLACK)]; // Single push
215
216         if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
217             r |= db[index(wksq, bksq, s + DELTA_N, BLACK)]; // Double push
218
219         if (r & WIN)
220             return WIN;
221     }
222
223     return r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
224   }
225
226   Result KPKPosition::classify(int idx, Result db[]) {
227
228     decode_index(idx);
229     return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db);
230   }
231
232 }