// Copyright 2020 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 SSE == 4
$assert not XOP or AVX
$assert BATCH_TILE % 8 == 0
$assert BATCH_TILE >= 8
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

$if XOP:
  #if defined(__GNUC__) || defined(__clang__)
    #include <x86intrin.h>
  #else
    #include <immintrin.h>
    #include <ammintrin.h>
  #endif
$else:
  #include <immintrin.h>

#include <xnnpack/intrinsics-polyfill.h>
#include <xnnpack/unaligned.h>
#include <xnnpack/vadd.h>


$PARAMS_STRUCT = {"QS8": "sse4_mul32", "QU8": "sse4"}[DATATYPE]
$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$_MM_CVTEPX8_EPI32 = {"QS8": "_mm_cvtepi8_epi32", "QU8": "_mm_cvtepu8_epi32"}[DATATYPE]
$_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE]
$_MM_MIN_EPX8 = {"QS8": "_mm_min_epi8", "QU8": "_mm_min_epu8"}[DATATYPE]
$_MM_MAX_EPX8 = {"QS8": "_mm_max_epi8", "QU8": "_mm_max_epu8"}[DATATYPE]
$ISA = "xop" if XOP else "avx" if AVX else {4: "sse41"}[SSE]
void xnn_${DATATYPE.lower()}_vaddc_minmax_ukernel__${ISA}_mul32_ld32_x${BATCH_TILE}(
    size_t n,
    const ${XINT8_T}* input_a,
    const ${XINT8_T}* input_b,
    ${XINT8_T}* output,
    const union xnn_${DATATYPE.lower()}_add_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  const __m128i va_multiplier = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.a_multiplier);
  const __m128i vshift = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.shift);
  const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_zero_point);
  const __m128i voutput_min = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_min);
  const __m128i voutput_max = _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.output_max);

  __m128i vbias = _mm_cvtsi32_si128(params->${PARAMS_STRUCT}.b_multiplier[0] * (int32_t) *input_b);
  vbias = _mm_shuffle_epi32(vbias, _MM_SHUFFLE(0, 0, 0, 0));
  vbias = _mm_add_epi32(vbias, _mm_load_si128((const __m128i*) params->${PARAMS_STRUCT}.bias));
  for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
    const __m128i va${ABC[0:4]} = ${_MM_CVTEPX8_EPI32}(_mm_cvtsi32_si128((int) unaligned_load_s32(input_a)));
    $for N in range(4, BATCH_TILE, 4):
      const __m128i va${ABC[N:N+4]} = ${_MM_CVTEPX8_EPI32}(_mm_cvtsi32_si128((int) unaligned_load_s32(input_a + ${N})));
    input_a += ${BATCH_TILE};
    input_b += ${BATCH_TILE};

    $if XOP:
      $for N in range(0, BATCH_TILE, 4):
        __m128i vacc${ABC[N:N+4]} = _mm_macc_epi32(va${ABC[N:N+4]}, va_multiplier, vbias);
    $else:
      $for N in range(0, BATCH_TILE, 4):
        __m128i vacc${ABC[N:N+4]} = _mm_add_epi32(vbias, _mm_mullo_epi32(va${ABC[N:N+4]}, va_multiplier));

    $for N in range(0, BATCH_TILE, 4):
      vacc${ABC[N:N+4]} = _mm_sra_epi32(vacc${ABC[N:N+4]}, vshift);

    $for N in range(0, BATCH_TILE, 8):
      const __m128i vout${ABC[N:N+8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[N:N+4]}, vacc${ABC[N+4:N+8]}), voutput_zero_point);

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

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

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

    $if BATCH_TILE >= 16:
      _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]});
    $else:
      _mm_storel_epi64((__m128i*) output, vout${ABC[0:8]}${ABC[0:8]});
    $for N in range(16, BATCH_TILE, 16):
      $if N + 8 < BATCH_TILE:
        _mm_storeu_si128((__m128i*) (output + ${N}), vout${ABC[N:N+16]});
      $else:
        _mm_storel_epi64((__m128i*) (output + ${N}), vout${ABC[N:N+8]}${ABC[N:N+8]});
    output += ${BATCH_TILE};
  }
  if XNN_UNLIKELY(n != 0) {
    ${"do " if BATCH_TILE > 8 else ""}{
      const __m128i va${ABC[0:4]} = ${_MM_CVTEPX8_EPI32}(_mm_cvtsi32_si128((int) unaligned_load_s32(input_a)));
      const __m128i va${ABC[4:8]} = ${_MM_CVTEPX8_EPI32}(_mm_cvtsi32_si128((int) unaligned_load_s32(input_a + 4)));
      $if BATCH_TILE > 8:
        input_a += 8;

      $if XOP:
        __m128i vacc${ABC[0:4]} = _mm_macc_epi32(va${ABC[0:4]}, va_multiplier, vbias);
        __m128i vacc${ABC[4:8]} = _mm_macc_epi32(va${ABC[4:8]}, va_multiplier, vbias);
      $else:
        __m128i vacc${ABC[0:4]} = _mm_add_epi32(vbias, _mm_mullo_epi32(va${ABC[0:4]}, va_multiplier));
        __m128i vacc${ABC[4:8]} = _mm_add_epi32(vbias, _mm_mullo_epi32(va${ABC[4:8]}, va_multiplier));

      vacc${ABC[0:4]} = _mm_sra_epi32(vacc${ABC[0:4]}, vshift);
      vacc${ABC[4:8]} = _mm_sra_epi32(vacc${ABC[4:8]}, vshift);

      const __m128i vout${ABC[0:8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[0:4]}, vacc${ABC[4:8]}), voutput_zero_point);

      __m128i vout${ABC[0:8]}${ABC[0:8]} = ${_MM_PACKXS_EPI16}(vout${ABC[0:8]}, vout${ABC[0:8]});
      vout${ABC[0:8]}${ABC[0:8]} = ${_MM_MAX_EPX8}(vout${ABC[0:8]}${ABC[0:8]}, voutput_min);
      vout${ABC[0:8]}${ABC[0:8]} = ${_MM_MIN_EPX8}(vout${ABC[0:8]}${ABC[0:8]}, voutput_max);

      $if BATCH_TILE > 8:
        if XNN_LIKELY(n >= (8 * sizeof(${XINT8_T}))) {
          _mm_storel_epi64((__m128i*) output, vout${ABC[0:8]}${ABC[0:8]});
          output += 8;
          n -= 8 * sizeof(${XINT8_T});
        } else {
          if (n & (4 * sizeof(${XINT8_T}))) {
            unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]}));
            vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
            output += 4;
          }
          if (n & (2 * sizeof(${XINT8_T}))) {
            unaligned_store_u16(output, (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0));
            vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
            output += 2;
          }
          if (n & (1 * sizeof(${XINT8_T}))) {
            *output = (${XINT8_T}) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
          }
          n = 0;
        }
      $else:
        if (n & (4 * sizeof(${XINT8_T}))) {
          unaligned_store_u32(output, (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]}));
          vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
          output += 4;
        }
        if (n & (2 * sizeof(${XINT8_T}))) {
          unaligned_store_u16(output, (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0));
          vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
          output += 2;
        }
        if (n & (1 * sizeof(${XINT8_T}))) {
          *output = (${XINT8_T}) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
        }
    }${" while (n != 0);" if BATCH_TILE > 8 else ""}
  }
}
