Skip to content

Commit 9b7983a

Browse files
mstemberavondele
authored andcommitted
Cleaned up MakeIndex()
The index order in kpp_board_index[][] is reversed to be more optimal for the access pattern STC https://tests.stockfishchess.org/tests/view/5fbd74f967cbf42301d6b24f LLR: 2.93 (-2.94,2.94) {-1.25,0.25} Total: 27504 W: 2686 L: 2607 D: 22211 Ptnml(0-2): 84, 2001, 9526, 2034, 107 closes official-stockfish/Stockfish#3233 No functional change
1 parent 190dd26 commit 9b7983a

File tree

4 files changed

+14
-35
lines changed

4 files changed

+14
-35
lines changed

src/nnue/evaluate_nnue.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@
3131

3232
namespace Eval::NNUE {
3333

34-
const uint32_t kpp_board_index[PIECE_NB][COLOR_NB] = {
35-
// convention: W - us, B - them
36-
// viewed from other side, W and B are reversed
37-
{ PS_NONE, PS_NONE },
38-
{ PS_W_PAWN, PS_B_PAWN },
39-
{ PS_W_KNIGHT, PS_B_KNIGHT },
40-
{ PS_W_BISHOP, PS_B_BISHOP },
41-
{ PS_W_ROOK, PS_B_ROOK },
42-
{ PS_W_QUEEN, PS_B_QUEEN },
43-
{ PS_W_KING, PS_B_KING },
44-
{ PS_NONE, PS_NONE },
45-
{ PS_NONE, PS_NONE },
46-
{ PS_B_PAWN, PS_W_PAWN },
47-
{ PS_B_KNIGHT, PS_W_KNIGHT },
48-
{ PS_B_BISHOP, PS_W_BISHOP },
49-
{ PS_B_ROOK, PS_W_ROOK },
50-
{ PS_B_QUEEN, PS_W_QUEEN },
51-
{ PS_B_KING, PS_W_KING },
52-
{ PS_NONE, PS_NONE }
53-
};
54-
5534
// Input feature converter
5635
LargePagePtr<FeatureTransformer> feature_transformer;
5736

src/nnue/features/half_kp.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ namespace Eval::NNUE::Features {
2828
return Square(int(s) ^ (bool(perspective) * 63));
2929
}
3030

31-
// Find the index of the feature quantity from the king position and PieceSquare
32-
template <Side AssociatedKing>
33-
inline IndexType HalfKP<AssociatedKing>::MakeIndex(
34-
Color perspective, Square s, Piece pc, Square ksq) {
35-
36-
return IndexType(orient(perspective, s) + kpp_board_index[pc][perspective] + PS_END * ksq);
31+
// Index of a feature for a given king position and another piece on some square
32+
inline IndexType make_index(Color perspective, Square s, Piece pc, Square ksq) {
33+
return IndexType(orient(perspective, s) + kpp_board_index[perspective][pc] + PS_END * ksq);
3734
}
3835

3936
// Get a list of indices for active features
@@ -45,7 +42,7 @@ namespace Eval::NNUE::Features {
4542
Bitboard bb = pos.pieces() & ~pos.pieces(KING);
4643
while (bb) {
4744
Square s = pop_lsb(&bb);
48-
active->push_back(MakeIndex(perspective, s, pos.piece_on(s), ksq));
45+
active->push_back(make_index(perspective, s, pos.piece_on(s), ksq));
4946
}
5047
}
5148

@@ -60,9 +57,9 @@ namespace Eval::NNUE::Features {
6057
Piece pc = dp.piece[i];
6158
if (type_of(pc) == KING) continue;
6259
if (dp.from[i] != SQ_NONE)
63-
removed->push_back(MakeIndex(perspective, dp.from[i], pc, ksq));
60+
removed->push_back(make_index(perspective, dp.from[i], pc, ksq));
6461
if (dp.to[i] != SQ_NONE)
65-
added->push_back(MakeIndex(perspective, dp.to[i], pc, ksq));
62+
added->push_back(make_index(perspective, dp.to[i], pc, ksq));
6663
}
6764
}
6865

src/nnue/features/half_kp.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ namespace Eval::NNUE::Features {
5252
// Get a list of indices for recently changed features
5353
static void AppendChangedIndices(const Position& pos, const DirtyPiece& dp, Color perspective,
5454
IndexList* removed, IndexList* added);
55-
56-
private:
57-
// Index of a feature for a given king position and another piece on some square
58-
static IndexType MakeIndex(Color perspective, Square s, Piece pc, Square sq_k);
5955
};
6056

6157
} // namespace Eval::NNUE::Features

src/nnue/nnue_common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ namespace Eval::NNUE {
9090
PS_END2 = 12 * SQUARE_NB + 1
9191
};
9292

93-
extern const uint32_t kpp_board_index[PIECE_NB][COLOR_NB];
93+
constexpr uint32_t kpp_board_index[COLOR_NB][PIECE_NB] = {
94+
// convention: W - us, B - them
95+
// viewed from other side, W and B are reversed
96+
{ PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE,
97+
PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE },
98+
{ PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE,
99+
PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE }
100+
};
94101

95102
// Type of input feature after conversion
96103
using TransformedFeatureType = std::uint8_t;

0 commit comments

Comments
 (0)