Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/nnue/features/full_threats.h
474 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
Stockfish is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
Stockfish is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
//Definition of input features Simplified_Threats of NNUE evaluation function
17
18
#ifndef NNUE_FEATURES_FULL_THREATS_INCLUDED
19
#define NNUE_FEATURES_FULL_THREATS_INCLUDED
20
21
#include <cstdint>
22
23
#include "../../misc.h"
24
#include "../../types.h"
25
#include "../nnue_common.h"
26
27
namespace Stockfish {
28
class Position;
29
}
30
31
namespace Stockfish::Eval::NNUE::Features {
32
33
static constexpr int numValidTargets[PIECE_NB] = {0, 6, 12, 10, 10, 12, 8, 0,
34
0, 6, 12, 10, 10, 12, 8, 0};
35
void init_threat_offsets();
36
37
class FullThreats {
38
public:
39
// Feature name
40
static constexpr const char* Name = "Full_Threats(Friend)";
41
42
// Hash value embedded in the evaluation file
43
static constexpr std::uint32_t HashValue = 0x8f234cb8u;
44
45
// Number of feature dimensions
46
static constexpr IndexType Dimensions = 79856;
47
48
// clang-format off
49
// Orient a square according to perspective (rotates by 180 for black)
50
static constexpr std::int8_t OrientTBL[SQUARE_NB] = {
51
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
52
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
53
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
54
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
55
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
56
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
57
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
58
SQ_A1, SQ_A1, SQ_A1, SQ_A1, SQ_H1, SQ_H1, SQ_H1, SQ_H1,
59
};
60
61
static constexpr int map[PIECE_TYPE_NB-2][PIECE_TYPE_NB-2] = {
62
{0, 1, -1, 2, -1, -1},
63
{0, 1, 2, 3, 4, 5},
64
{0, 1, 2, 3, -1, 4},
65
{0, 1, 2, 3, -1, 4},
66
{0, 1, 2, 3, 4, 5},
67
{0, 1, 2, 3, -1, -1}
68
};
69
// clang-format on
70
71
struct FusedUpdateData {
72
Bitboard dp2removedOriginBoard = 0;
73
Bitboard dp2removedTargetBoard = 0;
74
75
Square dp2removed;
76
};
77
78
// Maximum number of simultaneously active features.
79
static constexpr IndexType MaxActiveDimensions = 128;
80
using IndexList = ValueList<IndexType, MaxActiveDimensions>;
81
using DiffType = DirtyThreats;
82
83
static IndexType
84
make_index(Color perspective, Piece attkr, Square from, Square to, Piece attkd, Square ksq);
85
86
// Get a list of indices for active features
87
static void append_active_indices(Color perspective, const Position& pos, IndexList& active);
88
89
// Get a list of indices for recently changed features
90
static void append_changed_indices(Color perspective,
91
Square ksq,
92
const DiffType& diff,
93
IndexList& removed,
94
IndexList& added,
95
FusedUpdateData* fd = nullptr,
96
bool first = false);
97
98
// Returns whether the change stored in this DirtyPiece means
99
// that a full accumulator refresh is required.
100
static bool requires_refresh(const DiffType& diff, Color perspective);
101
};
102
103
} // namespace Stockfish::Eval::NNUE::Features
104
105
#endif // #ifndef NNUE_FEATURES_FULL_THREATS_INCLUDED
106
107