]> git.sesse.net Git - stockfish/blob - src/movepick.h
ef3be6c240f8d553b42c68756e48fa512b5bdf3d
[stockfish] / src / movepick.h
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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef MOVEPICK_H_INCLUDED
22 #define MOVEPICK_H_INCLUDED
23
24 #include <algorithm> // For std::max
25 #include <cstring>   // For std::memset
26
27 #include "movegen.h"
28 #include "position.h"
29 #include "types.h"
30
31
32 /// HistoryStats records how often quiet moves have been successful or unsuccessful
33 /// during the current search, and is used for reduction and move ordering decisions.
34 struct HistoryStats {
35
36   static const Value Max = Value(1 << 28);
37
38   Value get(Color c, Move m) const { return table[c][from_sq(m)][to_sq(m)]; }
39   void clear() { std::memset(table, 0, sizeof(table)); }
40   void update(Color c, Move m, Value v) {
41
42     Square from = from_sq(m);
43     Square to = to_sq(m);
44
45     const int denom = 324;
46
47     assert(abs(int(v)) <= denom); // Needed for stability.
48
49     table[c][from][to] -= table[c][from][to] * abs(int(v)) / denom;
50     table[c][from][to] += int(v) * 32;
51   }
52
53 private:
54   Value table[COLOR_NB][SQUARE_NB][SQUARE_NB];
55 };
56
57
58 /// A template struct, used to generate MoveStats and CounterMoveHistoryStats:
59 /// MoveStats store the move that refute a previous one.
60 /// CounterMoveHistoryStats is like HistoryStats, but with two consecutive moves.
61 /// Entries are stored using only the moving piece and destination square, hence
62 /// two moves with different origin but same destination and piece will be
63 /// considered identical.
64 template<typename T>
65 struct Stats {
66   const T* operator[](Piece pc) const { return table[pc]; }
67   T* operator[](Piece pc) { return table[pc]; }
68   void clear() { std::memset(table, 0, sizeof(table)); }
69   void fill(const Value& v) { std::fill(&table[0][0], &table[PIECE_NB-1][SQUARE_NB-1]+1, v); };
70   void update(Piece pc, Square to, Move m) { table[pc][to] = m; }
71   void update(Piece pc, Square to, Value v) {
72
73     const int denom = 936;
74
75     assert(abs(int(v)) <= denom); // Needed for stability.
76
77     table[pc][to] -= table[pc][to] * abs(int(v)) / denom;
78     table[pc][to] += int(v) * 32;
79   }
80
81 private:
82   T table[PIECE_NB][SQUARE_NB];
83 };
84
85 typedef Stats<Move> MoveStats;
86 typedef Stats<Value> CounterMoveStats;
87 typedef Stats<CounterMoveStats> CounterMoveHistoryStats;
88
89
90 /// MovePicker class is used to pick one pseudo legal move at a time from the
91 /// current position. The most important method is next_move(), which returns a
92 /// new pseudo legal move each time it is called, until there are no moves left,
93 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
94 /// beta algorithm, MovePicker attempts to return the moves which are most likely
95 /// to get a cut-off first.
96 namespace Search { struct Stack; }
97
98 class MovePicker {
99 public:
100   MovePicker(const MovePicker&) = delete;
101   MovePicker& operator=(const MovePicker&) = delete;
102
103   MovePicker(const Position&, Move, Value);
104   MovePicker(const Position&, Move, Depth, Square);
105   MovePicker(const Position&, Move, Depth, Search::Stack*);
106
107   Move next_move(bool skipQuiets = false);
108
109 private:
110   template<GenType> void score();
111   ExtMove* begin() { return cur; }
112   ExtMove* end() { return endMoves; }
113
114   const Position& pos;
115   const Search::Stack* ss;
116   Move countermove;
117   Depth depth;
118   Move ttMove;
119   Square recaptureSquare;
120   Value threshold;
121   int stage;
122   ExtMove *cur, *endMoves, *endBadCaptures;
123   ExtMove moves[MAX_MOVES];
124 };
125
126 #endif // #ifndef MOVEPICK_H_INCLUDED