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

$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <wasm_simd128.h>

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

$UNROLL = CHANNELS >> 2
void xnn_f32_rdsum_ukernel_${ACCUMULATORS}p${ACCUMULATORS}x__wasmsimd_c${CHANNELS}(
    size_t rows,
    size_t channels,
    const float* input,
    size_t input_stride,
    const float* zero,
    float* output,
    const struct xnn_f32_scale_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(rows != 0);
  assert(channels != 0);
  assert(input != NULL);
  assert(output != NULL);

  const v128_t vscale = wasm_v128_load32_splat(&params->scalar.scale);

  size_t input_increment = ${ACCUMULATORS} * input_stride;
  for (; channels >= ${CHANNELS}; channels -= ${CHANNELS}) {
    const float* i0 = input;
    $for i in range(1, ACCUMULATORS):
      const float* i${i} = (const float*) ((uintptr_t) input + ${i} * input_stride);

    $for i in range(UNROLL):
      v128_t vacc${i} = wasm_i32x4_const_splat(0.f);

    for (int r = rows; r > 0; r -= ${ACCUMULATORS}) {
      $for N in range(1, ACCUMULATORS, 2):
        if XNN_UNPREDICTABLE(r < ${N+1}) {
          i${N} = zero;
        }
        if XNN_UNPREDICTABLE(r <= ${N+1}) {
          i${N+1} = zero;
        }
      $for c in range(UNROLL):
        v128_t vin${c};
      $for j in range(ACCUMULATORS):
        $for c in range(UNROLL):
          vin${c} = wasm_v128_load(&i${j}[${c*4}]);
        $for c in range(UNROLL):
          vacc${c} = wasm_f32x4_add(vin${c}, vacc${c});
      $for N in range(0, ACCUMULATORS):
        i${N} = (const float*) ((uintptr_t) i${N} + input_increment);
    }
    $for i in range(UNROLL):
      vacc${i} = wasm_f32x4_mul(vacc${i}, vscale);

    const float* o = output;
    $for i in range(0, UNROLL):
      v128_t vo${i} = wasm_v128_load(o); o += 4;
    $for i in range(0, UNROLL):
      vacc${i} = wasm_f32x4_add(vo${i}, vacc${i});
    $for i in range(0, UNROLL):
      wasm_v128_store(output, vacc${i}); output += 4;

    input = (const float*) ((uintptr_t) input + ${CHANNELS} * sizeof(float));
  }
  if (channels != 0) {
    input_increment = ${ACCUMULATORS} * input_stride;
    const float* i0 = input;
    $for i in range(1, ACCUMULATORS):
      const float* i${i} = (const float*) ((uintptr_t) input + ${i} * input_stride);
    v128_t vacc[${UNROLL}];
    $for i in range(UNROLL):
      vacc[${i}] = wasm_i32x4_const_splat(0.f);

    const size_t num_chunks = round_up_po2(channels, 4) >> 2;
    for (int r = rows; r > 0; r -= ${ACCUMULATORS}) {
      $for N in range(1, ACCUMULATORS, 2):
        if XNN_UNPREDICTABLE(r < ${N+1}) {
          i${N} = zero;
        }
        if XNN_UNPREDICTABLE(r <= ${N+1}) {
          i${N+1} = zero;
        }
      for (int i = 0; i < num_chunks; ++i) {
        $for c in range(ACCUMULATORS):
          vacc[i] = wasm_f32x4_add(wasm_v128_load(&i${c}[i*4]), vacc[i]);
      }
      $for N in range(ACCUMULATORS):
        i${N} = (const float*) ((uintptr_t) i${N} + input_increment);
    }
    for (int i = 0; i < num_chunks; ++i) {
      vacc[i] = wasm_f32x4_mul(vacc[i], vscale);
    }

    v128_t vo[${UNROLL}];
    const float* o = output;
    for (int i = 0; i < channels >> 2; ++i) {
      vo[i] = wasm_v128_load(o); o += 4;
    }
    for (int i = 0; i < channels >> 2; ++i) {
      vacc[i] = wasm_f32x4_add(vo[i], vacc[i]);
    }
    for (int i = 0; i < channels >> 2; ++i) {
      wasm_v128_store(output, vacc[i]); output += 4;
    }
    const size_t pos = channels / 4;
    v128_t vout = vacc[pos];
    if (channels & 2) {
      v128_t vo = wasm_f32x4_make(output[0], output[1], 0.f, 0.f);
      wasm_v128_store64_lane(output, wasm_f32x4_add(vo, vout), 0);
      vout = wasm_v64x2_shuffle(vout, vout, 1, 1);
      output += 2;
    }
    if (channels & 1) {
      *output += wasm_f32x4_extract_lane(vout, 0);
    }
  }
}
