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

#include <immintrin.h>

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


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$_MM256_CVTEPX8_EPI16 = {"QS8": "_mm256_cvtepi8_epi16", "QU8": "_mm256_cvtepu8_epi16"}[DATATYPE]
$_MM256_PACKXS_EPI16 = {"QS8": "_mm256_packs_epi16", "QU8": "_mm256_packus_epi16"}[DATATYPE]
$_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vcvt_ukernel__avx2_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 __m256i vinput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.input_zero_point);
  const __m256i vmultiplier = _mm256_load_si256((const __m256i*) params->avx2.multiplier);
  const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.output_zero_point);
  $if BATCH_TILE > 16:
    for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      __m256i vacc${ABC[0]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x));
      $for N in range(1, 2*SIMD_TILE):
        __m256i vacc${ABC[N]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) (x + ${N * 16})));
      x += ${BATCH_TILE};

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

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

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

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

      $for N in range(SIMD_TILE):
        __m256i vy${ABC[N]} = ${_MM256_PACKXS_EPI16}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});

      $for N in range(SIMD_TILE):
        vy${ABC[N]} = _mm256_permute4x64_epi64(vy${ABC[N]}, _MM_SHUFFLE(3, 1, 2, 0));

      _mm256_storeu_si256((__m256i*) y, vy${ABC[0]});
      $for N in range(1, SIMD_TILE):
        _mm256_storeu_si256((__m256i*) (y + ${N * 32}), vy${ABC[N]});
      y += ${BATCH_TILE};
    }
  for (; n >= 16 * sizeof(${XINT8_T}); n -= 16 * sizeof(${XINT8_T})) {
    __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x));
    vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
    vacc = _mm256_slli_epi16(vacc, 7);
    vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
    vacc = _mm256_adds_epi16(vacc, voutput_zero_point);
    x += 16;

    const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
    const __m128i vy = ${_MM_PACKXS_EPI16}(_mm256_castsi256_si128(vacc), vacc_hi);
    _mm_storeu_si128((__m128i*) y, vy);
    y += 16;
  }
  if XNN_UNLIKELY(n != 0) {
    assert(n >= 1 * sizeof(${XINT8_T}));
    assert(n <= 15 * sizeof(${XINT8_T}));

    __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x));
    vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
    vacc = _mm256_slli_epi16(vacc, 7);
    vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
    vacc = _mm256_adds_epi16(vacc, voutput_zero_point);

    const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
    __m128i vy = ${_MM_PACKXS_EPI16}(_mm256_castsi256_si128(vacc), vacc_hi);
    if (n & (8 * sizeof(${XINT8_T}))) {
      _mm_storel_epi64((__m128i*) y, vy);
      vy = _mm_unpackhi_epi64(vy, vy);
      y += 8;
    }
    if (n & (4 * sizeof(${XINT8_T}))) {
      _mm_storeu_si32(y, vy);
      vy = _mm_srli_epi64(vy, 32);
      y += 4;
    }
    if (n & (2 * sizeof(${XINT8_T}))) {
      _mm_storeu_si16(y, vy);
      vy = _mm_srli_epi32(vy, 16);
      y += 2;
    }
    if (n & (1 * sizeof(${XINT8_T}))) {
      *y = (${XINT8_T}) _mm_extract_epi8(vy, 0);
    }
  }
}
