// 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 == "RNDNU"
$assert DATATYPE == "QU8"
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$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 <arm_neon.h>

#include "xnnpack/dwconv.h"
#include "xnnpack/math.h"

void xnn_qu8_dwconv_minmax_rndnu_ukernel_${FIRST_PASS_TILE}f${MIDDLE_PASS_TILE}m${LAST_PASS_TILE}l${CHANNEL_TILE}c${CHANNEL_SUBTILE}s${CHANNEL_ROUND}r__neon_mul8(
    size_t channels,
    size_t output_width,
    const uint8_t** input,
    const void* weights,
    uint8_t* output,
    intptr_t input_stride,
    size_t output_increment,
    size_t input_offset,
    const uint8_t* zero,
    size_t kernel_size,
    int32_t* buffer,
    const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(channels != 0);
  assert(output_width != 0);
  assert(kernel_size > ${FIRST_PASS_TILE});

  const uint16x8_t vkernel_zero_point = vmovl_u8(vld1_dup_u8(&params->rndnu_neon.kernel_zero_point));
  const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
  const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
  const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
  const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
  $if CHANNEL_TILE == 8:
    const uint8x8_t voutput_min = vld1_dup_u8(&params->rndnu_neon.output_min);
    const uint8x8_t voutput_max = vld1_dup_u8(&params->rndnu_neon.output_max);
  $else:
    const uint8x16_t voutput_min = vld1q_dup_u8(&params->rndnu_neon.output_min);
    const uint8x16_t voutput_max = vld1q_dup_u8(&params->rndnu_neon.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 uint8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const uint8_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):

          $for C in range(0, CHANNEL_TILE, 8):
            const uint8x8_t vi${K}x${ABC[C:C+8]} = vld1_u8(i${K}); i${K} += 8;
            const uint8x8_t vk${K}x${ABC[C:C+8]} = vld1_u8(w); w = (const int8_t*) w + 8;

          $for C in range(0, CHANNEL_TILE, 8):
            $if K == 0:
              uint16x8_t vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $if FIRST_PASS_TILE == 1:
              uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
            $if K == 1:
              uint16x8_t vsum${ABC[C:C+8]} = vaddl_u8(vi0x${ABC[C:C+8]}, vi1x${ABC[C:C+8]});
            $elif K > 1:
              vsum${ABC[C:C+8]} = vaddw_u8(vsum${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]});

          $for C in range(0, CHANNEL_TILE, 8):
            vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vprod${ABC[C:C+8]})));
            vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vprod${ABC[C:C+8]})));

        $for C in range(0, CHANNEL_TILE, 8):
          vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vsum${ABC[C:C+8]}), vget_low_u16(vkernel_zero_point)));
          vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vsum${ABC[C:C+8]}), vget_high_u16(vkernel_zero_point)));

        $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 uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K}); i${K} += 8;
              $else:
                const uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K});
              const uint8x8_t vk${K}x${ABC[0:8]} = vld1_u8(w); w = (const int8_t*) w + 8;

              $if K == 0:
                uint16x8_t vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $if FIRST_PASS_TILE == 1:
                uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
              $if K == 1:
                uint16x8_t vsum${ABC[0:8]} = vaddl_u8(vi0x${ABC[0:8]}, vi1x${ABC[0:8]});
              $elif K > 1:
                vsum${ABC[0:8]} = vaddw_u8(vsum${ABC[0:8]}, vi${K}x${ABC[0:8]});

              vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vprod${ABC[0:8]})));
              vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vprod${ABC[0:8]})));

            vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vsum${ABC[0:8]}), vget_low_u16(vkernel_zero_point)));
            vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vsum${ABC[0:8]}), vget_high_u16(vkernel_zero_point)));

            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 uint8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const uint8_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):

          $for C in range(0, CHANNEL_TILE, 8):
            const uint8x8_t vi${K}x${ABC[C:C+8]} = vld1_u8(i${K}); i${K} += 8;
            const uint8x8_t vk${K}x${ABC[C:C+8]} = vld1_u8(w); w = (const int8_t*) w + 8;

          $for C in range(0, CHANNEL_TILE, 8):
            $if K == 0:
              uint16x8_t vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $if MIDDLE_PASS_TILE == 1:
              uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
            $if K == 1:
              uint16x8_t vsum${ABC[C:C+8]} = vaddl_u8(vi0x${ABC[C:C+8]}, vi1x${ABC[C:C+8]});
            $elif K > 1:
              vsum${ABC[C:C+8]} = vaddw_u8(vsum${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]});

          $for C in range(0, CHANNEL_TILE, 8):
            vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vprod${ABC[C:C+8]})));
            vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vprod${ABC[C:C+8]})));

        $for C in range(0, CHANNEL_TILE, 8):
          vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vsum${ABC[C:C+8]}), vget_low_u16(vkernel_zero_point)));
          vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vsum${ABC[C:C+8]}), vget_high_u16(vkernel_zero_point)));

        $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 uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K}); i${K} += 8;
              $else:
                const uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K});
              const uint8x8_t vk${K}x${ABC[0:8]} = vld1_u8(w); w = (const int8_t*) w + 8;

              $if K == 0:
                uint16x8_t vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $else:
                vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
              $if MIDDLE_PASS_TILE == 1:
                uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
              $if K == 1:
                uint16x8_t vsum${ABC[0:8]} = vaddl_u8(vi0x${ABC[0:8]}, vi1x${ABC[0:8]});
              $elif K > 1:
                vsum${ABC[0:8]} = vaddw_u8(vsum${ABC[0:8]}, vi${K}x${ABC[0:8]});

              vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vprod${ABC[0:8]})));
              vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vprod${ABC[0:8]})));

            vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vsum${ABC[0:8]}), vget_low_u16(vkernel_zero_point)));
            vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vsum${ABC[0:8]}), vget_high_u16(vkernel_zero_point)));

            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.
    {
      int32_t* b = buffer;

      $for K in range(LAST_PASS_TILE):
        const uint8_t* i${K} = input[${K}];
        assert(i${K} != NULL);
        if XNN_UNPREDICTABLE(i${K} != zero) {
          i${K} = (const uint8_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):

          $for C in range(0, CHANNEL_TILE, 8):
            const uint8x8_t vi${K}x${ABC[C:C+8]} = vld1_u8(i${K}); i${K} += 8;
            const uint8x8_t vk${K}x${ABC[C:C+8]} = vld1_u8(w); w = (const int8_t*) w + 8;

          $for C in range(0, CHANNEL_TILE, 8):
            $if K == 0:
              uint16x8_t vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $else:
              vprod${ABC[C:C+8]} = vmull_u8(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]});
            $if LAST_PASS_TILE == 1:
              uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
            $if K == 1:
              uint16x8_t vsum${ABC[C:C+8]} = vaddl_u8(vi0x${ABC[C:C+8]}, vi1x${ABC[C:C+8]});
            $elif K > 1:
              vsum${ABC[C:C+8]} = vaddw_u8(vsum${ABC[C:C+8]}, vi${K}x${ABC[C:C+8]});

          $for C in range(0, CHANNEL_TILE, 8):
            vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vprod${ABC[C:C+8]})));
            vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vprod${ABC[C:C+8]})));

        $for C in range(0, CHANNEL_TILE, 8):
          vacc${ABC[C:C+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C:C+4]}), vget_low_u16(vsum${ABC[C:C+8]}), vget_low_u16(vkernel_zero_point)));
          vacc${ABC[C+4:C+8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[C+4:C+8]}), vget_high_u16(vsum${ABC[C:C+8]}), vget_high_u16(vkernel_zero_point)));

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

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

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            uint8x16_t vout${ABC[C:C+16]} = vqmovun_high_s16(vqmovun_s16(vacc${ABC[C:C+8]}), vacc${ABC[C+8:C+16]});
          $else:
            uint8x8_t vout${ABC[C:C+8]} = vqmovun_s16(vacc${ABC[C:C+8]});
