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

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


void xnn_f32_vsigmoid_ukernel__scalar_rr2_p5_div_u${BATCH_TILE}(
    size_t batch,
    const float* input,
    float* output,
    const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)])
{
  assert(batch != 0);
  assert(batch % sizeof(float) == 0);
  assert(input != NULL);
  assert(output != NULL);

  const float vmagic_bias = 0x1.8000FEp23f;
  const float vminus_log2e = -0x1.715476p0f;
  const float vln2_hi = 0x1.62E400p-1f;
  const float vln2_lo = 0x1.7F7D1Cp-20f;
  const float vc5 = -0x1.0F9F9Cp-7f;
  const float vc4 = 0x1.573A1Ap-5f;
  const float vc3 = -0x1.555A80p-3f;
  const float vc2 = 0x1.FFFDC6p-2f;
  const float vc1 = -0x1.FFFFF6p-1f;
  const float vone = 1.0f;
  const float vdenorm_cutoff = 0x1.5D589Ep+6f;

  $if BATCH_TILE > 1:
    for (; batch >= ${BATCH_TILE} * sizeof(float); batch -= ${BATCH_TILE} * sizeof(float)) {
      $for N in range(BATCH_TILE):
        const float vx${N} = input[${N}];
      input += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        const float vz${N} = fabsf(vx${N});

      $for N in range(BATCH_TILE):
        float vn${N} = vz${N} * vminus_log2e + vmagic_bias;

      $for N in range(BATCH_TILE):
        const float vs${N} = uint32_as_float(float_as_uint32(vn${N}) << 23);

      $for N in range(BATCH_TILE):
        vn${N} -= vmagic_bias;

      $for N in range(BATCH_TILE):
        float vt${N} = vn${N} * vln2_hi + vz${N};

      $for N in range(BATCH_TILE):
        vt${N} = vn${N} * vln2_lo + vt${N};

      $for N in range(BATCH_TILE):
        float vp${N} = vt${N} * vc5 + vc4;

      $for N in range(BATCH_TILE):
        vp${N} = vt${N} * vp${N} + vc3;

      $for N in range(BATCH_TILE):
        vp${N} = vt${N} * vp${N} + vc2;

      $for N in range(BATCH_TILE):
        vp${N} = vt${N} * vp${N} + vc1;

      $for N in range(BATCH_TILE):
        vt${N} *= vs${N};

      $for N in range(BATCH_TILE):
        const float ve${N} = vt${N} * vp${N} + vs${N};

      $for N in range(BATCH_TILE):
        const float vd${N} = ve${N} + vone;

      $for N in range(BATCH_TILE):
        float vf${N} = ve${N} / vd${N};

      $for N in range(BATCH_TILE):
        if XNN_UNPREDICTABLE(vz${N} > vdenorm_cutoff) {
          vf${N} = 0.0f;
        }

      $for N in range(BATCH_TILE):
        if XNN_UNPREDICTABLE(vx${N} > 0.0f) {
          vf${N} = vone - vf${N};
        }

      $for N in range(BATCH_TILE):
        output[${N}] = vf${N};
      output += ${BATCH_TILE};
    }
  $if BATCH_TILE == 1:
    do {
      const float vx = *input++;

      const float vz = fabsf(vx);

      float vn = vz * vminus_log2e + vmagic_bias;
      const float vs = uint32_as_float(float_as_uint32(vn) << 23);
      vn -= vmagic_bias;

      float vt = vn * vln2_hi + vz;
      vt = vn * vln2_lo + vt;

      float vp = vt * vc5 + vc4;
      vp = vt * vp + vc3;
      vp = vt * vp + vc2;
      vp = vt * vp + vc1;

      vt *= vs;
      const float ve = vt * vp + vs;
      const float vd = ve + vone;

      float vf = ve / vd;
      if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
        vf = 0.0f;
      }
      if XNN_UNPREDICTABLE(vx > 0.0f) {
        vf = vone - vf;
      }

      *output++ = vf;

      batch -= sizeof(float);
    } while (batch != 0);
  $elif BATCH_TILE == 2:
    if XNN_UNLIKELY(batch != 0) {
      const float vx = *input;

      const float vz = fabsf(vx);

      float vn = vz * vminus_log2e + vmagic_bias;
      const float vs = uint32_as_float(float_as_uint32(vn) << 23);
      vn -= vmagic_bias;

      float vt = vn * vln2_hi + vz;
      vt = vn * vln2_lo + vt;

      float vp = vt * vc5 + vc4;
      vp = vt * vp + vc3;
      vp = vt * vp + vc2;
      vp = vt * vp + vc1;

      vt *= vs;
      const float ve = vt * vp + vs;
      const float vd = ve + vone;

      float vf = ve / vd;
      if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
        vf = 0.0f;
      }
      if XNN_UNPREDICTABLE(vx > 0.0f) {
        vf = vone - vf;
      }

      *output = vf;
    }
  $else:
    if XNN_UNLIKELY(batch != 0) {
      do {
        const float vx = *input++;

        const float vz = fabsf(vx);

        float vn = vz * vminus_log2e + vmagic_bias;
        const float vs = uint32_as_float(float_as_uint32(vn) << 23);
        vn -= vmagic_bias;

        float vt = vn * vln2_hi + vz;
        vt = vn * vln2_lo + vt;

        float vp = vt * vc5 + vc4;
        vp = vt * vp + vc3;
        vp = vt * vp + vc2;
        vp = vt * vp + vc1;

        vt *= vs;
        const float ve = vt * vp + vs;
        const float vd = ve + vone;

        float vf = ve / vd;
        if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
          vf = 0.0f;
        }
        if XNN_UNPREDICTABLE(vx > 0.0f) {
          vf = vone - vf;
        }

        *output++ = vf;

        batch -= sizeof(float);
      } while (batch != 0);
    }
}
