Line data Source code
1 : #include "definition.hpp"
2 :
3 : #include "logging.hpp"
4 : #include "moveApply.hpp"
5 : #include "moveGen.hpp"
6 : #include "movePseudoLegal.hpp"
7 : #include "position.hpp"
8 : #include "positionTools.hpp"
9 :
10 5 : void PerftAccumulator::Display() const{
11 5 : Logging::LogIt(Logging::logInfo) << "pseudoNodes " << pseudoNodes;
12 5 : Logging::LogIt(Logging::logInfo) << "validNodes " << validNodes;
13 5 : Logging::LogIt(Logging::logInfo) << "captureNodes " << captureNodes;
14 5 : Logging::LogIt(Logging::logInfo) << "epNodes " << epNodes;
15 5 : Logging::LogIt(Logging::logInfo) << "castling " << castling;
16 5 : Logging::LogIt(Logging::logInfo) << "promotion " << promotion;
17 5 : Logging::LogIt(Logging::logInfo) << "checkNode " << checkNode;
18 5 : Logging::LogIt(Logging::logInfo) << "checkMateNode " << checkMateNode;
19 5 : }
20 :
21 161406629 : Counter perft(const Position& p, DepthType depth, PerftAccumulator& acc) {
22 161406629 : if (depth == 0) return 0;
23 6531718 : MoveList moves;
24 6531718 : PerftAccumulator accLoc;
25 : #ifndef DEBUG_PERFT
26 6531718 : if (isPosInCheck(p)) MoveGen::generate<MoveGen::GP_evasion>(p, moves);
27 : else
28 6431959 : MoveGen::generate<MoveGen::GP_all>(p, moves);
29 : const size_t n = moves.size();
30 170493670 : for (size_t k = 0; k < n; ++k) {
31 : const Move& m = moves[k];
32 : #else
33 : for (MiniMove m = std::numeric_limits<MiniMove>::min(); m < std::numeric_limits<MiniMove>::max(); ++m) {
34 : if (!isPseudoLegal(p, m)) continue;
35 : #endif
36 163961952 : ++accLoc.pseudoNodes;
37 163961952 : Position p2 = p;
38 : #if defined(WITH_NNUE) && defined(DEBUG_NNUE_UPDATE)
39 : NNUEEvaluator evaluator;
40 : p2.associateEvaluator(evaluator);
41 : p2.resetNNUEEvaluator(p2.evaluator());
42 : #endif
43 163961952 : if (const MoveInfo moveInfo(p2, m); !applyMove(p2, moveInfo)) continue;
44 161406624 : ++accLoc.validNodes;
45 161406624 : MType t = Move2Type(m);
46 161406624 : if (t == T_ep) ++accLoc.epNodes;
47 161406624 : if (isCapture(t)) ++accLoc.captureNodes;
48 161406624 : if (isCastling(t)) ++accLoc.castling;
49 161406624 : if (isPromotion(t)) ++accLoc.promotion;
50 161406624 : perft(p2, depth - 1, acc);
51 163961952 : }
52 6531718 : if (depth == 1) acc += accLoc;
53 6531718 : return acc.validNodes;
54 : }
55 :
56 4 : [[nodiscard]] bool perft_test(const std::string& fen, DepthType d, uint64_t expected) {
57 4 : RootPosition p;
58 : #ifdef WITH_NNUE
59 4 : NNUEEvaluator evaluator;
60 : p.associateEvaluator(evaluator);
61 : #endif
62 4 : readFEN(fen, p);
63 8 : Logging::LogIt(Logging::logInfo) << ToString(p);
64 4 : PerftAccumulator acc;
65 4 : const uint64_t n = perft(p, d, acc);
66 4 : acc.Display();
67 4 : if (n != expected){
68 0 : Logging::LogIt(Logging::logError) << "Error !! " << fen << " " << expected;
69 0 : return false;
70 : }
71 : return true;
72 4 : }
|