+ const auto output = reinterpret_cast<OutputType*>(buffer);
+ const auto inputVector = reinterpret_cast<const vec_t*>(input);
+
+ static_assert(OutputDimensions % OutputSimdWidth == 0 || OutputDimensions == 1);
+
+ // OutputDimensions is either 1 or a multiple of SimdWidth
+ // because then it is also an input dimension.
+ if constexpr (OutputDimensions % OutputSimdWidth == 0)
+ {
+ constexpr IndexType NumChunks = PaddedInputDimensions / 4;
+
+ const auto input32 = reinterpret_cast<const std::int32_t*>(input);
+ vec_t* outptr = reinterpret_cast<vec_t*>(output);
+ std::memcpy(output, biases, OutputDimensions * sizeof(OutputType));
+
+ for (int i = 0; i < (int)NumChunks - 3; i += 4)
+ {
+ const vec_t in0 = vec_set_32(input32[i + 0]);
+ const vec_t in1 = vec_set_32(input32[i + 1]);
+ const vec_t in2 = vec_set_32(input32[i + 2]);
+ const vec_t in3 = vec_set_32(input32[i + 3]);
+ const auto col0 = reinterpret_cast<const vec_t*>(&weights[(i + 0) * OutputDimensions * 4]);
+ const auto col1 = reinterpret_cast<const vec_t*>(&weights[(i + 1) * OutputDimensions * 4]);
+ const auto col2 = reinterpret_cast<const vec_t*>(&weights[(i + 2) * OutputDimensions * 4]);
+ const auto col3 = reinterpret_cast<const vec_t*>(&weights[(i + 3) * OutputDimensions * 4]);
+ for (int j = 0; j * OutputSimdWidth < OutputDimensions; ++j)
+ vec_add_dpbusd_32x4(outptr[j], in0, col0[j], in1, col1[j], in2, col2[j], in3, col3[j]);
+ }
+ for (int i = 0; i < canSaturate16.count; ++i)
+ output[canSaturate16.ids[i].out] += input[canSaturate16.ids[i].in] * canSaturate16.ids[i].w;
+ }
+ else if constexpr (OutputDimensions == 1)
+ {
+#if defined (USE_AVX512)
+ if constexpr (PaddedInputDimensions % (SimdWidth * 2) != 0)
+ {
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const auto inputVector256 = reinterpret_cast<const __m256i*>(input);
+
+ __m256i sum0 = _mm256_setzero_si256();
+ const auto row0 = reinterpret_cast<const __m256i*>(&weights[0]);
+
+ for (int j = 0; j < (int)NumChunks; ++j)
+ {
+ const __m256i in = inputVector256[j];
+ m256_add_dpbusd_epi32(sum0, in, row0[j]);
+ }
+ output[0] = m256_hadd(sum0, biases[0]);
+ }
+ else
+#endif
+ {
+#if defined (USE_AVX512)
+ constexpr IndexType NumChunks = PaddedInputDimensions / (SimdWidth * 2);
+#else
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+#endif
+ vec_t sum0 = vec_setzero();
+ const auto row0 = reinterpret_cast<const vec_t*>(&weights[0]);
+
+ for (int j = 0; j < (int)NumChunks; ++j)
+ {
+ const vec_t in = inputVector[j];
+ vec_add_dpbusd_32(sum0, in, row0[j]);
+ }
+ output[0] = vec_hadd(sum0, biases[0]);
+ }
+ }
+
+#else
+
+// Use old implementation for the other architectures.
+
+ auto output = reinterpret_cast<OutputType*>(buffer);
+
+#if defined(USE_SSE2)
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const __m128i Zeros = _mm_setzero_si128();
+ const auto inputVector = reinterpret_cast<const __m128i*>(input);
+
+#elif defined(USE_MMX)
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const __m64 Zeros = _mm_setzero_si64();
+ const auto inputVector = reinterpret_cast<const __m64*>(input);
+
+#elif defined(USE_NEON)
+ constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
+ const auto inputVector = reinterpret_cast<const int8x8_t*>(input);
+#endif
+
+ for (IndexType i = 0; i < OutputDimensions; ++i) {
+ const IndexType offset = i * PaddedInputDimensions;
+
+#if defined(USE_SSE2)
+ __m128i sumLo = _mm_cvtsi32_si128(biases[i]);
+ __m128i sumHi = Zeros;
+ const auto row = reinterpret_cast<const __m128i*>(&weights[offset]);
+ for (IndexType j = 0; j < NumChunks; ++j) {