#else
        $for C in range(0, CHANNEL_TILE, 8):
          const int16x8_t vacc${ABC[C:C+8]} = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc${ABC[C:C+4]}), vqmovn_s32(vacc${ABC[C+4:C+8]})), voutput_zero_point);

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            uint8x16_t vout${ABC[C:C+16]} = vcombine_u8(vqmovun_s16(vacc${ABC[C:C+8]}), vqmovun_s16(vacc${ABC[C+8:C+16]}));
          $else:
            uint8x8_t vout${ABC[C:C+8]} = vqmovun_s16(vacc${ABC[C:C+8]});
#endif

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

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

        $for C in range(0, CHANNEL_TILE, 16):
          $if C + 8 < CHANNEL_TILE:
            vst1q_u8(output, vout${ABC[C:C+16]}); output += 16;
          $else:
            vst1_u8(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 uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K}); i${K} += 8;
            $else:
              const uint8x8_t vi${K}x${ABC[0:8]} = vld1_u8(i${K});
            const uint8x8_t vk${K}x${ABC[0:8]} = vld1_u8(w); w = (const int8_t*) w + 8;

            $if K == 0:
              uint16x8_t vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
            $else:
              vprod${ABC[0:8]} = vmull_u8(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]});
            $if FIRST_PASS_TILE == 1:
              uint16x8_t vsum${ABC[0:8]} = vmovl_u8(vi${K}x${ABC[0:8]});
            $if K == 1:
              uint16x8_t vsum${ABC[0:8]} = vaddl_u8(vi0x${ABC[0:8]}, vi1x${ABC[0:8]});
            $elif K > 1:
              vsum${ABC[0:8]} = vaddw_u8(vsum${ABC[0:8]}, vi${K}x${ABC[0:8]});

            vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vprod${ABC[0:8]})));
            vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vaddw_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vprod${ABC[0:8]})));

          vacc${ABC[0:0+4]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0:0+4]}), vget_low_u16(vsum${ABC[0:8]}), vget_low_u16(vkernel_zero_point)));
          vacc${ABC[0+4:8]} = vreinterpretq_s32_u32(vmlsl_u16(vreinterpretq_u32_s32(vacc${ABC[0+4:8]}), vget_high_u16(vsum${ABC[0:8]}), vget_high_u16(vkernel_zero_point)));

          vacc${ABC[0:4]} = vrshlq_s32(vacc${ABC[0:4]}, vright_pre_shift);
          vacc${ABC[4:8]} = vrshlq_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);

