// Copyright 2023 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.

$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$assert REQUANTIZATION == "FP32" or not REQUANTIZATION
$assert DATATYPE in ["QC8", "QS8", "QD8"]
#include <assert.h>

#include <immintrin.h>

#include "xnnpack/common.h"
#include "xnnpack/gemm.h"
#include "xnnpack/intrinsics-polyfill.h"
#include "xnnpack/math.h"
#include "xnnpack/unaligned.h"
$if PREFETCH:
  #include "xnnpack/prefetch.h"


$GFNI = 0
$DATATYPE_SPEC = {"QC8": "qs8_qc8w", "QD8": "qd8_f32_qc8w", "QS8": "qs8", "QU8": "qu8"}[DATATYPE]
$REQUANTIZATION_SPEC = "" if DATATYPE in ["QD8", "QC4"] else "_" + REQUANTIZATION.lower()
$PARAMS_STRUCT = REQUANTIZATION.lower() + "_scalar" if REQUANTIZATION else "scalar"
$PARAMS_TYPE = {"QC8": "union xnn_qs8_qc8w_conv_minmax_params", "QD8": "union xnn_f32_minmax_params", "QS8": "union xnn_qs8_conv_minmax_params", "QU8": "union xnn_qu8_conv_minmax_params"}[DATATYPE]
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
$OUT_T = "float" if DATATYPE in ["QD8", "QC4"] else XINT8_T
$_MM_PACKXS_EPI16 = "_mm_packus_epi16" if DATATYPE == "QU8" else "_mm_packs_epi16"
$_MM_MAX_EPX8 = "_mm_max_epu8" if DATATYPE == "QU8" else "_mm_max_epi8"
$_MM512_CVTXEPI32_EPI8 = "_mm512_cvtusepi32_epi8" if DATATYPE == "QU8" else "_mm512_cvtsepi32_epi8"
void xnn_${DATATYPE_SPEC}_igemm_minmax${REQUANTIZATION_SPEC}_ukernel_${MR}x16c4__avx512vnni${"_gfni" if GFNI else ""}${"_prfm" if PREFETCH else ""}(
    size_t mr,
    size_t nc,
    size_t kc,
    size_t ks,
    const ${XINT8_T}** restrict a,
    const void* restrict w,
    ${OUT_T}* restrict c,
    size_t cm_stride,
    size_t cn_stride,
    size_t a_offset,
    const ${XINT8_T}* zero,
    $if DATATYPE in ["QD8", "QC4"]:
      const int8_t* zero_data,
      const ${PARAMS_TYPE} params[restrict XNN_MIN_ELEMENTS(1)],
      const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
    $else:
      const ${PARAMS_TYPE} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(mr != 0);
  assert(mr <= ${MR});
  assert(nc != 0);
  assert(kc != 0);
  assert(kc % sizeof(${XINT8_T}) == 0);
  assert(a != NULL);
  assert(w != NULL);
  assert(c != NULL);

  kc = round_up_po2(kc, 4 * sizeof(${XINT8_T}));
  ${OUT_T}* c0 = c;
  $for M in range(1, MR):
    ${OUT_T}* c${M} = (${OUT_T}*) ((uintptr_t) c${M-1} + cm_stride);
    $if M % 2 == 0:
      if XNN_UNPREDICTABLE(mr <= ${M}) {
        c${M} = c${M-1};
      }
    $elif M + 1 == MR:
      if XNN_UNPREDICTABLE(mr != ${M+1}) {
        c${M} = c${M-1};
      }
    $else:
      if XNN_UNPREDICTABLE(mr < ${M+1}) {
        c${M} = c${M-1};
      }

  $if DATATYPE in ["QD8", "QC4"]:
    const __m512i vinput_zero_point = _mm512_set1_epi32((int) quantization_params->zero_point);
    const __m512 vinput_inv_scale = _mm512_set1_ps(quantization_params->inv_scale);
    const __m512 voutput_min = _mm512_set1_ps(params->scalar.min);
    const __m512 voutput_max = _mm512_set1_ps(params->scalar.max);
    XNN_FORCE_REALIZATION(vinput_zero_point);
    // XNN_FORCE_REALIZATION(vinput_inv_scale);
    // XNN_FORCE_REALIZATION(voutput_min);
    // XNN_FORCE_REALIZATION(voutput_max);
    $if DATATYPE == "QC4":
      const __m256i vmask = _mm256_set1_epi8(0xF0);
      XNN_FORCE_REALIZATION(vmask);
      $if GFNI:
        const __m512i vshl4 = _mm512_set1_epi64(0x01020408);
        XNN_FORCE_REALIZATION(vshl4);
  $else:
    const __m512i vsign_mask = _mm512_set1_epi8(0x80);
    XNN_FORCE_REALIZATION(vsign_mask);
    $if DATATYPE != "QC8":
      const __m512 vscale = _mm512_set1_ps(params->${PARAMS_STRUCT}.scale);
      // XNN_FORCE_REALIZATION(vscale);
    const __m512 voutput_max_less_zero_point = _mm512_set1_ps((int32_t) params->${PARAMS_STRUCT}.output_max - (int32_t) params->${PARAMS_STRUCT}.output_zero_point);
    const __m512i voutput_zero_point = _mm512_set1_epi32(params->${PARAMS_STRUCT}.output_zero_point);
    const __m128i voutput_min = _mm_set1_epi8(params->${PARAMS_STRUCT}.output_min);
    // XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
    // XNN_FORCE_REALIZATION(voutput_zero_point);
    // XNN_FORCE_REALIZATION(voutput_min);
  do {
    $if DATATYPE in ["QD8", "QC4"]:
      const __m512i vksum0123456789ABCDEF = _mm512_load_epi32(w);
      __m512i vacc0x0123456789ABCDEF = _mm512_mullo_epi32(vksum0123456789ABCDEF, vinput_zero_point);
    $else:
      __m512i vacc0x0123456789ABCDEF = _mm512_load_epi32(w);
    __m512i vacc1x0x0123456789ABCDEF = _mm512_setzero_epi32();
    $for M in range(1, MR):
      __m512i vacc${M}x0123456789ABCDEF = vacc0x0123456789ABCDEF;
      __m512i vacc1x${M}x0123456789ABCDEF = _mm512_setzero_epi32();
    w = (const int32_t*) w + 16;

    size_t p = ks;
    do {
      $for M in range(MR):
        const ${XINT8_T}* restrict a${M} = a[${M}];
        if XNN_UNPREDICTABLE(a${M} != zero) {
          a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} + a_offset);
        $if DATATYPE == "QD8":
          } else {
            a${M} = zero_data;
        }
      a += ${MR};

      size_t k = kc;
      while (k >= 8 * sizeof(int8_t)) {
        $for M in range(MR):
          $if DATATYPE in ["QD8", "QC4"]:
            const __m512i va${M}x0123 = _mm512_set1_epi32((int) unaligned_load_u32(a${M}));
            const __m512i va${M}x4567 = _mm512_set1_epi32((int) unaligned_load_u32(a${M} + 4));
          $else:
            const __m512i va${M}x0123 = _mm512_xor_epi32(_mm512_set1_epi32((int) unaligned_load_u32(a${M})), vsign_mask);
            const __m512i va${M}x4567 = _mm512_xor_epi32(_mm512_set1_epi32((int) unaligned_load_u32(a${M} + 4)), vsign_mask);
          a${M} += 8;

        $if DATATYPE == "QC4":
          const __m512i vbb0123456789ABCDEFx01234567 = _mm512_load_si512(w);
          $if GFNI:
            const __m512i vb0123456789ABCDEFx0123 = _mm512_gf2p8affine_epi64_epi8(vbb0123456789ABCDEFx01234567, vshl4, 0);
            const __m512i vb0123456789ABCDEFx4567 = _mm512_and_si512(vbb0123456789ABCDEFx01234567, vmask);
          $else:
            const __m512i vbs0123456789ABCDEFx0123 = _mm512_slli_epi32(vbb0123456789ABCDEFx01234567, 4);
            const __m512i vb0123456789ABCDEFx4567 = _mm512_and_si512(vbb0123456789ABCDEFx01234567, vmask);
            const __m512i vb0123456789ABCDEFx0123 = _mm512_and_si512(vbs0123456789ABCDEFx0123, vmask);
        $else:
          const __m512i vb0123456789ABCDEFx0123 = _mm512_load_si512(w);
          const __m512i vb0123456789ABCDEFx4567 = _mm512_load_si512((const ${XINT8_T}*) w + 64);
          $if PREFETCH:
            xnn_prefetch_to_l1((const ${XINT8_T}*) w + 896);

        $for M in range(MR):
          vacc${M}x0123456789ABCDEF = _mm512_dpbusd_epi32(vacc${M}x0123456789ABCDEF, va${M}x0123, vb0123456789ABCDEFx0123);
        $if PREFETCH:
          xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);
        $for M in range(MR):
          vacc1x${M}x0123456789ABCDEF = _mm512_dpbusd_epi32(vacc1x${M}x0123456789ABCDEF, va${M}x4567, vb0123456789ABCDEFx4567);

        $if DATATYPE == "QC4":
          w = (const ${XINT8_T}*) w + 64;
        $else:
          w = (const ${XINT8_T}*) w + 128;
        k -= 8 * sizeof(${XINT8_T});
      }

      if (k != 0) {
        $for M in range(MR):
          $if DATATYPE in ["QD8", "QC4"]:
            const __m512i va${M}x0123 = _mm512_set1_epi32((int) unaligned_load_u32(a${M}));
          $else:
            const __m512i va${M}x0123 = _mm512_xor_epi32(_mm512_set1_epi32((int) unaligned_load_u32(a${M})), vsign_mask);
          a${M} += 4;

        $if DATATYPE == "QC4":
          const __m512i vbb0123456789ABCDEF = _mm512_load_si512(w);
          $if GFNI:
            const __m512i vb0123456789ABCDEF = _mm512_gf2p8affine_epi64_epi8(vbb0123456789ABCDEF, vshl4, 0);
          $else:
            const __m512i vbs0123456789ABCDEF = _mm512_slli_epi32(vbb0123456789ABCDEF, 4);
            const __m512i vb0123456789ABCDEF = _mm512_and_si512(vbs0123456789ABCDEF, vmask);
        $else:
          const __m512i vb0123456789ABCDEF = _mm512_load_si512(w);
        $if PREFETCH:
          xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);

        $for M in range(MR):
          vacc${M}x0123456789ABCDEF = _mm512_dpbusd_epi32(vacc${M}x0123456789ABCDEF, va${M}x0123, vb0123456789ABCDEF);

        w = (const ${XINT8_T}*) w + 64;
        k -= 4 * sizeof(${XINT8_T});
      }

      p -= ${MR} * sizeof(void*);
    } while (p != 0);

    $for M in range(MR):
      vacc${M}x0123456789ABCDEF = _mm512_add_epi32(vacc${M}x0123456789ABCDEF, vacc1x${M}x0123456789ABCDEF);

    $if DATATYPE == "QC4":
      $for M in range(MR):
        vacc${M}x0123456789ABCDEF = _mm512_srai_epi32(vacc${M}x0123456789ABCDEF, 4);
    $for M in range(MR):
      __m512 vscaled${M}x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc${M}x0123456789ABCDEF);

    $if DATATYPE in ["QD8", "QC4"]:
      $for M in range(MR):
        vscaled${M}x0123456789ABCDEF = _mm512_mul_ps(vscaled${M}x0123456789ABCDEF, vinput_inv_scale);

      const __m512 vfilter_output_scale0123456789ABCDEF = _mm512_load_ps((const float*) w);
      const __m512 vbias0123456789ABCDEF = _mm512_load_ps((const float*) w + 16);
      w = (const float*) w + 32;

      $for M in range(MR):
        vscaled${M}x0123456789ABCDEF = _mm512_fmadd_ps(vscaled${M}x0123456789ABCDEF, vfilter_output_scale0123456789ABCDEF, vbias0123456789ABCDEF);

      $for M in range(MR):
        vscaled${M}x0123456789ABCDEF = _mm512_max_ps(vscaled${M}x0123456789ABCDEF, voutput_min);

      $for M in range(MR):
        vscaled${M}x0123456789ABCDEF = _mm512_min_ps(vscaled${M}x0123456789ABCDEF, voutput_max);

      if XNN_LIKELY(nc >= 16) {
        $for M in reversed(range(MR)):
          _mm512_storeu_ps(c${M}, vscaled${M}x0123456789ABCDEF);
          c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
        a = (const ${XINT8_T}**restrict) ((uintptr_t) a - ks);
        nc -= 16;
      } else {
        // Prepare mask for valid 32-bit elements (depends on nc).
        const __mmask16 vmask = _cvtu32_mask16((UINT32_C(1) << nc) - 1);
        $for M in reversed(range(MR)):
          _mm512_mask_storeu_ps(c${M}, vmask, vscaled${M}x0123456789ABCDEF);
        nc = 0;
      }
    $else:
      $if DATATYPE == "QC8":
        const __m512 vscale012345678ABCDEF = _mm512_load_ps(w);
        w = (const float*) w + 16;
        $for M in range(MR):
          vscaled${M}x0123456789ABCDEF = _mm512_mul_ps(vscaled${M}x0123456789ABCDEF, vscale012345678ABCDEF);
      $else:
        $for M in range(MR):
          vscaled${M}x0123456789ABCDEF = _mm512_mul_ps(vscaled${M}x0123456789ABCDEF, vscale);

      $for M in range(MR):
        vscaled${M}x0123456789ABCDEF = _mm512_min_ps(vscaled${M}x0123456789ABCDEF, voutput_max_less_zero_point);

      $for M in range(MR):
        vacc${M}x0123456789ABCDEF = _mm512_cvtps_epi32(vscaled${M}x0123456789ABCDEF);

      $for M in range(MR):
        vacc${M}x0123456789ABCDEF = _mm512_add_epi32(vacc${M}x0123456789ABCDEF, voutput_zero_point);

      $for M in range(MR):
        __m128i vout${M}x0123456789ABCDEF = ${_MM512_CVTXEPI32_EPI8}(vacc${M}x0123456789ABCDEF);

      $for M in range(MR):
        vout${M}x0123456789ABCDEF = ${_MM_MAX_EPX8}(vout${M}x0123456789ABCDEF, voutput_min);

      if (nc >= 16) {
        $for M in reversed(range(MR)):
          _mm_storeu_si128((__m128i*) c${M}, vout${M}x0123456789ABCDEF);
          c${M} = (${OUT_T}*) ((uintptr_t) c${M} + cn_stride);
        a = (const ${XINT8_T}**restrict) ((uintptr_t) a - ks);
        nc -= 16;
      } else {
        // Prepare mask for valid 8-bit elements (depends on nc).
        const __mmask16 vmask = _cvtu32_mask16((UINT32_C(1) << nc) - UINT32_C(1));
        $for M in reversed(range(MR)):
          _mm_mask_storeu_epi8(c${M}, vmask, vout${M}x0123456789ABCDEF);
        nc = 0;
      }
  } while (nc != 0);
}
