// Copyright 2020 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 % 16 == 0
$assert CHANNEL_TILE >= 16
$assert ROW_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <immintrin.h>

#include <xnnpack/intrinsics-polyfill.h>
#include <xnnpack/math.h>
#include <xnnpack/prelu.h>


void xnn_f32_prelu_ukernel__avx512f_${ROW_TILE}x${CHANNEL_TILE}(
    size_t rows,
    size_t channels,
    const float*restrict input,
    size_t input_stride,
    const float*restrict weights,
    float*restrict output,
    size_t output_stride)
{
  assert(rows != 0);
  assert(channels != 0);
  assert(channels % sizeof(float) == 0);

  const float* i0 = input;
  float* o0 = output;
  $for M in range(1, ROW_TILE):
    const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride);
    float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride);

  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
  const size_t output_increment = output_stride * ${ROW_TILE} - channels;

  const __m512 vzero = _mm512_setzero_ps();
  do {
    $for M in range(1, ROW_TILE):
      $if M % 2 == 0:
        if XNN_UNPREDICTABLE(rows <= ${M}) {
          i${M} = i${M-1};
          o${M} = o${M-1};
        }
      $else:
        if XNN_UNPREDICTABLE(rows < ${M+1}) {
          i${M} = i${M-1};
          o${M} = o${M-1};
        }

    const float* w = weights;
    size_t c = channels;
    for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
      const __m512 vw${ABC[0:16]} = _mm512_load_ps(w);
      $for C in range(16, CHANNEL_TILE, 16):
        const __m512 vw${ABC[C:C+16]} = _mm512_load_ps(w + ${C});
      w += ${CHANNEL_TILE};

      $for M in range(ROW_TILE):
        const __m512 vi${M}x${ABC[0:16]} = _mm512_loadu_ps(i${M});
        $for C in range(16, CHANNEL_TILE, 16):
          const __m512 vi${M}x${ABC[C:C+16]} = _mm512_loadu_ps(i${M} + ${C});
        i${M} += ${CHANNEL_TILE};

      $for M in range(ROW_TILE):
        $for C in range(0, CHANNEL_TILE, 16):
          const __mmask16 vsign${M}x${ABC[C:C+16]} = _mm512_cmp_ps_mask(vi${M}x${ABC[C:C+16]}, vzero, _CMP_LT_OQ);
          const __m512 vacc${M}x${ABC[C:C+16]} = _mm512_mask_mul_ps(vi${M}x${ABC[C:C+16]}, vsign${M}x${ABC[C:C+16]}, vi${M}x${ABC[C:C+16]}, vw${ABC[C:C+16]});

      $for M in range(ROW_TILE):
        _mm512_storeu_ps(o${M}, vacc${M}x${ABC[0:16]});
        $for C in range(16, CHANNEL_TILE, 16):
          _mm512_storeu_ps(o${M} + ${C}, vacc${M}x${ABC[C:C+16]});
        o${M} += ${CHANNEL_TILE};
    }
    $if CHANNEL_TILE > 16:
      for (; c >= 16 * sizeof(float); c -= 16 * sizeof(float)) {
        const __m512 vw = _mm512_load_ps(w);
        w += 16;

        $for M in range(ROW_TILE):
          const __m512 vi${M} = _mm512_loadu_ps(i${M});
          i${M} += 16;

        $for M in range(ROW_TILE):
          const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ);
          const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw);

        $for M in range(ROW_TILE):
          _mm512_storeu_ps(o${M}, vacc${M});
          o${M} += 16;
      }
    if XNN_UNLIKELY(c != 0) {
      assert(c >= 1 * sizeof(float));
      assert(c <= 15 * sizeof(float));
      // Prepare mask for valid 32-bit elements (depends on c).
      const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << (c >> 2 /* log2(sizeof(float))*/)) - UINT32_C(1)));

      const __m512 vw = _mm512_maskz_loadu_ps(vmask, w);

      $for M in range(ROW_TILE):
        const __m512 vi${M} = _mm512_maskz_loadu_ps(vmask, i${M});
        i${M} = (const float*) ((uintptr_t) i${M} + c);

      $for M in range(ROW_TILE):
        const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ);
        const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw);

      $for M in range(ROW_TILE):
        _mm512_mask_storeu_ps(o${M}, vmask, vacc${M});
        o${M} = (float*) ((uintptr_t) o${M} + c);
    }
    $for M in range(ROW_TILE):
      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
    rows = doz(rows, ${ROW_TILE});
  } while (rows != 0);
}
