// Copyright 2021 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 REQUANTIZATION == "FP32"
$assert BATCH_TILE >= 1
#include <assert.h>

#include "xnnpack/math.h"
#include "xnnpack/vbinary.h"


$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vmulc_minmax_${REQUANTIZATION.lower()}_ukernel__scalar_u${BATCH_TILE}(
    size_t batch,
    const ${XINT8_T}* input_a,
    const ${XINT8_T}* input_b,
    ${XINT8_T}* output,
    const union xnn_${DATATYPE.lower()}_mul_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(${XINT8_T}) == 0);
  assert(input_a != NULL);
  assert(input_b != NULL);
  assert(output != NULL);

  const int32_t va_zero_point = params->scalar.a_zero_point;
  const float vscale = params->scalar.scale;
  const float voutput_min_less_zero_point = (int32_t) params->scalar.output_min - (int32_t) params->scalar.output_zero_point;
  const float voutput_max_less_zero_point = (int32_t) params->scalar.output_max - (int32_t) params->scalar.output_zero_point;
  const float vmagic_bias = 12582912.0f;
  const int32_t vmagic_bias_less_output_zero_point = INT32_C(0x4B400000) - (int32_t) params->scalar.output_zero_point;

  const int32_t vb = (int32_t) *input_b - params->scalar.b_zero_point;
  $if BATCH_TILE == 1:
    do {
      const int32_t va = (int32_t) *input_a++ - va_zero_point;
      const int32_t vacc = va * vb;

      float vfpacc = (float) vacc * vscale;
      vfpacc = math_max_f32(vfpacc, voutput_min_less_zero_point);
      vfpacc = math_min_f32(vfpacc, voutput_max_less_zero_point);
      vfpacc += vmagic_bias;
      const int32_t vout = (int32_t) float_as_uint32(vfpacc) - vmagic_bias_less_output_zero_point;
      *output++ = (${XINT8_T}) vout;

      batch -= sizeof(${XINT8_T});
    } while (batch != 0);
  $else:
    for (; batch >= ${BATCH_TILE} * sizeof(${XINT8_T}); batch -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
      $for N in range(BATCH_TILE):
        const int32_t va${N} = input_a[${N}] - va_zero_point;
      input_a += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        const int32_t vacc${N} = va${N} * vb;

      $for N in range(BATCH_TILE):
        float vfpacc${N} = (float) vacc${N} * vscale;

      $for N in range(BATCH_TILE):
        vfpacc${N} = math_max_f32(vfpacc${N}, voutput_min_less_zero_point);

      $for N in range(BATCH_TILE):
        vfpacc${N} = math_min_f32(vfpacc${N}, voutput_max_less_zero_point);

      $for N in range(BATCH_TILE):
        vfpacc${N} += vmagic_bias;

      $for N in range(BATCH_TILE):
        const int32_t vout${N} = (int32_t) float_as_uint32(vfpacc${N}) - vmagic_bias_less_output_zero_point;

      $for N in range(BATCH_TILE):
        output[${N}] = (${XINT8_T}) vout${N};
      output += ${BATCH_TILE};
    }
    if XNN_UNLIKELY(batch != 0) {
      $if BATCH_TILE == 2:
        const int32_t va = (int32_t) *input_a - va_zero_point;
        const int32_t vacc = va * vb;

        float vfpacc = (float) vacc * vscale;
        vfpacc = math_max_f32(vfpacc, voutput_min_less_zero_point);
        vfpacc = math_min_f32(vfpacc, voutput_max_less_zero_point);
        vfpacc += vmagic_bias;
        const int32_t vout = (int32_t) float_as_uint32(vfpacc) - vmagic_bias_less_output_zero_point;
        *output = (${XINT8_T}) vout;
      $else:
        do {
          const int32_t va = (int32_t) *input_a++ - va_zero_point;
          const int32_t vacc = va * vb;

          float vfpacc = (float) vacc * vscale;
          vfpacc = math_max_f32(vfpacc, voutput_min_less_zero_point);
          vfpacc = math_min_f32(vfpacc, voutput_max_less_zero_point);
          vfpacc += vmagic_bias;
          const int32_t vout = (int32_t) float_as_uint32(vfpacc) - vmagic_bias_less_output_zero_point;
          *output++ = (${XINT8_T}) vout;

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