// 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 <wasm_simd128.h>

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


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$WASM_X16X8_LOAD8X8 = {"QS8": "wasm_i16x8_load8x8", "QU8": "wasm_u16x8_load8x8"}[DATATYPE]
void xnn_${DATATYPE.lower()}_f32_vcvt_ukernel__wasmsimd_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 v128_t vminus_zero_point = wasm_v128_load64_splat(params->wasmsimd.minus_zero_point);
  const v128_t vscale = wasm_v128_load64_splat(params->wasmsimd.scale);
  $if BATCH_TILE > 8:
    for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      v128_t vx${ABC[0:8]} = ${WASM_X16X8_LOAD8X8}(x);
      $for N in range(8, BATCH_TILE, 8):
        v128_t vx${ABC[N:N+8]} = ${WASM_X16X8_LOAD8X8}(x + ${N});
      x += ${BATCH_TILE};

      $for N in range(0, BATCH_TILE, 8):
        vx${ABC[N:N+8]} = wasm_i16x8_add(vx${ABC[N:N+8]}, vminus_zero_point);

      $for N in range(0, BATCH_TILE, 8):
        v128_t vy${ABC[N:N+4]} = wasm_i32x4_extend_low_i16x8(vx${ABC[N:N+8]});
        v128_t vy${ABC[N+4:N+8]} = wasm_i32x4_extend_high_i16x8(vx${ABC[N:N+8]});

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

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

      wasm_v128_store(y, vy${ABC[0:4]});
      $for N in range(4, BATCH_TILE, 4):
        wasm_v128_store(y + ${N}, vy${ABC[N:N+4]});
      y += ${BATCH_TILE};
    }
  for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) {
    v128_t vx = ${WASM_X16X8_LOAD8X8}(x);
    vx = wasm_i16x8_add(vx, vminus_zero_point);
    x += 8;

    v128_t vy_lo = wasm_i32x4_extend_low_i16x8(vx);
    v128_t vy_hi = wasm_i32x4_extend_high_i16x8(vx);

    vy_lo = wasm_f32x4_convert_i32x4(vy_lo);
    vy_hi = wasm_f32x4_convert_i32x4(vy_hi);

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

    wasm_v128_store(y, vy_lo);
    wasm_v128_store(y + 4, vy_hi);
    y += 8;
  }
  if XNN_UNLIKELY(n != 0) {
    assert(n >= 1 * sizeof(${XINT8_T}));
    assert(n <= 7 * sizeof(${XINT8_T}));

    v128_t vx = ${WASM_X16X8_LOAD8X8}(x);
    vx = wasm_i16x8_add(vx, vminus_zero_point);
    x += 8;

    v128_t vy = wasm_i32x4_extend_low_i16x8(vx);
    vy = wasm_f32x4_convert_i32x4(vy);
    vy = wasm_f32x4_mul(vy, vscale);

    if (n & (4 * sizeof(${XINT8_T}))) {
      wasm_v128_store(y, vy); y += 4;
      vy = wasm_i32x4_extend_high_i16x8(vx);
      vy = wasm_f32x4_convert_i32x4(vy);
      vy = wasm_f32x4_mul(vy, vscale);
    }
    if (n & (2 * sizeof(${XINT8_T}))) {
      *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
      vy = wasm_v64x2_shuffle(vy, vy, 1, 1);
      y += 2;
    }
    if (n & (1 * sizeof(${XINT8_T}))) {
      *y = wasm_f32x4_extract_lane(vy, 0);
    }
  }
}
