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 HalfKAv2_hm of NNUE evaluation function
21 #include "half_ka_v2_hm.h"
23 #include "../../position.h"
25 namespace Stockfish::Eval::NNUE::Features {
27 // Orient a square according to perspective (rotates by 180 for black)
28 inline Square HalfKAv2_hm::orient(Color perspective, Square s, Square ksq) {
29 return Square(int(s) ^ (bool(perspective) * SQ_A8) ^ ((file_of(ksq) < FILE_E) * SQ_H1));
32 // Index of a feature for a given king position and another piece on some square
33 inline IndexType HalfKAv2_hm::make_index(Color perspective, Square s, Piece pc, Square ksq) {
34 Square o_ksq = orient(perspective, ksq, ksq);
35 return IndexType(orient(perspective, s, ksq) + PieceSquareIndex[perspective][pc] + PS_NB * KingBuckets[o_ksq]);
38 // Get a list of indices for active features
39 void HalfKAv2_hm::append_active_indices(
42 ValueListInserter<IndexType> active
44 Square ksq = pos.square<KING>(perspective);
45 Bitboard bb = pos.pieces();
48 Square s = pop_lsb(bb);
49 active.push_back(make_index(perspective, s, pos.piece_on(s), ksq));
54 // append_changed_indices() : get a list of indices for recently changed features
56 void HalfKAv2_hm::append_changed_indices(
60 ValueListInserter<IndexType> removed,
61 ValueListInserter<IndexType> added
63 const auto& dp = st->dirtyPiece;
64 for (int i = 0; i < dp.dirty_num; ++i) {
65 Piece pc = dp.piece[i];
66 if (dp.from[i] != SQ_NONE)
67 removed.push_back(make_index(perspective, dp.from[i], pc, ksq));
68 if (dp.to[i] != SQ_NONE)
69 added.push_back(make_index(perspective, dp.to[i], pc, ksq));
73 int HalfKAv2_hm::update_cost(StateInfo* st) {
74 return st->dirtyPiece.dirty_num;
77 int HalfKAv2_hm::refresh_cost(const Position& pos) {
78 return pos.count<ALL_PIECES>();
81 bool HalfKAv2_hm::requires_refresh(StateInfo* st, Color perspective) {
82 return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
85 } // namespace Stockfish::Eval::NNUE::Features