mirror of
https://github.com/sockspls/badfish
synced 2025-05-01 01:03:09 +00:00
Simplify away ValueListInserter
plus minor cleanups STC: https://tests.stockfishchess.org/tests/view/616f059b40f619782fd4f73f LLR: 2.94 (-2.94,2.94) <-2.50,0.50> Total: 84992 W: 21244 L: 21197 D: 42551 Ptnml(0-2): 279, 9005, 23868, 9078, 266 closes https://github.com/official-stockfish/Stockfish/pull/3749 No functional change
This commit is contained in:
parent
8a8640a761
commit
644f6d4790
4 changed files with 24 additions and 42 deletions
17
src/misc.h
17
src/misc.h
|
@ -111,22 +111,6 @@ class RunningAverage {
|
||||||
int64_t average;
|
int64_t average;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class ValueListInserter {
|
|
||||||
public:
|
|
||||||
ValueListInserter(T* v, std::size_t& s) :
|
|
||||||
values(v),
|
|
||||||
size(&s)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void push_back(const T& value) { values[(*size)++] = value; }
|
|
||||||
private:
|
|
||||||
T* values;
|
|
||||||
std::size_t* size;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, std::size_t MaxSize>
|
template <typename T, std::size_t MaxSize>
|
||||||
class ValueList {
|
class ValueList {
|
||||||
|
|
||||||
|
@ -140,7 +124,6 @@ public:
|
||||||
const T& operator[](std::size_t index) const { return values_[index]; }
|
const T& operator[](std::size_t index) const { return values_[index]; }
|
||||||
const T* begin() const { return values_; }
|
const T* begin() const { return values_; }
|
||||||
const T* end() const { return values_ + size_; }
|
const T* end() const { return values_ + size_; }
|
||||||
operator ValueListInserter<T>() { return ValueListInserter(values_, size_); }
|
|
||||||
|
|
||||||
void swap(ValueList& other) {
|
void swap(ValueList& other) {
|
||||||
const std::size_t maxSize = std::max(size_, other.size_);
|
const std::size_t maxSize = std::max(size_, other.size_);
|
||||||
|
|
|
@ -39,7 +39,7 @@ namespace Stockfish::Eval::NNUE::Features {
|
||||||
void HalfKAv2_hm::append_active_indices(
|
void HalfKAv2_hm::append_active_indices(
|
||||||
const Position& pos,
|
const Position& pos,
|
||||||
Color perspective,
|
Color perspective,
|
||||||
ValueListInserter<IndexType> active
|
IndexList& active
|
||||||
) {
|
) {
|
||||||
Square ksq = pos.square<KING>(perspective);
|
Square ksq = pos.square<KING>(perspective);
|
||||||
Bitboard bb = pos.pieces();
|
Bitboard bb = pos.pieces();
|
||||||
|
@ -55,22 +55,20 @@ namespace Stockfish::Eval::NNUE::Features {
|
||||||
|
|
||||||
void HalfKAv2_hm::append_changed_indices(
|
void HalfKAv2_hm::append_changed_indices(
|
||||||
Square ksq,
|
Square ksq,
|
||||||
StateInfo* st,
|
const DirtyPiece& dp,
|
||||||
Color perspective,
|
Color perspective,
|
||||||
ValueListInserter<IndexType> removed,
|
IndexList& removed,
|
||||||
ValueListInserter<IndexType> added
|
IndexList& added
|
||||||
) {
|
) {
|
||||||
const auto& dp = st->dirtyPiece;
|
|
||||||
for (int i = 0; i < dp.dirty_num; ++i) {
|
for (int i = 0; i < dp.dirty_num; ++i) {
|
||||||
Piece pc = dp.piece[i];
|
|
||||||
if (dp.from[i] != SQ_NONE)
|
if (dp.from[i] != SQ_NONE)
|
||||||
removed.push_back(make_index(perspective, dp.from[i], pc, ksq));
|
removed.push_back(make_index(perspective, dp.from[i], dp.piece[i], ksq));
|
||||||
if (dp.to[i] != SQ_NONE)
|
if (dp.to[i] != SQ_NONE)
|
||||||
added.push_back(make_index(perspective, dp.to[i], pc, ksq));
|
added.push_back(make_index(perspective, dp.to[i], dp.piece[i], ksq));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int HalfKAv2_hm::update_cost(StateInfo* st) {
|
int HalfKAv2_hm::update_cost(const StateInfo* st) {
|
||||||
return st->dirtyPiece.dirty_num;
|
return st->dirtyPiece.dirty_num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +76,7 @@ namespace Stockfish::Eval::NNUE::Features {
|
||||||
return pos.count<ALL_PIECES>();
|
return pos.count<ALL_PIECES>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HalfKAv2_hm::requires_refresh(StateInfo* st, Color perspective) {
|
bool HalfKAv2_hm::requires_refresh(const StateInfo* st, Color perspective) {
|
||||||
return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
|
return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ namespace Stockfish::Eval::NNUE::Features {
|
||||||
PS_W_QUEEN = 8 * SQUARE_NB,
|
PS_W_QUEEN = 8 * SQUARE_NB,
|
||||||
PS_B_QUEEN = 9 * SQUARE_NB,
|
PS_B_QUEEN = 9 * SQUARE_NB,
|
||||||
PS_KING = 10 * SQUARE_NB,
|
PS_KING = 10 * SQUARE_NB,
|
||||||
PS_NB = 11 * SQUARE_NB
|
PS_NB = 11 * SQUARE_NB
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
|
static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
|
||||||
|
@ -85,36 +85,38 @@ namespace Stockfish::Eval::NNUE::Features {
|
||||||
-1, -1, -1, -1, 23, 22, 21, 20,
|
-1, -1, -1, -1, 23, 22, 21, 20,
|
||||||
-1, -1, -1, -1, 19, 18, 17, 16,
|
-1, -1, -1, -1, 19, 18, 17, 16,
|
||||||
-1, -1, -1, -1, 15, 14, 13, 12,
|
-1, -1, -1, -1, 15, 14, 13, 12,
|
||||||
-1, -1, -1, -1, 11, 10, 9, 8,
|
-1, -1, -1, -1, 11, 10, 9, 8,
|
||||||
-1, -1, -1, -1, 7, 6, 5, 4,
|
-1, -1, -1, -1, 7, 6, 5, 4,
|
||||||
-1, -1, -1, -1, 3, 2, 1, 0
|
-1, -1, -1, -1, 3, 2, 1, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
// Maximum number of simultaneously active features.
|
// Maximum number of simultaneously active features.
|
||||||
static constexpr IndexType MaxActiveDimensions = 32;
|
static constexpr IndexType MaxActiveDimensions = 32;
|
||||||
|
using IndexList = ValueList<IndexType, MaxActiveDimensions>;
|
||||||
|
|
||||||
// Get a list of indices for active features
|
// Get a list of indices for active features
|
||||||
static void append_active_indices(
|
static void append_active_indices(
|
||||||
const Position& pos,
|
const Position& pos,
|
||||||
Color perspective,
|
Color perspective,
|
||||||
ValueListInserter<IndexType> active);
|
IndexList& active);
|
||||||
|
|
||||||
// Get a list of indices for recently changed features
|
// Get a list of indices for recently changed features
|
||||||
static void append_changed_indices(
|
static void append_changed_indices(
|
||||||
Square ksq,
|
Square ksq,
|
||||||
StateInfo* st,
|
const DirtyPiece& dp,
|
||||||
Color perspective,
|
Color perspective,
|
||||||
ValueListInserter<IndexType> removed,
|
IndexList& removed,
|
||||||
ValueListInserter<IndexType> added);
|
IndexList& added
|
||||||
|
);
|
||||||
|
|
||||||
// Returns the cost of updating one perspective, the most costly one.
|
// Returns the cost of updating one perspective, the most costly one.
|
||||||
// Assumes no refresh needed.
|
// Assumes no refresh needed.
|
||||||
static int update_cost(StateInfo* st);
|
static int update_cost(const StateInfo* st);
|
||||||
static int refresh_cost(const Position& pos);
|
static int refresh_cost(const Position& pos);
|
||||||
|
|
||||||
// Returns whether the change stored in this StateInfo means that
|
// Returns whether the change stored in this StateInfo means that
|
||||||
// a full accumulator refresh is required.
|
// a full accumulator refresh is required.
|
||||||
static bool requires_refresh(StateInfo* st, Color perspective);
|
static bool requires_refresh(const StateInfo* st, Color perspective);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Stockfish::Eval::NNUE::Features
|
} // namespace Stockfish::Eval::NNUE::Features
|
||||||
|
|
|
@ -370,7 +370,6 @@ namespace Stockfish::Eval::NNUE {
|
||||||
// That might depend on the feature set and generally relies on the
|
// That might depend on the feature set and generally relies on the
|
||||||
// feature set's update cost calculation to be correct and never
|
// feature set's update cost calculation to be correct and never
|
||||||
// allow updates with more added/removed features than MaxActiveDimensions.
|
// allow updates with more added/removed features than MaxActiveDimensions.
|
||||||
using IndexList = ValueList<IndexType, FeatureSet::MaxActiveDimensions>;
|
|
||||||
|
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
// Gcc-10.2 unnecessarily spills AVX2 registers if this array
|
// Gcc-10.2 unnecessarily spills AVX2 registers if this array
|
||||||
|
@ -404,12 +403,12 @@ namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
// Gather all features to be updated.
|
// Gather all features to be updated.
|
||||||
const Square ksq = pos.square<KING>(perspective);
|
const Square ksq = pos.square<KING>(perspective);
|
||||||
IndexList removed[2], added[2];
|
FeatureSet::IndexList removed[2], added[2];
|
||||||
FeatureSet::append_changed_indices(
|
FeatureSet::append_changed_indices(
|
||||||
ksq, next, perspective, removed[0], added[0]);
|
ksq, next->dirtyPiece, perspective, removed[0], added[0]);
|
||||||
for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
|
for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
|
||||||
FeatureSet::append_changed_indices(
|
FeatureSet::append_changed_indices(
|
||||||
ksq, st2, perspective, removed[1], added[1]);
|
ksq, st2->dirtyPiece, perspective, removed[1], added[1]);
|
||||||
|
|
||||||
// Mark the accumulators as computed.
|
// Mark the accumulators as computed.
|
||||||
next->accumulator.computed[perspective] = true;
|
next->accumulator.computed[perspective] = true;
|
||||||
|
@ -534,7 +533,7 @@ namespace Stockfish::Eval::NNUE {
|
||||||
// Refresh the accumulator
|
// Refresh the accumulator
|
||||||
auto& accumulator = pos.state()->accumulator;
|
auto& accumulator = pos.state()->accumulator;
|
||||||
accumulator.computed[perspective] = true;
|
accumulator.computed[perspective] = true;
|
||||||
IndexList active;
|
FeatureSet::IndexList active;
|
||||||
FeatureSet::append_active_indices(pos, perspective, active);
|
FeatureSet::append_active_indices(pos, perspective, active);
|
||||||
|
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
|
|
Loading…
Add table
Reference in a new issue