// 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.
#include <assert.h>
#include <math.h>

#include <arm_neon.h>

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


$PARAMS_STRUCT = "fp32_neon"
void xnn_qs8_rdsum_ukernel_${ACCUMULATORS}p${ACCUMULATORS}x__neon_c${CHANNELS}(
    size_t rows,
    size_t channels,
    const int8_t* input,
    size_t input_stride,
    const int8_t* zero,
    int32_t* output,
    const struct xnn_qs8_rsum_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(rows != 0);
  assert(channels != 0);
  assert(input != NULL);
  assert(output != NULL);

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

    $for C in range(0, CHANNELS, 4):
      int32x4_t vacc${C} = vdupq_n_s32(0);

    // 256 int8s may be summed into an int16 before overflowing
    // To prevent handling the tails of the inner 256 loop, we round 256 down to
    // the nearest integer multiple of ACCUMULATORS.
    $OVERFLOW = (256 // ACCUMULATORS) * ACCUMULATORS
    int r = rows;
    while (r > 0) {
      $for C in range(0, CHANNELS, 8):
        int16x8_t vacc16_${C} = vmovq_n_s16(0);
      for (int current_batch = min(r, ${OVERFLOW}); current_batch > 0; current_batch -= ${ACCUMULATORS}) {
        $for N in range(1, ACCUMULATORS, 2):
          if XNN_UNPREDICTABLE(current_batch < ${N+1}) {
            i${N} = zero;
          }
          if XNN_UNPREDICTABLE(current_batch <= ${N+1}) {
            i${N+1} = zero;
          }
        $for C in range(0, CHANNELS, 8):
          int8x8_t vin${C};
        $for ACC in range(ACCUMULATORS):
          $for C in range(0, CHANNELS, 8):
            vin${C} = vld1_s8(&i${ACC}[${C}]);
          $for C in range(0, CHANNELS, 8):
            vacc16_${C} = vaddw_s8(vacc16_${C}, vin${C});
        $for N in range(0, ACCUMULATORS):
          i${N} = (const int8_t*) ((uintptr_t) i${N} + input_increment);
      }
      $for C in range(0, CHANNELS, 8):
        vacc${C} = vaddw_s16(vacc${C}, vget_low_s16(vacc16_${C}));
        vacc${C+4} = vaddw_s16(vacc${C+4}, vget_high_s16(vacc16_${C}));
      r = doz(r, ${OVERFLOW});
    }

    const int32_t* o = output;
    $for C in range(0, CHANNELS, 4):
      int32x4_t vo${C} = vld1q_s32(o); o += 4;
    $for C in range(0, CHANNELS, 4):
      vacc${C} = vaddq_s32(vo${C}, vacc${C});
    $for C in range(0, CHANNELS, 4):
      vst1q_s32(output, vacc${C}); output += 4;

    input = (const int8_t*) ((uintptr_t) input + ${CHANNELS} * sizeof(int8_t));
  }
  if (channels != 0) {
    input_increment = ${ACCUMULATORS} * input_stride;
    // 256 int8s may be summed into an int16 before overflowing.
    do {
      int num_batches = floor((rows + ${OVERFLOW - 1}) / ${OVERFLOW});
      int r = rows;
      const int8_t* i0 = input;
      $for ACC in range(1, ACCUMULATORS):
        const int8_t* i${ACC} = (const int8_t*) ((uintptr_t) input + ${ACC} * input_stride);

      int32x4_t vacc0 = vdupq_n_s32(0);
      int32x4_t vacc1 = vdupq_n_s32(0);

      for (; num_batches > 0; --num_batches) {
        int16x8_t vacc16 = vmovq_n_s16(0);
        for (int current_batch = min(r, ${OVERFLOW}); current_batch > 0; current_batch -= ${ACCUMULATORS}) {
          $for N in range(1, ACCUMULATORS, 2):
            if XNN_UNPREDICTABLE(current_batch < ${N+1}) {
              i${N} = zero;
            }
            if XNN_UNPREDICTABLE(current_batch <= ${N+1}) {
              i${N+1} = zero;
            }

          $for ACC in range(ACCUMULATORS):
            int8x8_t vin${ACC} = vld1_s8(&i${ACC}[0]);
          $for ACC in range(ACCUMULATORS):
            vacc16 = vaddw_s8(vacc16, vin${ACC});
          $for N in range(ACCUMULATORS):
            i${N} = (const int8_t*) ((uintptr_t) i${N} + input_increment);
        }
        vacc0 = vaddw_s16(vacc0, vget_low_s16(vacc16));
        vacc1 = vaddw_s16(vacc1, vget_high_s16(vacc16));
        r = doz(r, ${OVERFLOW});
      }

      if XNN_LIKELY(channels >= 8) {
        int32x4_t vo0 = vld1q_s32(output);
        int32x4_t vo1 = vld1q_s32(output + 4);
        vo0 = vaddq_s32(vo0, vacc0);
        vo1 = vaddq_s32(vo1, vacc1);
        vst1q_s32(output, vo0); output += 4;
        vst1q_s32(output, vo1); output += 4;
        channels -= 8;
        input = (const int8_t*) ((uintptr_t) input + 8 * sizeof(int8_t));
      } else {
        if (channels & 4) {
          int32x4_t vo = vld1q_s32(output);
          vo = vaddq_s32(vo, vacc0);
          vst1q_s32(output, vo); output += 4;
          vacc0 = vacc1;
        }
        if (channels & 2) {
          int32x2_t vo = vld1_s32(output);
          vo = vadd_s32(vo, vget_low_s32(vacc0));
          vst1_s32(output, vo); output += 2;
          vacc0 = vextq_s32(vacc0, vacc0, 2);
        }
        if (channels & 1) {
          *output += vgetq_lane_s32(vacc0, 0);
        }
        channels = 0;
      }
    } while (channels != 0);
  }
}
