// 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 SSE in [2, 4]
$assert DATATYPE in ["QS8", "QU8"]
$assert BATCH_TILE % 8 == 0
$assert BATCH_TILE >= 8
$SSE_HEADER = {2: "emmintrin.h", 4: "smmintrin.h"}[SSE]
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <${SSE_HEADER}>

#include "xnnpack/common.h"
#include "xnnpack/unaligned.h"
#include "xnnpack/vcvt.h"


$ISA = {2: "sse2", 4: "sse41"}[SSE]
$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[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__${ISA}_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)]) XNN_OOB_READS
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input != NULL);
  assert(output != NULL);

  const __m128 vscale = _mm_set1_ps(params->scalar.scale);
  const __m128 voutput_max_less_zero_point = _mm_set1_ps((float) ((int32_t) ${OUTPUT_MAX} - (int32_t) params->scalar.output_zero_point));
  const __m128i voutput_zero_point = _mm_set1_epi16(params->scalar.output_zero_point);
  XNN_FORCE_REALIZATION(vscale);
  XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
  XNN_FORCE_REALIZATION(voutput_zero_point);

  $if BATCH_TILE > 8:
    for (; batch >= ${BATCH_TILE} * sizeof(float); batch -= ${BATCH_TILE} * sizeof(float)) {
      __m128 vx${ABC[0:4]} = _mm_loadu_ps(input);
      $for N in range(4, BATCH_TILE, 4):
        __m128 vx${ABC[N:N+4]} = _mm_loadu_ps(input + ${N});
      input += ${BATCH_TILE};

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

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

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

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

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

      $for N in range(0, BATCH_TILE, 16):
        $if N + 8 < BATCH_TILE:
          __m128i vy${ABC[N:N+16]} = ${_MM_PACKXS_EPI16}(vy${ABC[N:N+8]}, vy${ABC[N+8:N+16]});
        $else:
          vy${ABC[N:N+8]} = ${_MM_PACKXS_EPI16}(vy${ABC[N:N+8]}, vy${ABC[N:N+8]});

      _mm_storeu_si128((__m128i*) output, vy${ABC[0:16]});
      $for N in range(16, BATCH_TILE, 16):
        $if N + 8 < BATCH_TILE:
          _mm_storeu_si128((__m128i*) (output + ${N}), vy${ABC[N:N+16]});
        $else:
          _mm_storel_epi64((__m128i*) (output + ${N}), vy${ABC[N:N+8]});
      output += ${BATCH_TILE};
    }
  for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
    __m128 vx_lo = _mm_loadu_ps(input);
    __m128 vx_hi = _mm_loadu_ps(input + 4);
    input += 8;

    vx_lo = _mm_mul_ps(vx_lo, vscale);
    vx_hi = _mm_mul_ps(vx_hi, vscale);

    vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
    vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);

    const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
    const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);

    __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
    vy = _mm_adds_epi16(vy, voutput_zero_point);
    vy = ${_MM_PACKXS_EPI16}(vy, vy);

    _mm_storel_epi64((__m128i*) output, vy);
    output += 8;
  }
  if XNN_UNLIKELY(batch != 0) {
    __m128 vx_lo = _mm_loadu_ps(input);
    const float* x_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
    __m128 vx_hi = _mm_loadu_ps(x_hi);

    vx_lo = _mm_mul_ps(vx_lo, vscale);
    vx_hi = _mm_mul_ps(vx_hi, vscale);

    vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
    vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);

    const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
    const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);

    __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
    vy = _mm_adds_epi16(vy, voutput_zero_point);
    vy = ${_MM_PACKXS_EPI16}(vy, vy);

    if (batch & (4 * sizeof(float))) {
      unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vy));
      output += 4;
      vy = _mm_srli_epi64(vy, 32);
    }
    $if SSE == 4:
      if (batch & (2 * sizeof(float))) {
        unaligned_store_u16(output, (uint16_t) _mm_extract_epi16(vy, 0));
        output += 2;
        vy = _mm_srli_epi32(vy, 16);
      }
      if (batch & (1 * sizeof(float))) {
        *output = (${XINT8_T}) _mm_extract_epi8(vy, 0);
      }
    $else:
      {
        uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
        if (batch & (2 * sizeof(float))) {
          unaligned_store_u16(output, (uint16_t) vy_lo);
          output += 2;
          vy_lo >>= 16;
        }
        if (batch & (1 * sizeof(float))) {
          *output = (${XINT8_T}) vy_lo;
        }
      }
  }
}
