// Copyright 2022 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 >= 8
$assert BATCH_TILE == 8 or BATCH_TILE % 16 == 0
$SIMD_TILE = BATCH_TILE // 16
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <arm_neon.h>

#include "xnnpack/common.h"
#include "xnnpack/vunary.h"


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$XINT8X8_T = {"QS8": "int8x8_t", "QU8": "uint8x8_t"}[DATATYPE]
$XINT8X16_T = {"QS8": "int8x16_t", "QU8": "uint8x16_t"}[DATATYPE]
$XINT16X8_T = {"QS8": "int16x8_t", "QU8": "uint16x8_t"}[DATATYPE]
$VLD1_X8 = {"QS8": "vld1_s8", "QU8": "vld1_u8"}[DATATYPE]
$VLD1Q_X8 = {"QS8": "vld1q_s8", "QU8": "vld1q_u8"}[DATATYPE]
$VLD1Q_DUP_X16 = {"QS8": "vld1q_dup_s16", "QU8": "vld1q_dup_u16"}[DATATYPE]
$VQMOVXN_S16 = {"QS8": "vqmovn_s16", "QU8": "vqmovun_s16"}[DATATYPE]
$VGET_LOW_X8 = {"QS8": "vget_low_s8", "QU8": "vget_low_u8"}[DATATYPE]
$VGET_HIGH_X8 = {"QS8": "vget_high_s8", "QU8": "vget_high_u8"}[DATATYPE]
$VCOMBINE_X8 = {"QS8": "vcombine_s8", "QU8": "vcombine_u8"}[DATATYPE]
$VREINTERPRET_U32_X8 = {"QS8": "vreinterpret_u32_s8", "QU8": "vreinterpret_u32_u8"}[DATATYPE]
$VREINTERPRET_U16_X8 = {"QS8": "vreinterpret_u16_s8", "QU8": "vreinterpret_u16_u8"}[DATATYPE]
$VEXT_X8 = {"QS8": "vext_s8", "QU8": "vext_u8"}[DATATYPE]
$VST1_X8 = {"QS8": "vst1_s8", "QU8": "vst1_u8"}[DATATYPE]
$VST1Q_X8 = {"QS8": "vst1q_s8", "QU8": "vst1q_u8"}[DATATYPE]
$VST1_LANE_X8 = {"QS8": "vst1_lane_s8", "QU8": "vst1_lane_u8"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vlrelu_ukernel__neon_u${BATCH_TILE}(
    size_t batch,
    const ${XINT8_T}* input,
    ${XINT8_T}* output,
    const struct xnn_${DATATYPE.lower()}_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
{
  assert(batch != 0);
  assert(batch % sizeof(${XINT8_T}) == 0);
  assert(input != NULL);
  assert(output != NULL);

  const ${XINT16X8_T} vinput_zero_point = ${VLD1Q_DUP_X16}(&params->scalar.input_zero_point);
  const int16x8_t vpositive_multiplier = vdupq_n_s16(-params->scalar.positive_multiplier);
  const int16x8_t vnegative_multiplier = vdupq_n_s16(-params->scalar.negative_multiplier);
  const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->scalar.output_zero_point);
  $if BATCH_TILE > 8:
    for (; batch >= ${BATCH_TILE} * sizeof(${XINT8_T}); batch -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      $for N in range(SIMD_TILE):
        const ${XINT8X16_T} vx${ABC[N]} = ${VLD1Q_X8}(input); input += 16;

      $for N in range(SIMD_TILE):
        $if DATATYPE == "QU8":
          int16x8_t vacc${ABC[2*N]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]})));
          int16x8_t vacc${ABC[2*N+1]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]})));
        $else:
          int16x8_t vacc${ABC[2*N]} = vsubw_s8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]}));
          int16x8_t vacc${ABC[2*N+1]} = vsubw_s8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]}));

      $for N in range(2*SIMD_TILE):
        const uint16x8_t vmask${ABC[N]} = vcltq_s16(vacc${ABC[N]}, vmovq_n_s16(0));

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vshlq_n_s16(vacc${ABC[N]}, 7);

      $for N in range(2*SIMD_TILE):
        const int16x8_t vmultiplier${ABC[N]} = vbslq_s16(vmask${ABC[N]}, vpositive_multiplier, vnegative_multiplier);

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vqrdmulhq_s16(vacc${ABC[N]}, vmultiplier${ABC[N]});

      $for N in range(2*SIMD_TILE):
        vacc${ABC[N]} = vqaddq_s16(vacc${ABC[N]}, voutput_zero_point);

      $for N in range(SIMD_TILE):
        const ${XINT8X16_T} vy${ABC[N]} = ${VCOMBINE_X8}(${VQMOVXN_S16}(vacc${ABC[2*N]}), ${VQMOVXN_S16}(vacc${ABC[2*N+1]}));

      $for N in range(SIMD_TILE):
        ${VST1Q_X8}(output, vy${ABC[N]}); output += 16;
    }
  for (; batch >= 8 * sizeof(${XINT8_T}); batch -= 8 * sizeof(${XINT8_T})) {
    const ${XINT8X8_T} vx = ${VLD1_X8}(input); input += 8;
    $if DATATYPE == "QU8":
      int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
    $else:
      int16x8_t vacc = vsubw_s8(vinput_zero_point, vx);
    const uint16x8_t vmask = vcltq_s16(vacc, vmovq_n_s16(0));
    vacc = vshlq_n_s16(vacc, 7);
    const int16x8_t vmultiplier = vbslq_s16(vmask, vpositive_multiplier, vnegative_multiplier);
    vacc = vqrdmulhq_s16(vacc, vmultiplier);
    vacc = vqaddq_s16(vacc, voutput_zero_point);
    const ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc);
    ${VST1_X8}(output, vy); output += 8;
  }
  if XNN_UNLIKELY(batch != 0) {
    assert(batch >= 1 * sizeof(${XINT8_T}));
    assert(batch <= 7 * sizeof(${XINT8_T}));

    const ${XINT8X8_T} vx = ${VLD1_X8}(input);
    $if DATATYPE == "QU8":
      int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
    $else:
      int16x8_t vacc = vsubw_s8(vinput_zero_point, vx);
    const uint16x8_t vmask = vcltq_s16(vacc, vmovq_n_s16(0));
    vacc = vshlq_n_s16(vacc, 7);
    const int16x8_t vmultiplier = vbslq_s16(vmask, vpositive_multiplier, vnegative_multiplier);
    vacc = vqrdmulhq_s16(vacc, vmultiplier);
    vacc = vqaddq_s16(vacc, voutput_zero_point);
    ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc);

    if (batch & (4 * sizeof(${XINT8_T}))) {
      vst1_lane_u32((void*) output, ${VREINTERPRET_U32_X8}(vy), 0); output += 4;
      vy = ${VEXT_X8}(vy, vy, 4);
    }
    if (batch & (2 * sizeof(${XINT8_T}))) {
      vst1_lane_u16((void*) output, ${VREINTERPRET_U16_X8}(vy), 0); output += 2;
      vy = ${VEXT_X8}(vy, vy, 2);
    }
    if (batch & (1 * sizeof(${XINT8_T}))) {
      ${VST1_LANE_X8}(output, vy, 0);
    }
  }
}
