// 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 CHANNEL_TILE >= 1
$assert ROW_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

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


$MIN_F32 = "__builtin_wasm_min_f32" if WASM else "math_min_f32"
$MAX_F32 = "__builtin_wasm_max_f32" if WASM else "math_max_f32"
void xnn_f32_vmulcaddc_minmax_ukernel_c${CHANNEL_TILE}__${"wasm" if WASM else "scalar"}_${ROW_TILE}x(
    size_t rows,
    size_t channels,
    const float* restrict input,
    size_t input_stride,
    const float* restrict weights,
    float* restrict output,
    size_t output_stride,
    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(rows != 0);
  assert(channels != 0);
  assert(channels % sizeof(float) == 0);

  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
  const size_t output_increment = output_stride * ${ROW_TILE} - channels;

  const float* i0 = input;
  float* o0 = output;
  $for M in range(1, ROW_TILE):
    const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride);
    float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride);

  const float vmin = params->scalar.min;
  const float vmax = params->scalar.max;
  do {
    $for M in range(1, ROW_TILE):
      $if M % 2 == 0:
        if XNN_UNPREDICTABLE(rows <= ${M}) {
          i${M} = i${M-1};
          o${M} = o${M-1};
        }
      $else:
        if XNN_UNPREDICTABLE(rows < ${M+1}) {
          i${M} = i${M-1};
          o${M} = o${M-1};
        }

    const float* w = weights;
    size_t c = channels;
    $if CHANNEL_TILE > 1:
      for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
        $for C in range(CHANNEL_TILE):
          const float vscale${ABC[C]} = w[${C}];

        $for M in range(ROW_TILE):
          $for C in range(CHANNEL_TILE):
            float vacc${M}x${ABC[C]} = i${M}[${C}];
          i${M} += ${CHANNEL_TILE};

        $for C in range(CHANNEL_TILE):
          const float vbias${ABC[C]} = w[${C + CHANNEL_TILE}];

        $for M in range(ROW_TILE):
          $for C in range(CHANNEL_TILE):
            vacc${M}x${ABC[C]} = vacc${M}x${ABC[C]} * vscale${ABC[C]} + vbias${ABC[C]};

        $for M in range(ROW_TILE):
          $for C in range(CHANNEL_TILE):
            vacc${M}x${ABC[C]} = ${MAX_F32}(vacc${M}x${ABC[C]}, vmin);

        $for M in range(ROW_TILE):
          $for C in range(CHANNEL_TILE):
            vacc${M}x${ABC[C]} = ${MIN_F32}(vacc${M}x${ABC[C]}, vmax);

        $for M in range(ROW_TILE):
          $for C in range(CHANNEL_TILE):
            o${M}[${C}] = vacc${M}x${ABC[C]};
          o${M} += ${CHANNEL_TILE};

        w += ${CHANNEL_TILE * 2};
      }
      if XNN_UNLIKELY(c != 0) {
        do {
          const float vscale = *w++;

          $for M in range(ROW_TILE):
            float vacc${M} = *i${M}++;

          const float vbias = w[${CHANNEL_TILE - 1}];

          $for M in range(ROW_TILE):
            vacc${M} = vacc${M} * vscale + vbias;

          $for M in range(ROW_TILE):
            vacc${M} = ${MAX_F32}(vacc${M}, vmin);

          $for M in range(ROW_TILE):
            vacc${M} = ${MIN_F32}(vacc${M}, vmax);

          $for M in range(ROW_TILE):
            *o${M}++ = vacc${M};

          c -= sizeof(float);
        } while (c != 0);
      }
    $else:
      do {
        const float vscale = w[0];

        $for M in range(ROW_TILE):
          float vacc${M} = *i${M}++;

        const float vbias = w[1];

        $for M in range(ROW_TILE):
          vacc${M} = vacc${M} * vscale + vbias;

        $for M in range(ROW_TILE):
          vacc${M} = ${MAX_F32}(vacc${M}, vmin);

        $for M in range(ROW_TILE):
          vacc${M} = ${MIN_F32}(vacc${M}, vmax);

        $for M in range(ROW_TILE):
          *o${M}++ = vacc${M};

        w += 2;
        c -= sizeof(float);
      } while (c != 0);
    $for M in range(ROW_TILE):
      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
    rows = doz(rows, ${ROW_TILE});
  } while (rows != 0);
}
