]> git.sesse.net Git - stockfish/blob - src/movepick.h
add clang-format
[stockfish] / src / movepick.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 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 <cassert>
24 #include <cstdint>
25 #include <cstdlib>
26 #include <limits>
27 #include <type_traits>  // IWYU pragma: keep
28
29 #include "movegen.h"
30 #include "types.h"
31
32 namespace Stockfish {
33 class Position;
34
35 // StatsEntry stores the stat table value. It is usually a number but could
36 // be a move or even a nested history. We use a class instead of a naked value
37 // to directly call history update operator<<() on the entry so to use stats
38 // tables at caller sites as simple multi-dim arrays.
39 template<typename T, int D>
40 class StatsEntry {
41
42     T entry;
43
44    public:
45     void operator=(const T& v) { entry = v; }
46     T*   operator&() { return &entry; }
47     T*   operator->() { return &entry; }
48     operator const T&() const { return entry; }
49
50     void operator<<(int bonus) {
51         assert(abs(bonus) <= D);  // Ensure range is [-D, D]
52         static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");
53
54         entry += (bonus * D - entry * abs(bonus)) / (D * 5 / 4);
55
56         assert(abs(entry) <= D);
57     }
58 };
59
60 // Stats is a generic N-dimensional array used to store various statistics.
61 // The first template parameter T is the base type of the array, and the second
62 // template parameter D limits the range of updates in [-D, D] when we update
63 // values with the << operator, while the last parameters (Size and Sizes)
64 // encode the dimensions of the array.
65 template<typename T, int D, int Size, int... Sizes>
66 struct Stats: public std::array<Stats<T, D, Sizes...>, Size> {
67     using stats = Stats<T, D, Size, Sizes...>;
68
69     void fill(const T& v) {
70
71         // For standard-layout 'this' points to the first struct member
72         assert(std::is_standard_layout_v<stats>);
73
74         using entry = StatsEntry<T, D>;
75         entry* p    = reinterpret_cast<entry*>(this);
76         std::fill(p, p + sizeof(*this) / sizeof(entry), v);
77     }
78 };
79
80 template<typename T, int D, int Size>
81 struct Stats<T, D, Size>: public std::array<StatsEntry<T, D>, Size> {};
82
83 // In stats table, D=0 means that the template parameter is not used
84 enum StatsParams {
85     NOT_USED = 0
86 };
87 enum StatsType {
88     NoCaptures,
89     Captures
90 };
91
92 // ButterflyHistory records how often quiet moves have been successful or
93 // unsuccessful during the current search, and is used for reduction and move
94 // ordering decisions. It uses 2 tables (one for each color) indexed by
95 // the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
96 // (~11 elo)
97 using ButterflyHistory = Stats<int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;
98
99 // CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
100 // move, see www.chessprogramming.org/Countermove_Heuristic
101 using CounterMoveHistory = Stats<Move, NOT_USED, PIECE_NB, SQUARE_NB>;
102
103 // CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
104 using CapturePieceToHistory = Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
105
106 // PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
107 using PieceToHistory = Stats<int16_t, 29952, PIECE_NB, SQUARE_NB>;
108
109 // ContinuationHistory is the combined history of a given pair of moves, usually
110 // the current one given a previous one. The nested history table is based on
111 // PieceToHistory instead of ButterflyBoards.
112 // (~63 elo)
113 using ContinuationHistory = Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB>;
114
115
116 // MovePicker class is used to pick one pseudo-legal move at a time from the
117 // current position. The most important method is next_move(), which returns a
118 // new pseudo-legal move each time it is called, until there are no moves left,
119 // when MOVE_NONE is returned. In order to improve the efficiency of the
120 // alpha-beta algorithm, MovePicker attempts to return the moves which are most
121 // likely to get a cut-off first.
122 class MovePicker {
123
124     enum PickType {
125         Next,
126         Best
127     };
128
129    public:
130     MovePicker(const MovePicker&)            = delete;
131     MovePicker& operator=(const MovePicker&) = delete;
132     MovePicker(const Position&,
133                Move,
134                Depth,
135                const ButterflyHistory*,
136                const CapturePieceToHistory*,
137                const PieceToHistory**,
138                Move,
139                const Move*);
140     MovePicker(const Position&,
141                Move,
142                Depth,
143                const ButterflyHistory*,
144                const CapturePieceToHistory*,
145                const PieceToHistory**,
146                Square);
147     MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
148     Move next_move(bool skipQuiets = false);
149
150    private:
151     template<PickType T, typename Pred>
152     Move select(Pred);
153     template<GenType>
154     void     score();
155     ExtMove* begin() { return cur; }
156     ExtMove* end() { return endMoves; }
157
158     const Position&              pos;
159     const ButterflyHistory*      mainHistory;
160     const CapturePieceToHistory* captureHistory;
161     const PieceToHistory**       continuationHistory;
162     Move                         ttMove;
163     ExtMove                      refutations[3], *cur, *endMoves, *endBadCaptures;
164     int                          stage;
165     Square                       recaptureSquare;
166     Value                        threshold;
167     Depth                        depth;
168     ExtMove                      moves[MAX_MOVES];
169 };
170
171 }  // namespace Stockfish
172
173 #endif  // #ifndef MOVEPICK_H_INCLUDED