// Copyright 2019 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 BATCH_TILE % 16 == 0
$assert BATCH_TILE >= 16
$SIMD_TILE = BATCH_TILE // 16
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$assert OP in ["ADD", "DIV", "MAX", "MIN", "MUL", "SUB", "SQRDIFF", "PRELU"]
#include <assert.h>

#include <immintrin.h>

#include "xnnpack/common.h"
#include "xnnpack/intrinsics-polyfill.h"
#include "xnnpack/vbinary.h"


$_MM512_OP_PS = {
$  "ADD": "_mm512_add_ps",
$  "DIV": "_mm512_div_ps",
$  "MAX": "_mm512_max_ps",
$  "MIN": "_mm512_min_ps",
$  "MUL": "_mm512_mul_ps",
$  "SUB": "_mm512_sub_ps",
$  "SQRDIFF": "_mm512_sub_ps",
$  "PRELU": "_mm512_mul_ps",
$}[OP]
$_MM512_MASKZ_OP_PS = {
$  "ADD": "_mm512_maskz_add_ps",
$  "DIV": "_mm512_maskz_div_ps",
$  "MAX": "_mm512_maskz_max_ps",
$  "MIN": "_mm512_maskz_min_ps",
$  "MUL": "_mm512_maskz_mul_ps",
$  "SUB": "_mm512_maskz_sub_ps",
$  "SQRDIFF": "_mm512_maskz_sub_ps",
$  "PRELU": "_mm512_maskz_mul_ps",
$}[OP]
void xnn_f32_v${OP.lower()}_ukernel__avx512f_u${BATCH_TILE}(
    size_t batch,
    const float* input_a,
    const float* input_b,
    float* output,
    const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input_a != NULL);
  assert(input_b != NULL);
  assert(output != NULL);

  $if OP == "PRELU":
    const __m512 vzero = _mm512_setzero_ps();

  $if BATCH_TILE > 16:
    for (; batch >= ${BATCH_TILE} * sizeof(float); batch -= ${BATCH_TILE} * sizeof(float)) {
      const __m512 va${ABC[0]} = _mm512_loadu_ps(input_a);
      $for N in range(1, SIMD_TILE):
        const __m512 va${ABC[N]} = _mm512_loadu_ps(input_a + ${N * 16});
      input_a += ${BATCH_TILE};

      $if OP == "PRELU":
        $for N in range(SIMD_TILE):
          const __mmask16 vsign${ABC[N]} = _mm512_cmp_ps_mask(va${ABC[N]}, vzero, _CMP_LT_OQ);
          __m512 vacc${ABC[N]} = _mm512_mask_mul_ps(va${ABC[N]}, vsign${ABC[N]}, va${ABC[N]}, _mm512_loadu_ps(input_b + ${N * 16}));
        input_b += ${BATCH_TILE};
      $else:
        __m512 vacc${ABC[0]} = ${_MM512_OP_PS}(va${ABC[0]}, _mm512_loadu_ps(input_b));
        $for N in range(1, SIMD_TILE):
          __m512 vacc${ABC[N]} = ${_MM512_OP_PS}(va${ABC[N]}, _mm512_loadu_ps(input_b + ${N * 16}));
        input_b += ${BATCH_TILE};

        $if OP == "SQRDIFF":
          $for N in range(SIMD_TILE):
            vacc${ABC[N]} = _mm512_mul_ps(vacc${ABC[N]}, vacc${ABC[N]});

      _mm512_storeu_ps(output, vacc${ABC[0]});
      $for N in range(1, SIMD_TILE):
        _mm512_storeu_ps(output + ${N * 16}, vacc${ABC[N]});
      output += ${BATCH_TILE};
    }
  for (; batch >= 16 * sizeof(float); batch -= 16 * sizeof(float)) {
    const __m512 va = _mm512_loadu_ps(input_a);
    input_a += 16;

    $if OP == "PRELU":
      const __mmask16 vsign = _mm512_cmp_ps_mask(va, vzero, _CMP_LT_OQ);
      __m512 vacc = _mm512_mask_mul_ps(va, vsign, va, _mm512_loadu_ps(input_b));
      input_b += 16;
    $else:
      __m512 vacc = ${_MM512_OP_PS}(va, _mm512_loadu_ps(input_b));
      input_b += 16;

      $if OP == "SQRDIFF":
        vacc = _mm512_mul_ps(vacc, vacc);

    _mm512_storeu_ps(output, vacc);
    output += 16;
  }
  if XNN_UNLIKELY(batch != 0) {
    assert(batch >= 1 * sizeof(float));
    assert(batch <= 15 * sizeof(float));
    // Prepare mask for valid 32-bit elements (depends on batch).
    batch >>= XNN_LOG2_SIZEOF_FLOAT;
    const __mmask16 vmask = _cvtu32_mask16((uint32_t) ((UINT32_C(1) << batch) - UINT32_C(1)));

    const __m512 va = _mm512_maskz_loadu_ps(vmask, input_a);
    $if OP == "PRELU":
      const __mmask16 vsign = _mm512_cmp_ps_mask(va, vzero, _CMP_LT_OQ);
      __m512 vacc = _mm512_mask_mul_ps(va, vsign, va, _mm512_maskz_loadu_ps(vmask, input_b));
      input_b += 16;
    $else:
      __m512 vacc = ${_MM512_MASKZ_OP_PS}(vmask, va, _mm512_maskz_loadu_ps(vmask, input_b));
      $if OP == "SQRDIFF":
        vacc = _mm512_maskz_mul_ps(vmask, vacc, vacc);

    _mm512_mask_storeu_ps(output, vmask, vacc);
  }
}
