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

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

#include <immintrin.h>

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

$DATATYPE_SPEC = {"QC8": "qs8_qc8w", "QD8": "qd8_f32_qc8w", "QC4": "qd8_f32_qc4w", "QS8": "qs8", "QU8": "qu8"}[DATATYPE]
$REQUANTIZATION_SPEC = "" if DATATYPE in ["QD8", "QC4"] else "_" + REQUANTIZATION.lower()
$PARAMS_STRUCT = REQUANTIZATION.lower() + "_scalar"
$PARAMS_TYPE = {"QC8": "union xnn_qs8_qc8w_conv_minmax_params", "QD8": "union xnn_f32_minmax_params", "QC4": "struct xnn_f32_qc4w_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_MAX_EPX8 = "_mm_max_epu8" if DATATYPE == "QU8" else "_mm_max_epi8"
$_MM_CVTEPX8_EPI16 = "_mm_cvtepu8_epi16" if DATATYPE == "QU8" else "_mm_cvtepi8_epi16"
void xnn_${DATATYPE_SPEC}_gemm_minmax${REQUANTIZATION_SPEC}_ukernel_${MR}x16c8__avx512skx${"_prfm" if PREFETCH else ""}(
    size_t mr,
    size_t nc,
    size_t kc,
    const ${XINT8_T}* restrict a,
    size_t a_stride,
    const void* restrict w,
    ${OUT_T}* restrict c,
    size_t cm_stride,
    size_t cn_stride,
    $if DATATYPE in ["QD8", "QC4"]:
      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, 8 * sizeof(${XINT8_T}));
  const ${XINT8_T}* a0 = a;
  ${OUT_T}* c0 = c;
  $for M in range(1, MR):
    const ${XINT8_T}* a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M-1} + a_stride);
    ${OUT_T}* c${M} = (${OUT_T}*) ((uintptr_t) c${M-1} + cm_stride);
    $if M % 2 == 0:
      if XNN_UNPREDICTABLE(mr <= ${M}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }
    $elif M + 1 == MR:
      if XNN_UNPREDICTABLE(mr != ${M+1}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }
    $else:
      if XNN_UNPREDICTABLE(mr < ${M+1}) {
        a${M} = a${M-1};
        c${M} = c${M-1};
      }

  const __mmask16 vbias_mask = _cvtu32_mask16(0x1111);
  $if DATATYPE in ["QD8", "QC4"]:
    $for M in range(MR):
      const __m512i vinput_zero_point${M} = _mm512_set1_epi32((int) quantization_params[${M}].zero_point);
    const __m512 voutput_min = _mm512_set1_ps(params->scalar.min);
    const __m512 voutput_max = _mm512_set1_ps(params->scalar.max);
    // XNN_FORCE_REALIZATION(voutput_min);
    // XNN_FORCE_REALIZATION(voutput_max);
    $if DATATYPE == "QC4":
      const __m256i vmask = _mm256_set1_epi8(0xF0);
      XNN_FORCE_REALIZATION(vmask);
  $else:
    $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);
    $if DATATYPE == "QU8":
      const __m512i vb_zero_point = _mm512_set1_epi16(params->${PARAMS_STRUCT}.kernel_zero_point);
      // XNN_FORCE_REALIZATION(vb_zero_point);

  do {
    $if DATATYPE in ["QD8", "QC4"]:
      const __m512i vksum0123 = _mm512_maskz_expandloadu_epi32(vbias_mask, w);
      $for N in range(4, 16, 4):
        const __m512i vksum${ABC[N:N+4]} = _mm512_maskz_expandloadu_epi32(vbias_mask, (const int32_t*) w + ${N});

      $for M in range(MR):
        $for N in range(0, 16, 4):
          __m512i vacc${M}x${ABC[N:N+4]} = _mm512_mullo_epi32(vksum${ABC[N:N+4]}, vinput_zero_point${M});
    $else:
      __m512i vacc0x0123 = _mm512_maskz_expandloadu_epi32(vbias_mask, w);
      $for N in range(4, 16, 4):
        __m512i vacc0x${ABC[N:N+4]} = _mm512_maskz_expandloadu_epi32(vbias_mask, (const int32_t*) w + ${N});
      $for M in range(1, MR):
        $for N in range(0, 16, 4):
          __m512i vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
    w = (const int32_t*) w + 16;

    size_t k = kc;
    $if DATATYPE == "QC4":
      while (k >= 16 * sizeof(${XINT8_T})) {
        $for M in range(MR):
          __m512i va${M} = _mm512_broadcast_i32x4(${_MM_CVTEPX8_EPI16}(_mm_loadl_epi64((const __m128i*) a${M})));
          a${M} += 8;

        $for N in range(0, 16, 4):
          __m256i vbb${ABC[N:N+4]} = _mm256_load_si256((const __m256i*) ((const int8_t*) w + ${N * 8}));
          __m256i vbs${ABC[N:N+4]} = _mm256_slli_epi32(vbb${ABC[N:N+4]}, 4);
          __m256i vbm${ABC[N:N+4]} = _mm256_and_si256(vbs${ABC[N:N+4]}, vmask);
          __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(vbm${ABC[N:N+4]});
          $if PREFETCH and N == 0:
            xnn_prefetch_to_l1((const ${XINT8_T}*) w + 896);
          $if PREFETCH and N == 8:
            xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);

          $for M in range(MR):
            vacc${M}x${ABC[N:N+4]} = _mm512_add_epi32(vacc${M}x${ABC[N:N+4]}, _mm512_madd_epi16(va${M}, vb${ABC[N:N+4]}));

        $for M in range(MR):
          va${M} = _mm512_broadcast_i32x4(${_MM_CVTEPX8_EPI16}(_mm_loadl_epi64((const __m128i*) a${M})));
          a${M} += 8;

        $for N in range(0, 16, 4):
          vbm${ABC[N:N+4]} = _mm256_and_si256(vbb${ABC[N:N+4]}, vmask);
          vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(vbm${ABC[N:N+4]});

          $for M in range(MR):
            vacc${M}x${ABC[N:N+4]} = _mm512_add_epi32(vacc${M}x${ABC[N:N+4]}, _mm512_madd_epi16(va${M}, vb${ABC[N:N+4]}));

        w = (const ${XINT8_T}*) w + 128;
        k -= 16 * sizeof(${XINT8_T});
      }

    while (k >= 8 * sizeof(${XINT8_T})) {
      $for M in range(MR):
        const __m512i va${M} = _mm512_broadcast_i32x4(${_MM_CVTEPX8_EPI16}(_mm_loadl_epi64((const __m128i*) a${M})));
        a${M} += 8;

      $for N in range(0, 16, 4):
        $if DATATYPE == "QC4":
          const __m256i vbb${ABC[N:N+4]} = _mm256_load_si256((const __m256i*) ((const int8_t*) w + ${N * 8}));
          const __m256i vbs${ABC[N:N+4]} = _mm256_slli_epi32(vbb${ABC[N:N+4]}, 4);
          const __m256i vbm${ABC[N:N+4]} = _mm256_and_si256(vbs${ABC[N:N+4]}, vmask);
          const __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(vbm${ABC[N:N+4]});
        $elif DATATYPE == "QU8":
          $if N == 0:
            const __m512i vb${ABC[N:N+4]} = _mm512_sub_epi16(_mm512_cvtepu8_epi16(_mm256_load_si256((const __m256i*) w)), vb_zero_point);
          $else:
            const __m512i vb${ABC[N:N+4]} = _mm512_sub_epi16(_mm512_cvtepu8_epi16(_mm256_load_si256((const __m256i*) ((const ${XINT8_T}*) w + ${N * 8}))), vb_zero_point);
        $else:
          $if N == 0:
            const __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(_mm256_load_si256((const __m256i*) w));
          $else:
            const __m512i vb${ABC[N:N+4]} = _mm512_cvtepi8_epi16(_mm256_load_si256((const __m256i*) ((const ${XINT8_T}*) w + ${N * 8})));
        $if PREFETCH and N == 0:
          xnn_prefetch_to_l1((const ${XINT8_T}*) w + 896);
        $if PREFETCH and N == 8:
          xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);

        $for M in range(MR):
          vacc${M}x${ABC[N:N+4]} = _mm512_add_epi32(vacc${M}x${ABC[N:N+4]}, _mm512_madd_epi16(va${M}, vb${ABC[N:N+4]}));

      w = (const ${XINT8_T}*) w + 128;
      k -= 8 * sizeof(${XINT8_T});
    }

    // Add 4 adjacent sums
    $for M in range(MR):
      const __m512i vacc${M}x04152637 = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x0123, vacc${M}x4567), _mm512_unpackhi_epi32(vacc${M}x0123, vacc${M}x4567));
      const __m512i vacc${M}x8C9DAEBF = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x89AB, vacc${M}xCDEF), _mm512_unpackhi_epi32(vacc${M}x89AB, vacc${M}xCDEF));

    $for M in range(MR):
      const __m512i vacc${M}x084C195D2A6E3B7F = _mm512_add_epi32(_mm512_unpacklo_epi32(vacc${M}x04152637, vacc${M}x8C9DAEBF), _mm512_unpackhi_epi32(vacc${M}x04152637, vacc${M}x8C9DAEBF));

    const __m512i vidx = _mm512_set_epi32(15, 11, 7, 3, 13, 9, 5, 1, 14, 10, 6, 2, 12, 8, 4, 0);
    $for M in range(MR):
      __m512i vacc${M}x0123456789ABCDEF = _mm512_permutexvar_epi32(vidx, vacc${M}x084C195D2A6E3B7F);

    $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, _mm512_set1_ps(quantization_params[${M}].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 range(MR):
          _mm512_storeu_ps(c${M}, vscaled${M}x0123456789ABCDEF);

        $for M in range(MR):
          a${M} = (const int8_t*) ((uintptr_t) a${M} - kc);

        $for M in range(MR):
          c${M} = (float*) ((uintptr_t) c${M} + cn_stride);

        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 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);

      $if DATATYPE == "QU8":
        $for M in range(MR):
          __m256i vaccph${M}x0123456789ABCDEF = _mm512_cvtsepi32_epi16(vacc${M}x0123456789ABCDEF);

        $for M in range(MR):
          __m128i vout${M}x0123456789ABCDEF = _mm_packus_epi16(_mm256_castsi256_si128(vaccph${M}x0123456789ABCDEF), _mm256_extracti128_si256(vaccph${M}x0123456789ABCDEF, 1));
      $else:
        $for M in range(MR):
          __m128i vout${M}x0123456789ABCDEF = _mm512_cvtsepi32_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 range(MR):
          _mm_storeu_si128((__m128i*) c${M}, vout${M}x0123456789ABCDEF);
          c${M} = (${OUT_T}*) ((uintptr_t) c${M} + cn_stride);
          a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - kc);

        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 range(MR):
          _mm_mask_storeu_epi8(c${M}, vmask, vout${M}x0123456789ABCDEF);

        nc = 0;
      }
  } while (nc != 0);
}
