From 3fcd374ad4306abf8a83b5c30227be6dc2716d52 Mon Sep 17 00:00:00 2001 From: Myself <63040919+AliceRoselia@users.noreply.github.com> Date: Sun, 17 May 2026 20:50:22 +0200 Subject: [PATCH] Simplify and refactor clipped_relu.h remove AVX2 special casing based on earlier https://github.com/official-stockfish/Stockfish/pull/6824 that passed non-regression STC: LLR: 3.56 (-2.94,2.94) <-1.75,0.25> Total: 49888 W: 12683 L: 12439 D: 24766 Ptnml(0-2): 104, 5170, 14153, 5412, 105 https://tests.stockfishchess.org/tests/view/6a0702c58d9bd4cd7cd69462 No change on non-AVX2 architectures. closes https://github.com/official-stockfish/Stockfish/pull/6825 No functional change --- src/nnue/layers/clipped_relu.h | 46 +++------------------------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/src/nnue/layers/clipped_relu.h b/src/nnue/layers/clipped_relu.h index 009e2e5ce..50964ae25 100644 --- a/src/nnue/layers/clipped_relu.h +++ b/src/nnue/layers/clipped_relu.h @@ -67,49 +67,9 @@ class ClippedReLU { // Forward propagation void propagate(const InputType* input, OutputType* output) const { -#if defined(USE_AVX2) - if constexpr (InputDimensions % SimdWidth == 0) - { - constexpr IndexType NumChunks = InputDimensions / SimdWidth; - const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0); - const auto in = reinterpret_cast(input); - const auto out = reinterpret_cast<__m256i*>(output); - for (IndexType i = 0; i < NumChunks; ++i) - { - const __m256i words0 = - _mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 0]), - _mm256_load_si256(&in[i * 4 + 1])), - WeightScaleBits); - const __m256i words1 = - _mm256_srli_epi16(_mm256_packus_epi32(_mm256_load_si256(&in[i * 4 + 2]), - _mm256_load_si256(&in[i * 4 + 3])), - WeightScaleBits); - _mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32( - _mm256_packs_epi16(words0, words1), Offsets)); - } - } - else - { - constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2); - const auto in = reinterpret_cast(input); - const auto out = reinterpret_cast<__m128i*>(output); - for (IndexType i = 0; i < NumChunks; ++i) - { - const __m128i words0 = _mm_srli_epi16( - _mm_packus_epi32(_mm_load_si128(&in[i * 4 + 0]), _mm_load_si128(&in[i * 4 + 1])), - WeightScaleBits); - const __m128i words1 = _mm_srli_epi16( - _mm_packus_epi32(_mm_load_si128(&in[i * 4 + 2]), _mm_load_si128(&in[i * 4 + 3])), - WeightScaleBits); - _mm_store_si128(&out[i], _mm_packs_epi16(words0, words1)); - } - } - constexpr IndexType Start = InputDimensions % SimdWidth == 0 - ? InputDimensions / SimdWidth * SimdWidth - : InputDimensions / (SimdWidth / 2) * (SimdWidth / 2); -#elif defined(USE_SSE2) - constexpr IndexType NumChunks = InputDimensions / SimdWidth; +#if defined(USE_SSE2) + constexpr IndexType NumChunks = InputDimensions / 16; #ifndef USE_SSE41 const __m128i k0x80s = _mm_set1_epi8(-128); @@ -138,7 +98,7 @@ class ClippedReLU { _mm_store_si128(&out[i], _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)); #endif } - constexpr IndexType Start = NumChunks * SimdWidth; + constexpr IndexType Start = NumChunks * 16; #elif defined(USE_NEON) constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);