// 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 BATCH_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>

#include <xnnpack/vunary.h>
#include <xnnpack/common.h>
#include <xnnpack/math.h>

void xnn_f32_vrelu_ukernel__scalar_x${BATCH_TILE}(
    size_t n,
    const float* x_ptr,
    float* y_ptr,
    const union xnn_f32_relu_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(n != 0);
  assert(n % sizeof(float) == 0);
  assert(x_ptr != NULL);
  assert(y_ptr != NULL);

  const uint32_t* x = (const uint32_t*)x_ptr;
  uint32_t* y = (uint32_t*)y_ptr;

  $if BATCH_TILE > 1:
    for (; n >= ${BATCH_TILE} * sizeof(uint32_t); n -= ${BATCH_TILE} * sizeof(uint32_t)) {
      $for N in range(BATCH_TILE):
        uint32_t vacc${ABC[N]} = x[${N}];
      x += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        vacc${ABC[N]} = ((vacc${ABC[N]} >> 31) - 1) & vacc${ABC[N]};

      $for N in range(BATCH_TILE):
        y[${N}] = vacc${ABC[N]};
      y += ${BATCH_TILE};
    }
    if XNN_UNLIKELY(n != 0) {
      $if BATCH_TILE > 2:
        do {
          uint32_t vacc = *x++;
          vacc =  ((vacc >> 31) - 1) & vacc;
          *y++ = vacc;
          n -= sizeof(uint32_t);
        } while (n != 0);
      $else:
        uint32_t vacc = *x;
        vacc =  ((vacc >> 31) - 1) & vacc;
        *y = vacc;
    }
  $else:
    for (; n >= sizeof(uint32_t); n -= sizeof(uint32_t)) {
      uint32_t vacc = *x++;
      vacc =  ((vacc >> 31) - 1) & vacc;
      *y++ = vacc;
    }
}
