// Copyright 2024 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 NR % 32 == 0
#include <assert.h>
#include <hexagon_types.h>
#include <hexagon_protos.h>
#include <hvx_hexagon_protos.h>

#include "xnnpack/igemm.h"
#include "xnnpack/intrinsics-polyfill.h"

void xnn_f32_igemm_minmax_ukernel_${MR}x${NR}__hvx_broadcast(
    size_t mr,
    size_t nc,
    size_t kc,
    size_t ks,
    const float** restrict a,
    const float* restrict w,
    float* restrict c,
    size_t cm_stride,
    size_t cn_stride,
    size_t a_offset,
    const float* zero,
    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(mr != 0);
  assert(mr <= ${MR});
  assert(nc != 0);
  assert(kc != 0);
  assert(kc % sizeof(float) == 0);
  assert(ks != 0);
  assert(ks % (${MR} * sizeof(void*)) == 0);
  assert(a_offset % sizeof(float) == 0);
  assert(a != NULL);
  assert(w != NULL);
  assert(c != NULL);

  float* c0 = c;
  $for M in range(1, MR):
    float* c${M} = (float*) ((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};
      }

  do {
    HVX_Vector vacc0x0 = *((HVX_Vector *)(w));
    $for N in range(32, NR, 32):
      HVX_Vector vacc0x${N//32} = *((HVX_Vector *)(w + ${N}));
    $for M in range(1, MR):
      $for N in range(0, NR, 32):
        HVX_Vector vacc${M}x${N//32} = vacc0x${N//32};
    w += ${NR};

    size_t p = ks;
    do {
      $for M in range(MR):
        const float* restrict a${M} = a[${M}];
        assert(a${M} != NULL);
        if XNN_UNPREDICTABLE(a${M} != zero) {
          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
        }
      a += ${MR};

      size_t k = kc;
      do {
        const HVX_Vector vb0 = *((HVX_Vector *)(w));
        $for N in range(32, NR, 32):
          const HVX_Vector vb${N//32} = *((HVX_Vector *)(w + ${N}));
        w += ${NR};

        $for M in range(MR):
          const HVX_Vector va${M} =  Q6_V_vsplat_R(*(uint32_t *)a${M});
          a${M} += 1;

        $for M in range(MR):
          $for N in range(0, NR, 32):
              vacc${M}x${N//32} = Q6_Vsf_equals_Vqf32(Q6_Vqf32_vadd_Vqf32Vsf(Q6_Vqf32_vmpy_VsfVsf(va${M}, vb${N//32}), vacc${M}x${N//32}));
        k -= sizeof(float);
      } while (k != 0);
      p -= ${MR} * sizeof(void*);
    } while (p != 0);

    const HVX_Vector vmin = Q6_V_vsplat_R(params->scalar.min);
    $for N in range(0, NR, 32):
      $for M in range(MR):
        vacc${M}x${N//32} = Q6_Vw_vmax_VwVw(vmin, vacc${M}x${N//32});

    const HVX_Vector vmax = Q6_V_vsplat_R(params->scalar.max);
    $for N in range(0, NR, 32):
      $for M in range(MR):
        vacc${M}x${N//32} = Q6_Vw_vmin_VwVw(vmax, vacc${M}x${N//32});

    if XNN_LIKELY(nc >= ${NR}) {
      $for M in reversed(range(MR)):
        *((HVX_UVector *)(c${M})) = vacc${M}x0;
        $for N in range(32, NR, 32):
          *((HVX_UVector *)(c${M} + ${N})) = vacc${M}x${int(N/32)};
        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);

      a = (const float**restrict) ((uintptr_t) a - ks);
      nc -= ${NR};
    } else {
      $for LOG2N in reversed(range(NR.bit_length())):
        $if NR != 1 << LOG2N:
          $if LOG2N >= 5:
            if (nc & ${1 << LOG2N}) {
              $for M in reversed(range(MR)):
                *((HVX_UVector *)c${M}) = vacc${M}x0;
                $for N in range(32, 1 << LOG2N, 32):
                  *((HVX_UVector *)(c${M} + ${N})) = vacc${M}x${N//32};

              $for M in reversed(range(MR)):
                $for N in range(0, 1 << LOG2N, 32):
                  vacc${M}x${N//32} = vacc${M}x${(N + (1<<LOG2N))//32};

              $for M in reversed(range(MR)):
                c${M} += ${1 << LOG2N};
              nc ^= ${1 << LOG2N};
            }
          $else:
            $for M in reversed(range(MR)):
              vstu_variable_scalar((char*)c${M}, nc*sizeof(float), vacc${M}x0);
            $break
        $elif LOG2N == 5:
            $for M in reversed(range(MR)):
              vstu_variable_scalar((char*)c${M}, nc*sizeof(float), vacc${M}x0);
            $break  

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