// 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]
$_MM256_MAX_EPX8 = {"QS8": "_mm256_max_epi8", "QU8": "_mm256_max_epu8"}[DATATYPE]
$_MM_MAX_EPX8 = {"QS8": "_mm_max_epi8", "QU8": "_mm_max_epu8"}[DATATYPE]
void xnn_f32_${DATATYPE.lower()}_vcvt_ukernel__avx2_x${BATCH_TILE}(
    size_t n,
    const float* x,
    ${XINT8_T}* y,
    const union xnn_f32_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(n != 0);
  assert(n % sizeof(float) == 0);
  assert(x != NULL);
  assert(y != NULL);

  const __m256 vscale = _mm256_load_ps(params->avx2.scale);
  const __m256 voutput_max_less_zero_point = _mm256_load_ps(params->avx2.output_max_less_zero_point);
  const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.output_zero_point);
  $if BATCH_TILE > 16:
    const __m256i vshuffle_mask = _mm256_load_si256((const __m256i*) params->avx2.shuffle_mask);
    const __m256i voutput_min = _mm256_load_si256((const __m256i*) params->avx2.output_min);
  $else:
    const __m128i voutput_min = _mm_load_si128((const __m128i*) params->avx2.output_min);

  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
    __m256 vx${ABC[0:2]} = _mm256_loadu_ps(x);
    $for N in range(2, SIMD_TILE, 2):
      __m256 vx${ABC[N:N+2]} = _mm256_loadu_ps(x + ${N * 4});
    x += ${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));

    $for N in range(0, SIMD_TILE, 8):
      $if N + 4 < SIMD_TILE:
        vy${ABC[N:N+8]} = ${_MM256_MAX_EPX8}(vy${ABC[N:N+8]}, voutput_min);
      $elif BATCH_TILE > 16:
        vy${ABC[N:N+4]} = ${_MM_MAX_EPX8}(vy${ABC[N:N+4]}, _mm256_castsi256_si128(voutput_min));
      $else:
        vy${ABC[N:N+4]} = ${_MM_MAX_EPX8}(vy${ABC[N:N+4]}, voutput_min);

    $if SIMD_TILE > 4:
      _mm256_storeu_si256((__m256i*) y, vy${ABC[0:8]});
    $else:
      _mm_storeu_si128((__m128i*) y, vy${ABC[0:4]});
    $for N in range(8, SIMD_TILE, 8):
      $if N + 4 < SIMD_TILE:
        _mm256_storeu_si256((__m256i*) (y + ${N * 4}), vy${ABC[N:N+8]});
      $else:
        _mm_storeu_si128((__m128i*) (y + ${N * 4}), vy${ABC[N:N+4]});
    y += ${BATCH_TILE};
  }
  for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
    __m256 vx = _mm256_loadu_ps(x);
    vx = _mm256_mul_ps(vx, vscale);
    vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
    x += 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);
    $if BATCH_TILE > 16:
      vy = ${_MM_MAX_EPX8}(vy, _mm256_castsi256_si128(voutput_min));
    $else:
      vy = ${_MM_MAX_EPX8}(vy, voutput_min);

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

    __m256 vx = _mm256_maskload_ps(x, 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_TILE > 16:
      vy = ${_MM_MAX_EPX8}(vy, _mm256_castsi256_si128(voutput_min));
    $else:
      vy = ${_MM_MAX_EPX8}(vy, voutput_min);

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