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

#include <emmintrin.h>

#include "xnnpack/common.h"
#include "xnnpack/intrinsics-polyfill.h"
#include "xnnpack/vcvt.h"


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$_MM_CVTEPX8_EPI32 = {"QS8": "_mm_cvtepi8_epi32", "QU8": "_mm_cvtepu8_epi32"}[DATATYPE]
void xnn_${DATATYPE.lower()}_f32_vcvt_ukernel__sse2_u${BATCH_TILE}(
    size_t batch,
    const ${XINT8_T}* input,
    float* output,
    const struct xnn_${DATATYPE.lower()}_f32_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);

  $if DATATYPE == "QS8":
    const __m128i vsign_mask = _mm_set1_epi8(UINT8_C(0x80));
    XNN_FORCE_REALIZATION(vsign_mask);
  const __m128i vmagic_exp = _mm_set1_epi16(UINT16_C(0x4B00));
  $if DATATYPE == "QS8":
    const __m128 vmagic_bias = _mm_set1_ps((float) (INT32_C(0x00800080) + (int32_t) params->scalar.zero_point));
  $else:
    const __m128 vmagic_bias = _mm_set1_ps((float) (INT32_C(0x00800000) + (int32_t) params->scalar.zero_point));
  const __m128 vscale = _mm_set1_ps(params->scalar.scale);
  const __m128i vzero = _mm_setzero_si128();
  XNN_FORCE_REALIZATION(vmagic_exp);
  XNN_FORCE_REALIZATION(vmagic_bias);
  XNN_FORCE_REALIZATION(vscale);
  $if BATCH_TILE > 8:
    for (; batch >= ${BATCH_TILE} * sizeof(${XINT8_T}); batch -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      __m128i vx${ABC[0:8]} = _mm_loadl_epi64((const __m128i*) input);
      $for N in range(8, BATCH_TILE, 8):
        __m128i vx${ABC[N:N+8]} = _mm_loadl_epi64((const __m128i*) (input + ${N}));
      input += ${BATCH_TILE};

      $if DATATYPE == "QS8":
        $for N in range(0, BATCH_TILE, 8):
          vx${ABC[N:N+8]} = _mm_xor_si128(vx${ABC[N:N+8]}, vsign_mask);

      $for N in range(0, BATCH_TILE, 8):
        vx${ABC[N:N+8]} = _mm_unpacklo_epi8(vx${ABC[N:N+8]}, vzero);

      $for N in range(0, BATCH_TILE, 8):
        __m128 vy${ABC[N:N+4]} = _mm_castsi128_ps(_mm_unpacklo_epi16(vx${ABC[N:N+8]}, vmagic_exp));
        __m128 vy${ABC[N+4:N+8]} = _mm_castsi128_ps(_mm_unpackhi_epi16(vx${ABC[N:N+8]}, vmagic_exp));

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

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

      _mm_storeu_ps(output, vy${ABC[0:4]});
      $for N in range(4, BATCH_TILE, 4):
        _mm_storeu_ps(output + ${N}, vy${ABC[N:N+4]});
      output += ${BATCH_TILE};
    }
  for (; batch >= 8 * sizeof(${XINT8_T}); batch -= 8 * sizeof(${XINT8_T})) {
    __m128i vx = _mm_loadl_epi64((const __m128i*) input);
    $if DATATYPE == "QS8":
      vx = _mm_xor_si128(vx, vsign_mask);
    vx = _mm_unpacklo_epi8(vx, vzero);
    input += 8;

    __m128 vy_lo = _mm_castsi128_ps(_mm_unpacklo_epi16(vx, vmagic_exp));
    __m128 vy_hi = _mm_castsi128_ps(_mm_unpackhi_epi16(vx, vmagic_exp));

    vy_lo = _mm_sub_ps(vy_lo, vmagic_bias);
    vy_hi = _mm_sub_ps(vy_hi, vmagic_bias);

    vy_lo = _mm_mul_ps(vy_lo, vscale);
    vy_hi = _mm_mul_ps(vy_hi, vscale);

    _mm_storeu_ps(output, vy_lo);
    _mm_storeu_ps(output + 4, vy_hi);
    output += 8;
  }
  if XNN_UNLIKELY(batch != 0) {
    assert(batch >= 1 * sizeof(${XINT8_T}));
    assert(batch <= 7 * sizeof(${XINT8_T}));

    __m128i vx = _mm_loadl_epi64((const __m128i*) input);
    $if DATATYPE == "QS8":
      vx = _mm_xor_si128(vx, vsign_mask);
    vx = _mm_unpacklo_epi8(vx, vzero);

    __m128 vy = _mm_castsi128_ps(_mm_unpacklo_epi16(vx, vmagic_exp));
    vy = _mm_sub_ps(vy, vmagic_bias);
    vy = _mm_mul_ps(vy, vscale);

    if (batch & (4 * sizeof(${XINT8_T}))) {
      _mm_storeu_ps(output, vy);
      vy = _mm_castsi128_ps(_mm_unpackhi_epi16(vx, vmagic_exp));
      vy = _mm_sub_ps(vy, vmagic_bias);
      vy = _mm_mul_ps(vy, vscale);
      output += 4;
    }
    if (batch & (2 * sizeof(${XINT8_T}))) {
      _mm_storel_pi((__m64*) output, vy);
      vy = _mm_movehl_ps(vy, vy);
      output += 2;
    }
    if (batch & (1 * sizeof(${XINT8_T}))) {
      _mm_store_ss(output, vy);
    }
  }
}
