]> git.sesse.net Git - stockfish/blob - src/movepick.h
Update copyright years
[stockfish] / src / movepick.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef MOVEPICK_H_INCLUDED
20 #define MOVEPICK_H_INCLUDED
21
22 #include <array>
23 #include <limits>
24 #include <type_traits>
25
26 #include "movegen.h"
27 #include "position.h"
28 #include "types.h"
29
30 /// StatsEntry stores the stat table value. It is usually a number but could
31 /// be a move or even a nested history. We use a class instead of naked value
32 /// to directly call history update operator<<() on the entry so to use stats
33 /// tables at caller sites as simple multi-dim arrays.
34 template<typename T, int D>
35 class StatsEntry {
36
37   T entry;
38
39 public:
40   void operator=(const T& v) { entry = v; }
41   T* operator&() { return &entry; }
42   T* operator->() { return &entry; }
43   operator const T&() const { return entry; }
44
45   void operator<<(int bonus) {
46     assert(abs(bonus) <= D); // Ensure range is [-D, D]
47     static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");
48
49     entry += bonus - entry * abs(bonus) / D;
50
51     assert(abs(entry) <= D);
52   }
53 };
54
55 /// Stats is a generic N-dimensional array used to store various statistics.
56 /// The first template parameter T is the base type of the array, the second
57 /// template parameter D limits the range of updates in [-D, D] when we update
58 /// values with the << operator, while the last parameters (Size and Sizes)
59 /// encode the dimensions of the array.
60 template <typename T, int D, int Size, int... Sizes>
61 struct Stats : public std::array<Stats<T, D, Sizes...>, Size>
62 {
63   typedef Stats<T, D, Size, Sizes...> stats;
64
65   void fill(const T& v) {
66
67     // For standard-layout 'this' points to first struct member
68     assert(std::is_standard_layout<stats>::value);
69
70     typedef StatsEntry<T, D> entry;
71     entry* p = reinterpret_cast<entry*>(this);
72     std::fill(p, p + sizeof(*this) / sizeof(entry), v);
73   }
74 };
75
76 template <typename T, int D, int Size>
77 struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> {};
78
79 /// In stats table, D=0 means that the template parameter is not used
80 enum StatsParams { NOT_USED = 0 };
81 enum StatsType { NoCaptures, Captures };
82
83 /// ButterflyHistory records how often quiet moves have been successful or
84 /// unsuccessful during the current search, and is used for reduction and move
85 /// ordering decisions. It uses 2 tables (one for each color) indexed by
86 /// the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
87 typedef Stats<int16_t, 13365, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
88
89 /// At higher depths LowPlyHistory records successful quiet moves near the root
90 /// and quiet moves which are/were in the PV (ttPv). It is cleared with each new
91 /// search and filled during iterative deepening.
92 constexpr int MAX_LPH = 4;
93 typedef Stats<int16_t, 10692, MAX_LPH, int(SQUARE_NB) * int(SQUARE_NB)> LowPlyHistory;
94
95 /// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
96 /// move, see www.chessprogramming.org/Countermove_Heuristic
97 typedef Stats<Move, NOT_USED, PIECE_NB, SQUARE_NB> CounterMoveHistory;
98
99 /// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
100 typedef Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToHistory;
101
102 /// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
103 typedef Stats<int16_t, 29952, PIECE_NB, SQUARE_NB> PieceToHistory;
104
105 /// ContinuationHistory is the combined history of a given pair of moves, usually
106 /// the current one given a previous one. The nested history table is based on
107 /// PieceToHistory instead of ButterflyBoards.
108 typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
109
110
111 /// MovePicker class is used to pick one pseudo legal move at a time from the
112 /// current position. The most important method is next_move(), which returns a
113 /// new pseudo legal move each time it is called, until there are no moves left,
114 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
115 /// beta algorithm, MovePicker attempts to return the moves which are most likely
116 /// to get a cut-off first.
117 class MovePicker {
118
119   enum PickType { Next, Best };
120
121 public:
122   MovePicker(const MovePicker&) = delete;
123   MovePicker& operator=(const MovePicker&) = delete;
124   MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
125   MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
126                                            const CapturePieceToHistory*,
127                                            const PieceToHistory**,
128                                            Square);
129   MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
130                                            const LowPlyHistory*,
131                                            const CapturePieceToHistory*,
132                                            const PieceToHistory**,
133                                            Move,
134                                            const Move*,
135                                            int);
136   Move next_move(bool skipQuiets = false);
137
138 private:
139   template<PickType T, typename Pred> Move select(Pred);
140   template<GenType> void score();
141   ExtMove* begin() { return cur; }
142   ExtMove* end() { return endMoves; }
143
144   const Position& pos;
145   const ButterflyHistory* mainHistory;
146   const LowPlyHistory* lowPlyHistory;
147   const CapturePieceToHistory* captureHistory;
148   const PieceToHistory** continuationHistory;
149   Move ttMove;
150   ExtMove refutations[3], *cur, *endMoves, *endBadCaptures;
151   int stage;
152   Square recaptureSquare;
153   Value threshold;
154   Depth depth;
155   int ply;
156   ExtMove moves[MAX_MOVES];
157 };
158
159 #endif // #ifndef MOVEPICK_H_INCLUDED