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

#include <tmmintrin.h>

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


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE]
$_MM_MULHI_EPX16 = {"QS8": "_mm_mulhi_epi16", "QU8": "_mm_mulhi_epu16"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vcvt_ukernel__sse2_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);

  $if DATATYPE == "QU8":
    const __m128i vmultiplier = _mm_set1_epi16(params->scalar.multiplier);
  $else:
    const __m128i vmultiplier = _mm_set1_epi16(-params->scalar.multiplier);
  const __m128i vbias = _mm_set1_epi32(
      (int32_t) (((uint32_t) (int32_t) params->scalar.output_zero_point) << 8) -
      (int32_t) params->scalar.multiplier * (int32_t) params->scalar.input_zero_point + 
      INT32_C(0x80));
  XNN_FORCE_REALIZATION(vmultiplier);
  XNN_FORCE_REALIZATION(vbias);
  $if DATATYPE == "QU8":
    const __m128i vzero = _mm_setzero_si128();
  $if BATCH_TILE > 8:
    for (; batch >= ${BATCH_TILE} * sizeof(${XINT8_T}); batch -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      const __m128i vx${ABC[0]} = _mm_loadu_si128((const __m128i*) input);
      $for N in range(1, SIMD_TILE):
        const __m128i vx${ABC[N]} = _mm_loadu_si128((const __m128i*) (input + ${N * 16}));
      input += ${BATCH_TILE};

      $for N in range(SIMD_TILE):
        $if DATATYPE == "QU8":
          const __m128i vextx${ABC[2*N]} = _mm_unpacklo_epi8(vx${ABC[N]}, vzero);
          const __m128i vextx${ABC[2*N+1]} = _mm_unpackhi_epi8(vx${ABC[N]}, vzero);
        $else:
          const __m128i vm${ABC[N]} = _mm_cmpgt_epi8(_mm_setzero_si128(), vx${ABC[N]});
          const __m128i vextx${ABC[2*N]} = _mm_unpacklo_epi8(vx${ABC[N]}, vm${ABC[N]});
          const __m128i vextx${ABC[2*N+1]} = _mm_unpackhi_epi8(vx${ABC[N]}, vm${ABC[N]});

      $for N in range(2*SIMD_TILE):
        const __m128i vprodlo${ABC[N]} = _mm_mullo_epi16(vextx${ABC[N]}, vmultiplier);
        const __m128i vprodhi${ABC[N]} = ${_MM_MULHI_EPX16}(vextx${ABC[N]}, vmultiplier);

      $for N in range(2*SIMD_TILE):
        __m128i vacc${ABC[2*N]} = _mm_unpacklo_epi16(vprodlo${ABC[N]}, vprodhi${ABC[N]});
        __m128i vacc${ABC[2*N+1]} = _mm_unpackhi_epi16(vprodlo${ABC[N]}, vprodhi${ABC[N]});

      $for N in range(4*SIMD_TILE):
        $if DATATYPE == "QU8":
          vacc${ABC[N]} = _mm_add_epi32(vacc${ABC[N]}, vbias);
        $else:
          vacc${ABC[N]} = _mm_sub_epi32(vbias, vacc${ABC[N]});

      $for N in range(4*SIMD_TILE):
        vacc${ABC[N]} = _mm_srai_epi32(vacc${ABC[N]}, 8);

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = _mm_packs_epi32(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});

      $for N in range(SIMD_TILE):
        const __m128i vy${ABC[N]} = ${_MM_PACKXS_EPI16}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});

      _mm_storeu_si128((__m128i*) output, vy${ABC[0]});
      $for N in range(1, SIMD_TILE):
        _mm_storeu_si128((__m128i*) (output + ${N * 16}), vy${ABC[N]});
      output += ${BATCH_TILE};
    }
  for (; batch >= 16 * sizeof(${XINT8_T}); batch -= 16 * sizeof(${XINT8_T})) {
    const __m128i vx = _mm_loadu_si128((const __m128i*) input);
    input += 16;

    $if DATATYPE == "QU8":
      const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vzero);
      const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vzero);
    $else:
      const __m128i vm = _mm_cmpgt_epi8(_mm_setzero_si128(), vx);
      const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vm);
      const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vm);

    const __m128i vprodlo_lo = _mm_mullo_epi16(vextx_lo, vmultiplier);
    const __m128i vprodlo_hi = _mm_mullo_epi16(vextx_hi, vmultiplier);
    const __m128i vprodhi_lo = ${_MM_MULHI_EPX16}(vextx_lo, vmultiplier);
    const __m128i vprodhi_hi = ${_MM_MULHI_EPX16}(vextx_hi, vmultiplier);

    __m128i vacc_ll = _mm_unpacklo_epi16(vprodlo_lo, vprodhi_lo);
    __m128i vacc_lh = _mm_unpackhi_epi16(vprodlo_lo, vprodhi_lo);
    __m128i vacc_hl = _mm_unpacklo_epi16(vprodlo_hi, vprodhi_hi);
    __m128i vacc_hh = _mm_unpackhi_epi16(vprodlo_hi, vprodhi_hi);

    $if DATATYPE == "QU8":
      vacc_ll = _mm_add_epi32(vacc_ll, vbias);
      vacc_lh = _mm_add_epi32(vacc_lh, vbias);
      vacc_hl = _mm_add_epi32(vacc_hl, vbias);
      vacc_hh = _mm_add_epi32(vacc_hh, vbias);
    $else:
      vacc_ll = _mm_sub_epi32(vbias, vacc_ll);
      vacc_lh = _mm_sub_epi32(vbias, vacc_lh);
      vacc_hl = _mm_sub_epi32(vbias, vacc_hl);
      vacc_hh = _mm_sub_epi32(vbias, vacc_hh);

    vacc_ll = _mm_srai_epi32(vacc_ll, 8);
    vacc_lh = _mm_srai_epi32(vacc_lh, 8);
    vacc_hl = _mm_srai_epi32(vacc_hl, 8);
    vacc_hh = _mm_srai_epi32(vacc_hh, 8);

    const __m128i vacc_lo = _mm_packs_epi32(vacc_ll, vacc_lh);
    const __m128i vacc_hi = _mm_packs_epi32(vacc_hl, vacc_hh);

    const __m128i vy = ${_MM_PACKXS_EPI16}(vacc_lo, 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}));

    const __m128i vx = _mm_loadu_si128((const __m128i*) input);

    $if DATATYPE == "QU8":
      const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vzero);
      const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vzero);
    $else:
      const __m128i vm = _mm_cmpgt_epi8(_mm_setzero_si128(), vx);
      const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vm);
      const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vm);

    const __m128i vprodlo_lo = _mm_mullo_epi16(vextx_lo, vmultiplier);
    const __m128i vprodlo_hi = _mm_mullo_epi16(vextx_hi, vmultiplier);
    const __m128i vprodhi_lo = ${_MM_MULHI_EPX16}(vextx_lo, vmultiplier);
    const __m128i vprodhi_hi = ${_MM_MULHI_EPX16}(vextx_hi, vmultiplier);

    __m128i vacc_ll = _mm_unpacklo_epi16(vprodlo_lo, vprodhi_lo);
    __m128i vacc_lh = _mm_unpackhi_epi16(vprodlo_lo, vprodhi_lo);
    __m128i vacc_hl = _mm_unpacklo_epi16(vprodlo_hi, vprodhi_hi);
    __m128i vacc_hh = _mm_unpackhi_epi16(vprodlo_hi, vprodhi_hi);

    $if DATATYPE == "QU8":
      vacc_ll = _mm_add_epi32(vacc_ll, vbias);
      vacc_lh = _mm_add_epi32(vacc_lh, vbias);
      vacc_hl = _mm_add_epi32(vacc_hl, vbias);
      vacc_hh = _mm_add_epi32(vacc_hh, vbias);
    $else:
      vacc_ll = _mm_sub_epi32(vbias, vacc_ll);
      vacc_lh = _mm_sub_epi32(vbias, vacc_lh);
      vacc_hl = _mm_sub_epi32(vbias, vacc_hl);
      vacc_hh = _mm_sub_epi32(vbias, vacc_hh);

    vacc_ll = _mm_srai_epi32(vacc_ll, 8);
    vacc_lh = _mm_srai_epi32(vacc_lh, 8);
    vacc_hl = _mm_srai_epi32(vacc_hl, 8);
    vacc_hh = _mm_srai_epi32(vacc_hh, 8);

    const __m128i vacc_lo = _mm_packs_epi32(vacc_ll, vacc_lh);
    const __m128i vacc_hi = _mm_packs_epi32(vacc_hl, vacc_hh);

    __m128i vy = ${_MM_PACKXS_EPI16}(vacc_lo, 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}))) {
      unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vy));
      vy = _mm_srli_epi64(vy, 32);
      output += 4;
    }
    uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
    if (batch & (2 * sizeof(${XINT8_T}))) {
      unaligned_store_u16(output, (uint16_t) vy_lo);
      vy_lo >>= 16;
      output += 2;
    }
    if (batch & (1 * sizeof(${XINT8_T}))) {
      *output = (${XINT8_T}) vy_lo;
    }
  }
}
