]> git.sesse.net Git - stockfish/blob - src/bitbase.cpp
Simplify bitbase.cpp
[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 #include <vector>
22
23 #include "bitboard.h"
24 #include "types.h"
25
26 namespace {
27
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
30
31   // Each uint32_t stores results of 32 positions, one per bit
32   uint32_t KPKBitbase[IndexMax / 32];
33
34   // A KPK bitbase index is an integer in [0, IndexMax] range
35   //
36   // Information is mapped in this way
37   //
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);
45   }
46
47   enum Result {
48     INVALID = 0,
49     UNKNOWN = 1,
50     DRAW    = 2,
51     WIN     = 4
52   };
53
54   inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
55
56   struct KPKPosition {
57
58     void classify_leaf(unsigned idx);
59
60     Result classify(const std::vector<KPKPosition>& db)
61     { return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
62
63     operator Result() const { return res; }
64
65   private:
66     template<Color Us> Bitboard k_attacks() const
67     { return StepAttacksBB[KING][Us == WHITE ? wksq : bksq]; }
68
69     template<Color Us> Result classify(const std::vector<KPKPosition>& db);
70
71     Color stm;
72     Square bksq, wksq, psq;
73     Result res;
74   };
75
76 } // namespace
77
78
79 bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
80
81   assert(file_of(wpsq) <= FILE_D);
82
83   unsigned idx = index(stm, bksq, wksq, wpsq);
84   return KPKBitbase[idx / 32] & (1 << (idx & 31));
85 }
86
87
88 void Bitbases::init_kpk() {
89
90   unsigned idx, repeat = 1;
91   std::vector<KPKPosition> db(IndexMax);
92
93   // Initialize db with known win / draw positions
94   for (idx = 0; idx < IndexMax; idx++)
95       db[idx].classify_leaf(idx);
96
97   // Iterate until all positions are classified (30 cycles needed)
98   while (repeat)
99       for (repeat = idx = 0; idx < IndexMax; idx++)
100           if (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN)
101               repeat = 1;
102
103   // Map 32 results into one KPKBitbase[] entry
104   for (idx = 0; idx < IndexMax; idx++)
105       if (db[idx] == WIN)
106           KPKBitbase[idx / 32] |= 1 << (idx & 31);
107 }
108
109
110 namespace {
111
112   void KPKPosition::classify_leaf(unsigned idx) {
113
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);
118
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)))
123         res = INVALID;
124
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
128              && stm == WHITE
129              && wksq != psq + DELTA_N
130              && (   square_distance(bksq, psq + DELTA_N) > 1
131                  ||(k_attacks<WHITE>() & (psq + DELTA_N))))
132         res = WIN;
133
134     // Check for known draw positions
135     //
136     // Case 1: Stalemate
137     else if (   stm == BLACK
138              && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | StepAttacksBB[PAWN][psq])))
139         res = DRAW;
140
141     // Case 2: King can capture undefended pawn
142     else if (   stm == BLACK
143              && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
144         res = DRAW;
145
146     // Case 3: Black king in front of white pawn
147     else if (   bksq == psq + DELTA_N
148              && rank_of(psq) < RANK_7)
149         res = DRAW;
150
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)
156         res = DRAW;
157
158     // Case 5: Stalemate with rook pawn
159     else if (   bksq == SQ_A8
160              && file_of(psq) == FILE_A)
161         res = DRAW;
162
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)
167              && bksq == wksq + 2)
168         res = DRAW;
169
170     else
171         res = UNKNOWN;
172   }
173
174   template<Color Us>
175   Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
176
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.
181     //
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.
186
187     Result r = INVALID;
188     Bitboard b = k_attacks<Us>();
189
190     while (b)
191     {
192         r |= Us == WHITE ? db[index(BLACK, bksq, pop_lsb(&b), psq)]
193                          : db[index(WHITE, pop_lsb(&b), wksq, psq)];
194
195         if (Us == WHITE && (r & WIN))
196             return res = WIN;
197
198         if (Us == BLACK && (r & DRAW))
199             return res = DRAW;
200     }
201
202     if (Us == WHITE && rank_of(psq) < RANK_7)
203     {
204         Square s = psq + DELTA_N;
205         r |= db[index(BLACK, bksq, wksq, s)]; // Single push
206
207         if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
208             r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
209
210         if (r & WIN)
211             return res = WIN;
212     }
213
214     return res = r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
215   }
216
217 }