2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 //Definition of input features HalfKP of NNUE evaluation function
22 #include "index_list.h"
24 namespace Stockfish::Eval::NNUE::Features {
26 // Orient a square according to perspective (rotates by 180 for black)
27 inline Square orient(Color perspective, Square s) {
28 return Square(int(s) ^ (bool(perspective) * 63));
31 // Index of a feature for a given king position and another piece on some square
32 inline IndexType make_index(Color perspective, Square s, Piece pc, Square ksq) {
33 return IndexType(orient(perspective, s) + PieceSquareIndex[perspective][pc] + PS_NB * ksq);
36 // Get a list of indices for active features
37 template <Side AssociatedKing>
38 void HalfKP<AssociatedKing>::append_active_indices(
39 const Position& pos, Color perspective, IndexList* active) {
41 Square ksq = orient(perspective, pos.square<KING>(perspective));
42 Bitboard bb = pos.pieces() & ~pos.pieces(KING);
45 Square s = pop_lsb(bb);
46 active->push_back(make_index(perspective, s, pos.piece_on(s), ksq));
51 // append_changed_indices() : get a list of indices for recently changed features
53 // IMPORTANT: The `pos` in this function is pretty much useless as it
54 // is not always the position the features are updated to. The feature
55 // transformer code right now can update multiple accumulators per move,
56 // but since Stockfish only keeps the full state of the current leaf
57 // search position it is not possible to always pass here the position for
58 // which the accumulator is being updated. Therefore the only thing that
59 // can be reliably extracted from `pos` is the king square for the king
60 // of the `perspective` color (note: not even the other king's square will
61 // match reality in all cases, this is also the reason why `dp` is passed
62 // as a parameter and not extracted from pos.state()). This is of particular
63 // problem for future nets with other feature sets, where updating the active
64 // feature might require more information from the intermediate positions. In
65 // this case the only easy solution is to remove the multiple updates from
66 // the feature transformer update code and only update the accumulator for
67 // the current leaf position (the position after the move).
69 template <Side AssociatedKing>
70 void HalfKP<AssociatedKing>::append_changed_indices(
71 const Position& pos, const DirtyPiece& dp, Color perspective,
72 IndexList* removed, IndexList* added) {
74 Square ksq = orient(perspective, pos.square<KING>(perspective));
75 for (int i = 0; i < dp.dirty_num; ++i) {
76 Piece pc = dp.piece[i];
77 if (type_of(pc) == KING) continue;
78 if (dp.from[i] != SQ_NONE)
79 removed->push_back(make_index(perspective, dp.from[i], pc, ksq));
80 if (dp.to[i] != SQ_NONE)
81 added->push_back(make_index(perspective, dp.to[i], pc, ksq));
85 template class HalfKP<Side::Friend>;
87 } // namespace Stockfish::Eval::NNUE::Features