Path: blob/master/src/nnue/features/half_ka_v2_hm.cpp
376 views
/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)34Stockfish is free software: you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.89Stockfish is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program. If not, see <http://www.gnu.org/licenses/>.16*/1718//Definition of input features HalfKAv2_hm of NNUE evaluation function1920#include "half_ka_v2_hm.h"2122#include "../../bitboard.h"23#include "../../position.h"24#include "../../types.h"25#include "../nnue_common.h"2627namespace Stockfish::Eval::NNUE::Features {2829// Index of a feature for a given king position and another piece on some square30template<Color Perspective>31inline IndexType HalfKAv2_hm::make_index(Square s, Piece pc, Square ksq) {32return IndexType((int(s) ^ OrientTBL[Perspective][ksq]) + PieceSquareIndex[Perspective][pc]33+ KingBuckets[Perspective][ksq]);34}3536// Get a list of indices for active features37template<Color Perspective>38void HalfKAv2_hm::append_active_indices(const Position& pos, IndexList& active) {39Square ksq = pos.square<KING>(Perspective);40Bitboard bb = pos.pieces();41while (bb)42{43Square s = pop_lsb(bb);44active.push_back(make_index<Perspective>(s, pos.piece_on(s), ksq));45}46}4748// Explicit template instantiations49template void HalfKAv2_hm::append_active_indices<WHITE>(const Position& pos, IndexList& active);50template void HalfKAv2_hm::append_active_indices<BLACK>(const Position& pos, IndexList& active);51template IndexType HalfKAv2_hm::make_index<WHITE>(Square s, Piece pc, Square ksq);52template IndexType HalfKAv2_hm::make_index<BLACK>(Square s, Piece pc, Square ksq);5354// Get a list of indices for recently changed features55template<Color Perspective>56void HalfKAv2_hm::append_changed_indices(Square ksq,57const DirtyPiece& dp,58IndexList& removed,59IndexList& added) {60removed.push_back(make_index<Perspective>(dp.from, dp.pc, ksq));61if (dp.to != SQ_NONE)62added.push_back(make_index<Perspective>(dp.to, dp.pc, ksq));6364if (dp.remove_sq != SQ_NONE)65removed.push_back(make_index<Perspective>(dp.remove_sq, dp.remove_pc, ksq));6667if (dp.add_sq != SQ_NONE)68added.push_back(make_index<Perspective>(dp.add_sq, dp.add_pc, ksq));69}7071// Explicit template instantiations72template void HalfKAv2_hm::append_changed_indices<WHITE>(Square ksq,73const DirtyPiece& dp,74IndexList& removed,75IndexList& added);76template void HalfKAv2_hm::append_changed_indices<BLACK>(Square ksq,77const DirtyPiece& dp,78IndexList& removed,79IndexList& added);8081bool HalfKAv2_hm::requires_refresh(const DirtyPiece& dirtyPiece, Color perspective) {82return dirtyPiece.pc == make_piece(perspective, KING);83}8485} // namespace Stockfish::Eval::NNUE::Features868788