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

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


$UNROLL = CHANNELS_BATCH >> 4
void xnn_f16_f32acc_rdsum_ukernel_${ACCUMULATORS}p${ACCUMULATORS}x__avx512skx_c${CHANNELS_BATCH}(
    size_t rows,
    size_t channels,
    const xnn_float16* input,
    size_t input_stride,
    const xnn_float16* zero,
    float* output,
    const struct xnn_f16_f32acc_scale_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(rows != 0);
  assert(channels != 0);
  assert(input != NULL);
  assert(output != NULL);

  const __m512 vscale = _mm512_set1_ps(params->scalar.scale);

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

    $for i in range(UNROLL):
      __m512 vacc${i} = _mm512_setzero_ps();

    for (int r = rows; r > 0; r -= ${ACCUMULATORS}) {
      $for ACC in range(1, ACCUMULATORS, 2):
        if XNN_UNPREDICTABLE(r < ${ACC+1}) {
          i${ACC} = (const uint16_t*) zero;
        }
        if XNN_UNPREDICTABLE(r <= ${ACC+1}) {
          i${ACC+1} = (const uint16_t*) zero;
        }
      $for c in range(UNROLL):
        __m512 vin${c};
      $for j in range(ACCUMULATORS):
        $for c in range(UNROLL):
          vin${c} = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i*) (&i${j}[${c*16}])));
        $for c in range(UNROLL):
          vacc${c} = _mm512_add_ps(vin${c}, vacc${c});
      $for ACC in range(0, ACCUMULATORS):
        i${ACC} = (const uint16_t*) ((uintptr_t) i${ACC} + input_increment);
    }
    $for i in range(UNROLL):
      vacc${i} = _mm512_mul_ps(vacc${i}, vscale);

    $for i in range(0, UNROLL):
      __m512 vo${i} = _mm512_loadu_ps(output + ${i} * 16);
    $for i in range(0, UNROLL):
      vacc${i} = _mm512_add_ps(vo${i}, vacc${i});
    $for i in range(0, UNROLL):
      _mm512_storeu_ps(output, vacc${i}); output = (void*) ((uintptr_t) output + 16 * sizeof(float));

    input = (const xnn_float16*) ((uintptr_t) input + ${CHANNELS_BATCH} * sizeof(uint16_t));
  }
  if (channels != 0) {
    input_increment = ${ACCUMULATORS} * input_stride;
    const uint16_t* i0 = (const uint16_t*) input;
    $for i in range(1, ACCUMULATORS):
      const uint16_t* i${i} = (const uint16_t*) ((uintptr_t) input + ${i} * input_stride);
    __m512 vacc[${UNROLL}];
    $for i in range(UNROLL):
      vacc[${i}] = _mm512_setzero_ps();

    const size_t num_full_chunks = channels >> 4;
    // AVX512 has 16 float lanes.
    const size_t num_chunks = round_up_po2(channels, 16) >> 4;
    // 0xF masks the remainder.
    const size_t remainder = channels & 0xF;
    const size_t batch = channels & 0xF;
    __mmask16 vmask;
    if (remainder) {
      assert(batch >= 1);
      assert(batch <= 15);
      vmask = _cvtu32_mask16((uint32_t) ((UINT32_C(1) << batch) - UINT32_C(1)));
    }
    for (int r = rows; r > 0; r -= ${ACCUMULATORS}) {
      $for ACC in range(1, ACCUMULATORS, 2):
        if XNN_UNPREDICTABLE(r < ${ACC+1}) {
          i${ACC} = (const uint16_t*) zero;
        }
        if XNN_UNPREDICTABLE(r <= ${ACC+1}) {
          i${ACC+1} = (const uint16_t*) zero;
        }
      for (int i = 0; i < num_full_chunks; ++i) {
        $for c in range(ACCUMULATORS):
          vacc[i] = _mm512_add_ps(_mm512_cvtph_ps(_mm256_loadu_si256((const __m256i*) &i${c}[i*16])), vacc[i]);
      }

      if (remainder) {
        $for c in range(ACCUMULATORS):
          vacc[num_full_chunks] = _mm512_maskz_add_ps(vmask, vacc[num_full_chunks],  _mm512_cvtph_ps(_mm256_maskz_loadu_epi16(vmask, &i${c}[num_full_chunks*16])));
      }
      $for ACC in range(ACCUMULATORS):
        i${ACC} = (const uint16_t*) ((uintptr_t) i${ACC} + input_increment);
    }
    for (size_t i = 0; i < num_chunks; ++i) {
      vacc[i] = _mm512_mul_ps(vacc[i], vscale);
    }

    __m512 vo[${UNROLL}];
    for (int i = 0; i < num_full_chunks; ++i) {
      vo[i] = _mm512_loadu_ps(output + i * 16);
    }
    for (int i = 0; i < num_full_chunks; ++i) {
      vacc[i] = _mm512_add_ps(vo[i], vacc[i]);
    }
    for (int i = 0; i < num_full_chunks; ++i) {
      _mm512_storeu_ps(output, vacc[i]); output = (void*) ((uintptr_t) output + 16 * sizeof(float));
    }
    if (remainder) {
      __m512 vout = vacc[num_full_chunks];
      vout = _mm512_maskz_add_ps(vmask, vout,  _mm512_maskz_loadu_ps(vmask, output));
      _mm512_mask_storeu_ps(output, vmask, vout);
    }
  }
}
