// 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 in ["FP32", "RNDNU"]
$assert DATATYPE in ["QC8", "QS8"]
$assert DATATYPE != "QC8" or REQUANTIZATION == "FP32"
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$assert LOAD_VARIANT in ["LD64", "LD128"]
$assert CHANNEL_TILE % {"LD64": 8, "LD128": 16}[LOAD_VARIANT] == 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 <arm_neon.h>

#include "xnnpack/dwconv.h"
$if REQUANTIZATION == "FP32" and ARMV8:
  #include "xnnpack/intrinsics-polyfill.h"
#include "xnnpack/math.h"


$DATATYPE_SPEC = {"QS8": "qs8", "QC8": "qs8_qc8w"}[DATATYPE]
$PARAMS_STRUCT = REQUANTIZATION.lower() + "_" + ("neonv8" if ARMV8 else "neon")
$PARAMS_TYPE = {"QC8": "union xnn_qs8_qc8w_conv_minmax_params", "QS8": "union xnn_qs8_conv_minmax_params"}[DATATYPE]
$ISA = "neonv8" if ARMV8 else "neon"
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}_${"mla8" if MLA else "mul8"}_${LOAD_VARIANT.lower()}(
    size_t channels,
    size_t output_width,
    const int8_t** input,
    const void* weights,
    int8_t* output,
    intptr_t input_stride,
    size_t output_increment,
    size_t input_offset,
    const int8_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 REQUANTIZATION == "RNDNU":
    const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->${PARAMS_STRUCT}.right_pre_shift);
    const int32x4_t vmultiplier = vld1q_dup_s32(&params->${PARAMS_STRUCT}.multiplier);
    const int32x4_t vright_post_shift = vld1q_dup_s32(&params->${PARAMS_STRUCT}.right_post_shift);
  $elif REQUANTIZATION == "FP32":
    $if DATATYPE != "QC8":
      const float32x4_t vscale = vld1q_dup_f32(&params->${PARAMS_STRUCT}.scale);
    $if not ARMV8:
      const float32x4_t vmagic_bias = vld1q_dup_f32(&params->${PARAMS_STRUCT}.magic_bias);
      const int32x4_t vmagic_bias_less_output_zero_point = vld1q_dup_s32(&params->${PARAMS_STRUCT}.magic_bias_less_output_zero_point);
  $if REQUANTIZATION != "FP32" or ARMV8:
    const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->${PARAMS_STRUCT}.output_zero_point);
  $if CHANNEL_TILE == 8:
    const int8x8_t voutput_min = vld1_dup_s8(&params->${PARAMS_STRUCT}.output_min);
    const int8x8_t voutput_max = vld1_dup_s8(&params->${PARAMS_STRUCT}.output_max);
  $else:
    const int8x16_t voutput_min = vld1q_dup_s8(&params->${PARAMS_STRUCT}.output_min);
    const int8x16_t voutput_max = vld1q_dup_s8(&params->${PARAMS_STRUCT}.output_max);
  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 int8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const int8_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}) {
        $for C in range(0, CHANNEL_TILE, 4):
          int32x4_t vacc${ABC[C:C+4]} = vld1q_s32(w); w = (const int32_t*) w + 4;
        $for K in range(FIRST_PASS_TILE):

          $if LOAD_VARIANT == "LD128":
            $for C in range(0, CHANNEL_TILE, 16):
              const int8x16_t vi${K}x${ABC[C:C+16]} = vld1q_s8(i${K}); i${K} += 16;
              const int8x16_t vk${K}x${ABC[C:C+16]} = vld1q_s8(w); w = (const int8_t*) w + 16;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 16):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                int16x8_t vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $else:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmlal_s8(vprod${ABC[C+8:C+16]}, vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
          $else:
            $for C in range(0, CHANNEL_TILE, 8):
              const int8x8_t vi${K}x${ABC[C:C+8]} = vld1_s8(i${K}); i${K} += 8;
              const int8x8_t vk${K}x${ABC[C:C+8]} = vld1_s8(w); w = (const int8_t*) w + 8;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 8):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});

          $if not MLA or K % 2 == 1 or K + 1 == FIRST_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              vacc${ABC[C:C+4]} = vaddw_s16(vacc${ABC[C:C+4]}, vget_low_s16(vprod${ABC[C:C+8]}));
              vacc${ABC[C+4:C+8]} = vaddw_s16(vacc${ABC[C+4:C+8]}, vget_high_s16(vprod${ABC[C:C+8]}));

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

      $if CHANNEL_TILE == 8:
          assert(c == 0);
      $else:
        if XNN_UNLIKELY(c != 0) {
          ${"do " if CHANNEL_TILE > 8 else ""}{
            int32x4_t vacc${ABC[0:4]} = vld1q_s32(w); w = (const int32_t*) w + 4;
            int32x4_t vacc${ABC[4:8]} = vld1q_s32(w); w = (const int32_t*) w + 4;
            $for K in range(FIRST_PASS_TILE):

              $if CHANNEL_TILE > 8:
                const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K}); i${K} += 8;
              $else:
                const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K});
              $if K == 0:
                const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};
              $else:
                const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};

              $if K == 0:
                int16x8_t vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $elif K % 2 == 0 or not MLA:
                vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = vmlal_s8(vprod${ABC[0:8]}, vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});

              $if not MLA or K % 2 == 1 or K + 1 == FIRST_PASS_TILE:
                vacc${ABC[0:4]} = vaddw_s16(vacc${ABC[0:4]}, vget_low_s16(vprod${ABC[0:8]}));
                vacc${ABC[4:8]} = vaddw_s16(vacc${ABC[4:8]}, vget_high_s16(vprod${ABC[0:8]}));

            vst1q_s32(b, vacc${ABC[0:4]}); b += 4;
            vst1q_s32(b, vacc${ABC[4:8]}); b += 4;
            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 int8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const int8_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}) {
        int32x4_t vacc${ABC[0:4]} = vld1q_s32(b);
        $for C in range(4, CHANNEL_TILE, 4):
          int32x4_t vacc${ABC[C:C+4]} = vld1q_s32(b + ${C});
        $for K in range(MIDDLE_PASS_TILE):

          $if LOAD_VARIANT == "LD128":
            $for C in range(0, CHANNEL_TILE, 16):
              const int8x16_t vi${K}x${ABC[C:C+16]} = vld1q_s8(i${K}); i${K} += 16;
              const int8x16_t vk${K}x${ABC[C:C+16]} = vld1q_s8(w); w = (const int8_t*) w + 16;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 16):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                int16x8_t vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $else:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmlal_s8(vprod${ABC[C+8:C+16]}, vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
          $else:
            $for C in range(0, CHANNEL_TILE, 8):
              const int8x8_t vi${K}x${ABC[C:C+8]} = vld1_s8(i${K}); i${K} += 8;
              const int8x8_t vk${K}x${ABC[C:C+8]} = vld1_s8(w); w = (const int8_t*) w + 8;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 8):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});

          $if not MLA or K % 2 == 1 or K + 1 == MIDDLE_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              vacc${ABC[C:C+4]} = vaddw_s16(vacc${ABC[C:C+4]}, vget_low_s16(vprod${ABC[C:C+8]}));
              vacc${ABC[C+4:C+8]} = vaddw_s16(vacc${ABC[C+4:C+8]}, vget_high_s16(vprod${ABC[C:C+8]}));

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

      $if CHANNEL_TILE == 8:
          assert(c == 0);
      $else:
        if XNN_UNLIKELY(c != 0) {
          ${"do " if CHANNEL_TILE > 8 else ""}{
            int32x4_t vacc${ABC[0:4]} = vld1q_s32(b);
            int32x4_t vacc${ABC[4:8]} = vld1q_s32(b + 4);
            $for K in range(MIDDLE_PASS_TILE):

              $if CHANNEL_TILE > 8:
                const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K}); i${K} += 8;
              $else:
                const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K});
              $if K == 0:
                const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};
              $else:
                const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};

              $if K == 0:
                int16x8_t vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $elif K % 2 == 0 or not MLA:
                vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = vmlal_s8(vprod${ABC[0:8]}, vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});

              $if not MLA or K % 2 == 1 or K + 1 == MIDDLE_PASS_TILE:
                vacc${ABC[0:4]} = vaddw_s16(vacc${ABC[0:4]}, vget_low_s16(vprod${ABC[0:8]}));
                vacc${ABC[4:8]} = vaddw_s16(vacc${ABC[4:8]}, vget_high_s16(vprod${ABC[0:8]}));

            vst1q_s32(b, vacc${ABC[0:4]}); b += 4;
            vst1q_s32(b, vacc${ABC[4:8]}); b += 4;
            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 int8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const int8_t*) ((uintptr_t) i${K} + input_offset);
        }

      size_t c = channels;
      for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) {
        $for C in range(0, CHANNEL_TILE, 4):
          int32x4_t vacc${ABC[C:C+4]} = vld1q_s32(b); b += 4;
        $for K in range(LAST_PASS_TILE):

          $if LOAD_VARIANT == "LD128":
            $for C in range(0, CHANNEL_TILE, 16):
              const int8x16_t vi${K}x${ABC[C:C+16]} = vld1q_s8(i${K}); i${K} += 16;
              const int8x16_t vk${K}x${ABC[C:C+16]} = vld1q_s8(w); w = (const int8_t*) w + 16;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 16):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                int16x8_t vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmull_s8(vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmull_s8(vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
            $else:
              $for C in range(0, CHANNEL_TILE, 16):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vget_low_s8(vi${K}x${ABC[C:C+16]}), vget_low_s8(vk${K}x${ABC[C:C+16]}));
                vprod${ABC[C+8:C+16]} = vmlal_s8(vprod${ABC[C+8:C+16]}, vget_high_s8(vi${K}x${ABC[C:C+16]}), vget_high_s8(vk${K}x${ABC[C:C+16]}));
          $else:
            $for C in range(0, CHANNEL_TILE, 8):
              const int8x8_t vi${K}x${ABC[C:C+8]} = vld1_s8(i${K}); i${K} += 8;
              const int8x8_t vk${K}x${ABC[C:C+8]} = vld1_s8(w); w = (const int8_t*) w + 8;

            $if K == 0:
              $for C in range(0, CHANNEL_TILE, 8):
                int16x8_t vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $elif K % 2 == 0 or not MLA:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmull_s8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              $for C in range(0, CHANNEL_TILE, 8):
                vprod${ABC[C:C+8]} = vmlal_s8(vprod${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});

          $if not MLA or K % 2 == 1 or K + 1 == LAST_PASS_TILE:
            $for C in range(0, CHANNEL_TILE, 8):
              vacc${ABC[C:C+4]} = vaddw_s16(vacc${ABC[C:C+4]}, vget_low_s16(vprod${ABC[C:C+8]}));
              vacc${ABC[C+4:C+8]} = vaddw_s16(vacc${ABC[C+4:C+8]}, vget_high_s16(vprod${ABC[C:C+8]}));

        $if REQUANTIZATION == "RNDNU":
          $for C in range(0, CHANNEL_TILE, 4):
            vacc${ABC[C:C+4]} = vqshlq_s32(vacc${ABC[C:C+4]}, vright_pre_shift);

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

          $for C in range(0, CHANNEL_TILE, 4):
            vacc${ABC[C:C+4]} = vrshlq_s32(vacc${ABC[C:C+4]}, vright_post_shift);
        $elif REQUANTIZATION == "FP32":
          $for C in range(0, CHANNEL_TILE, 4):
            float32x4_t vfpacc${ABC[C:C+4]} = vcvtq_f32_s32(vacc${ABC[C:C+4]});

          $if DATATYPE == "QC8":
            $for C in range(0, CHANNEL_TILE, 4):
              const float32x4_t vscale${ABC[C:C+4]} = vld1q_f32((const float*) w); w = (const float*) w + 4;

            $for C in range(0, CHANNEL_TILE, 4):
              vfpacc${ABC[C:C+4]} = vmulq_f32(vfpacc${ABC[C:C+4]}, vscale${ABC[C:C+4]});
          $else:
            $for C in range(0, CHANNEL_TILE, 4):
              vfpacc${ABC[C:C+4]} = vmulq_f32(vfpacc${ABC[C:C+4]}, vscale);

          $if ARMV8:
            $for C in range(0, CHANNEL_TILE, 4):
              vacc${ABC[C:C+4]} = vcvtnq_s32_f32(vfpacc${ABC[C:C+4]});
          $else:
            $for C in range(0, CHANNEL_TILE, 4):
              vacc${ABC[C:C+4]} = vreinterpretq_s32_f32(vaddq_f32(vfpacc${ABC[C:C+4]}, vmagic_bias));

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

#if XNN_ARCH_ARM64
        $for C in range(0, CHANNEL_TILE, 8):
          int16x8_t vacc${ABC[C:C+8]} = vqmovn_high_s32(vqmovn_s32(vacc${ABC[C:C+4]}), vacc${ABC[C+4:C+8]});

        $if REQUANTIZATION != "FP32" or ARMV8:
          $for C in range(0, CHANNEL_TILE, 8):
            vacc${ABC[C:C+8]} = vqaddq_s16(vacc${ABC[C:C+8]}, voutput_zero_point);

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            int8x16_t vout${ABC[C:C+16]} = vqmovn_high_s16(vqmovn_s16(vacc${ABC[C:C+8]}), vacc${ABC[C+8:C+16]});
          $else:
            int8x8_t vout${ABC[C:C+8]} = vqmovn_s16(vacc${ABC[C:C+8]});
#else  // !XNN_ARCH_ARM64
        $for C in range(0, CHANNEL_TILE, 8):
          int16x8_t vacc${ABC[C:C+8]} = vcombine_s16(vqmovn_s32(vacc${ABC[C:C+4]}), vqmovn_s32(vacc${ABC[C+4:C+8]}));

        $if REQUANTIZATION != "FP32" or ARMV8:
          $for C in range(0, CHANNEL_TILE, 8):
            vacc${ABC[C:C+8]} = vqaddq_s16(vacc${ABC[C:C+8]}, voutput_zero_point);

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            int8x16_t vout${ABC[C:C+16]} = vcombine_s8(vqmovn_s16(vacc${ABC[C:C+8]}), vqmovn_s16(vacc${ABC[C+8:C+16]}));
          $else:
            int8x8_t vout${ABC[C:C+8]} = vqmovn_s16(vacc${ABC[C:C+8]});
#endif  // !XNN_ARCH_ARM64

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            vout${ABC[C:C+16]} = vmaxq_s8(vout${ABC[C:C+16]}, voutput_min);
          $elif CHANNEL_TILE == 8:
            vout${ABC[C:C+8]} = vmax_s8(vout${ABC[C:C+8]}, voutput_min);
          $else:
            vout${ABC[C:C+8]} = vmax_s8(vout${ABC[C:C+8]}, vget_low_s8(voutput_min));

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            vout${ABC[C:C+16]} = vminq_s8(vout${ABC[C:C+16]}, voutput_max);
          $elif CHANNEL_TILE == 8:
            vout${ABC[C:C+8]} = vmin_s8(vout${ABC[C:C+8]}, voutput_max);
          $else:
            vout${ABC[C:C+8]} = vmin_s8(vout${ABC[C:C+8]}, vget_low_s8(voutput_max));

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


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

          $for K in range(LAST_PASS_TILE):
            $if CHANNEL_TILE > 8:
              const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K}); i${K} += 8;
            $else:
              const int8x8_t vi${K}x${ABC[0:8]} = vld1_s8(i${K});
            $if K == 0:
              const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};
            $else:
              const int8x8_t vk${K}x${ABC[0:8]} = vld1_s8(w); w = (const int8_t*) w + ${CHANNEL_SUBTILE};

            $if K == 0:
              int16x8_t vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
            $elif K % 2 == 0 or not MLA:
              vprod${ABC[0:8]} = vmull_s8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
            $else:
              vprod${ABC[0:8]} = vmlal_s8(vprod${ABC[0:8]}, vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});

            $if not MLA or K % 2 == 1 or K + 1 == LAST_PASS_TILE:
              vacc${ABC[0:4]} = vaddw_s16(vacc${ABC[0:4]}, vget_low_s16(vprod${ABC[0:8]}));
              vacc${ABC[4:8]} = vaddw_s16(vacc${ABC[4:8]}, vget_high_s16(vprod${ABC[0:8]}));

          $if REQUANTIZATION == "RNDNU":
            vacc${ABC[0:4]} = vqshlq_s32(vacc${ABC[0:4]}, vright_pre_shift);
            vacc${ABC[4:8]} = vqshlq_s32(vacc${ABC[4:8]}, vright_pre_shift);

            vacc${ABC[0:4]} = vqdmulhq_s32(vacc${ABC[0:4]}, vmultiplier);
            vacc${ABC[4:8]} = vqdmulhq_s32(vacc${ABC[4:8]}, vmultiplier);

            vacc${ABC[0:4]} = vrshlq_s32(vacc${ABC[0:4]}, vright_post_shift);
            vacc${ABC[4:8]} = vrshlq_s32(vacc${ABC[4:8]}, vright_post_shift);
          $elif REQUANTIZATION == "FP32":
            float32x4_t vfpacc${ABC[0:4]} = vcvtq_f32_s32(vacc${ABC[0:4]});
            float32x4_t vfpacc${ABC[4:8]} = vcvtq_f32_s32(vacc${ABC[4:8]});

            $if DATATYPE == "QC8":
              const float32x4_t vscale${ABC[0:4]} = vld1q_f32((const float*) w); w = (const float*) w + 4;
              const float32x4_t vscale${ABC[4:8]} = vld1q_f32((const float*) w); w = (const float*) w + 4;
              vfpacc${ABC[0:4]} = vmulq_f32(vfpacc${ABC[0:4]}, vscale${ABC[0:4]});
              vfpacc${ABC[4:8]} = vmulq_f32(vfpacc${ABC[4:8]}, vscale${ABC[4:8]});
            $else:
              vfpacc${ABC[0:4]} = vmulq_f32(vfpacc${ABC[0:4]}, vscale);
              vfpacc${ABC[4:8]} = vmulq_f32(vfpacc${ABC[4:8]}, vscale);

            $if ARMV8:
              vacc${ABC[0:4]} = vcvtnq_s32_f32(vfpacc${ABC[0:4]});
              vacc${ABC[4:8]} = vcvtnq_s32_f32(vfpacc${ABC[4:8]});
            $else:
              vacc${ABC[0:4]} = vreinterpretq_s32_f32(vaddq_f32(vfpacc${ABC[0:4]}, vmagic_bias));
              vacc${ABC[4:8]} = vreinterpretq_s32_f32(vaddq_f32(vfpacc${ABC[4:8]}, vmagic_bias));

              vacc${ABC[0:4]} = vqsubq_s32(vacc${ABC[0:4]}, vmagic_bias_less_output_zero_point);
              vacc${ABC[4:8]} = vqsubq_s32(vacc${ABC[4:8]}, vmagic_bias_less_output_zero_point);

