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

$SSE_HEADER = {1: "xmmintrin.h", 2: "emmintrin.h", 4: "smmintrin.h"}[SSE]
#include <${SSE_HEADER}>

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


$_MM_OP_PS = {
$  "ADD": "_mm_add_ps",
$  "DIV": "_mm_div_ps",
$  "MAX": "_mm_max_ps",
$  "MIN": "_mm_min_ps",
$  "MUL": "_mm_mul_ps",
$  "SUB": "_mm_sub_ps",
$  "SQRDIFF": "_mm_sub_ps",
$  "PRELU": "_mm_mul_ps",
$}[OP]
$ISA = {1: "sse", 2: "sse2", 4: "sse41"}[SSE]
void xnn_f32_v${OP.lower()}_ukernel__${ISA}_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)]) XNN_OOB_READS
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input_a != NULL);
  assert(input_b != NULL);
  assert(output != NULL);

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

      const __m128 vb${ABC[0]} = _mm_loadu_ps(input_b);
      $for N in range(1, SIMD_TILE):
        const __m128 vb${ABC[N]} = _mm_loadu_ps(input_b + ${N * 4});
      input_b += ${BATCH_TILE};

      $for N in range(SIMD_TILE):
        __m128 vacc${ABC[N]} = ${_MM_OP_PS}(va${ABC[N]}, vb${ABC[N]});

      $if OP == "SQRDIFF":
        $for N in range(SIMD_TILE):
          vacc${ABC[N]} = _mm_mul_ps(vacc${ABC[N]}, vacc${ABC[N]});
      $elif OP == "PRELU":
        $for N in range(SIMD_TILE):
          $if SSE == 2:
            const __m128 vmask${ABC[N]} = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(va${ABC[N]})));

        $for N in range(SIMD_TILE):
          $if SSE == 2:
            vacc${ABC[N]} = _mm_or_ps(_mm_and_ps(vacc${ABC[N]}, vmask${ABC[N]}), _mm_andnot_ps(vmask${ABC[N]}, va${ABC[N]}));
          $elif SSE == 4:
            vacc${ABC[N]} = _mm_blendv_ps(va${ABC[N]}, vacc${ABC[N]}, va${ABC[N]});

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

    const __m128 vb = _mm_loadu_ps(input_b);
    input_b += 4;

    __m128 vacc = ${_MM_OP_PS}(va, vb);
    $if OP == "SQRDIFF":
      vacc = _mm_mul_ps(vacc, vacc);
    $elif OP == "PRELU":
      $if SSE == 2:
        const __m128 vmask = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(va)));
        vacc = _mm_or_ps(_mm_and_ps(vacc, vmask), _mm_andnot_ps(vmask, va));
      $elif SSE == 4:
        vacc = _mm_blendv_ps(va, vacc, va);

    _mm_storeu_ps(output, vacc);
    output += 4;
  }
  if XNN_UNLIKELY(batch != 0) {
    const __m128 va = _mm_loadu_ps(input_a);
    const __m128 vb = _mm_loadu_ps(input_b);

    __m128 vacc = ${_MM_OP_PS}(va, vb);
    $if OP == "SQRDIFF":
      vacc = _mm_mul_ps(vacc, vacc);
    $elif OP == "PRELU":
      $if SSE == 2:
        const __m128 vmask = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(va)));
        vacc = _mm_or_ps(_mm_and_ps(vacc, vmask), _mm_andnot_ps(vmask, va));
      $elif SSE == 4:
        vacc = _mm_blendv_ps(va, vacc, va);

    if (batch & (2 * sizeof(float))) {
      _mm_storel_pi((__m64*) output, vacc);
      vacc = _mm_movehl_ps(vacc, vacc);
      output += 2;
    }
    if (batch & (1 * sizeof(float))) {
      _mm_store_ss(output, vacc);
    }
  }
}
