// 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_u${BATCH_TILE}(
    size_t batch,
    const ${XINT8_T}* input,
    ${XINT8_T}* output,
    const struct xnn_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(batch != 0);
  assert(batch % sizeof(${XINT8_T}) == 0);
  assert(input != NULL);
  assert(output != NULL);

  const __m256i vinput_zero_point = _mm256_set1_epi16(params->scalar.input_zero_point);
  const __m256i vmultiplier = _mm256_set1_epi16(-params->scalar.multiplier);
  const __m256i voutput_zero_point = _mm256_set1_epi16(params->scalar.output_zero_point);
  XNN_FORCE_REALIZATION(vinput_zero_point);
  XNN_FORCE_REALIZATION(vmultiplier);
  XNN_FORCE_REALIZATION(voutput_zero_point);
  $if BATCH_TILE > 16:
    for (; batch >= ${BATCH_TILE} * sizeof(${XINT8_T}); batch -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      __m256i vacc${ABC[0]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) input));
      $for N in range(1, 2*SIMD_TILE):
        __m256i vacc${ABC[N]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) (input + ${N * 16})));
      input += ${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*) output, vy${ABC[0]});
      $for N in range(1, SIMD_TILE):
        _mm256_storeu_si256((__m256i*) (output + ${N * 32}), vy${ABC[N]});
      output += ${BATCH_TILE};
    }
  for (; batch >= 16 * sizeof(${XINT8_T}); batch -= 16 * sizeof(${XINT8_T})) {
    __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) input));
    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);
    input += 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*) output, vy);
    output += 16;
  }
  if XNN_UNLIKELY(batch != 0) {
    assert(batch >= 1 * sizeof(${XINT8_T}));
    assert(batch <= 15 * sizeof(${XINT8_T}));

    __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) input));
    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 (batch & (8 * sizeof(${XINT8_T}))) {
      _mm_storel_epi64((__m128i*) output, vy);
      vy = _mm_unpackhi_epi64(vy, vy);
      output += 8;
    }
    if (batch & (4 * sizeof(${XINT8_T}))) {
      _mm_storeu_si32(output, vy);
      vy = _mm_srli_epi64(vy, 32);
      output += 4;
    }
    if (batch & (2 * sizeof(${XINT8_T}))) {
      _mm_storeu_si16(output, vy);
      vy = _mm_srli_epi32(vy, 16);
      output += 2;
    }
    if (batch & (1 * sizeof(${XINT8_T}))) {
      *output = (${XINT8_T}) _mm_extract_epi8(vy, 0);
    }
  }
}
