// Copyright 2022 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 BATCH_TILE >= 8
$assert BATCH_TILE == 8 or BATCH_TILE % 16 == 0
$SIMD_TILE = BATCH_TILE // 16
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <arm_neon.h>

#include <xnnpack/common.h>
#include <xnnpack/vcvt.h>


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$XINT8X8_T = {"QS8": "int8x8_t", "QU8": "uint8x8_t"}[DATATYPE]
$XINT8X16_T = {"QS8": "int8x16_t", "QU8": "uint8x16_t"}[DATATYPE]
$XINT16X8_T = {"QS8": "int16x8_t", "QU8": "uint16x8_t"}[DATATYPE]
$VLD1_X8 = {"QS8": "vld1_s8", "QU8": "vld1_u8"}[DATATYPE]
$VLD1Q_X8 = {"QS8": "vld1q_s8", "QU8": "vld1q_u8"}[DATATYPE]
$VLD1Q_DUP_X16 = {"QS8": "vld1q_dup_s16", "QU8": "vld1q_dup_u16"}[DATATYPE]
$VQMOVXN_S16 = {"QS8": "vqmovn_s16", "QU8": "vqmovun_s16"}[DATATYPE]
$VGET_LOW_X8 = {"QS8": "vget_low_s8", "QU8": "vget_low_u8"}[DATATYPE]
$VGET_HIGH_X8 = {"QS8": "vget_high_s8", "QU8": "vget_high_u8"}[DATATYPE]
$VCOMBINE_X8 = {"QS8": "vcombine_s8", "QU8": "vcombine_u8"}[DATATYPE]
$VREINTERPRET_U32_X8 = {"QS8": "vreinterpret_u32_s8", "QU8": "vreinterpret_u32_u8"}[DATATYPE]
$VREINTERPRET_U16_X8 = {"QS8": "vreinterpret_u16_s8", "QU8": "vreinterpret_u16_u8"}[DATATYPE]
$VEXT_X8 = {"QS8": "vext_s8", "QU8": "vext_u8"}[DATATYPE]
$VST1_X8 = {"QS8": "vst1_s8", "QU8": "vst1_u8"}[DATATYPE]
$VST1Q_X8 = {"QS8": "vst1q_s8", "QU8": "vst1q_u8"}[DATATYPE]
$VST1_LANE_X8 = {"QS8": "vst1_lane_s8", "QU8": "vst1_lane_u8"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vcvt_ukernel__neon_x${BATCH_TILE}(
    size_t n,
    const ${XINT8_T}* x,
    ${XINT8_T}* y,
    const union xnn_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(n != 0);
  assert(n % sizeof(${XINT8_T}) == 0);
  assert(x != NULL);
  assert(y != NULL);

  const ${XINT16X8_T} vinput_zero_point = ${VLD1Q_DUP_X16}(&params->neon.input_zero_point);
  const int16x8_t vmultiplier = vld1q_dup_s16(&params->neon.multiplier);
  const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->neon.output_zero_point);
  $if BATCH_TILE > 8:
    for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      $for N in range(SIMD_TILE):
        const ${XINT8X16_T} vx${ABC[N]} = ${VLD1Q_X8}(x); x += 16;

      $for N in range(SIMD_TILE):
        $if DATATYPE == "QU8":
          int16x8_t vacc${ABC[2*N]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]})));
          int16x8_t vacc${ABC[2*N+1]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]})));
        $else:
          int16x8_t vacc${ABC[2*N]} = vsubw_s8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]}));
          int16x8_t vacc${ABC[2*N+1]} = vsubw_s8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]}));

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vshlq_n_s16(vacc${ABC[N]}, 7);

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vqrdmulhq_s16(vacc${ABC[N]}, vmultiplier);

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vqaddq_s16(vacc${ABC[N]}, voutput_zero_point);

      $for N in range(SIMD_TILE):
        const ${XINT8X16_T} vy${ABC[N]} = ${VCOMBINE_X8}(${VQMOVXN_S16}(vacc${ABC[2*N]}), ${VQMOVXN_S16}(vacc${ABC[2*N+1]}));

      $for N in range(SIMD_TILE):
        ${VST1Q_X8}(y, vy${ABC[N]}); y += 16;
    }
  for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) {
    const ${XINT8X8_T} vx = ${VLD1_X8}(x); x += 8;
    $if DATATYPE == "QU8":
      int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
    $else:
      int16x8_t vacc = vsubw_s8(vinput_zero_point, vx);
    vacc = vshlq_n_s16(vacc, 7);
    vacc = vqrdmulhq_s16(vacc, vmultiplier);
    vacc = vqaddq_s16(vacc, voutput_zero_point);
    const ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc);
    ${VST1_X8}(y, vy); y += 8;
  }
  if XNN_UNLIKELY(n != 0) {
    assert(n >= 1 * sizeof(${XINT8_T}));
    assert(n <= 7 * sizeof(${XINT8_T}));

    const ${XINT8X8_T} vx = ${VLD1_X8}(x);
    $if DATATYPE == "QU8":
      int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
    $else:
      int16x8_t vacc = vsubw_s8(vinput_zero_point, vx);
    vacc = vshlq_n_s16(vacc, 7);
    vacc = vqrdmulhq_s16(vacc, vmultiplier);
    vacc = vqaddq_s16(vacc, voutput_zero_point);
    ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc);

    if (n & (4 * sizeof(${XINT8_T}))) {
      vst1_lane_u32((void*) y, ${VREINTERPRET_U32_X8}(vy), 0); y += 4;
      vy = ${VEXT_X8}(vy, vy, 4);
    }
    if (n & (2 * sizeof(${XINT8_T}))) {
      vst1_lane_u16((void*) y, ${VREINTERPRET_U16_X8}(vy), 0); y += 2;
      vy = ${VEXT_X8}(vy, vy, 2);
    }
    if (n & (1 * sizeof(${XINT8_T}))) {
      ${VST1_LANE_X8}(y, vy, 0);
    }
  }
}
