Path: blob/master/src/nnue/features/half_ka_v2_hm.h
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 HalfKP of NNUE evaluation function1920#ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED21#define NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED2223#include <cstdint>2425#include "../../misc.h"26#include "../../types.h"27#include "../nnue_common.h"2829namespace Stockfish {30class Position;31}3233namespace Stockfish::Eval::NNUE::Features {3435// Feature HalfKAv2_hm: Combination of the position of own king and the36// position of pieces. Position mirrored such that king is always on e..h files.37class HalfKAv2_hm {3839// Unique number for each piece type on each square40enum {41PS_NONE = 0,42PS_W_PAWN = 0,43PS_B_PAWN = 1 * SQUARE_NB,44PS_W_KNIGHT = 2 * SQUARE_NB,45PS_B_KNIGHT = 3 * SQUARE_NB,46PS_W_BISHOP = 4 * SQUARE_NB,47PS_B_BISHOP = 5 * SQUARE_NB,48PS_W_ROOK = 6 * SQUARE_NB,49PS_B_ROOK = 7 * SQUARE_NB,50PS_W_QUEEN = 8 * SQUARE_NB,51PS_B_QUEEN = 9 * SQUARE_NB,52PS_KING = 10 * SQUARE_NB,53PS_NB = 11 * SQUARE_NB54};5556static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {57// Convention: W - us, B - them58// Viewed from other side, W and B are reversed59{PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE,60PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_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,62PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE}};6364public:65// Feature name66static constexpr const char* Name = "HalfKAv2_hm(Friend)";6768// Hash value embedded in the evaluation file69static constexpr std::uint32_t HashValue = 0x7f234cb8u;7071// Number of feature dimensions72static constexpr IndexType Dimensions =73static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB) / 2;7475#define B(v) (v * PS_NB)76// clang-format off77static constexpr int KingBuckets[COLOR_NB][SQUARE_NB] = {78{ B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28),79B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),80B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),81B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),82B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),83B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),84B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),85B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0) },86{ B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0),87B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),88B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),89B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),90B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),91B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),92B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),93B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28) }94};95// clang-format on96#undef B97// clang-format off98// Orient a square according to perspective (rotates by 180 for black)99static constexpr int OrientTBL[COLOR_NB][SQUARE_NB] = {100{ SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,101SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,102SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,103SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,104SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,105SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,106SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,107SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1 },108{ SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,109SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,110SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,111SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,112SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,113SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,114SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,115SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8 }116};117// clang-format on118119// Maximum number of simultaneously active features.120static constexpr IndexType MaxActiveDimensions = 32;121using IndexList = ValueList<IndexType, MaxActiveDimensions>;122123// Index of a feature for a given king position and another piece on some square124template<Color Perspective>125static IndexType make_index(Square s, Piece pc, Square ksq);126127// Get a list of indices for active features128template<Color Perspective>129static void append_active_indices(const Position& pos, IndexList& active);130131// Get a list of indices for recently changed features132template<Color Perspective>133static void134append_changed_indices(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);135136// Returns whether the change stored in this DirtyPiece means137// that a full accumulator refresh is required.138static bool requires_refresh(const DirtyPiece& dirtyPiece, Color perspective);139};140141} // namespace Stockfish::Eval::NNUE::Features142143#endif // #ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED144145146