// Copyright 2019 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 ["F32", "QC8"]
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>
$if DATATYPE == "QC8" and not NR % 4 == 0:
  #include "xnnpack/unaligned.h"

#include "xnnpack/math.h"
#include "xnnpack/spmm.h"

$DATATYPE_SPEC = {"F32": "f32", "QC8": "f32_qc8w"}[DATATYPE]

void xnn_${DATATYPE_SPEC}_spmm_minmax_ukernel_${MR}x${NR}__scalar${"_x" + str(UNROLL) if UNROLL > 1 else ""}(
    size_t mc,
    size_t nc,
    const float* input,
    $if DATATYPE == "F32":
      const float* weights,
    $else:
      const void* weights,
    const int32_t* widx_dmap,
    const uint32_t* nidx_nnzmap,
    float* output,
    size_t output_stride,
    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(mc != 0);
  assert(mc % sizeof(float) == 0);
  assert(nc != 0);

  const float vmin = params->scalar.min;
  const float vmax = params->scalar.max;
  size_t output_decrement = output_stride * nc - ${MR} * sizeof(float);
  while (mc >= ${MR} * sizeof(float)) {
    $if DATATYPE == "F32":
      const float* w = weights;
    $else:
      const void* w = weights;
    const int32_t* dmap = widx_dmap;
    const uint32_t* nnzmap = nidx_nnzmap;
    size_t n = nc;
    while (n >= ${NR}) {
      uint32_t nnz = *nnzmap++;
      $if DATATYPE == "F32":
        $for N in range(NR):
          float vacc0x${N} = *w++;
      $else:
        $for N in range(NR):
          $if NR % 4 == 0:
            float vacc0x${N} = ((const float*)w)[${N}];
          $else:
            float vacc0x${N} = unaligned_indexed_load_f32(w, ${N});
        w = (const float*) w + ${NR};
      $for M in range(1, MR):
        $for N in range(NR):
          float vacc${ABC[M]}x${N} = vacc0x${N};
      if XNN_LIKELY(nnz != 0) {
        do {
          const intptr_t diff = *dmap++;
          $for M in range(MR):
            const float vi${ABC[M]} = input[${M}];
          input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
          $if DATATYPE == "F32":
            $for N in range(NR):
              const float vw${N} = *w++;
          $elif DATATYPE == "QC8":
            $for N in range(NR):
              const float vw${N} = (float) ((const int8_t*) w)[${N}];
            w = (const int8_t*) w + ${NR};
          $for N in range(NR):
            $for M in range(MR):
              vacc${ABC[M]}x${N} += vi${ABC[M]} * vw${N};
        } while (--nnz != 0);
      }
      $if DATATYPE == "QC8":
        $for N in range(NR):
          $if NR % 4 == 0:
            const float vscale${N} = ((const float*)w)[${N}];
          $else:
            const float vscale${N} = unaligned_indexed_load_f32(w, ${N});
        w = (const float*) w + ${NR};
        $for N in range(NR):
          $for M in range(MR):
            vacc${ABC[M]}x${N} *= vscale${N};
      $for N in range(NR):
        $for M in range(MR):
          float vout${ABC[M]}x${N} = math_min_f32(vacc${ABC[M]}x${N}, vmax);
      $for N in range(NR):
        $for M in range(MR):
          vout${ABC[M]}x${N} = math_max_f32(vout${ABC[M]}x${N}, vmin);
      $for M in range(MR):
        output[${M}] = vout${ABC[M]}x${N};
      $for N in range(NR):
        $for M in range(MR):
          output[${M}] = vout${ABC[M]}x${N};
        output = (float*restrict) ((uintptr_t) output + output_stride);
      n -= ${NR};
    }
    if XNN_UNLIKELY(n != 0) {
      do {
        uint32_t nnz = *nnzmap++;
        $if DATATYPE == "F32":
          float vacc0 = *w++;
        $else:
          $if NR % 4 == 0:
            float vacc0 = *((const float*)w);
          $else:
            float vacc0 = unaligned_load_f32(w);
          w = (const float*) w + 1;
        $for M in range(1, MR):
          float vacc${ABC[M]} = vacc0;
        if XNN_LIKELY(nnz != 0) {
          do {
            const intptr_t diff = *dmap++;
            $for M in range(MR):
              const float vi${ABC[M]} = input[${M}];
            input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
            $if DATATYPE == "F32":
              const float vw = *w++;
            $elif DATATYPE == "QC8":
              const float vw = (float) ((const int8_t*) w)[${N}];
              w = (const int8_t*) w + 1;
            $for M in range(MR):
              vacc${ABC[M]} += vi${ABC[M]} * vw;
          } while (--nnz != 0);
        }
        $if DATATYPE == "QC8":
          $if NR % 4 == 0:
            float vscale = *((const float*)w);
          $else:
            float vscale = unaligned_load_f32(w);
          w = (const float*) w + 1;
          $for M in range(MR):
            vacc${ABC[M]} *= vscale;
        $for M in range(MR):
          float vout${ABC[M]} = math_min_f32(vacc${ABC[M]}, vmax);
        $for M in range(MR):
          vout${ABC[M]} = math_max_f32(vout${ABC[M]}, vmin);
        $for M in range(MR):
          output[${M}] = vout${ABC[M]};
        output = (float*restrict) ((uintptr_t) output + output_stride);
        n -= 1;
      } while (n != 0);
    }
    output = (float*restrict) ((uintptr_t) output - output_decrement);
    input += ${MR};
    mc -= ${MR} * sizeof(float);
  }
  if XNN_UNLIKELY(mc != 0) {
    $for LOG2M in reversed(range((MR - 1).bit_length())):
      $SUBMR = 1 << LOG2M
      $if SUBMR * 2 >= MR:
        output_decrement += ${MR - SUBMR} * sizeof(float);
      $else:
        output_decrement += ${SUBMR} * sizeof(float);
      if (mc & (${SUBMR} * sizeof(float))) {
        $if DATATYPE == "F32":
          const float* w = weights;
        $else:
          const void* w = weights;
        const int32_t* dmap = widx_dmap;
        const uint32_t* nnzmap = nidx_nnzmap;
        size_t n = nc;
        while (n >= ${NR}) {
          uint32_t nnz = *nnzmap++;
          $if DATATYPE == "F32":
            $for N in range(NR):
              float vacc0x${N} = *w++;
          $else:
            $for N in range(NR):
              $if NR % 4 == 0:
                float vacc0x${N} = ((const float*)w)[${N}];
              $else:
                float vacc0x${N} = unaligned_indexed_load_f32(w, ${N});
            w = (const float*) w + ${NR};
          $for N in range(NR):
            $for M in range(1, SUBMR):
              float vacc${ABC[M]}x${N} = vacc0x${N};
          if XNN_LIKELY(nnz != 0) {
            do {
              const intptr_t diff = *dmap++;
              $for M in range(SUBMR):
                const float vi${ABC[M]} = input[${M}];
              input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
              $if DATATYPE == "F32":
                $for N in range(NR):
                  const float vw${N} = *w++;
              $elif DATATYPE == "QC8":
                $for N in range(NR):
                  const float vw${N} = (float) ((const int8_t*) w)[${N}];
                w = (const int8_t*) w + ${NR};
              $for N in range(NR):
                $for M in range(SUBMR):
                  vacc${ABC[M]}x${N} += vi${ABC[M]} * vw${N};
            } while (--nnz != 0);
          }
          $if DATATYPE == "QC8":
            $for N in range(NR):
              $if NR % 4 == 0:
                const float vscale${N} = ((const float*)w)[${N}];
              $else:
                const float vscale${N} = unaligned_indexed_load_f32(w, ${N});
            w = (const float*) w + ${NR};
            $for N in range(NR):
              $for M in range(SUBMR):
                vacc${ABC[M]}x${N} *= vscale${N};
          $for N in range(NR):
            $for M in range(SUBMR):
              float vout${ABC[M]}x${N} = math_min_f32(vacc${ABC[M]}x${N}, vmax);
          $for N in range(NR):
            $for M in range(SUBMR):
              vout${ABC[M]}x${N} = math_max_f32(vout${ABC[M]}x${N}, vmin);
          $for N in range(NR):
            $for M in range(SUBMR):
              output[${M}] = vout${ABC[M]}x${N};
            output = (float*restrict) ((uintptr_t) output + output_stride);
          n -= ${NR};
        }
        if XNN_UNLIKELY(n != 0) {
          do {
            uint32_t nnz = *nnzmap++;
            $if DATATYPE == "F32":
              float vacc0 = *w++;
            $else:
              $if NR % 4 == 0:
                float vacc0 = *((const float*)w);
              $else:
                float vacc0 = unaligned_load_f32(w);
              w = (const float*) w + 1;
            $for M in range(1, SUBMR):
              float vacc${ABC[M]} = vacc0;
            if XNN_LIKELY(nnz != 0) {
              do {
                const intptr_t diff = *dmap++;
                $for M in range(SUBMR):
                  const float vi${ABC[M]} = input[${M}];
                input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
                $if DATATYPE == "F32":
                  const float vw = *w++;
                $elif DATATYPE == "QC8":
                  const float vw = (float) ((const int8_t*) w)[${N}];
                  w = (const int8_t*) w + 1;
                $for M in range(SUBMR):
                  vacc${ABC[M]} += vi${ABC[M]} * vw;
              } while (--nnz != 0);
            }
            $if DATATYPE == "QC8":
              $if NR % 4 == 0:
                float vscale = *((const float*)w);
              $else:
                float vscale = unaligned_load_f32(w);
              w = (const float*) w + 1;
              $for M in range(SUBMR):
                vacc${ABC[M]} *= vscale;
            $for M in range(SUBMR):
              float vout${ABC[M]} = math_min_f32(vacc${ABC[M]}, vmax);
            $for M in range(SUBMR):
              vout${ABC[M]} = math_max_f32(vout${ABC[M]}, vmin);
            $for M in range(SUBMR):
              output[${M}] = vout${ABC[M]};
            output = (float*restrict) ((uintptr_t) output + output_stride);
            n -= 1;
          } while (n != 0);
        }
        output = (float*restrict) ((uintptr_t) output - output_decrement);
        input += ${SUBMR};
      }
  }
}
