]> git.sesse.net Git - stockfish/blob - src/nnue/features/half_kp.cpp
Cleanup and simplify NNUE code.
[stockfish] / src / nnue / features / half_kp.cpp
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 //Definition of input features HalfKP of NNUE evaluation function
20
21 #include "half_kp.h"
22
23 #include "../../position.h"
24
25 namespace Stockfish::Eval::NNUE::Features {
26
27   // Orient a square according to perspective (rotates by 180 for black)
28   inline Square HalfKP::orient(Color perspective, Square s) {
29     return Square(int(s) ^ (bool(perspective) * 63));
30   }
31
32   // Index of a feature for a given king position and another piece on some square
33   inline IndexType HalfKP::make_index(Color perspective, Square s, Piece pc, Square ksq) {
34     return IndexType(orient(perspective, s) + PieceSquareIndex[perspective][pc] + PS_NB * ksq);
35   }
36
37   // Get a list of indices for active features
38   void HalfKP::append_active_indices(
39     const Position& pos,
40     Color perspective,
41     ValueListInserter<IndexType> active
42   ) {
43     Square ksq = orient(perspective, pos.square<KING>(perspective));
44     Bitboard bb = pos.pieces() & ~pos.pieces(KING);
45     while (bb)
46     {
47       Square s = pop_lsb(bb);
48       active.push_back(make_index(perspective, s, pos.piece_on(s), ksq));
49     }
50   }
51
52
53   // append_changed_indices() : get a list of indices for recently changed features
54
55   void HalfKP::append_changed_indices(
56     Square ksq,
57     StateInfo* st,
58     Color perspective,
59     ValueListInserter<IndexType> removed,
60     ValueListInserter<IndexType> added
61   ) {
62     const auto& dp = st->dirtyPiece;
63     Square oriented_ksq = orient(perspective, ksq);
64     for (int i = 0; i < dp.dirty_num; ++i) {
65       Piece pc = dp.piece[i];
66       if (type_of(pc) == KING) continue;
67       if (dp.from[i] != SQ_NONE)
68         removed.push_back(make_index(perspective, dp.from[i], pc, oriented_ksq));
69       if (dp.to[i] != SQ_NONE)
70         added.push_back(make_index(perspective, dp.to[i], pc, oriented_ksq));
71     }
72   }
73
74   int HalfKP::update_cost(StateInfo* st) {
75     return st->dirtyPiece.dirty_num;
76   }
77
78   int HalfKP::refresh_cost(const Position& pos) {
79     return pos.count<ALL_PIECES>() - 2;
80   }
81
82   bool HalfKP::requires_refresh(StateInfo* st, Color perspective) {
83     return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
84   }
85
86 }  // namespace Stockfish::Eval::NNUE::Features