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

#include <arm_neon.h>

#include <xnnpack/math.h>
#include <xnnpack/prelu.h>


void xnn_f32_prelu_ukernel__neon_${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) XNN_OOB_READS
{
  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;

  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)) {
      $for C in range(0, CHANNEL_TILE, 4):
        const float32x4_t vw${ABC[C:C+4]} = vld1q_f32(w); w += 4;

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

      $for M in range(ROW_TILE):
        $for C in range(0, CHANNEL_TILE, 4):
          float32x4_t vacc${M}x${ABC[C:C+4]} = vmulq_f32(vi${M}x${ABC[C:C+4]}, vw${ABC[C:C+4]});
          const uint32x4_t vm${M}x${ABC[C:C+4]} = vcltq_s32(vreinterpretq_s32_f32(vi${M}x${ABC[C:C+4]}), vmovq_n_s32(0));

      $for M in range(ROW_TILE):
        $for C in range(0, CHANNEL_TILE, 4):
          vacc${M}x${ABC[C:C+4]} = vbslq_f32(vm${M}x${ABC[C:C+4]}, vacc${M}x${ABC[C:C+4]}, vi${M}x${ABC[C:C+4]});

      $for M in range(ROW_TILE):
        $for C in range(0, CHANNEL_TILE, 4):
          vst1q_f32(o${M}, vacc${M}x${ABC[C:C+4]}); o${M} += 4;
    }
    $if CHANNEL_TILE != 4:
      for (; c >= 4 * sizeof(float); c -= 4 * sizeof(float)) {
        const float32x4_t vw0123 = vld1q_f32(w); w += 4;

        $for M in range(ROW_TILE):
          const float32x4_t vi${M}x0123 = vld1q_f32(i${M});
          i${M} += 4;

        $for M in range(ROW_TILE):
          float32x4_t vacc${M}x0123 = vmulq_f32(vi${M}x0123, vw0123);
          const uint32x4_t vm${M}x0123 = vcltq_s32(vreinterpretq_s32_f32(vi${M}x0123), vmovq_n_s32(0));

        $for M in range(ROW_TILE):
          vacc${M}x0123 = vbslq_f32(vm${M}x0123, vacc${M}x0123, vi${M}x0123);

        $for M in range(ROW_TILE):
          vst1q_f32(o${M}, vacc${M}x0123); o${M} += 4;
      }
    if XNN_UNLIKELY(c != 0) {
      const float32x4_t vw0123 = vld1q_f32(w); w += 4;

      $for M in range(ROW_TILE):
        const float32x4_t vi${M}x0123 = vld1q_f32(i${M});
        i${M} = (const float*) ((uintptr_t) i${M} + c);

      $for M in range(ROW_TILE):
        float32x4_t vacc${M}x0123 = vmulq_f32(vi${M}x0123, vw0123);
        const uint32x4_t vm${M}x0123 = vcltq_s32(vreinterpretq_s32_f32(vi${M}x0123), vmovq_n_s32(0));

      $for M in range(ROW_TILE):
        vacc${M}x0123 = vbslq_f32(vm${M}x0123, vacc${M}x0123, vi${M}x0123);

      $for M in range(ROW_TILE):
        float32x2_t vacc${M}x01 = vget_low_f32(vacc${M}x0123);
      if (c & (2 * sizeof(float))) {
        $for M in range(ROW_TILE):
          vst1_f32(o${M}, vacc${M}x01); o${M} += 2;

        $for M in range(ROW_TILE):
          vacc${M}x01 = vget_high_f32(vacc${M}x0123);
      }
      if (c & (1 * sizeof(float))) {
        $for M in range(ROW_TILE):
          vst1_lane_f32(o${M}, vacc${M}x01, 0); o${M} += 1;
      }
    }
    $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);
}
