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

#include <arm_neon.h>

#include <xnnpack/common.h>
#include <xnnpack/intrinsics-polyfill.h>
#include <xnnpack/vcvt.h>


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$XINT8X8_T = {"QS8": "int8x8_t", "QU8": "uint8x8_t"}[DATATYPE]
$VLD1_X8 = {"QS8": "vld1_s8", "QU8": "vld1_u8"}[DATATYPE]
$VLD1_X16 = {"QS8": "vld1_s16", "QU8": "vld1_u16"}[DATATYPE]
void xnn_${DATATYPE.lower()}_f32_vcvt_ukernel__neon_x${BATCH_TILE}(
    size_t n,
    const ${XINT8_T}* x,
    float* y,
    const union xnn_${DATATYPE.lower()}_f32_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 int16x8_t vminus_zero_point = vreinterpretq_s16_u32(vld1q_dup_u32((const void*) params->neon.minus_zero_point));
  const float32x4_t vscale = vld1q_dup_f32(&params->neon.scale);
  $if BATCH_TILE > 8:
    for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      $for N in range(0, BATCH_TILE, 8):
        const ${XINT8X8_T} vx${ABC[N:N+8]} = ${VLD1_X8}(x); x += 8;

      $for N in range(0, BATCH_TILE, 8):
        $if DATATYPE == "QU8":
          const int16x8_t vhx${ABC[N:N+8]} = vreinterpretq_s16_u16(vaddw_u8(vreinterpretq_u16_s16(vminus_zero_point), vx${ABC[N:N+8]}));
        $else:
          const int16x8_t vhx${ABC[N:N+8]} = vaddw_s8(vminus_zero_point, vx${ABC[N:N+8]});

      $for N in range(0, BATCH_TILE, 8):
        const int32x4_t vwx${ABC[N:N+4]} = vmovl_s16(vget_low_s16(vhx${ABC[N:N+8]}));
        const int32x4_t vwx${ABC[N+4:N+8]} = vmovl_s16(vget_high_s16(vhx${ABC[N:N+8]}));

      $for N in range(0, BATCH_TILE, 4):
        float32x4_t vy${ABC[N:N+4]} = vcvtq_f32_s32(vwx${ABC[N:N+4]});

      $for N in range(0, BATCH_TILE, 4):
        vy${ABC[N:N+4]} = vmulq_f32(vy${ABC[N:N+4]}, vscale);

      $for N in range(0, BATCH_TILE, 4):
        vst1q_f32(y, vy${ABC[N:N+4]}); y += 4;
    }
  for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) {
    const ${XINT8X8_T} vx = ${VLD1_X8}(x); x += 8;

    $if DATATYPE == "QU8":
      const int16x8_t vhx = vreinterpretq_s16_u16(vaddw_u8(vreinterpretq_u16_s16(vminus_zero_point), vx));
    $else:
      const int16x8_t vhx = vaddw_s8(vminus_zero_point, vx);

    const int32x4_t vwx_lo = vmovl_s16(vget_low_s16(vhx));
    const int32x4_t vwx_hi = vmovl_s16(vget_high_s16(vhx));

    float32x4_t vy_lo = vcvtq_f32_s32(vwx_lo);
    float32x4_t vy_hi = vcvtq_f32_s32(vwx_hi);

    vy_lo = vmulq_f32(vy_lo, vscale);
    vy_hi = vmulq_f32(vy_hi, vscale);

    vst1q_f32(y, vy_lo); y += 4;
    vst1q_f32(y, vy_hi); y += 4;
  }
  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":
      const int16x8_t vhx = vreinterpretq_s16_u16(vaddw_u8(vreinterpretq_u16_s16(vminus_zero_point), vx));
    $else:
      const int16x8_t vhx = vaddw_s8(vminus_zero_point, vx);

    const int32x4_t vwx_lo = vmovl_s16(vget_low_s16(vhx));
    const int32x4_t vwx_hi = vmovl_s16(vget_high_s16(vhx));

    float32x4_t vy = vcvtq_f32_s32(vwx_lo);
    vy = vmulq_f32(vy, vscale);

    if (n & (4 * sizeof(${XINT8_T}))) {
      vst1q_f32(y, vy); y += 4;
      vy = vcvtq_f32_s32(vwx_hi);
      vy = vmulq_f32(vy, vscale);
    }
    float32x2_t vy_lo = vget_low_f32(vy);
    if (n & (2 * sizeof(${XINT8_T}))) {
      vst1_f32(y, vy_lo); y += 2;
      vy_lo = vget_high_f32(vy);
    }
    if (n & (1 * sizeof(${XINT8_T}))) {
      vst1_lane_f32(y, vy_lo, 0);
    }
  }
}
