]> git.sesse.net Git - stockfish/blob - src/bitbase.cpp
Fix a possible 'Division by zero'
[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 probe_kpk_bitbase(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 kpk_bitbase_init() {
73
74   Result db[IndexMax];
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
95
96 namespace {
97
98   // A KPK bitbase index is an integer in [0, IndexMax] range
99   //
100   // Information is mapped in this way
101   //
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)
107
108   int index(Square w, Square b, Square p, Color c) {
109
110     assert(file_of(p) <= FILE_D);
111
112     return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
113   }
114
115   void KPKPosition::decode_index(int idx) {
116
117     stm  = Color(idx & 1);
118     bksq = Square((idx >> 1) & 63);
119     wksq = Square((idx >> 7) & 63);
120     psq  = make_square(File((idx >> 13) & 3), Rank((idx >> 15) + 1));
121   }
122
123   Result KPKPosition::classify_leaf(int idx) {
124
125     decode_index(idx);
126
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)))
131         return INVALID;
132
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
136         && stm == WHITE
137         && wksq != psq + DELTA_N
138         && (   square_distance(bksq, psq + DELTA_N) > 1
139             ||(k_attacks<WHITE>() & (psq + DELTA_N))))
140         return WIN;
141
142     // Check for known draw positions
143     //
144     // Case 1: Stalemate
145     if (   stm == BLACK
146         && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
147         return DRAW;
148
149     // Case 2: King can capture undefended pawn
150     if (   stm == BLACK
151         && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
152         return DRAW;
153
154     // Case 3: Black king in front of white pawn
155     if (   bksq == psq + DELTA_N
156         && rank_of(psq) < RANK_7)
157         return DRAW;
158
159     // Case 4: White king in front of pawn and black has opposition
160     if (   stm == WHITE
161         && wksq == psq + DELTA_N
162         && bksq == wksq + DELTA_N + DELTA_N
163         && rank_of(psq) < RANK_5)
164         return DRAW;
165
166     // Case 5: Stalemate with rook pawn
167     if (   bksq == SQ_A8
168         && file_of(psq) == FILE_A)
169         return DRAW;
170
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)
175         && bksq == wksq + 2)
176         return DRAW;
177
178     return UNKNOWN;
179   }
180
181   template<Color Us>
182   Result KPKPosition::classify(const Result db[]) const {
183
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.
188     //
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.
193
194     Result r = INVALID;
195     Bitboard b = k_attacks<Us>();
196
197     while (b)
198     {
199         r |= Us == WHITE ? db[index(pop_1st_bit(&b), bksq, psq, BLACK)]
200                          : db[index(wksq, pop_1st_bit(&b), psq, WHITE)];
201
202         if (Us == WHITE && (r & WIN))
203             return WIN;
204
205         if (Us == BLACK && (r & DRAW))
206             return DRAW;
207     }
208
209     if (Us == WHITE && rank_of(psq) < RANK_7)
210     {
211         Square s = psq + DELTA_N;
212         r |= db[index(wksq, bksq, s, BLACK)]; // Single push
213
214         if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
215             r |= db[index(wksq, bksq, s + DELTA_N, BLACK)]; // Double push
216
217         if (r & WIN)
218             return WIN;
219     }
220
221     return r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
222   }
223
224   Result KPKPosition::classify(int idx, Result db[]) {
225
226     decode_index(idx);
227     return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db);
228   }
229
230 }