// 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 DATATYPE in ["QS8", "QU8"]
$assert BATCH_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

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


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$OUTPUT_MIN = {"QS8": -128, "QU8": 0}[DATATYPE]
$OUTPUT_MAX = {"QS8": 127, "QU8": 255}[DATATYPE]
void xnn_${DATATYPE.lower()}_vlrelu_ukernel__scalar_andxor_x${BATCH_TILE}(
    size_t n,
    const ${XINT8_T}* x,
    ${XINT8_T}* y,
    const union xnn_${DATATYPE.lower()}_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  const int32_t vinput_zero_point = params->scalar_andxor.input_zero_point;
  const int32_t vmultiplier_diff = params->scalar_andxor.multiplier_diff;
  const int32_t vmultiplier_base = params->scalar_andxor.multiplier_base;
  const int32_t vbias = params->scalar_andxor.bias;
  $if BATCH_TILE == 1:
    do {
      int32_t vacc = (int32_t) *x++ - vinput_zero_point;
      const int32_t vmultiplier = vmultiplier_base ^ (vmultiplier_diff & math_asr_s32(vacc, 31));
      vacc = vbias + vacc * vmultiplier;

      int32_t vout = math_asr_s32(vacc, 8);
      vout = math_max_s32(vout, ${OUTPUT_MIN});
      vout = math_min_s32(vout, ${OUTPUT_MAX});
      *y++ = (${XINT8_T}) vout;

      n -= sizeof(${XINT8_T});
    } while (n != 0);
  $else:
    for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      $for N in range(BATCH_TILE):
        int32_t vacc${ABC[N]} = (int32_t) x[${N}];
      x += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        vacc${ABC[N]} -= vinput_zero_point;

      $for N in range(BATCH_TILE):
        int32_t vmultiplier${ABC[N]} = math_asr_s32(vacc${ABC[N]}, 31);

      $for N in range(BATCH_TILE):
        vmultiplier${ABC[N]} &= vmultiplier_diff;

      $for N in range(BATCH_TILE):
        vmultiplier${ABC[N]} ^= vmultiplier_base;

      $for N in range(BATCH_TILE):
        vacc${ABC[N]} = vbias + vacc${ABC[N]} * vmultiplier${ABC[N]};

      $for N in range(BATCH_TILE):
        int32_t vout${ABC[N]} = math_asr_s32(vacc${ABC[N]}, 8);

      $for N in range(BATCH_TILE):
        vout${ABC[N]} = math_max_s32(vout${ABC[N]}, ${OUTPUT_MIN});

      $for N in range(BATCH_TILE):
        vout${ABC[N]} = math_min_s32(vout${ABC[N]}, ${OUTPUT_MAX});

      $for N in range(BATCH_TILE):
        y[${N}] = (${XINT8_T}) vout${ABC[N]};
      y += ${BATCH_TILE};
    }
    if XNN_UNLIKELY(n != 0) {
      $if BATCH_TILE == 2:
        int32_t vacc = (int32_t) *x++ - vinput_zero_point;
        const int32_t vmultiplier = vmultiplier_base ^ (vmultiplier_diff & math_asr_s32(vacc, 31));
        vacc = vbias + vacc * vmultiplier;

        int32_t vout = math_asr_s32(vacc, 8);
        vout = math_max_s32(vout, ${OUTPUT_MIN});
        vout = math_min_s32(vout, ${OUTPUT_MAX});
        *y = (${XINT8_T}) vout;
      $else:
        do {
          int32_t vacc = (int32_t) *x++ - vinput_zero_point;
          const int32_t vmultiplier = vmultiplier_base ^ (vmultiplier_diff & math_asr_s32(vacc, 31));
          vacc = vbias + vacc * vmultiplier;

          int32_t vout = math_asr_s32(vacc, 8);
          vout = math_max_s32(vout, ${OUTPUT_MIN});
          vout = math_min_s32(vout, ${OUTPUT_MAX});
          *y++ = (${XINT8_T}) vout;

          n -= sizeof(${XINT8_T});
        } while (n != 0);
    }
}
