// 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.
$assert CHANNEL_TILE % 64 == 0
$assert CHANNEL_TILE >= 64
$SIMD_TILE = CHANNEL_TILE // 64
$assert ACCUMULATORS <= SIMD_TILE

#include <assert.h>

#include <immintrin.h>

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

$ACC_SUFFIX = "" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS
void xnn_qs8_rsum_ukernel__avx512skx_u${CHANNEL_TILE}${ACC_SUFFIX}(
    size_t batch,
    const int8_t* input,
    int32_t* output,
    const struct xnn_qs8_rsum_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(input != NULL);
  assert(output != NULL);
  assert(params != NULL);

  const __m512i vone = _mm512_set1_epi8(1);
  const __m512i vone_16 = _mm512_set1_epi16(1);
  $for ACC in range(ACCUMULATORS):
    __m512i vacc${ACC} = _mm512_setzero_si512();

  // 256 int8s may be summed into an int16 before overflowing.
  // Each register has 32 lanes and there are ${ACCUMULATORS} accumulators so batch size is ${ACCUMULATORS*256*32}
  for (; batch >= ${ACCUMULATORS*256*32}; batch -= ${ACCUMULATORS*256*32}) {
    $for ACC in range(ACCUMULATORS):
      __m512i vacc16_${ACC} = _mm512_setzero_si512();
    for (size_t current_batch = ${ACCUMULATORS*256*32}; current_batch > 0; current_batch -= ${CHANNEL_TILE}) {
      $for N in range(SIMD_TILE):
        const __m512i vt${N} = _mm512_maddubs_epi16(vone, _mm512_loadu_si512((const __m512i*) input)); input += 64;
      $for N in range(SIMD_TILE):
        vacc16_${N % ACCUMULATORS} = _mm512_add_epi16(vacc16_${N % ACCUMULATORS}, vt${N});
    }
    $for ACC in range(ACCUMULATORS):
      vacc${ACC} = _mm512_add_epi32(vacc${ACC}, _mm512_madd_epi16(vone_16, vacc16_${ACC}));
  }

  $if CHANNEL_TILE > 64:
    if (XNN_LIKELY(batch >= ${CHANNEL_TILE})) {
      assert(batch >= 1 && batch < ${ACCUMULATORS*256*32});
      $for ACC in range(ACCUMULATORS):
        __m512i vacc16_${ACC} = _mm512_setzero_si512();
      for (; batch >= ${CHANNEL_TILE}; batch -= ${CHANNEL_TILE}) {
        $for N in range(SIMD_TILE):
          const __m512i vt${N} = _mm512_maddubs_epi16(vone, _mm512_loadu_si512((const __m512i*) input)); input += 64;
        $for N in range(SIMD_TILE):
          vacc16_${N % ACCUMULATORS} = _mm512_add_epi16(vacc16_${N % ACCUMULATORS}, vt${N});
      }
      $for ACC in range(ACCUMULATORS):
        vacc${ACC} = _mm512_add_epi32(vacc${ACC}, _mm512_madd_epi16(vone_16, vacc16_${ACC}));
    }
  if (XNN_UNLIKELY(batch != 0)) {
    assert(batch >= 1 && batch < ${256*32});
    __m512i vacc16 = _mm512_setzero_si512();
    for (; batch >= 64; batch -= 64) {
      const __m512i vt = _mm512_maddubs_epi16(vone, _mm512_loadu_si512((const __m512i*) input)); input += 64;
      vacc16 = _mm512_add_epi16(vacc16, vt);
    }

    if (XNN_UNLIKELY(batch != 0)) {
      assert(batch >= 1 && batch <= 63);
      // Prepare mask for valid 8-bit elements (depends on batch).
      const __mmask64 vmask = _cvtu64_mask64((UINT64_C(1) << batch) - 1);
      const __m512i vt = _mm512_maddubs_epi16(vone, _mm512_maskz_loadu_epi8(vmask, input));
      vacc16 = _mm512_add_epi16(vacc16, vt);
    }
    vacc0 = _mm512_add_epi32(vacc0, _mm512_madd_epi16(vone_16, vacc16));
  }
  $if ACCUMULATORS > 1:
    $ACC_SLICE = 1
    $while ACC_SLICE < ACCUMULATORS:
      $for A in range(0, ACCUMULATORS, ACC_SLICE * 2):
        $if A + ACC_SLICE < ACCUMULATORS:
          vacc${A} = _mm512_add_epi32(vacc${A}, vacc${A + ACC_SLICE});
      $ACC_SLICE *= 2

  int32_t res = _mm512_reduce_add_epi32(vacc0);

  *output += res;
}
