// 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 SSE in [2, 4]
$assert not AVX or SSE == 4
$assert REQUANTIZATION == "FP32"
$assert DATATYPE in ["QC8", "QS8", "QU8"]
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$SSE_HEADER = {2: "emmintrin.h", 4: "smmintrin.h"}[SSE]
$assert CHANNEL_TILE % 8 == 0
$assert CHANNEL_TILE >= 8
$CHANNEL_SUBTILE = 8
$assert CHANNEL_TILE % CHANNEL_SUBTILE == 0
$CHANNEL_ROUND = 8
$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 <${SSE_HEADER}>

#include "xnnpack/dwconv.h"
#include "xnnpack/math.h"
#include "xnnpack/unaligned.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()
$ISA = "avx" if AVX else {2: "sse2", 4: "sse41"}[SSE]
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
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__${ISA}_mul16${"_add16" if ADD16 else ""}(
    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 __m128i vk_zero_point = _mm_set1_epi16(params->${PARAMS_STRUCT}.kernel_zero_point);
    XNN_FORCE_REALIZATION(vk_zero_point);

  $if DATATYPE != "QC8":
    const __m128 vscale = _mm_set1_ps(params->${PARAMS_STRUCT}.scale);
    XNN_FORCE_REALIZATION(vscale);

  const __m128 voutput_max_less_zero_point = _mm_set1_ps((int32_t) params->${PARAMS_STRUCT}.output_max - (int32_t) params->${PARAMS_STRUCT}.output_zero_point);
  const __m128i voutput_zero_point = _mm_set1_epi16(params->${PARAMS_STRUCT}.output_zero_point);
  $if DATATYPE == "QU8":
    const __m128i voutput_min = _mm_set1_epi8(params->${PARAMS_STRUCT}.output_min);
  $else:
    $if SSE < 4:
      const __m128i voutput_min = _mm_set1_epi16(params->${PARAMS_STRUCT}.output_min);
    $elif SSE == 4:
      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 = round_up_po2(channels, ${CHANNEL_ROUND});

      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) w);
        $for C in range(4, CHANNEL_TILE, 4):
          __m128i vacc${ABC[C:C+4]} = _mm_loadu_si128((const __m128i*) ((const int32_t*) w + ${C}));


        $for K in range(FIRST_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 8):
            $if C == 0:
              const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
            $else:
              const __m128i vi${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) (i${K} + ${C}));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[C:C+8]});
              $else:
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[C:C+8]});
            const __m128i vk${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[C:C+8]}), vk_zero_point);
              $else:
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[C:C+8]});
          i${K} += ${CHANNEL_TILE};

          $if SSE < 4:
            $if DATATYPE == "QU8":
              $if K == 0:
                const __m128i vzero = _mm_setzero_si128();
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vzero);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vzero), vk_zero_point);
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}), 8);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]}), 8);

          $for C in range(0, CHANNEL_TILE, 8):
            $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
              const __m128i vprod${K}x${ABC[C:C+8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
              const __m128i vprod${K}x${ABC[C:C+8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K == 0:
              __m128i vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not ADD16:
              vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = _mm_add_epi16(vprod${ABC[C:C+8]}, _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]}));

          $if not ADD16 or K % 2 == 1 or K + 1 == FIRST_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
              $elif SSE < 4:
                const __m128i vsignprod${K}x${ABC[C:C+8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[C:C+8]});
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
              $else:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_cvtepi16_epi32(vprod${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vprod${ABC[C:C+8]}), 16));

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

        $for C in range(0, CHANNEL_TILE, 4):
          $if C == 0:
            _mm_storeu_si128((__m128i*) b, vacc${ABC[C:C+4]});
          $else:
            _mm_storeu_si128((__m128i*) (b + ${C}), vacc${ABC[C:C+4]});
        b += ${CHANNEL_TILE};
      }

      $if CHANNEL_TILE == 8:
          assert(c == 0);
      $else:
        if XNN_UNLIKELY(c != 0) {
          ${"do " if CHANNEL_TILE > 8 else ""}{
            __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) w);
            __m128i vacc${ABC[4:8]} = _mm_loadu_si128((const __m128i*) ((const int32_t*) w + 4));

            $for K in range(FIRST_PASS_TILE):

              const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
              $if SSE == 4:
                $if DATATYPE == "QU8":
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[0:8]});
                $else:
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[0:8]});
              $if CHANNEL_TILE > 8:
                $if K == 0:
                  const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + sizeof(int32_t) * ${CHANNEL_SUBTILE} * sizeof(${XINT8_T})));
                $else:
                  const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + sizeof(int32_t) * ${CHANNEL_SUBTILE} + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})));
              $else:
                const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE} * sizeof(${XINT8_T})));
              $if SSE == 4:
                $if DATATYPE == "QU8":
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[0:8]}), vk_zero_point);
                $else:
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[0:8]});
              $if CHANNEL_TILE > 8:
                i${K} += 8;

              $if SSE < 4:
                $if DATATYPE == "QU8":
                  $if K == 0:
                    const __m128i vzero = _mm_setzero_si128();
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vzero);
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vzero), vk_zero_point);
                $else:
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vi${K}x${ABC[0:8]}), 8);
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]}), 8);

              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                const __m128i vprod${K}x${ABC[0:8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
                const __m128i vprod${K}x${ABC[0:8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $elif K == 0:
                __m128i vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $elif K % 2 == 0 or not ADD16:
                vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = _mm_add_epi16(vprod${ABC[0:8]}, _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]}));

              $if not ADD16 or K % 2 == 1 or K + 1 == FIRST_PASS_TILE:
                $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
                $elif SSE < 4:
                  const __m128i vsignprod${K}x${ABC[0:8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[0:8]});
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
                $else:
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_cvtepi16_epi32(vprod${ABC[0:8]}));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[0:8]}, vprod${ABC[0:8]}), 16));

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

            _mm_storeu_si128((__m128i*) (b), vacc0123);
            _mm_storeu_si128((__m128i*) (b + 4), vacc4567);
            b += 8;
            c -= 8;
          }${" while (c != 0);" if CHANNEL_TILE > 8 else ""}
        }
    }

    // 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 = round_up_po2(channels, ${CHANNEL_ROUND});

      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) b);
        $for C in range(4, CHANNEL_TILE, 4):
          __m128i vacc${ABC[C:C+4]} = _mm_loadu_si128((const __m128i*) (b + ${C}));


        $for K in range(MIDDLE_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 8):
            $if C == 0:
              const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
            $else:
              const __m128i vi${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) (i${K} + ${C}));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[C:C+8]});
              $else:
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[C:C+8]});
            const __m128i vk${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[C:C+8]}), vk_zero_point);
              $else:
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[C:C+8]});
          i${K} += ${CHANNEL_TILE};

          $if SSE < 4:
            $if DATATYPE == "QU8":
              $if K == 0:
                const __m128i vzero = _mm_setzero_si128();
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vzero);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vzero), vk_zero_point);
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}), 8);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]}), 8);

          $for C in range(0, CHANNEL_TILE, 8):
            $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
              const __m128i vprod${K}x${ABC[C:C+8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
              const __m128i vprod${K}x${ABC[C:C+8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K == 0:
              __m128i vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not ADD16:
              vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = _mm_add_epi16(vprod${ABC[C:C+8]}, _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]}));

          $if not ADD16 or K % 2 == 1 or K + 1 == MIDDLE_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
              $elif SSE < 4:
                const __m128i vsignprod${K}x${ABC[C:C+8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[C:C+8]});
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
              $else:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_cvtepi16_epi32(vprod${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vprod${ABC[C:C+8]}), 16));

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

        $for C in range(0, CHANNEL_TILE, 4):
          $if C == 0:
            _mm_storeu_si128((__m128i*) (b), vacc${ABC[C:C+4]});
          $else:
            _mm_storeu_si128((__m128i*) (b + ${C}), vacc${ABC[C:C+4]});
        b += ${CHANNEL_TILE};
      }

      $if CHANNEL_TILE == 8:
          assert(c == 0);
      $else:
        if XNN_UNLIKELY(c != 0) {
          ${"do " if CHANNEL_TILE > 8 else ""}{
            __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) b);
            __m128i vacc${ABC[4:8]} = _mm_loadu_si128((const __m128i*) (b + 4));

            $for K in range(MIDDLE_PASS_TILE):

              const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
              $if SSE == 4:
                $if DATATYPE == "QU8":
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[0:8]});
                $else:
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[0:8]});
              $if CHANNEL_TILE > 8:
                $if K == 0:
                  const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) w);
                $else:
                  const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})));
              $else:
                const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE} * sizeof(${XINT8_T})));
              $if SSE == 4:
                $if DATATYPE == "QU8":
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[0:8]}), vk_zero_point);
                $else:
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[0:8]});
              $if CHANNEL_TILE > 8:
                i${K} += 8;

              $if SSE < 4:
                $if DATATYPE == "QU8":
                  $if K == 0:
                    const __m128i vzero = _mm_setzero_si128();
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vzero);
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vzero), vk_zero_point);
                $else:
                  const __m128i vxi${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vi${K}x${ABC[0:8]}), 8);
                  const __m128i vxk${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]}), 8);

              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                const __m128i vprod${K}x${ABC[0:8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
                const __m128i vprod${K}x${ABC[0:8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $elif K == 0:
                __m128i vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $elif K % 2 == 0 or not ADD16:
                vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = _mm_add_epi16(vprod${ABC[0:8]}, _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]}));

              $if not ADD16 or K % 2 == 1 or K + 1 == MIDDLE_PASS_TILE:
                $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
                $elif SSE < 4:
                  const __m128i vsignprod${K}x${ABC[0:8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[0:8]});
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
                $else:
                  vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_cvtepi16_epi32(vprod${ABC[0:8]}));
                  vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[0:8]}, vprod${ABC[0:8]}), 16));

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

            _mm_storeu_si128((__m128i*) (b), vacc0123);
            _mm_storeu_si128((__m128i*) (b + 4), vacc4567);
            b += 8;
            c -= 8;
          }${" while (c != 0);" if CHANNEL_TILE > 8 else ""}
        }
    }

    // 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}) {
        __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) b);
        $for C in range(4, CHANNEL_TILE, 4):
          __m128i vacc${ABC[C:C+4]} = _mm_loadu_si128((const __m128i*) (b + ${C}));
        b += ${CHANNEL_TILE};

        $for K in range(LAST_PASS_TILE):

          $for C in range(0, CHANNEL_TILE, 8):
            $if C == 0:
              const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
            $else:
              const __m128i vi${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) (i${K} + ${C}));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[C:C+8]});
              $else:
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[C:C+8]});
            const __m128i vk${K}x${ABC[C:C+8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE + C} * sizeof(${XINT8_T})));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[C:C+8]}), vk_zero_point);
              $else:
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[C:C+8]});
          i${K} += ${CHANNEL_TILE};

          $if SSE < 4:
            $if DATATYPE == "QU8":
              $if K == 0:
                const __m128i vzero = _mm_setzero_si128();
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vzero);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vzero), vk_zero_point);
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                const __m128i vxi${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}), 8);
                const __m128i vxk${K}x${ABC[C:C+8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]}), 8);

          $for C in range(0, CHANNEL_TILE, 8):
            $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
              const __m128i vprod${K}x${ABC[C:C+8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
              const __m128i vprod${K}x${ABC[C:C+8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K == 0:
              __m128i vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not ADD16:
              vprod${ABC[C:C+8]} = _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = _mm_add_epi16(vprod${ABC[C:C+8]}, _mm_mullo_epi16(vxi${K}x${ABC[C:C+8]}, vxk${K}x${ABC[C:C+8]}));

          $if not ADD16 or K % 2 == 1 or K + 1 == LAST_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[C:C+8]}lo, vprod${K}x${ABC[C:C+8]}hi));
              $elif SSE < 4:
                const __m128i vsignprod${K}x${ABC[C:C+8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[C:C+8]});
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_unpacklo_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vsignprod${K}x${ABC[C:C+8]}));
              $else:
                vacc${ABC[C:C+4]} = _mm_add_epi32(vacc${ABC[C:C+4]}, _mm_cvtepi16_epi32(vprod${ABC[C:C+8]}));
                vacc${ABC[C+4:C+8]} = _mm_add_epi32(vacc${ABC[C+4:C+8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[C:C+8]}, vprod${ABC[C:C+8]}), 16));

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

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

        $if DATATYPE == "QC8":
          const __m128 vscale${ABC[0:4]} = _mm_loadu_ps((const float*) w);
          $for C in range(4, CHANNEL_TILE, 4):
            const __m128 vscale${ABC[C:C+4]} = _mm_loadu_ps((const float*) w + ${C});
          w = (const void*) ((const float*) w + ${CHANNEL_TILE});
          $for C in range(0, CHANNEL_TILE, 4):
            vscaled${ABC[C:C+4]} = _mm_mul_ps(vscaled${ABC[C:C+4]}, vscale${ABC[C:C+4]});
        $else:
          $for C in range(0, CHANNEL_TILE, 4):
            vscaled${ABC[C:C+4]} = _mm_mul_ps(vscaled${ABC[C:C+4]}, vscale);

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

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

        $for C in range(0, CHANNEL_TILE, 8):
          __m128i vout${ABC[C:C+8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[C:C+4]}, vacc${ABC[C+4:C+8]}), voutput_zero_point);

        $if DATATYPE == "QU8":
          $for C in range(0, CHANNEL_TILE, 16):
            $if C + 8 < CHANNEL_TILE:
              __m128i vout${ABC[C:C+16]} = _mm_packus_epi16(vout${ABC[C:C+8]}, vout${ABC[C+8:C+16]});
            $else:
              __m128i vout${ABC[C:C+8]}${ABC[C:C+8]} = _mm_packus_epi16(vout${ABC[C:C+8]}, vout${ABC[C:C+8]});

          $for C in range(0, CHANNEL_TILE, 16):
            $if C + 8 < CHANNEL_TILE:
              vout${ABC[C:C+16]} = _mm_max_epu8(vout${ABC[C:C+16]}, voutput_min);
            $else:
              vout${ABC[C:C+8]}${ABC[C:C+8]} = _mm_max_epu8(vout${ABC[C:C+8]}${ABC[C:C+8]}, voutput_min);
        $else:
          $if SSE < 4:
            $for C in range(0, CHANNEL_TILE, 8):
              vout${ABC[C:C+8]} = _mm_max_epi16(vout${ABC[C:C+8]}, voutput_min);

          $for C in range(0, CHANNEL_TILE, 16):
            $if C + 8 < CHANNEL_TILE:
              __m128i vout${ABC[C:C+16]} = _mm_packs_epi16(vout${ABC[C:C+8]}, vout${ABC[C+8:C+16]});
            $else:
              __m128i vout${ABC[C:C+8]}${ABC[C:C+8]} = _mm_packs_epi16(vout${ABC[C:C+8]}, vout${ABC[C:C+8]});

          $if SSE == 4:
            $for C in range(0, CHANNEL_TILE, 16):
              $if C + 8 < CHANNEL_TILE:
                vout${ABC[C:C+16]} = _mm_max_epi8(vout${ABC[C:C+16]}, voutput_min);
              $else:
                vout${ABC[C:C+8]}${ABC[C:C+8]} = _mm_max_epi8(vout${ABC[C:C+8]}${ABC[C:C+8]}, voutput_min);

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

      if XNN_UNLIKELY(c != 0) {
        ${"do " if CHANNEL_TILE > 8 else ""}{
          __m128i vacc${ABC[0:4]} = _mm_loadu_si128((const __m128i*) b);
          __m128i vacc${ABC[4:8]} = _mm_loadu_si128((const __m128i*) (b + 4));
          b += 8;

          $for K in range(LAST_PASS_TILE):

            const __m128i vi${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) i${K});
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepu8_epi16(vi${K}x${ABC[0:8]});
              $else:
                const __m128i vxi${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vi${K}x${ABC[0:8]});
            $if CHANNEL_TILE > 8:
              $if K == 0:
                const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) w);
              $else:
                const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_SUBTILE} * sizeof(${XINT8_T})));
            $else:
              const __m128i vk${K}x${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + ${K * CHANNEL_TILE} * sizeof(${XINT8_T})));
            $if SSE == 4:
              $if DATATYPE == "QU8":
                const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_cvtepu8_epi16(vk${K}x${ABC[0:8]}), vk_zero_point);
              $else:
                const __m128i vxk${K}x${ABC[0:8]} = _mm_cvtepi8_epi16(vk${K}x${ABC[0:8]});
            $if CHANNEL_TILE > 8:
              i${K} += 8;

            $if SSE < 4:
              $if DATATYPE == "QU8":
                $if K == 0:
                  const __m128i vzero = _mm_setzero_si128();
                const __m128i vxi${K}x${ABC[0:8]} = _mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vzero);
                const __m128i vxk${K}x${ABC[0:8]} = _mm_sub_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vzero), vk_zero_point);
              $else:
                const __m128i vxi${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vi${K}x${ABC[0:8]}, vi${K}x${ABC[0:8]}), 8);
                const __m128i vxk${K}x${ABC[0:8]} = _mm_srai_epi16(_mm_unpacklo_epi8(vk${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]}), 8);

            $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
              const __m128i vprod${K}x${ABC[0:8]}lo = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
              const __m128i vprod${K}x${ABC[0:8]}hi = _mm_mulhi_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
            $elif K == 0:
              __m128i vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
            $elif K % 2 == 0 or not ADD16:
              vprod${ABC[0:8]} = _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]});
            $else:
              vprod${ABC[0:8]} = _mm_add_epi16(vprod${ABC[0:8]}, _mm_mullo_epi16(vxi${K}x${ABC[0:8]}, vxk${K}x${ABC[0:8]}));

            $if not ADD16 or K % 2 == 1 or K + 1 == LAST_PASS_TILE:
              $if DATATYPE == "QU8" or SSE < 4 and not ADD16:
                vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
                vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${K}x${ABC[0:8]}lo, vprod${K}x${ABC[0:8]}hi));
              $elif SSE < 4:
                const __m128i vsignprod${K}x${ABC[0:8]} = _mm_cmpgt_epi16(_mm_setzero_si128(), vprod${ABC[0:8]});
                vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_unpacklo_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
                vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_unpackhi_epi16(vprod${ABC[0:8]}, vsignprod${K}x${ABC[0:8]}));
              $else:
                vacc${ABC[0:4]} = _mm_add_epi32(vacc${ABC[0:4]}, _mm_cvtepi16_epi32(vprod${ABC[0:8]}));
                vacc${ABC[4:8]} = _mm_add_epi32(vacc${ABC[4:8]}, _mm_srai_epi32(_mm_unpackhi_epi16(vprod${ABC[0:8]}, vprod${ABC[0:8]}), 16));

          __m128 vscaled${ABC[0:4]} = _mm_cvtepi32_ps(vacc${ABC[0:4]});
          __m128 vscaled${ABC[4:8]} = _mm_cvtepi32_ps(vacc${ABC[4:8]});

          $if DATATYPE == "QC8":
            const __m128 vscale${ABC[0:4]} = _mm_loadu_ps((const float*) ((uintptr_t) w + ${CHANNEL_SUBTILE * LAST_PASS_TILE} * sizeof(${XINT8_T})));
            const __m128 vscale${ABC[4:8]} = _mm_loadu_ps((const float*) ((uintptr_t) w + ${CHANNEL_SUBTILE * LAST_PASS_TILE} * sizeof(${XINT8_T}) + 4 * sizeof(float)));
            vscaled${ABC[0:4]} = _mm_mul_ps(vscaled${ABC[0:4]}, vscale${ABC[0:4]});
            vscaled${ABC[4:8]} = _mm_mul_ps(vscaled${ABC[4:8]}, vscale${ABC[4:8]});
          $else:
            vscaled${ABC[0:4]} = _mm_mul_ps(vscaled${ABC[0:4]}, vscale);
            vscaled${ABC[4:8]} = _mm_mul_ps(vscaled${ABC[4:8]}, vscale);

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

          vscaled${ABC[0:4]} = _mm_min_ps(vscaled${ABC[0:4]}, voutput_max_less_zero_point);
          vscaled${ABC[4:8]} = _mm_min_ps(vscaled${ABC[4:8]}, voutput_max_less_zero_point);

          vacc${ABC[0:4]} = _mm_cvtps_epi32(vscaled${ABC[0:4]});
          vacc${ABC[4:8]} = _mm_cvtps_epi32(vscaled${ABC[4:8]});

          __m128i vout${ABC[0:8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[0:4]}, vacc${ABC[4:8]}), voutput_zero_point);

          $if DATATYPE == "QU8":
            __m128i vout${ABC[0:8]}${ABC[0:8]} = _mm_packus_epi16(vout${ABC[0:8]}, vout${ABC[0:8]});

            vout${ABC[0:8]}${ABC[0:8]} = _mm_max_epu8(vout${ABC[0:8]}${ABC[0:8]}, voutput_min);
          $else:
            $if SSE < 4:
              vout${ABC[0:8]} = _mm_max_epi16(vout${ABC[0:8]}, voutput_min);

            __m128i vout${ABC[0:8]}${ABC[0:8]} = _mm_packs_epi16(vout${ABC[0:8]}, vout${ABC[0:8]});

            $if SSE == 4:
              vout${ABC[0:8]}${ABC[0:8]} = _mm_max_epi8(vout${ABC[0:8]}${ABC[0:8]}, voutput_min);

          $if CHANNEL_TILE > 8:
            if XNN_LIKELY(c >= 8) {
              _mm_storel_epi64((__m128i*) output, vout${ABC[0:8]}${ABC[0:8]});
              output += 8;
              c -= 8;
            } else {
              if (c & 4) {
                unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]}));
                vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
                output += 4;
              }
              if (c & 2) {
                unaligned_store_u16(output, (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0));
                vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
                output += 2;
              }
              if (c & 1) {
                $if SSE == 4:
                  *output = (${XINT8_T}) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
                $else:
                  *output = (${XINT8_T}) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
                output += 1;
              }
              c = 0;
            }
          $else:
            if (c & 4) {
              unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]}));
              vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
              output += 4;
            }
            if (c & 2) {
              unaligned_store_u16(output, (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0));
              vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
              output += 2;
            }
            if (c & 1) {
              $if SSE == 4:
                *output = (${XINT8_T}) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
              $else:
                *output = (${XINT8_T}) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
              output += 1;
            }
        }${" while (c != 0);" if CHANNEL_TILE > 8 else ""}
      }
    }

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