#if XNN_ARCH_ARM64
          const int16x8_t vacc${ABC[0:8]} = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc${ABC[0:4]}), vacc${ABC[4:8]}), voutput_zero_point);
          uint8x8_t vout${ABC[0:8]} = vqmovun_s16(vacc${ABC[0:8]});
#else
          const int16x8_t vacc${ABC[0:8]} = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc${ABC[0:4]}), vqmovn_s32(vacc${ABC[4:8]})), voutput_zero_point);
          uint8x8_t vout${ABC[0:8]} = vqmovun_s16(vacc${ABC[0:8]});
#endif

          $if CHANNEL_TILE == 8:
            vout${ABC[0:8]} = vmax_u8(vout${ABC[0:8]}, voutput_min);
            vout${ABC[0:8]} = vmin_u8(vout${ABC[0:8]}, voutput_max);
          $else:
            vout${ABC[0:8]} = vmax_u8(vout${ABC[0:8]}, vget_low_u8(voutput_min));
            vout${ABC[0:8]} = vmin_u8(vout${ABC[0:8]}, vget_low_u8(voutput_max));

          $if CHANNEL_TILE > 8:
            if XNN_LIKELY(c >= 8) {
              vst1_u8(output, vout${ABC[0:8]}); output += 8;
              c -= 8;
            } else {
              if (c & 4) {
                vst1_lane_u32((void*) output, vreinterpret_u32_u8(vout${ABC[0:8]}), 0); output += 4;
                vout${ABC[0:8]} = vext_u8(vout${ABC[0:8]}, vout${ABC[0:8]}, 4);
              }
              if (c & 2) {
                vst1_lane_u16((void*) output, vreinterpret_u16_u8(vout${ABC[0:8]}), 0); output += 2;
                vout${ABC[0:8]} = vext_u8(vout${ABC[0:8]}, vout${ABC[0:8]}, 2);
              }
              if (c & 1) {
                vst1_lane_u8(output, vout${ABC[0:8]}, 0); output += 1;
              }
              c = 0;
            }
          $else:
            if (c & 4) {
              vst1_lane_u32((void*) output, vreinterpret_u32_u8(vout${ABC[0:8]}), 0); output += 4;
              vout${ABC[0:8]} = vext_u8(vout${ABC[0:8]}, vout${ABC[0:8]}, 4);
            }
            if (c & 2) {
              vst1_lane_u16((void*) output, vreinterpret_u16_u8(vout${ABC[0:8]}), 0); output += 2;
              vout${ABC[0:8]} = vext_u8(vout${ABC[0:8]}, vout${ABC[0:8]}, 2);
            }
            if (c & 1) {
              vst1_lane_u8(output, vout${ABC[0:8]}, 0); output += 1;
            }
        }${" while (c != 0);" if CHANNEL_TILE > 8 else ""}
      }
    }

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