2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 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 HalfKAv2_hm of NNUE evaluation function
21 #include "half_ka_v2_hm.h"
23 #include "../../bitboard.h"
24 #include "../../position.h"
25 #include "../../types.h"
26 #include "../nnue_common.h"
28 namespace Stockfish::Eval::NNUE::Features {
30 // Index of a feature for a given king position and another piece on some square
31 template<Color Perspective>
32 inline IndexType HalfKAv2_hm::make_index(Square s, Piece pc, Square ksq) {
33 return IndexType((int(s) ^ OrientTBL[Perspective][ksq]) + PieceSquareIndex[Perspective][pc] + KingBuckets[Perspective][ksq]);
36 // Get a list of indices for active features
37 template<Color Perspective>
38 void HalfKAv2_hm::append_active_indices(
42 Square ksq = pos.square<KING>(Perspective);
43 Bitboard bb = pos.pieces();
46 Square s = pop_lsb(bb);
47 active.push_back(make_index<Perspective>(s, pos.piece_on(s), ksq));
51 // Explicit template instantiations
52 template void HalfKAv2_hm::append_active_indices<WHITE>(const Position& pos, IndexList& active);
53 template void HalfKAv2_hm::append_active_indices<BLACK>(const Position& pos, IndexList& active);
55 // append_changed_indices() : get a list of indices for recently changed features
56 template<Color Perspective>
57 void HalfKAv2_hm::append_changed_indices(
63 for (int i = 0; i < dp.dirty_num; ++i) {
64 if (dp.from[i] != SQ_NONE)
65 removed.push_back(make_index<Perspective>(dp.from[i], dp.piece[i], ksq));
66 if (dp.to[i] != SQ_NONE)
67 added.push_back(make_index<Perspective>(dp.to[i], dp.piece[i], ksq));
71 // Explicit template instantiations
72 template void HalfKAv2_hm::append_changed_indices<WHITE>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
73 template void HalfKAv2_hm::append_changed_indices<BLACK>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
75 int HalfKAv2_hm::update_cost(const StateInfo* st) {
76 return st->dirtyPiece.dirty_num;
79 int HalfKAv2_hm::refresh_cost(const Position& pos) {
80 return pos.count<ALL_PIECES>();
83 bool HalfKAv2_hm::requires_refresh(const StateInfo* st, Color perspective) {
84 return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
87 } // namespace Stockfish::Eval::NNUE::Features