diff --git a/src/nnue/nnue_accumulator.cpp b/src/nnue/nnue_accumulator.cpp index 3ed729ffb..e63a68cb7 100644 --- a/src/nnue/nnue_accumulator.cpp +++ b/src/nnue/nnue_accumulator.cpp @@ -18,7 +18,6 @@ #include "nnue_accumulator.h" -#include #include #include #include @@ -338,22 +337,23 @@ struct AccumulatorUpdateContext { }; fused_row_reduce( - (from.template acc()).accumulation[perspective], - (to.template acc()).accumulation[perspective], to_weight_vector(indices)...); + (from.template acc()).accumulation[perspective].data(), + (to.template acc()).accumulation[perspective].data(), + to_weight_vector(indices)...); fused_row_reduce( - (from.template acc()).psqtAccumulation[perspective], - (to.template acc()).psqtAccumulation[perspective], + (from.template acc()).psqtAccumulation[perspective].data(), + (to.template acc()).psqtAccumulation[perspective].data(), to_psqt_weight_vector(indices)...); } void apply(const typename FeatureSet::IndexList& added, const typename FeatureSet::IndexList& removed) { - const auto fromAcc = from.template acc().accumulation[perspective]; - const auto toAcc = to.template acc().accumulation[perspective]; + const auto& fromAcc = from.template acc().accumulation[perspective]; + auto& toAcc = to.template acc().accumulation[perspective]; - const auto fromPsqtAcc = from.template acc().psqtAccumulation[perspective]; - const auto toPsqtAcc = to.template acc().psqtAccumulation[perspective]; + const auto& fromPsqtAcc = from.template acc().psqtAccumulation[perspective]; + auto& toPsqtAcc = to.template acc().psqtAccumulation[perspective]; #ifdef VECTOR using Tiling = SIMDTiling; @@ -448,8 +448,8 @@ struct AccumulatorUpdateContext { #else - std::copy_n(fromAcc, Dimensions, toAcc); - std::copy_n(fromPsqtAcc, PSQTBuckets, toPsqtAcc); + toAcc = fromAcc; + toPsqtAcc = fromPsqtAcc; for (const auto index : removed) { @@ -589,13 +589,10 @@ void update_accumulator_incremental( auto& targetAcc = target_state.template acc(); const auto& sourceAcc = computed.template acc(); - std::memcpy(targetAcc.accumulation[perspective], sourceAcc.accumulation[perspective], - sizeof(targetAcc.accumulation[perspective])); - std::memcpy(targetAcc.psqtAccumulation[perspective], - sourceAcc.psqtAccumulation[perspective], - sizeof(targetAcc.psqtAccumulation[perspective])); + targetAcc.accumulation[perspective] = sourceAcc.accumulation[perspective]; + targetAcc.psqtAccumulation[perspective] = sourceAcc.psqtAccumulation[perspective]; + targetAcc.computed[perspective] = true; - targetAcc.computed[perspective] = true; return; } @@ -643,15 +640,16 @@ void update_accumulator_incremental( (target_state.template acc()).computed[perspective] = true; } -Bitboard get_changed_pieces(const Piece oldPieces[SQUARE_NB], const Piece newPieces[SQUARE_NB]) { +Bitboard get_changed_pieces(const std::array& oldPieces, + const std::array& newPieces) { #if defined(USE_AVX512) || defined(USE_AVX2) static_assert(sizeof(Piece) == 1); Bitboard sameBB = 0; for (int i = 0; i < 64; i += 32) { - const __m256i old_v = _mm256_loadu_si256(reinterpret_cast(oldPieces + i)); - const __m256i new_v = _mm256_loadu_si256(reinterpret_cast(newPieces + i)); + const __m256i old_v = _mm256_loadu_si256(reinterpret_cast(&oldPieces[i])); + const __m256i new_v = _mm256_loadu_si256(reinterpret_cast(&newPieces[i])); const __m256i cmpEqual = _mm256_cmpeq_epi8(old_v, new_v); const std::uint32_t equalMask = _mm256_movemask_epi8(cmpEqual); sameBB |= static_cast(equalMask) << i; @@ -680,7 +678,7 @@ void update_accumulator_refresh_cache(Color pers auto& entry = cache[ksq][perspective]; PSQFeatureSet::IndexList removed, added; - const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array().data()); + const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array()); Bitboard removedBB = changedBB & entry.pieceBB; Bitboard addedBB = changedBB & pos.pieces(); @@ -696,7 +694,7 @@ void update_accumulator_refresh_cache(Color pers } entry.pieceBB = pos.pieces(); - std::copy_n(pos.piece_array().begin(), SQUARE_NB, entry.pieces); + entry.pieces = pos.piece_array(); auto& accumulator = accumulatorState.acc(); accumulator.computed[perspective] = true; @@ -812,12 +810,8 @@ void update_accumulator_refresh_cache(Color pers // The accumulator of the refresh entry has been updated. // Now copy its content to the actual accumulator we were refreshing. - - std::memcpy(accumulator.accumulation[perspective], entry.accumulation.data(), - sizeof(BiasType) * Dimensions); - - std::memcpy(accumulator.psqtAccumulation[perspective], entry.psqtAccumulation.data(), - sizeof(int32_t) * PSQTBuckets); + accumulator.accumulation[perspective] = entry.accumulation; + accumulator.psqtAccumulation[perspective] = entry.psqtAccumulation; #endif } diff --git a/src/nnue/nnue_accumulator.h b/src/nnue/nnue_accumulator.h index 181412b43..69ab49632 100644 --- a/src/nnue/nnue_accumulator.h +++ b/src/nnue/nnue_accumulator.h @@ -46,9 +46,9 @@ class FeatureTransformer; // Class that holds the result of affine transformation of input features template struct alignas(CacheLineSize) Accumulator { - std::int16_t accumulation[COLOR_NB][Size]; - std::int32_t psqtAccumulation[COLOR_NB][PSQTBuckets]; - std::array computed = {}; + std::array, COLOR_NB> accumulation; + std::array, COLOR_NB> psqtAccumulation; + std::array computed = {}; }; @@ -71,7 +71,7 @@ struct AccumulatorCaches { struct alignas(CacheLineSize) Entry { std::array accumulation; std::array psqtAccumulation; - Piece pieces[SQUARE_NB]; + std::array pieces; Bitboard pieceBB; // To initialize a refresh entry, we set all its bitboards empty,