]> git.sesse.net Git - stockfish/blob - src/bitbase.cpp
Revert "Check for an available slave early on"
[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-2014 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   // There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7
29   const unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
30
31   // Each uint32_t stores results of 32 positions, one per bit
32   uint32_t KPKBitbase[MAX_INDEX / 32];
33
34   // A KPK bitbase index is an integer in [0, IndexMax] range
35   //
36   // Information is mapped in a way that minimizes the number of iterations:
37   //
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 RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
43   unsigned index(Color us, Square bksq, Square wksq, Square psq) {
44     return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((RANK_7 - rank_of(psq)) << 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     KPKPosition(unsigned idx);
59     operator Result() const { return result; }
60     Result classify(const std::vector<KPKPosition>& db)
61     { return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
62
63   private:
64     template<Color Us> Result classify(const std::vector<KPKPosition>& db);
65
66     Color us;
67     Square bksq, wksq, psq;
68     Result result;
69   };
70
71 } // namespace
72
73
74 bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
75
76   assert(file_of(wpsq) <= FILE_D);
77
78   unsigned idx = index(us, bksq, wksq, wpsq);
79   return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
80 }
81
82
83 void Bitbases::init_kpk() {
84
85   unsigned idx, repeat = 1;
86   std::vector<KPKPosition> db;
87   db.reserve(MAX_INDEX);
88
89   // Initialize db with known win / draw positions
90   for (idx = 0; idx < MAX_INDEX; ++idx)
91       db.push_back(KPKPosition(idx));
92
93   // Iterate through the positions until none of the unknown positions can be
94   // changed to either wins or draws (15 cycles needed).
95   while (repeat)
96       for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
97           repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
98
99   // Map 32 results into one KPKBitbase[] entry
100   for (idx = 0; idx < MAX_INDEX; ++idx)
101       if (db[idx] == WIN)
102           KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
103 }
104
105
106 namespace {
107
108   KPKPosition::KPKPosition(unsigned idx) {
109
110     wksq   = Square((idx >>  0) & 0x3F);
111     bksq   = Square((idx >>  6) & 0x3F);
112     us     = Color ((idx >> 12) & 0x01);
113     psq    = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
114     result = UNKNOWN;
115
116     // Check if two pieces are on the same square or if a king can be captured
117     if (   square_distance(wksq, bksq) <= 1
118         || wksq == psq
119         || bksq == psq
120         || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
121         result = INVALID;
122
123     else if (us == WHITE)
124     {
125         // Immediate win if a pawn can be promoted without getting captured
126         if (   rank_of(psq) == RANK_7
127             && wksq != psq + DELTA_N
128             && (   square_distance(bksq, psq + DELTA_N) > 1
129                 ||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
130             result = WIN;
131     }
132     // Immediate draw if it is a stalemate or a king captures undefended pawn
133     else if (  !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
134              || (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
135         result = DRAW;
136   }
137
138   template<Color Us>
139   Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
140
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 as DRAW, otherwise the current
144     // position is classified as UNKNOWN.
145     //
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 as WIN, otherwise the current position is
149     // classified as UNKNOWN.
150
151     const Color Them = (Us == WHITE ? BLACK : WHITE);
152
153     Result r = INVALID;
154     Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
155
156     while (b)
157         r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
158                          : db[index(Them, pop_lsb(&b), wksq, psq)];
159
160     if (Us == WHITE && rank_of(psq) < RANK_7)
161     {
162         Square s = psq + DELTA_N;
163         r |= db[index(BLACK, bksq, wksq, s)]; // Single push
164
165         if (rank_of(psq) == RANK_2 && s != wksq && s != bksq)
166             r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
167     }
168
169     if (Us == WHITE)
170         return result = r & WIN  ? WIN  : r & UNKNOWN ? UNKNOWN : DRAW;
171     else
172         return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
173   }
174
175 } // namespace