// 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 <xmmintrin.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__sse_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 __m128 vscale = _mm_set1_ps(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):
      __m128 vacc${i} = _mm_setzero_ps();

    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):
        __m128 vin${c};
      $for j in range(ACCUMULATORS):
        $for c in range(UNROLL):
          vin${c} = _mm_loadu_ps(&i${j}[${c*4}]);
        $for c in range(UNROLL):
          vacc${c} = _mm_add_ps(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} = _mm_mul_ps(vacc${i}, vscale);

    const float* o = output;
    $for i in range(0, UNROLL):
      __m128 vo${i} = _mm_loadu_ps(o); o += 4;
    $for i in range(0, UNROLL):
      vacc${i} = _mm_add_ps(vo${i}, vacc${i});
    $for i in range(0, UNROLL):
      _mm_storeu_ps(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);
    __m128 vacc[${UNROLL}];
    $for i in range(UNROLL):
      vacc[${i}] = _mm_setzero_ps();

    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] = _mm_add_ps(_mm_loadu_ps(&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] = _mm_mul_ps(vacc[i], vscale);
    }

    __m128 vo[${UNROLL}];
    const float* o = output;
    for (int i = 0; i < channels >> 2; ++i) {
      vo[i] = _mm_loadu_ps(o); o += 4;
    }
    for (int i = 0; i < channels >> 2; ++i) {
      vacc[i] = _mm_add_ps(vo[i], vacc[i]);
    }
    for (int i = 0; i < channels >> 2; ++i) {
      _mm_storeu_ps(output, vacc[i]); output += 4;
    }
    const size_t pos = channels >> 2;
    channels &= 0x3;
    __m128 vout = vacc[pos];
    if (channels & 2) {
      __m128 vo = _mm_loadl_pi(vscale, (__m64*) output);
      _mm_storel_pi((__m64*) output, _mm_add_ps(vo, vout));
      vout = _mm_movehl_ps(vout, vout);
      output += 2;
    }
    if (channels & 1) {
      __m128 vo = _mm_load_ss(output);
      _mm_store_ss(output, _mm_add_ps(vo, vout));
    }
  }
}
