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

$assert BATCH_TILE >= 1
$assert OP in ["MAX", "MIN", "MINMAX"]
#include <assert.h>

#include "xnnpack/common.h"
$if not WASM:
  #include "xnnpack/math.h"
#include "xnnpack/reduce.h"
$if DATATYPE == "F16":
  #include "xnnpack/fp16.h"

$ACC_SUFFIX = "" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS
$MAX = "__builtin_wasm_max_f32" if WASM else "math_max_f32"
$MIN = "__builtin_wasm_min_f32" if WASM else "math_min_f32"
$EMIT_MIN = "MIN" in OP
$EMIT_MAX = "MAX" in OP
$ISA = "wasm" if WASM else "scalar"
$MAX_POS = 1 if OP == "MINMAX" else 0
$ITYPE = {"F32": "float", "F16": "uint16_t"}[DATATYPE]
void xnn_${DATATYPE.lower()}_r${OP.lower()}_ukernel__${ISA}_u${BATCH_TILE}${ACC_SUFFIX}(
    size_t batch,
    $if DATATYPE == "F32":
      const float* input,
      float* output,
    $elif DATATYPE == "F16":
      const void* input,
      void* output,
    const struct xnn_${DATATYPE.lower()}_default_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(${ITYPE}) == 0);
  assert(input != NULL);
  assert(output != NULL);

  $if DATATYPE == "F32":
    const float* i = input;
  $elif DATATYPE == "F16":
    const uint16_t* i = (const uint16_t*) input;
    uint16_t* o = (uint16_t*) output;

  $if DATATYPE == "F32":
    $if EMIT_MIN:
      float vmin0 = *i;
    $if EMIT_MAX:
      float vmax0 = *i;
  $elif DATATYPE == "F16":
    $if EMIT_MIN:
      float vmin0 = fp16_ieee_to_fp32_value(*i);
    $if EMIT_MAX:
      float vmax0 = fp16_ieee_to_fp32_value(*i);
  $for A in range(1, ACCUMULATORS):
    $if EMIT_MIN:
      float vmin${A} = vmin0;
    $if EMIT_MAX:
      float vmax${A} = vmax0;
  $if BATCH_TILE == 1:
    do {
      $if DATATYPE == "F32":
        const float vt = *i++;
      $elif DATATYPE == "F16":
        const float vt = fp16_ieee_to_fp32_value(*i++);
      $if EMIT_MIN:
        vmin0 = ${MIN}(vmin0, vt);
      $if EMIT_MAX:
        vmax0 = ${MAX}(vmax0, vt);
      batch -= sizeof(${ITYPE});
    } while (batch != 0);
  $else:
    for (; batch >= ${BATCH_TILE} * sizeof(${ITYPE}); batch -= ${BATCH_TILE} * sizeof(${ITYPE})) {
      $for N in range(BATCH_TILE):
        $if DATATYPE == "F32":
          const float vt${N} = i[${N}];
        $elif DATATYPE == "F16":
          const float vt${N} = fp16_ieee_to_fp32_value(i[${N}]);
      i += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        $if EMIT_MIN:
          vmin${N % ACCUMULATORS} = ${MIN}(vmin${N % ACCUMULATORS}, vt${N});
        $if EMIT_MAX:
          vmax${N % ACCUMULATORS} = ${MAX}(vmax${N % ACCUMULATORS}, vt${N});
    }
    $if ACCUMULATORS > 1:
      $ACC_SLICE = 1
      $while ACC_SLICE < ACCUMULATORS:
        $for A in range(0, ACCUMULATORS, ACC_SLICE * 2):
          $if A + ACC_SLICE < ACCUMULATORS:
            $if EMIT_MIN:
              vmin${A} = ${MIN}(vmin${A}, vmin${A + ACC_SLICE});
            $if EMIT_MAX:
              vmax${A} = ${MAX}(vmax${A}, vmax${A + ACC_SLICE});
        $ACC_SLICE *= 2

    if XNN_UNLIKELY(batch != 0) {
      $if BATCH_TILE == 2:
        $if DATATYPE == "F32":
          const float vt = *i;
        $elif DATATYPE == "F16":
          const float vt = fp16_ieee_to_fp32_value(*i);
        $if EMIT_MIN:
          vmin0 = ${MIN}(vmin0, vt);
        $if EMIT_MAX:
          vmax0 = ${MAX}(vmax0, vt);
      $else:
        do {
          $if DATATYPE == "F32":
            const float vt = *i++;
          $elif DATATYPE == "F16":
            const float vt = fp16_ieee_to_fp32_value(*i++);
          $if EMIT_MIN:
            vmin0 = ${MIN}(vmin0, vt);
          $if EMIT_MAX:
            vmax0 = ${MAX}(vmax0, vt);
          batch -= sizeof(${ITYPE});
        } while (batch != 0);
    }
  $if DATATYPE == "F32":
    $if EMIT_MIN:
      output[0] = vmin0;
    $if EMIT_MAX:
      output[${MAX_POS}] = vmax0;
  $elif DATATYPE == "F16":
    $if EMIT_MIN:
      o[0] = xnn_float16_from_float(vmin0);
    $if EMIT_MAX:
      o[${MAX_POS}] = xnn_float16_from_float(vmax0);
}
