// 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 DATATYPE in ["QS8", "QU8"]
$assert BATCH_TILE % 16 == 0
$assert BATCH_TILE >= 16
$SIMD_TILE = BATCH_TILE // 4
$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_PACKXS_EPI16 = {"QS8": "_mm256_packs_epi16", "QU8": "_mm256_packus_epi16"}[DATATYPE]
$_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE]
$OUTPUT_MAX = {"QS8": 127, "QU8": 255}[DATATYPE]
void xnn_f32_${DATATYPE.lower()}_vcvt_ukernel__avx2_u${BATCH_TILE}(
    size_t batch,
    const float* input,
    ${XINT8_T}* output,
    const struct xnn_f32_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input != NULL);
  assert(output != NULL);

  static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};

  const __m256 vscale = _mm256_set1_ps(params->scalar.scale);
  const __m256 voutput_max_less_zero_point = _mm256_set1_ps((float) ((int32_t) ${OUTPUT_MAX} - (int32_t) params->scalar.output_zero_point));
  const __m256i voutput_zero_point = _mm256_set1_epi16(params->scalar.output_zero_point);
  $if BATCH_TILE > 16:
    XNN_ALIGN(32) static const uint32_t shuffle_mask[8] = {0, 4, 1, 5, 2, 6, 3, 7};
    const __m256i vshuffle_mask = _mm256_load_si256((const __m256i*) shuffle_mask);
  XNN_FORCE_REALIZATION(vscale);
  XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
  XNN_FORCE_REALIZATION(voutput_zero_point);

  for (; batch >= ${BATCH_TILE} * sizeof(float); batch -= ${BATCH_TILE} * sizeof(float)) {
    __m256 vx${ABC[0:2]} = _mm256_loadu_ps(input);
    $for N in range(2, SIMD_TILE, 2):
      __m256 vx${ABC[N:N+2]} = _mm256_loadu_ps(input + ${N * 4});
    input += ${BATCH_TILE};

    $for N in range(0, SIMD_TILE, 2):
      vx${ABC[N:N+2]} = _mm256_mul_ps(vx${ABC[N:N+2]}, vscale);

    $for N in range(0, SIMD_TILE, 2):
      vx${ABC[N:N+2]} = _mm256_min_ps(vx${ABC[N:N+2]}, voutput_max_less_zero_point);

    $for N in range(0, SIMD_TILE, 2):
      const __m256i vacc${ABC[N:N+2]} = _mm256_cvtps_epi32(vx${ABC[N:N+2]});

    $for N in range(0, SIMD_TILE, 4):
      __m256i vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]} = _mm256_packs_epi32(vacc${ABC[N:N+2]}, vacc${ABC[N+2:N+4]});

    $for N in range(0, SIMD_TILE, 4):
      vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]} = _mm256_adds_epi16(vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]}, voutput_zero_point);

    $for N in range(0, SIMD_TILE, 8):
      $if N + 4 < SIMD_TILE:
        const __m256i vy${ABC[N]}${ABC[N+2]}${ABC[N+4]}${ABC[N+6]}${ABC[N+1]}${ABC[N+3]}${ABC[N+5]}${ABC[N+7]} = ${_MM256_PACKXS_EPI16}(vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]}, vacc${ABC[N+4]}${ABC[N+6]}${ABC[N+5]}${ABC[N+7]});
      $else:
        const __m128i vy${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]} = ${_MM_PACKXS_EPI16}(_mm256_castsi256_si128(vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]}), _mm256_extracti128_si256(vacc${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]}, 1));

    $for N in range(0, SIMD_TILE, 8):
      $if N + 4 < SIMD_TILE:
        __m256i vy${ABC[N:N+8]} = _mm256_permutevar8x32_epi32(vy${ABC[N]}${ABC[N+2]}${ABC[N+4]}${ABC[N+6]}${ABC[N+1]}${ABC[N+3]}${ABC[N+5]}${ABC[N+7]}, vshuffle_mask);
      $else:
        __m128i vy${ABC[N:N+4]} = _mm_shuffle_epi32(vy${ABC[N]}${ABC[N+2]}${ABC[N+1]}${ABC[N+3]}, _MM_SHUFFLE(3, 1, 2, 0));

    $if SIMD_TILE > 4:
      _mm256_storeu_si256((__m256i*) output, vy${ABC[0:8]});
    $else:
      _mm_storeu_si128((__m128i*) output, vy${ABC[0:4]});
    $for N in range(8, SIMD_TILE, 8):
      $if N + 4 < SIMD_TILE:
        _mm256_storeu_si256((__m256i*) (output + ${N * 4}), vy${ABC[N:N+8]});
      $else:
        _mm_storeu_si128((__m128i*) (output + ${N * 4}), vy${ABC[N:N+4]});
    output += ${BATCH_TILE};
  }
  for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
    __m256 vx = _mm256_loadu_ps(input);
    vx = _mm256_mul_ps(vx, vscale);
    vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
    input += 8;

    const __m256i vacc = _mm256_cvtps_epi32(vx);

    __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
    vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
    vy = ${_MM_PACKXS_EPI16}(vy, vy);

    _mm_storel_epi64((__m128i*) output, vy);
    output += 8;
  }
  if XNN_UNLIKELY(batch != 0) {
    assert(batch >= 1 * sizeof(float));
    assert(batch <= 7 * sizeof(float));
    const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - batch));

    __m256 vx = _mm256_maskload_ps(input, vmask);
    vx = _mm256_mul_ps(vx, vscale);
    vx = _mm256_min_ps(vx, voutput_max_less_zero_point);

    const __m256i vacc = _mm256_cvtps_epi32(vx);

    __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
    vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
    vy = ${_MM_PACKXS_EPI16}(vy, vy);

    if (batch & (4 * sizeof(float))) {
      _mm_storeu_si32(output, vy);
      output += 4;
      vy = _mm_srli_epi64(vy, 32);
    }
    if (batch & (2 * sizeof(float))) {
      _mm_storeu_si16(output, vy);
      output += 2;
      vy = _mm_srli_epi32(vy, 16);
    }
    if (batch & (1 * sizeof(float))) {
      *output = (${XINT8_T}) _mm_extract_epi8(vy, 0);
    }
  }
}
