]> git.sesse.net Git - stockfish/blob - src/nnue/features/half_ka_v2_hm.cpp
Merge remote-tracking branch 'upstream/master'
[stockfish] / src / nnue / features / half_ka_v2_hm.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 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 HalfKAv2_hm of NNUE evaluation function
20
21 #include "half_ka_v2_hm.h"
22
23 #include "../../position.h"
24
25 namespace Stockfish::Eval::NNUE::Features {
26
27   // Index of a feature for a given king position and another piece on some square
28   template<Color Perspective>
29   inline IndexType HalfKAv2_hm::make_index(Square s, Piece pc, Square ksq) {
30     return IndexType((int(s) ^ OrientTBL[Perspective][ksq]) + PieceSquareIndex[Perspective][pc] + KingBuckets[Perspective][ksq]);
31   }
32
33   // Get a list of indices for active features
34   template<Color Perspective>
35   void HalfKAv2_hm::append_active_indices(
36     const Position& pos,
37     IndexList& active
38   ) {
39     Square ksq = pos.square<KING>(Perspective);
40     Bitboard bb = pos.pieces();
41     while (bb)
42     {
43       Square s = pop_lsb(bb);
44       active.push_back(make_index<Perspective>(s, pos.piece_on(s), ksq));
45     }
46   }
47
48   // Explicit template instantiations
49   template void HalfKAv2_hm::append_active_indices<WHITE>(const Position& pos, IndexList& active);
50   template void HalfKAv2_hm::append_active_indices<BLACK>(const Position& pos, IndexList& active);
51
52   // append_changed_indices() : get a list of indices for recently changed features
53   template<Color Perspective>
54   void HalfKAv2_hm::append_changed_indices(
55     Square ksq,
56     const DirtyPiece& dp,
57     IndexList& removed,
58     IndexList& added
59   ) {
60     for (int i = 0; i < dp.dirty_num; ++i) {
61       if (dp.from[i] != SQ_NONE)
62         removed.push_back(make_index<Perspective>(dp.from[i], dp.piece[i], ksq));
63       if (dp.to[i] != SQ_NONE)
64         added.push_back(make_index<Perspective>(dp.to[i], dp.piece[i], ksq));
65     }
66   }
67
68   // Explicit template instantiations
69   template void HalfKAv2_hm::append_changed_indices<WHITE>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
70   template void HalfKAv2_hm::append_changed_indices<BLACK>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
71
72   int HalfKAv2_hm::update_cost(const StateInfo* st) {
73     return st->dirtyPiece.dirty_num;
74   }
75
76   int HalfKAv2_hm::refresh_cost(const Position& pos) {
77     return pos.count<ALL_PIECES>();
78   }
79
80   bool HalfKAv2_hm::requires_refresh(const StateInfo* st, Color perspective) {
81     return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
82   }
83
84 }  // namespace Stockfish::Eval::NNUE::Features