// 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 ["QD8", "QS8", "QC4", "QB4", "QC8"]
$assert VARIANT in ["", "MADD"]
#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"


$#
$INDENT = 0
$_ = ""
$#
$def SET_INDENT(NEW_INDENT):
  $global INDENT
  $global _
  $INDENT=NEW_INDENT
  $_ = "  " * INDENT
$#
$def BEGIN():
  $SET_INDENT(INDENT + 1)
  $return '{'
$#
$def END():
  $SET_INDENT(INDENT - 1)
  $return _ + '}'
$#
$DATATYPE_SPEC = {"QC8": "qs8_qc8w", "QD8": "qd8_f32_qc8w", "QS8": "qs8", "QU8": "qu8", "QC4": "qd8_f32_qc4w", "QB4": "qd8_f32_qb4w"}[DATATYPE]
$REQUANTIZATION_SPEC = "" if DATATYPE in ["QD8", "QC4", "QB4"] 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", "QC4": "struct xnn_f32_qc4w_minmax_params", "QB4": "struct xnn_f32_qb4w_minmax_params"}[DATATYPE]
$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
$OUT_T = "float" if DATATYPE in ["QD8", "QC4", "QB4"] 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"
$_MM512_DPBUSD_EPI32 = "_mm512_dpbusd_epi32_madd" if VARIANT == "MADD" else "_mm512_dpbusd_epi32"
$ISA = "skx" if VARIANT == "MADD" else "vnni"
$_MM512_LOADX_SI512 = "_mm512_loadu_si512" if DATATYPE == "QB4" else "_mm512_load_si512"
void xnn_${DATATYPE_SPEC}_gemm_minmax${REQUANTIZATION_SPEC}_ukernel_${MR}x16c8__avx512${ISA}${"gfni" if GFNI else ""}${"_madd" if VARIANT == "MADD" else ""}${"_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", "QB4"]:
      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(int8_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};
      }
  $if DATATYPE == "QB4":
    size_t bl = params->scalar.blocksize;
    assert(bl != 0);
    assert(bl <= kc);
    assert(kc % bl == 0);
    assert(bl % 32 == 0);

  $if DATATYPE in ["QD8", "QC4", "QB4"]:
    $for M in range(MR):
      $if DATATYPE == "QB4":
        const __m512 vinput_zero_point${M} = _mm512_set1_ps((float) quantization_params[${M}].zero_point);
      $else:
        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 in ["QC4", "QB4"]:
      $if VARIANT == "MADD":
        const __m512i vmask = _mm512_set1_epi8(0x0F);
      $else:
        const __m512i vmask = _mm512_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);
      $for M in range(MR):
        __m512i vsum${M}x0123456789ABCDEF = _mm512_mullo_epi32(vksum0123456789ABCDEF, vinput_zero_point${M});
        __m512i vacc${M}x01234567 = _mm512_cvtepu32_epi64(_mm512_extracti64x4_epi64(vsum${M}x0123456789ABCDEF, 0));
        __m512i vacc${M}x89ABCDEF = _mm512_cvtepu32_epi64(_mm512_extracti64x4_epi64(vsum${M}x0123456789ABCDEF, 1));
    $elif DATATYPE == "QB4":
      const __m512 vksum0123456789ABCDEF = _mm512_loadu_ps(w);
      $for M in range(MR):
        __m512 vscaled${M}x0123456789ABCDEF = _mm512_mul_ps(vksum0123456789ABCDEF, vinput_zero_point${M});
    $else:
      __m512i vacc0x01234567 = _mm512_cvtepu32_epi64(_mm256_load_si256((const __m256i*) w));
      __m512i vacc0x89ABCDEF = _mm512_cvtepu32_epi64(_mm256_load_si256((const __m256i*) ((const int32_t*) w + 8)));
      $for M in range(1, MR):
        __m512i vacc${M}x01234567 = vacc0x01234567;
        __m512i vacc${M}x89ABCDEF = vacc0x89ABCDEF;
    $if DATATYPE != "QB4":
      $if MR < 4:
        $for M in range(MR):
          __m512i vacc1x${M}x01234567 = _mm512_setzero_epi32();
          __m512i vacc1x${M}x89ABCDEF = _mm512_setzero_epi32();
    w = (const int32_t*) w + 16;

    $if DATATYPE == "QB4":
      $SET_INDENT(1)
      for (size_t kb=0; kb < kc; kb+=bl) {
        $for M in range(MR):
          __m512i vacc${M}x01234567 = _mm512_setzero_epi32();
          __m512i vacc${M}x89ABCDEF = _mm512_setzero_epi32();
        $if MR < 4:
          $for M in range(MR):
            __m512i vacc1x${M}x01234567 = _mm512_setzero_epi32();
            __m512i vacc1x${M}x89ABCDEF = _mm512_setzero_epi32();
      ${_}size_t k = bl;
    $else:
      size_t k = kc;
    ${_}while (k >= 16 * sizeof(int8_t)) {
      $for M in range(MR):
        $if DATATYPE in ["QD8", "QC4", "QB4"]:
          ${_}const __m512i va${M}x01234567 = _mm512_set1_epi64((int64_t) unaligned_load_u64(a${M}));
          ${_}const __m512i va${M}x89ABCDEF = _mm512_set1_epi64((int64_t) unaligned_load_u64(a${M} + 8));
        $else:
          ${_}const __m512i va${M}x01234567 = _mm512_xor_epi64(_mm512_set1_epi64((int64_t) unaligned_load_u64(a${M})), vsign_mask);
          ${_}const __m512i va${M}x89ABCDEF = _mm512_xor_epi64(_mm512_set1_epi64((int64_t) unaligned_load_u64(a${M} + 8)), vsign_mask);
        ${_}a${M} += 16;

      $if DATATYPE in ["QC4", "QB4"]:
        ${_}const __m512i vbb01234567x01234567 = ${_MM512_LOADX_SI512}(w);
        ${_}const __m512i vbb89ABCDEFx01234567 = ${_MM512_LOADX_SI512}((const ${XINT8_T}*) w + 64);
        $if GFNI:
          ${_}const __m512i vb01234567x01234567 = _mm512_gf2p8affine_epi64_epi8(vbb01234567x01234567, vshl4, 0);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_gf2p8affine_epi64_epi8(vbb89ABCDEFx01234567, vshl4, 0);
          ${_}const __m512i vb01234567x89ABCDEF = _mm512_and_si512(vbb01234567x01234567, vmask);
          ${_}const __m512i vb89ABCDEFx89ABCDEF = _mm512_and_si512(vbb89ABCDEFx01234567, vmask);
        $elif VARIANT == "MADD":
          ${_}const __m512i vbs01234567x89ABCDEF = _mm512_srli_epi32(vbb01234567x01234567, 4);
          ${_}const __m512i vbs89ABCDEFx89ABCDEF = _mm512_srli_epi32(vbb89ABCDEFx01234567, 4);
          ${_}const __m512i vb01234567x01234567 = _mm512_and_si512(vbb01234567x01234567, vmask);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_and_si512(vbb89ABCDEFx01234567, vmask);
          ${_}const __m512i vb01234567x89ABCDEF = _mm512_and_si512(vbs01234567x89ABCDEF, vmask);
          ${_}const __m512i vb89ABCDEFx89ABCDEF = _mm512_and_si512(vbs89ABCDEFx89ABCDEF, vmask);
        $else:
          ${_}const __m512i vbs01234567x01234567 = _mm512_slli_epi32(vbb01234567x01234567, 4);
          ${_}const __m512i vbs89ABCDEFx01234567 = _mm512_slli_epi32(vbb89ABCDEFx01234567, 4);
          ${_}const __m512i vb01234567x89ABCDEF = _mm512_and_si512(vbb01234567x01234567, vmask);
          ${_}const __m512i vb89ABCDEFx89ABCDEF = _mm512_and_si512(vbb89ABCDEFx01234567, vmask);
          ${_}const __m512i vb01234567x01234567 = _mm512_and_si512(vbs01234567x01234567, vmask);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_and_si512(vbs89ABCDEFx01234567, vmask);
      $else:
        ${_}const __m512i vb01234567x01234567 = _mm512_load_si512(w);
        ${_}const __m512i vb89ABCDEFx01234567 = _mm512_load_si512((const ${XINT8_T}*) w + 64);
        ${_}const __m512i vb01234567x89ABCDEF = _mm512_load_si512((const ${XINT8_T}*) w + 128);
        ${_}const __m512i vb89ABCDEFx89ABCDEF = _mm512_load_si512((const ${XINT8_T}*) w + 192);
        $if PREFETCH:
          ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 768);
          ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 832);

      $for M in range(MR):
        ${_}vacc${M}x01234567 = ${_MM512_DPBUSD_EPI32}(vacc${M}x01234567, va${M}x01234567, vb01234567x01234567);
        ${_}vacc${M}x89ABCDEF = ${_MM512_DPBUSD_EPI32}(vacc${M}x89ABCDEF, va${M}x01234567, vb89ABCDEFx01234567);
      $if PREFETCH:
        ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 896);
        ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);
      $for M in range(MR):
        $if MR < 4:
          ${_}vacc1x${M}x01234567 = ${_MM512_DPBUSD_EPI32}(vacc1x${M}x01234567, va${M}x89ABCDEF, vb01234567x89ABCDEF);
          ${_}vacc1x${M}x89ABCDEF = ${_MM512_DPBUSD_EPI32}(vacc1x${M}x89ABCDEF, va${M}x89ABCDEF, vb89ABCDEFx89ABCDEF);
        $else:
          ${_}vacc${M}x01234567 = ${_MM512_DPBUSD_EPI32}(vacc${M}x01234567, va${M}x89ABCDEF, vb01234567x89ABCDEF);
          ${_}vacc${M}x89ABCDEF = ${_MM512_DPBUSD_EPI32}(vacc${M}x89ABCDEF, va${M}x89ABCDEF, vb89ABCDEFx89ABCDEF);

      $if DATATYPE in ["QC4", "QB4"]:
        ${_}w = (const ${XINT8_T}*) w + 128;
      $else:
        ${_}w = (const ${XINT8_T}*) w + 256;
      ${_}k -= 16 * sizeof(${XINT8_T});
    ${_}}

    ${_}if (k != 0) {
      $for M in range(MR):
        $if DATATYPE in ["QD8", "QC4", "QB4"]:
          ${_}const __m512i va${M}x01234567 = _mm512_set1_epi64((int64_t) unaligned_load_u64(a${M}));
        $else:
          ${_}const __m512i va${M}x01234567 = _mm512_xor_epi64(_mm512_set1_epi64((int64_t) unaligned_load_u64(a${M})), vsign_mask);
        ${_}a${M} += 8;

      $if DATATYPE in ["QC4", "QB4"]:
        ${_}const __m512i vbb01234567x01234567 = _mm512_load_si512(w);
        ${_}const __m512i vbb89ABCDEFx01234567 = _mm512_load_si512((const ${XINT8_T}*) w + 64);
        $if GFNI:
          ${_}const __m512i vb01234567x01234567 = _mm512_gf2p8affine_epi64_epi8(vbb01234567x01234567, vshl4, 0);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_gf2p8affine_epi64_epi8(vbb89ABCDEFx01234567, vshl4, 0);
        $elif VARIANT == "MADD":
          ${_}const __m512i vb01234567x01234567 = _mm512_and_si512(vbb01234567x01234567, vmask);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_and_si512(vbb89ABCDEFx01234567, vmask);
        $else:
          ${_}const __m512i vb01234567x01234567 = _mm512_slli_epi32(vbb01234567x01234567, 4);
          ${_}const __m512i vb89ABCDEFx01234567 = _mm512_slli_epi32(vbb89ABCDEFx01234567, 4);
      $else:
        ${_}const __m512i vb01234567x01234567 = _mm512_load_si512(w);
        ${_}const __m512i vb89ABCDEFx01234567 = _mm512_load_si512((const ${XINT8_T}*) w + 64);

      $for M in range(MR):
        ${_}vacc${M}x01234567 = ${_MM512_DPBUSD_EPI32}(vacc${M}x01234567, va${M}x01234567, vb01234567x01234567);
        ${_}vacc${M}x89ABCDEF = ${_MM512_DPBUSD_EPI32}(vacc${M}x89ABCDEF, va${M}x01234567, vb89ABCDEFx01234567);
      $if PREFETCH:
        ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 896);
        ${_}xnn_prefetch_to_l1((const ${XINT8_T}*) w + 960);

      ${_}w = (const ${XINT8_T}*) w + 128;
      ${_}k -= 8 * sizeof(${XINT8_T});
    ${_}}
    $if DATATYPE == "QB4":
      ${_}const __m512 vfilter_output_scale0123456789ABCDEF = _mm512_castsi512_ps(_mm512_slli_epi32(
      ${_}      _mm512_cvtepu16_epi32(_mm256_load_si256((const __m256i*) w)), 16));
      ${_}w = (const uint16_t*) w + 16;
    $if MR < 4:
      $for M in range(MR):
        ${_}vacc${M}x01234567 = _mm512_add_epi32(vacc${M}x01234567, vacc1x${M}x01234567);
        ${_}vacc${M}x89ABCDEF = _mm512_add_epi32(vacc${M}x89ABCDEF, vacc1x${M}x89ABCDEF);

    ${_}// Add adjacent pairs
    ${_}const __m512i vidx = _mm512_set_epi32(30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0);
    $for M in range(MR):
      ${_}const __m512i vsum${M}x01234567 = _mm512_add_epi32(vacc${M}x01234567, _mm512_srli_epi64(vacc${M}x01234567, 32));
      ${_}const __m512i vsum${M}x89ABCDEF = _mm512_add_epi32(vacc${M}x89ABCDEF, _mm512_srli_epi64(vacc${M}x89ABCDEF, 32));
      ${_}__m512i vacc${M}x0123456789ABCDEF = _mm512_permutex2var_epi32(vsum${M}x01234567, vidx, vsum${M}x89ABCDEF);
    $if DATATYPE == "QB4":
      $for M in range(MR):
        ${_}__m512 vf${M}x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc${M}x0123456789ABCDEF);

    $if DATATYPE == "QC4" and VARIANT != "MADD":
      $for M in range(MR):
        ${_}vacc${M}x0123456789ABCDEF = _mm512_srai_epi32(vacc${M}x0123456789ABCDEF, 4);
    $if DATATYPE != "QB4":
      $for M in range(MR):
        ${_}__m512 vscaled${M}x0123456789ABCDEF = _mm512_cvtepi32_ps(vacc${M}x0123456789ABCDEF);
    $else:
      $for M in range(MR):
        ${_}vscaled${M}x0123456789ABCDEF = _mm512_fmadd_ps(vf${M}x0123456789ABCDEF, vfilter_output_scale0123456789ABCDEF, vscaled${M}x0123456789ABCDEF);
      }

    $if DATATYPE in ["QD8", "QC4", "QB4"]:
      $if DATATYPE != "QB4":
        $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;
      $else:
        $for M in range(MR):
          vscaled${M}x0123456789ABCDEF = _mm512_mul_ps(vscaled${M}x0123456789ABCDEF, _mm512_set1_ps(quantization_params[${M}].inv_scale));

        const __m512 vbias0123456789ABCDEF = _mm512_loadu_ps((const float*) w);
        w = (const float*) w + 16;

      $for M in range(MR):
        $if DATATYPE != "QB4":
          vscaled${M}x0123456789ABCDEF = _mm512_fmadd_ps(vscaled${M}x0123456789ABCDEF, vfilter_output_scale0123456789ABCDEF, vbias0123456789ABCDEF);
        $else:
          vscaled${M}x0123456789ABCDEF = _mm512_add_ps(vscaled${M}x0123456789ABCDEF, 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);

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