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.h
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 HalfKP of NNUE evaluation function
20
21
#ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
22
#define NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
23
24
#include <cstdint>
25
26
#include "../../misc.h"
27
#include "../../types.h"
28
#include "../nnue_common.h"
29
30
namespace Stockfish {
31
class Position;
32
}
33
34
namespace Stockfish::Eval::NNUE::Features {
35
36
// Feature HalfKAv2_hm: Combination of the position of own king and the
37
// position of pieces. Position mirrored such that king is always on e..h files.
38
class HalfKAv2_hm {
39
40
// Unique number for each piece type on each square
41
enum {
42
PS_NONE = 0,
43
PS_W_PAWN = 0,
44
PS_B_PAWN = 1 * SQUARE_NB,
45
PS_W_KNIGHT = 2 * SQUARE_NB,
46
PS_B_KNIGHT = 3 * SQUARE_NB,
47
PS_W_BISHOP = 4 * SQUARE_NB,
48
PS_B_BISHOP = 5 * SQUARE_NB,
49
PS_W_ROOK = 6 * SQUARE_NB,
50
PS_B_ROOK = 7 * SQUARE_NB,
51
PS_W_QUEEN = 8 * SQUARE_NB,
52
PS_B_QUEEN = 9 * SQUARE_NB,
53
PS_KING = 10 * SQUARE_NB,
54
PS_NB = 11 * SQUARE_NB
55
};
56
57
static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
58
// Convention: W - us, B - them
59
// Viewed from other side, W and B are reversed
60
{PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE,
61
PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE},
62
{PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE,
63
PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE}};
64
65
public:
66
// Feature name
67
static constexpr const char* Name = "HalfKAv2_hm(Friend)";
68
69
// Hash value embedded in the evaluation file
70
static constexpr std::uint32_t HashValue = 0x7f234cb8u;
71
72
// Number of feature dimensions
73
static constexpr IndexType Dimensions =
74
static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB) / 2;
75
76
#define B(v) (v * PS_NB)
77
// clang-format off
78
static constexpr int KingBuckets[COLOR_NB][SQUARE_NB] = {
79
{ B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28),
80
B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),
81
B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),
82
B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),
83
B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),
84
B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),
85
B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),
86
B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0) },
87
{ B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0),
88
B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),
89
B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),
90
B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),
91
B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),
92
B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),
93
B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),
94
B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28) }
95
};
96
// clang-format on
97
#undef B
98
// clang-format off
99
// Orient a square according to perspective (rotates by 180 for black)
100
static constexpr int OrientTBL[COLOR_NB][SQUARE_NB] = {
101
{ SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
102
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
103
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
104
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
105
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
106
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
107
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
108
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1 },
109
{ SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
110
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
111
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
112
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
113
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
114
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
115
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
116
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8 }
117
};
118
// clang-format on
119
120
// Maximum number of simultaneously active features.
121
static constexpr IndexType MaxActiveDimensions = 32;
122
using IndexList = ValueList<IndexType, MaxActiveDimensions>;
123
124
// Index of a feature for a given king position and another piece on some square
125
template<Color Perspective>
126
static IndexType make_index(Square s, Piece pc, Square ksq);
127
128
// Get a list of indices for active features
129
template<Color Perspective>
130
static void append_active_indices(const Position& pos, IndexList& active);
131
132
// Get a list of indices for recently changed features
133
template<Color Perspective>
134
static void
135
append_changed_indices(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
136
137
// Returns whether the change stored in this DirtyPiece means
138
// that a full accumulator refresh is required.
139
static bool requires_refresh(const DirtyPiece& dirtyPiece, Color perspective);
140
};
141
142
} // namespace Stockfish::Eval::NNUE::Features
143
144
#endif // #ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
145
146