// 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 % 4 == 0
$assert BATCH_TILE >= 4
$SIMD_TILE = BATCH_TILE // 4
$assert ACCUMULATORS <= SIMD_TILE
$assert OP in ["MAX", "MIN", "MINMAX"]
$assert MINMAX in ["MINMAX", "PMINMAX"]
#include <assert.h>

#include <wasm_simd128.h>

#include "xnnpack/common.h"
#include "xnnpack/reduce.h"


$ACC_SUFFIX = "" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS
$EMIT_MIN = "MIN" in OP
$EMIT_MAX = "MAX" in OP
$WASM_F32X4_MIN = {"MINMAX": "wasm_f32x4_min", "PMINMAX": "wasm_f32x4_pmin"}[MINMAX]
$WASM_F32X4_MAX = {"MINMAX": "wasm_f32x4_max", "PMINMAX": "wasm_f32x4_pmax"}[MINMAX]
$MINMAX_SUFFIX = {"MINMAX": "_minmax", "PMINMAX": "_pminmax"}[MINMAX]
void xnn_f32_r${OP.lower()}_ukernel__wasmsimd${MINMAX_SUFFIX}_u${BATCH_TILE}${ACC_SUFFIX}(
    size_t batch,
    const float* input,
    float* output,
    const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input != NULL);
  assert(output != NULL);

  $if EMIT_MIN:
    v128_t vmin0 = wasm_v128_load32_splat(input);
    $if EMIT_MAX:
      v128_t vmax0 = vmin0;
  $elif EMIT_MAX:
    v128_t vmax0 = wasm_v128_load32_splat(input);
  $for A in range(1, ACCUMULATORS):
    $if EMIT_MIN:
      v128_t vmin${A} = vmin0;
    $if EMIT_MAX:
      v128_t vmax${A} = vmax0;
  $if BATCH_TILE > 4:
    for (; batch >= ${BATCH_TILE} * sizeof(float); batch -= ${BATCH_TILE} * sizeof(float)) {
      const v128_t vt0 = wasm_v128_load(input);
      $for N in range(1, SIMD_TILE):
        const v128_t vt${N} = wasm_v128_load(input + ${N * 4});
      input += ${BATCH_TILE};

      $for N in range(SIMD_TILE):
        $if EMIT_MIN:
          vmin${N % ACCUMULATORS} = ${WASM_F32X4_MIN}(vmin${N % ACCUMULATORS}, vt${N});
        $if EMIT_MAX:
          vmax${N % ACCUMULATORS} = ${WASM_F32X4_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} = ${WASM_F32X4_MIN}(vmin${A}, vmin${A + ACC_SLICE});
            $if EMIT_MAX:
              vmax${A} = ${WASM_F32X4_MAX}(vmax${A}, vmax${A + ACC_SLICE});
        $ACC_SLICE *= 2
  for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
    const v128_t vt = wasm_v128_load(input);
    input += 4;

    $if EMIT_MIN:
      vmin0 = ${WASM_F32X4_MIN}(vmin0, vt);
    $if EMIT_MAX:
      vmax0 = ${WASM_F32X4_MAX}(vmax0, vt);
  }
  $if EMIT_MIN:
    vmin0 = ${WASM_F32X4_MIN}(vmin0, wasm_v64x2_shuffle(vmin0, vmin0, 1, 1));
  $if EMIT_MAX:
    vmax0 = ${WASM_F32X4_MAX}(vmax0, wasm_v64x2_shuffle(vmax0, vmax0, 1, 1));
  if XNN_UNLIKELY(batch & (2 * sizeof(float))) {
    const v128_t vt = wasm_v128_load64_zero(input);
    input += 2;
    $if EMIT_MIN:
      vmin0 = ${WASM_F32X4_MIN}(vmin0, vt);
    $if EMIT_MAX:
      vmax0 = ${WASM_F32X4_MAX}(vmax0, vt);
  }
  $if EMIT_MIN:
    vmin0 = ${WASM_F32X4_MIN}(vmin0, wasm_v32x4_shuffle(vmin0, vmin0, 1, 1, 1, 1));
  $if EMIT_MAX:
    vmax0 = ${WASM_F32X4_MAX}(vmax0, wasm_v32x4_shuffle(vmax0, vmax0, 1, 1, 1, 1));
  if XNN_UNLIKELY(batch & (1 * sizeof(float))) {
    const v128_t vt = wasm_v128_load32_zero(input);
    $if EMIT_MIN:
      vmin0 = ${WASM_F32X4_MIN}(vmin0, vt);
    $if EMIT_MAX:
      vmax0 = ${WASM_F32X4_MAX}(vmax0, vt);
  }
  $if EMIT_MIN:
    wasm_v128_store32_lane(output, vmin0, 0);
    $if EMIT_MAX:
      wasm_v128_store32_lane(output + 1, vmax0, 0);
  $elif EMIT_MAX:
    wasm_v128_store32_lane(output, vmax0, 0);
}
