]> git.sesse.net Git - stockfish/blob - src/nnue/features/half_kp.cpp
Remove EvalList
[stockfish] / src / nnue / features / half_kp.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 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 #include "index_list.h"
23
24 namespace Eval::NNUE::Features {
25
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));
29   }
30
31   // Find the index of the feature quantity from the king position and PieceSquare
32   template <Side AssociatedKing>
33   inline IndexType HalfKP<AssociatedKing>::MakeIndex(
34       Color perspective, Square s, Piece pc, Square ksq) {
35
36     return IndexType(orient(perspective, s) + kpp_board_index[pc][perspective] + PS_END * ksq);
37   }
38
39   // Get a list of indices for active features
40   template <Side AssociatedKing>
41   void HalfKP<AssociatedKing>::AppendActiveIndices(
42       const Position& pos, Color perspective, IndexList* active) {
43
44     Square ksq = orient(perspective, pos.square<KING>(perspective));
45     Bitboard bb = pos.pieces() & ~pos.pieces(KING);
46     while (bb) {
47       Square s = pop_lsb(&bb);
48       active->push_back(MakeIndex(perspective, s, pos.piece_on(s), ksq));
49     }
50   }
51
52   // Get a list of indices for recently changed features
53   template <Side AssociatedKing>
54   void HalfKP<AssociatedKing>::AppendChangedIndices(
55       const Position& pos, Color perspective,
56       IndexList* removed, IndexList* added) {
57
58     Square ksq = orient(perspective, pos.square<KING>(perspective));
59     const auto& dp = pos.state()->dirtyPiece;
60     for (int i = 0; i < dp.dirty_num; ++i) {
61       Piece pc = dp.piece[i];
62       if (type_of(pc) == KING) continue;
63       if (dp.from[i] != SQ_NONE)
64         removed->push_back(MakeIndex(perspective, dp.from[i], pc, ksq));
65       if (dp.to[i] != SQ_NONE)
66         added->push_back(MakeIndex(perspective, dp.to[i], pc, ksq));
67     }
68   }
69
70   template class HalfKP<Side::kFriend>;
71
72 }  // namespace Eval::NNUE::Features