diff --git a/src/misc.h b/src/misc.h index d1c368fdd..d69f7ef38 100644 --- a/src/misc.h +++ b/src/misc.h @@ -201,6 +201,12 @@ class ValueList { assert(size_ < MaxSize); values_[size_++] = value; } + // pushes back value if value < max + void push_back_if_lt(const T& value, const T& max) { + assert(size_ < MaxSize); + values_[size_] = value; + size_ += (value < max); + } const T* begin() const { return values_; } const T* end() const { return values_ + size_; } const T& operator[](int index) const { return values_[index]; } diff --git a/src/nnue/features/full_threats.cpp b/src/nnue/features/full_threats.cpp index 6cae5d0ed..a62a664e0 100644 --- a/src/nnue/features/full_threats.cpp +++ b/src/nnue/features/full_threats.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -236,8 +235,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos, Piece attacked = pos.piece_on(to); IndexType index = make_index(perspective, attacker, from, to, attacked, ksq); - if (index < Dimensions) - active.push_back(index); + active.push_back_if_lt(index, Dimensions); } while (attacks_right) @@ -247,8 +245,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos, Piece attacked = pos.piece_on(to); IndexType index = make_index(perspective, attacker, from, to, attacked, ksq); - if (index < Dimensions) - active.push_back(index); + active.push_back_if_lt(index, Dimensions); } // Set of pawns which are prevented from movement by a pawn in front of them @@ -261,8 +258,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos, assert(type_of(attacked) == PAWN); IndexType index = make_index(perspective, attacker, from, to, attacked, ksq); - if (index < Dimensions) - active.push_back(index); + active.push_back_if_lt(index, Dimensions); } } else @@ -279,8 +275,7 @@ void FullThreats::append_active_indices(Color perspective, const Position& pos, IndexType index = make_index(perspective, attacker, from, to, attacked, ksq); - if (index < Dimensions) - active.push_back(index); + active.push_back_if_lt(index, Dimensions); } } } @@ -342,13 +337,10 @@ void FullThreats::append_changed_indices(Color perspective, auto& insert = add ? added : removed; const IndexType index = make_index(perspective, attacker, from, to, attacked, ksq); - if (index < Dimensions) - { - if (prefetchBase) - prefetch( - prefetchBase + static_cast(index) * prefetchStride); - insert.push_back(index); - } + if (prefetchBase) + prefetch(reinterpret_cast( + reinterpret_cast(prefetchBase) + index * prefetchStride)); + insert.push_back_if_lt(index, Dimensions); } }