Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/perft.h
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 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 PERFT_H_INCLUDED
20
#define PERFT_H_INCLUDED
21
22
#include <cstdint>
23
24
#include "movegen.h"
25
#include "position.h"
26
#include "types.h"
27
#include "uci.h"
28
29
namespace Stockfish::Benchmark {
30
31
// Utility to verify move generation. All the leaf nodes up
32
// to the given depth are generated and counted, and the sum is returned.
33
template<bool Root>
34
uint64_t perft(Position& pos, Depth depth) {
35
36
StateInfo st;
37
38
uint64_t cnt, nodes = 0;
39
const bool leaf = (depth == 2);
40
41
for (const auto& m : MoveList<LEGAL>(pos))
42
{
43
if (Root && depth <= 1)
44
cnt = 1, nodes++;
45
else
46
{
47
pos.do_move(m, st);
48
cnt = leaf ? MoveList<LEGAL>(pos).size() : perft<false>(pos, depth - 1);
49
nodes += cnt;
50
pos.undo_move(m);
51
}
52
if (Root)
53
sync_cout << UCIEngine::move(m, pos.is_chess960()) << ": " << cnt << sync_endl;
54
}
55
return nodes;
56
}
57
58
inline uint64_t perft(const std::string& fen, Depth depth, bool isChess960) {
59
StateInfo st;
60
Position p;
61
p.set(fen, isChess960, &st);
62
63
return perft<true>(p, depth);
64
}
65
}
66
67
#endif // PERFT_H_INCLUDED
68
69