// Copyright 2023 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

$assert REQUANTIZATION == "FP32"
$assert DATATYPE in ["QC8", "QS8", "QU8"]
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$assert CHANNEL_TILE % 16 == 0
$assert CHANNEL_TILE >= 16
$CHANNEL_SUBTILE = 16
$assert CHANNEL_TILE % CHANNEL_SUBTILE == 0
$CHANNEL_ROUND = 1
$assert MIDDLE_PASS_TILE <= LAST_PASS_TILE
$assert FIRST_PASS_TILE >= 1
$assert MIDDLE_PASS_TILE >= 1
$assert LAST_PASS_TILE >= 1
#include <assert.h>

#include <immintrin.h>

#include "xnnpack/dwconv.h"
#include "xnnpack/intrinsics-polyfill.h"


$DATATYPE_SPEC = {"QS8": "qs8", "QC8": "qs8_qc8w", "QU8": "qu8"}[DATATYPE]
$PARAMS_STRUCT = REQUANTIZATION.lower() + "_scalar"
$PARAMS_TYPE = "union xnn_qs8_qc8w_conv_minmax_params" if DATATYPE == "QC8" else "union xnn_%s_conv_minmax_params" % DATATYPE.lower()
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
$_MM512_CVTEPX8_EPI32 = "_mm512_cvtepu8_epi32" if DATATYPE == "QU8" else "_mm512_cvtepi8_epi32"
$_MM256_PACKXS_EPI16 = "_mm256_packus_epi16" if DATATYPE == "QU8" else "_mm256_packs_epi16"
$_MM_PACKXS_EPI16 = "_mm_packus_epi16" if DATATYPE == "QU8" else "_mm_packs_epi16"
$_MM256_MAX_EPX8 = "_mm256_max_epu8" if DATATYPE == "QU8" else "_mm256_max_epi8"
$_MM_MAX_EPX8 = "_mm_max_epu8" if DATATYPE == "QU8" else "_mm_max_epi8"
void xnn_${DATATYPE_SPEC}_dwconv_minmax_${REQUANTIZATION.lower()}_ukernel_${FIRST_PASS_TILE}f${MIDDLE_PASS_TILE}m${LAST_PASS_TILE}l${CHANNEL_TILE}c${CHANNEL_SUBTILE}s${CHANNEL_ROUND}r__avx512skx_mul32(
    size_t channels,
    size_t output_width,
    const ${XINT8_T}** input,
    const void* weights,
    ${XINT8_T}* output,
    intptr_t input_stride,
    size_t output_increment,
    size_t input_offset,
    const ${XINT8_T}* zero,
    size_t kernel_size,
    int32_t* buffer,
    const ${PARAMS_TYPE} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(channels != 0);
  assert(output_width != 0);
  assert(kernel_size > ${FIRST_PASS_TILE});

  $if DATATYPE == "QU8":
    const __m512i vk_zero_point = _mm512_set1_epi32(params->${PARAMS_STRUCT}.kernel_zero_point);
    // XNN_FORCE_REALIZATION(vk_zero_point);

  $if DATATYPE != "QC8":
    const __m512 vscale = _mm512_set1_ps(params->${PARAMS_STRUCT}.scale);
    // XNN_FORCE_REALIZATION(vscale);
  const __m512 voutput_max_less_zero_point = _mm512_set1_ps((int32_t) params->${PARAMS_STRUCT}.output_max - (int32_t) params->${PARAMS_STRUCT}.output_zero_point);
  $if CHANNEL_TILE > 16:
    const __m512i voutput_zero_point = _mm512_set1_epi16((int16_t) params->${PARAMS_STRUCT}.output_zero_point);
    const __m256i voutput_min = _mm256_set1_epi8(params->${PARAMS_STRUCT}.output_min);
    const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 5, 1, 6, 2, 4, 0);
  $else:
    const __m256i voutput_zero_point = _mm256_set1_epi16((int16_t) params->${PARAMS_STRUCT}.output_zero_point);
    const __m128i voutput_min = _mm_set1_epi8(params->${PARAMS_STRUCT}.output_min);
  // XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
  // XNN_FORCE_REALIZATION(voutput_zero_point);
  // XNN_FORCE_REALIZATION(voutput_min);

  do {
    const void* w = weights;

    // First pass to process ${FIRST_PASS_TILE} inputs.
    {
      int32_t* b = buffer;
      $for K in range(FIRST_PASS_TILE):
        const ${XINT8_T}* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const ${XINT8_T}*) ((uintptr_t) i${K} + input_offset);
        }
      input += ${FIRST_PASS_TILE};

      size_t c = channels;

      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(w);
        $for C in range(16, CHANNEL_TILE, 16):
          __m512i vacc${ABC[C:C+16]} = _mm512_loadu_si512((const void*) ((const int32_t*) w + ${C}));
        $for K in range(FIRST_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 16):
            $if C == 0:
              const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
            $else:
              const __m512i vi${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) (i${K} + ${C})));
            $if DATATYPE == "QU8":
              const __m512i vk${K}x${ABC[C:C+16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})))), vk_zero_point);
            $else:
              const __m512i vk${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T}))));
          i${K} += ${CHANNEL_TILE};

          $for C in range(0, CHANNEL_TILE, 16):
            vacc${ABC[C:C+16]} = _mm512_add_epi32(vacc${ABC[C:C+16]}, _mm512_mullo_epi32(vi${K}x${ABC[C:C+16]}, vk${K}x${ABC[C:C+16]}));

        w = (const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${FIRST_PASS_TILE * CHANNEL_TILE} * sizeof(${XINT8_T}));

        _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
        $for C in range(16, CHANNEL_TILE, 16):
          _mm512_storeu_si512((__m512i*) (b + ${C}), vacc${ABC[C:C+16]});
        b += ${CHANNEL_TILE};
      }

      $if CHANNEL_TILE > 16:
        if XNN_UNLIKELY(c != 0) {
          for (; c >= 16; c -= 16) {
            __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(w);
            $for K in range(FIRST_PASS_TILE):

              const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
              $if DATATYPE == "QU8":
                const __m512i vk${K}x${ABC[0:16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})))), vk_zero_point);
              $else:
                const __m512i vk${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T}))));
              $if CHANNEL_TILE > 16:
                i${K} += 16;

              vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]}));

            w = (const void*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${FIRST_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}));

            _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
            b += 16;
          }
        }

      if (c != 0) {
        assert(c >= 1);
        assert(c <= ${CHANNEL_SUBTILE-1});
        __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(w);
        $for K in range(FIRST_PASS_TILE):

          const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
          $if DATATYPE == "QU8":
            const __m512i vk${K}x${ABC[0:16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})))), vk_zero_point);
          $else:
            const __m512i vk${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T}))));
          vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]}));

        w = (const void*) ((uintptr_t) w + ${CHANNEL_SUBTILE} * sizeof(int32_t) + ${FIRST_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}));

        _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
      }
    }

    // Middle pass to process ${MIDDLE_PASS_TILE} inputs in each iteration.
    for (size_t ks = kernel_size - ${FIRST_PASS_TILE}; ks > ${LAST_PASS_TILE}; ks -= ${MIDDLE_PASS_TILE}) {
      int32_t* b = buffer;
      $for K in range(MIDDLE_PASS_TILE):
        const ${XINT8_T}* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const ${XINT8_T}*) ((uintptr_t) i${K} + input_offset);
        }
      input += ${MIDDLE_PASS_TILE};

      size_t c = channels;

      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(b);
        $for C in range(16, CHANNEL_TILE, 16):
          __m512i vacc${ABC[C:C+16]} = _mm512_loadu_si512((const void*) ((const int32_t*) b + ${C}));
        $for K in range(MIDDLE_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 16):
            $if C == 0:
              const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
            $else:
              const __m512i vi${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) (i${K} + ${C})));
            $if DATATYPE == "QU8":
              const __m512i vk${K}x${ABC[C:C+16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})))), vk_zero_point);
            $else:
              const __m512i vk${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T}))));
          i${K} += ${CHANNEL_TILE};

          $for C in range(0, CHANNEL_TILE, 16):
            vacc${ABC[C:C+16]} = _mm512_add_epi32(vacc${ABC[C:C+16]}, _mm512_mullo_epi32(vi${K}x${ABC[C:C+16]}, vk${K}x${ABC[C:C+16]}));

        w = (const void*) ((uintptr_t) w + ${MIDDLE_PASS_TILE * CHANNEL_TILE} * sizeof(${XINT8_T}));

        _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
        $for C in range(16, CHANNEL_TILE, 16):
          _mm512_storeu_si512((__m512i*) (b + ${C}), vacc${ABC[C:C+16]});
        b += ${CHANNEL_TILE};
      }

      $if CHANNEL_TILE > 16:
        if XNN_UNLIKELY(c != 0) {
          for (; c >= 16; c -= 16) {
            __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(b);
            $for K in range(MIDDLE_PASS_TILE):

              const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
              $if DATATYPE == "QU8":
                const __m512i vk${K}x${ABC[0:16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})))), vk_zero_point);
              $else:
                const __m512i vk${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T}))));
              $if CHANNEL_TILE > 16:
                i${K} += 16;

              vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]}));

            w = (const void*) ((uintptr_t) w + ${MIDDLE_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}));

            _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
            b += 16;
          }
        }

      if (c != 0) {
        assert(c >= 1);
        assert(c <= ${CHANNEL_SUBTILE-1});
        __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(b);
        $for K in range(MIDDLE_PASS_TILE):

          const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
          $if DATATYPE == "QU8":
            const __m512i vk${K}x${ABC[0:16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})))), vk_zero_point);
          $else:
            const __m512i vk${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T}))));
          vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]}));

        w = (const void*) ((uintptr_t) w + ${MIDDLE_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}));

        _mm512_storeu_si512((__m512i*) b, vacc${ABC[0:16]});
      }
    }

    // Last pass to process up to ${LAST_PASS_TILE} inputs.
    {
      const int32_t* b = buffer;
      $for K in range(LAST_PASS_TILE):
        const ${XINT8_T}* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const ${XINT8_T}*) ((uintptr_t) i${K} + input_offset);
        }

      size_t c = channels;

      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(b);
        $for C in range(16, CHANNEL_TILE, 16):
          __m512i vacc${ABC[C:C+16]} = _mm512_loadu_si512((const void*) ((const int32_t*) b + ${C}));
        b += ${CHANNEL_TILE};
        $for K in range(LAST_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 16):
            $if C == 0:
              const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
            $else:
              const __m512i vi${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) (i${K} + ${C})));
            $if DATATYPE == "QU8":
              const __m512i vk${K}x${ABC[C:C+16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})))), vk_zero_point);
            $else:
              const __m512i vk${K}x${ABC[C:C+16]} = ${_MM512_CVTEPX8_EPI32}(_mm_load_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T}))));
          i${K} += ${CHANNEL_TILE};

          $for C in range(0, CHANNEL_TILE, 16):
            vacc${ABC[C:C+16]} = _mm512_add_epi32(vacc${ABC[C:C+16]}, _mm512_mullo_epi32(vi${K}x${ABC[C:C+16]}, vk${K}x${ABC[C:C+16]}));

        w = (const void*) ((uintptr_t) w + ${LAST_PASS_TILE * CHANNEL_TILE} * sizeof(${XINT8_T}));

        $for C in range(0, CHANNEL_TILE, 16):
          __m512 vscaled${ABC[C:C+16]} = _mm512_cvtepi32_ps(vacc${ABC[C:C+16]});

        $if DATATYPE == "QC8":
          const __m512 vscale${ABC[0:16]} = _mm512_loadu_ps(w);
          $for C in range(16, CHANNEL_TILE, 16):
            const __m512 vscale${ABC[C:C+16]} = _mm512_loadu_ps((const void*) ((uintptr_t) w + ${C} * sizeof(float)));
          w = (const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(float));
          $for C in range(0, CHANNEL_TILE, 16):
            vscaled${ABC[C:C+16]} = _mm512_mul_ps(vscaled${ABC[C:C+16]}, vscale${ABC[C:C+16]});
        $else:
          $for C in range(0, CHANNEL_TILE, 16):
            vscaled${ABC[C:C+16]} = _mm512_mul_ps(vscaled${ABC[C:C+16]}, vscale);

        $for C in range(0, CHANNEL_TILE, 16):
          vscaled${ABC[C:C+16]} = _mm512_min_ps(vscaled${ABC[C:C+16]}, voutput_max_less_zero_point);

        $for C in range(0, CHANNEL_TILE, 16):
          vacc${ABC[C:C+16]} = _mm512_cvtps_epi32(vscaled${ABC[C:C+16]});

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 16 < CHANNEL_TILE:
            __m512i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm512_adds_epi16(_mm512_packs_epi32(vacc${ABC[C:C+16]}, vacc${ABC[C+16:C+32]}), voutput_zero_point);
          $elif CHANNEL_TILE > 16:
            __m256i vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[C:C+16]}), _mm512_extracti32x8_epi32(vacc${ABC[C:C+16]}, 1)), _mm512_castsi512_si256(voutput_zero_point));
          $else:
            __m256i vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[C:C+16]}), _mm512_extracti32x8_epi32(vacc${ABC[C:C+16]}, 1)), voutput_zero_point);

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 16 < CHANNEL_TILE:
            const __m256i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]} = _mm512_castsi512_si256(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]});
            const __m256i vout${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = _mm512_extracti32x8_epi32(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}, 1);
            const __m256i vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+12:C+16]}${ABC[C+28:C+32]} = ${_MM256_PACKXS_EPI16}(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}, vout${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+12:C+16]}${ABC[C+28:C+32]});
            __m256i vout${ABC[C:C+32]} = _mm256_permutevar8x32_epi32(vout${ABC[C:C+4]}${ABC[C+16:C+20]}${ABC[C+8:C+12]}${ABC[C+24:C+28]}${ABC[C+4:C+8]}${ABC[C+20:C+24]}${ABC[C+12:C+16]}${ABC[C+28:C+32]}, vpermute_mask);
          $else:
            const __m128i vout${ABC[C:C+4]}${ABC[C+8:C+12]} = _mm256_castsi256_si128(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]});
            const __m128i vout${ABC[C+4:C+8]}${ABC[C+12:C+16]} = _mm256_extracti128_si256(vout${ABC[C:C+4]}${ABC[C+8:C+12]}${ABC[C+4:C+8]}${ABC[C+12:C+16]}, 1);
            __m128i vout${ABC[C:C+16]} = _mm_shuffle_epi32(${_MM_PACKXS_EPI16}(vout${ABC[C:C+4]}${ABC[C+8:C+12]}, vout${ABC[C+4:C+8]}${ABC[C+12:C+16]}), _MM_SHUFFLE(3, 1, 2, 0));

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 16 < CHANNEL_TILE:
            vout${ABC[C:C+32]} = ${_MM256_MAX_EPX8}(vout${ABC[C:C+32]}, voutput_min);
          $elif CHANNEL_TILE > 16:
            vout${ABC[C:C+16]} = ${_MM_MAX_EPX8}(vout${ABC[C:C+16]}, _mm256_castsi256_si128(voutput_min));
          $else:
            vout${ABC[C:C+16]} = ${_MM_MAX_EPX8}(vout${ABC[C:C+16]}, voutput_min);

        $if CHANNEL_TILE > 16:
          _mm256_storeu_si256((__m256i*) output, vout${ABC[0:32]});
        $else:
          _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]});
        $for C in range(16, CHANNEL_TILE, 16):
          $if C + 16 < CHANNEL_TILE:
            _mm256_storeu_si256((__m256i*) (output + ${C}), vout${ABC[C:C+32]});
          $else:
            _mm_storeu_si128((__m128i*) (output + ${C}), vout${ABC[C:C+16]});
        output += ${CHANNEL_TILE};
      }

      if XNN_UNLIKELY(c != 0) {
        // Prepare mask for valid 8-bit elements (depends on nc).
        const __mmask16 vmask = _cvtu32_mask16((uint32_t) ((UINT32_C(1) << (c & 15)) - UINT32_C(1)));
        ${"do " if CHANNEL_TILE > 16 else ""}{
          __m512i vacc${ABC[0:16]} = _mm512_loadu_si512(b);
          b += 16;
          $for K in range(LAST_PASS_TILE):

            const __m512i vi${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) i${K}));
            $if DATATYPE == "QU8":
              const __m512i vk${K}x${ABC[0:16]} = _mm512_sub_epi32(${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})))), vk_zero_point);
            $else:
              const __m512i vk${K}x${ABC[0:16]} = ${_MM512_CVTEPX8_EPI32}(_mm_loadu_si128((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T}))));
            $if CHANNEL_TILE > 16:
              i${K} += 16;

            vacc${ABC[0:16]} = _mm512_add_epi32(vacc${ABC[0:16]}, _mm512_mullo_epi32(vi${K}x${ABC[0:16]}, vk${K}x${ABC[0:16]}));

          __m512 vscaled${ABC[0:16]} = _mm512_cvtepi32_ps(vacc${ABC[0:16]});
          $if DATATYPE == "QC8":
            const __m512 vscale${ABC[0:16]} = _mm512_loadu_ps((const void*) ((uintptr_t) w + ${CHANNEL_SUBTILE * LAST_PASS_TILE} * sizeof(${XINT8_T})));
            vscaled${ABC[0:16]} = _mm512_mul_ps(vscaled${ABC[0:16]}, vscale${ABC[0:16]});
          $else:
            vscaled${ABC[0:16]} = _mm512_mul_ps(vscaled${ABC[0:16]}, vscale);
          vscaled${ABC[0:16]} = _mm512_min_ps(vscaled${ABC[0:16]}, voutput_max_less_zero_point);
          vacc${ABC[0:16]} = _mm512_cvtps_epi32(vscaled${ABC[0:16]});

          $if CHANNEL_TILE > 16:
            $if DATATYPE == "QC8":
              w = (void*) ((uintptr_t) w + ${LAST_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}) + 16 * sizeof(float));
            $else:
              w = (void*) ((uintptr_t) w + ${LAST_PASS_TILE * CHANNEL_SUBTILE} * sizeof(${XINT8_T}));

          $if CHANNEL_TILE > 16:
            __m256i vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[0:16]}), _mm512_extracti32x8_epi32(vacc${ABC[0:16]}, 1)), _mm512_castsi512_si256(voutput_zero_point));
          $else:
            __m256i vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]} = _mm256_adds_epi16(_mm256_packs_epi32(_mm512_castsi512_si256(vacc${ABC[0:16]}), _mm512_extracti32x8_epi32(vacc${ABC[0:16]}, 1)), voutput_zero_point);

          const __m128i vout${ABC[0:4]}${ABC[8:12]} = _mm256_castsi256_si128(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]});
          const __m128i vout${ABC[4:8]}${ABC[12:16]} = _mm256_extracti128_si256(vout${ABC[0:4]}${ABC[8:12]}${ABC[4:8]}${ABC[12:16]}, 1);
          __m128i vout${ABC[0:16]} = _mm_shuffle_epi32(${_MM_PACKXS_EPI16}(vout${ABC[0:4]}${ABC[8:12]}, vout${ABC[4:8]}${ABC[12:16]}), _MM_SHUFFLE(3, 1, 2, 0));
          $if CHANNEL_TILE > 16:
            vout${ABC[0:16]} = ${_MM_MAX_EPX8}(vout${ABC[0:16]}, _mm256_castsi256_si128(voutput_min));
          $else:
            vout${ABC[0:16]} = ${_MM_MAX_EPX8}(vout${ABC[0:16]}, voutput_min);


          $if CHANNEL_TILE > 16:
            if XNN_LIKELY(c >= 16) {
              _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]});
              output += 16;
              c -= 16;
            } else {
              _mm_mask_storeu_epi8(output, vmask, vout${ABC[0:16]});
              output = (${XINT8_T}*) ((uintptr_t) output + c);
              c = 0;
            }
          $else:
            _mm_mask_storeu_epi8(output, vmask, vout${ABC[0:16]});
            output = (${XINT8_T}*) ((uintptr_t) output + c);
        }${" while (c != 0);" if CHANNEL_TILE > 16 else ""}
      }
    }

    input = (const ${XINT8_T}**) ((uintptr_t) input + input_stride);
    output = (${XINT8_T}*) ((uintptr_t) output + output_increment);
  } while (--output_width != 0);
}