#if XNN_ARCH_ARM64
          int16x8_t vacc${ABC[0:8]} = vqmovn_high_s32(vqmovn_s32(vacc${ABC[0:4]}), vacc${ABC[4:8]});
#else
          int16x8_t vacc${ABC[0:8]} = vcombine_s16(vqmovn_s32(vacc${ABC[0:4]}), vqmovn_s32(vacc${ABC[4:8]}));
#endif
          $if REQUANTIZATION != "FP32" or ARMV8:
            vacc${ABC[0:8]} = vqaddq_s16(vacc${ABC[0:8]}, voutput_zero_point);

          int8x8_t vout${ABC[0:8]} = vqmovn_s16(vacc${ABC[0:8]});
          $if CHANNEL_TILE == 8:
            vout${ABC[0:8]} = vmax_s8(vout${ABC[0:8]}, voutput_min);
            vout${ABC[0:8]} = vmin_s8(vout${ABC[0:8]}, voutput_max);
          $else:
            vout${ABC[0:8]} = vmax_s8(vout${ABC[0:8]}, vget_low_s8(voutput_min));
            vout${ABC[0:8]} = vmin_s8(vout${ABC[0:8]}, vget_low_s8(voutput_max));

          $if CHANNEL_TILE > 8:
            if XNN_LIKELY(c >= 8) {
              vst1_s8(output, vout${ABC[0:8]}); output += 8;
              c -= 8;
            } else {
              if (c & 4) {
                vst1_lane_u32((void*) output, vreinterpret_u32_s8(vout${ABC[0:8]}), 0); output += 4;
                vout${ABC[0:8]} = vext_s8(vout${ABC[0:8]}, vout${ABC[0:8]}, 4);
              }
              if (c & 2) {
                vst1_lane_u16((void*) output, vreinterpret_u16_s8(vout${ABC[0:8]}), 0); output += 2;
                vout${ABC[0:8]} = vext_s8(vout${ABC[0:8]}, vout${ABC[0:8]}, 2);
              }
              if (c & 1) {
                vst1_lane_s8(output, vout${ABC[0:8]}, 0); output += 1;
              }
              c = 0;
            }
          $else:
            if (c & 4) {
              vst1_lane_u32((void*) output, vreinterpret_u32_s8(vout${ABC[0:8]}), 0); output += 4;
              vout${ABC[0:8]} = vext_s8(vout${ABC[0:8]}, vout${ABC[0:8]}, 4);
            }
            if (c & 2) {
              vst1_lane_u16((void*) output, vreinterpret_u16_s8(vout${ABC[0:8]}), 0); output += 2;
              vout${ABC[0:8]} = vext_s8(vout${ABC[0:8]}, vout${ABC[0:8]}, 2);
            }
            if (c & 1) {
              vst1_lane_s8(output, vout${ABC[0:8]}, 0); output += 1;
            }

        }${" while (c != 0);" if CHANNEL_TILE > 8 else ""}
      }
    }

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