Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/features/half_ka_v2_hm.cpp
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 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 "../../bitboard.h"
24
#include "../../position.h"
25
#include "../../types.h"
26
#include "../nnue_common.h"
27
28
namespace Stockfish::Eval::NNUE::Features {
29
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]
34
+ KingBuckets[Perspective][ksq]);
35
}
36
37
// Get a list of indices for active features
38
template<Color Perspective>
39
void HalfKAv2_hm::append_active_indices(const Position& pos, IndexList& active) {
40
Square ksq = pos.square<KING>(Perspective);
41
Bitboard bb = pos.pieces();
42
while (bb)
43
{
44
Square s = pop_lsb(bb);
45
active.push_back(make_index<Perspective>(s, pos.piece_on(s), ksq));
46
}
47
}
48
49
// Explicit template instantiations
50
template void HalfKAv2_hm::append_active_indices<WHITE>(const Position& pos, IndexList& active);
51
template void HalfKAv2_hm::append_active_indices<BLACK>(const Position& pos, IndexList& active);
52
template IndexType HalfKAv2_hm::make_index<WHITE>(Square s, Piece pc, Square ksq);
53
template IndexType HalfKAv2_hm::make_index<BLACK>(Square s, Piece pc, Square ksq);
54
55
// Get a list of indices for recently changed features
56
template<Color Perspective>
57
void HalfKAv2_hm::append_changed_indices(Square ksq,
58
const DirtyPiece& dp,
59
IndexList& removed,
60
IndexList& added) {
61
removed.push_back(make_index<Perspective>(dp.from, dp.pc, ksq));
62
if (dp.to != SQ_NONE)
63
added.push_back(make_index<Perspective>(dp.to, dp.pc, ksq));
64
65
if (dp.remove_sq != SQ_NONE)
66
removed.push_back(make_index<Perspective>(dp.remove_sq, dp.remove_pc, ksq));
67
68
if (dp.add_sq != SQ_NONE)
69
added.push_back(make_index<Perspective>(dp.add_sq, dp.add_pc, ksq));
70
}
71
72
// Explicit template instantiations
73
template void HalfKAv2_hm::append_changed_indices<WHITE>(Square ksq,
74
const DirtyPiece& dp,
75
IndexList& removed,
76
IndexList& added);
77
template void HalfKAv2_hm::append_changed_indices<BLACK>(Square ksq,
78
const DirtyPiece& dp,
79
IndexList& removed,
80
IndexList& added);
81
82
bool HalfKAv2_hm::requires_refresh(const DirtyPiece& dirtyPiece, Color perspective) {
83
return dirtyPiece.pc == make_piece(perspective, KING);
84
}
85
86
} // namespace Stockfish::Eval::NNUE::Features
87
88