F32 Sigmoid micro-kernels in AVX2 implementation
PiperOrigin-RevId: 288397166
diff --git a/src/f32-sigmoid/avx2-p5.c.in b/src/f32-sigmoid/avx2-p5.c.in
new file mode 100644
index 0000000..2ebfe87
--- /dev/null
+++ b/src/f32-sigmoid/avx2-p5.c.in
@@ -0,0 +1,315 @@
+// 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 % 8 == 0
+$assert BATCH_TILE >= 8
+$assert RR_STEPS in [1, 2]
+$assert DIV_ALGO in ["div", "nr1fma", "nr2fma"]
+$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+$SIMD_TILE = BATCH_TILE // 8
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr${RR_STEPS}_p5_${DIV_ALGO}_x${BATCH_TILE}(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ $if RR_STEPS == 1:
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ $else:
+ const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ $if BATCH_TILE > 8:
+ for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
+ const __m256 vx${ABC[0]} = _mm256_loadu_ps(x);
+ $for N in range(1, SIMD_TILE):
+ const __m256 vx${ABC[N]} = _mm256_loadu_ps(x + ${N * 8});
+ x += ${BATCH_TILE};
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ $for N in range(SIMD_TILE):
+ const __m256 vz${ABC[N]} = _mm256_or_ps(vx${ABC[N]}, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ $for N in range(SIMD_TILE):
+ __m256 vn${ABC[N]} = _mm256_fmadd_ps(vz${ABC[N]}, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ $for N in range(SIMD_TILE):
+ const __m256 vs${ABC[N]} = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn${ABC[N]}), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ $for N in range(SIMD_TILE):
+ vn${ABC[N]} = _mm256_sub_ps(vn${ABC[N]}, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ $if RR_STEPS == 1:
+ $for N in range(SIMD_TILE):
+ __m256 vt${ABC[N]} = _mm256_fmadd_ps(vn${ABC[N]}, vminus_ln2, vz${ABC[N]});
+ $else:
+ // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
+ $for N in range(SIMD_TILE):
+ __m256 vt${ABC[N]} = _mm256_fmadd_ps(vn${ABC[N]}, vminus_ln2_hi, vz${ABC[N]});
+
+ $for N in range(SIMD_TILE):
+ vt${ABC[N]} = _mm256_fmadd_ps(vn${ABC[N]}, vminus_ln2_lo, vt${ABC[N]});
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ $for N in range(SIMD_TILE):
+ __m256 vp${ABC[N]} = _mm256_fmadd_ps(vc5, vt${ABC[N]}, vc4);
+
+ $for N in range(SIMD_TILE):
+ vp${ABC[N]} = _mm256_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc3);
+
+ $for N in range(SIMD_TILE):
+ vp${ABC[N]} = _mm256_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc2);
+
+ $for N in range(SIMD_TILE):
+ vp${ABC[N]} = _mm256_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ $for N in range(SIMD_TILE):
+ vt${ABC[N]} = _mm256_mul_ps(vt${ABC[N]}, vs${ABC[N]});
+
+ $for N in range(SIMD_TILE):
+ const __m256 ve${ABC[N]} = _mm256_fmadd_ps(vt${ABC[N]}, vp${ABC[N]}, vs${ABC[N]});
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ $for N in range(SIMD_TILE):
+ const __m256 vd${ABC[N]} = _mm256_add_ps(ve${ABC[N]}, vone);
+
+ $if DIV_ALGO == "div":
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ $for N in range(SIMD_TILE):
+ __m256 vf${ABC[N]} = _mm256_div_ps(ve${ABC[N]}, vd${ABC[N]});
+ $else:
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ $for N in range(SIMD_TILE):
+ __m256 vr${ABC[N]} = _mm256_rcp_ps(vd${ABC[N]});
+
+ $for N in range(SIMD_TILE):
+ vr${ABC[N]} = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]});
+
+ $if DIV_ALGO == "nr2fma":
+ $for N in range(SIMD_TILE):
+ vr${ABC[N]} = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]});
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ $for N in range(SIMD_TILE):
+ __m256 vf${ABC[N]} = _mm256_mul_ps(ve${ABC[N]}, vr${ABC[N]});
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ $for N in range(SIMD_TILE):
+ vf${ABC[N]} = _mm256_andnot_ps(_mm256_cmp_ps(vz${ABC[N]}, vdenorm_cutoff, _CMP_LT_OS), vf${ABC[N]});
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ $for N in range(SIMD_TILE):
+ vf${ABC[N]} = _mm256_blendv_ps(_mm256_sub_ps(vone, vf${ABC[N]}), vf${ABC[N]}, vx${ABC[N]});
+
+ _mm256_storeu_ps(y, vf${ABC[0]});
+ $for N in range(1, SIMD_TILE):
+ _mm256_storeu_ps(y + ${N * 8}, vf${ABC[N]});
+ y += ${BATCH_TILE};
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ $if RR_STEPS == 1:
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+ $else:
+ // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz);
+ vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ $if DIV_ALGO == "div":
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+ $else:
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ $if DIV_ALGO == "nr2fma":
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ $if RR_STEPS == 1:
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+ $else:
+ // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz);
+ vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ $if DIV_ALGO == "div":
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+ $else:
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ $if DIV_ALGO == "nr2fma":
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x16.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x16.c
new file mode 100644
index 0000000..e10049c
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x16.c
@@ -0,0 +1,246 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x16(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ x += 16;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ y += 16;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x24.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x24.c
new file mode 100644
index 0000000..c588f32
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x24.c
@@ -0,0 +1,263 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x24(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ x += 24;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ y += 24;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x32.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x32.c
new file mode 100644
index 0000000..a461b4a
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x32.c
@@ -0,0 +1,280 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x32(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 32 * sizeof(float); n -= 32 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ x += 32;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ y += 32;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x40.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x40.c
new file mode 100644
index 0000000..ffb8371
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x40.c
@@ -0,0 +1,297 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x40(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 40 * sizeof(float); n -= 40 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ x += 40;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ y += 40;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x48.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x48.c
new file mode 100644
index 0000000..25e4d0c
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x48.c
@@ -0,0 +1,314 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x48(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 48 * sizeof(float); n -= 48 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ x += 48;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+ __m256 vf5 = _mm256_div_ps(ve5, vd5);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ y += 48;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x56.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x56.c
new file mode 100644
index 0000000..68957bd
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x56.c
@@ -0,0 +1,331 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x56(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 56 * sizeof(float); n -= 56 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ x += 56;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+ __m256 vf5 = _mm256_div_ps(ve5, vd5);
+ __m256 vf6 = _mm256_div_ps(ve6, vd6);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ y += 56;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x64.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x64.c
new file mode 100644
index 0000000..907cf4b
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x64.c
@@ -0,0 +1,348 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x64(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 64 * sizeof(float); n -= 64 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ x += 64;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+ __m256 vf5 = _mm256_div_ps(ve5, vd5);
+ __m256 vf6 = _mm256_div_ps(ve6, vd6);
+ __m256 vf7 = _mm256_div_ps(ve7, vd7);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ y += 64;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x72.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x72.c
new file mode 100644
index 0000000..ce7795d
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x72.c
@@ -0,0 +1,365 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x72(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 72 * sizeof(float); n -= 72 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ x += 72;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+ __m256 vf5 = _mm256_div_ps(ve5, vd5);
+ __m256 vf6 = _mm256_div_ps(ve6, vd6);
+ __m256 vf7 = _mm256_div_ps(ve7, vd7);
+ __m256 vf8 = _mm256_div_ps(ve8, vd8);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ y += 72;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x8.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x8.c
new file mode 100644
index 0000000..fee99ca
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x8.c
@@ -0,0 +1,165 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x8(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-div-x80.c b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x80.c
new file mode 100644
index 0000000..76f1104
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-div-x80.c
@@ -0,0 +1,382 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x80(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 80 * sizeof(float); n -= 80 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ const __m256 vx9 = _mm256_loadu_ps(x + 72);
+ x += 80;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+ const __m256 vz9 = _mm256_or_ps(vx9, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+ __m256 vn9 = _mm256_fmadd_ps(vz9, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+ const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn9), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+ vn9 = _mm256_sub_ps(vn9, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+ __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2, vz9);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+ __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+ vt9 = _mm256_mul_ps(vt9, vs9);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+ const __m256 ve9 = _mm256_fmadd_ps(vt9, vp9, vs9);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+ const __m256 vd9 = _mm256_add_ps(ve9, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf0 = _mm256_div_ps(ve0, vd0);
+ __m256 vf1 = _mm256_div_ps(ve1, vd1);
+ __m256 vf2 = _mm256_div_ps(ve2, vd2);
+ __m256 vf3 = _mm256_div_ps(ve3, vd3);
+ __m256 vf4 = _mm256_div_ps(ve4, vd4);
+ __m256 vf5 = _mm256_div_ps(ve5, vd5);
+ __m256 vf6 = _mm256_div_ps(ve6, vd6);
+ __m256 vf7 = _mm256_div_ps(ve7, vd7);
+ __m256 vf8 = _mm256_div_ps(ve8, vd8);
+ __m256 vf9 = _mm256_div_ps(ve9, vd9);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+ vf9 = _mm256_andnot_ps(_mm256_cmp_ps(vz9, vdenorm_cutoff, _CMP_LT_OS), vf9);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+ vf9 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf9), vf9, vx9);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ _mm256_storeu_ps(y + 72, vf9);
+ y += 80;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
+ __m256 vf = _mm256_div_ps(ve, vd);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x16.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x16.c
new file mode 100644
index 0000000..559ccb2
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x16.c
@@ -0,0 +1,268 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x16(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ x += 16;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ y += 16;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x24.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x24.c
new file mode 100644
index 0000000..7472015
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x24.c
@@ -0,0 +1,287 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x24(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ x += 24;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ y += 24;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x32.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x32.c
new file mode 100644
index 0000000..b02763e
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x32.c
@@ -0,0 +1,306 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x32(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 32 * sizeof(float); n -= 32 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ x += 32;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ y += 32;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x40.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x40.c
new file mode 100644
index 0000000..40b4a69
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x40.c
@@ -0,0 +1,325 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x40(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 40 * sizeof(float); n -= 40 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ x += 40;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ y += 40;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x48.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x48.c
new file mode 100644
index 0000000..d226873
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x48.c
@@ -0,0 +1,344 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x48(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 48 * sizeof(float); n -= 48 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ x += 48;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ y += 48;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x56.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x56.c
new file mode 100644
index 0000000..50f3d32
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x56.c
@@ -0,0 +1,363 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x56(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 56 * sizeof(float); n -= 56 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ x += 56;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ y += 56;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x64.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x64.c
new file mode 100644
index 0000000..22d9817
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x64.c
@@ -0,0 +1,382 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x64(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 64 * sizeof(float); n -= 64 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ x += 64;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ y += 64;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x72.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x72.c
new file mode 100644
index 0000000..3c8b19a
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x72.c
@@ -0,0 +1,401 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x72(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 72 * sizeof(float); n -= 72 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ x += 72;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+ __m256 vr8 = _mm256_rcp_ps(vd8);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+ __m256 vf8 = _mm256_mul_ps(ve8, vr8);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ y += 72;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x8.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x8.c
new file mode 100644
index 0000000..594dc8f
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x8.c
@@ -0,0 +1,177 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x8(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x80.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x80.c
new file mode 100644
index 0000000..5e29a80
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr1fma-x80.c
@@ -0,0 +1,420 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x80(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 80 * sizeof(float); n -= 80 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ const __m256 vx9 = _mm256_loadu_ps(x + 72);
+ x += 80;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+ const __m256 vz9 = _mm256_or_ps(vx9, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+ __m256 vn9 = _mm256_fmadd_ps(vz9, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+ const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn9), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+ vn9 = _mm256_sub_ps(vn9, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+ __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2, vz9);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+ __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+ vt9 = _mm256_mul_ps(vt9, vs9);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+ const __m256 ve9 = _mm256_fmadd_ps(vt9, vp9, vs9);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+ const __m256 vd9 = _mm256_add_ps(ve9, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+ __m256 vr8 = _mm256_rcp_ps(vd8);
+ __m256 vr9 = _mm256_rcp_ps(vd9);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+ vr9 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr9, vd9, vone), vr9, vr9);
+
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+ __m256 vf8 = _mm256_mul_ps(ve8, vr8);
+ __m256 vf9 = _mm256_mul_ps(ve9, vr9);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+ vf9 = _mm256_andnot_ps(_mm256_cmp_ps(vz9, vdenorm_cutoff, _CMP_LT_OS), vf9);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+ vf9 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf9), vf9, vx9);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ _mm256_storeu_ps(y + 72, vf9);
+ y += 80;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x16.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x16.c
new file mode 100644
index 0000000..dde224c
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x16.c
@@ -0,0 +1,272 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x16(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ x += 16;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ y += 16;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x24.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x24.c
new file mode 100644
index 0000000..f60515e
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x24.c
@@ -0,0 +1,292 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x24(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ x += 24;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ y += 24;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x32.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x32.c
new file mode 100644
index 0000000..58f8b0c
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x32.c
@@ -0,0 +1,312 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x32(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 32 * sizeof(float); n -= 32 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ x += 32;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ y += 32;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x40.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x40.c
new file mode 100644
index 0000000..8c66197
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x40.c
@@ -0,0 +1,332 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x40(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 40 * sizeof(float); n -= 40 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ x += 40;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ y += 40;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x48.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x48.c
new file mode 100644
index 0000000..744d77d
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x48.c
@@ -0,0 +1,352 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x48(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 48 * sizeof(float); n -= 48 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ x += 48;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ y += 48;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x56.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x56.c
new file mode 100644
index 0000000..b61b3c5
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x56.c
@@ -0,0 +1,372 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x56(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 56 * sizeof(float); n -= 56 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ x += 56;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ y += 56;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x64.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x64.c
new file mode 100644
index 0000000..72ee291
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x64.c
@@ -0,0 +1,392 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x64(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 64 * sizeof(float); n -= 64 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ x += 64;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ y += 64;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x72.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x72.c
new file mode 100644
index 0000000..edc2949
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x72.c
@@ -0,0 +1,412 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x72(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 72 * sizeof(float); n -= 72 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ x += 72;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+ __m256 vr8 = _mm256_rcp_ps(vd8);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+ __m256 vf8 = _mm256_mul_ps(ve8, vr8);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ y += 72;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x8.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x8.c
new file mode 100644
index 0000000..467036a
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x8.c
@@ -0,0 +1,179 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x8(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x80.c b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x80.c
new file mode 100644
index 0000000..c95f664
--- /dev/null
+++ b/src/f32-sigmoid/gen/avx2-rr1-p5-nr2fma-x80.c
@@ -0,0 +1,432 @@
+// Auto-generated file. Do not edit!
+// Template: src/f32-sigmoid/avx2-p5.c.in
+// Generator: tools/xngen
+//
+// 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.
+
+#include <assert.h>
+
+#include <immintrin.h>
+
+#include <xnnpack/common.h>
+#include <xnnpack/vunary.h>
+
+
+static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
+
+void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x80(
+ size_t n,
+ const float* x,
+ float* y,
+ const void* params)
+{
+ assert(n % sizeof(float) == 0);
+
+ const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
+ // The smallest x for which sigmoidf(x) is normalized.
+ // This number is also the smallest x for which expf(x) is normalized.
+ const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
+ const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
+ const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
+ const __m256 vone = _mm256_set1_ps(1.0f);
+ const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
+
+ const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
+ const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
+ const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
+ const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
+ const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
+
+ for (; n >= 80 * sizeof(float); n -= 80 * sizeof(float)) {
+ const __m256 vx0 = _mm256_loadu_ps(x);
+ const __m256 vx1 = _mm256_loadu_ps(x + 8);
+ const __m256 vx2 = _mm256_loadu_ps(x + 16);
+ const __m256 vx3 = _mm256_loadu_ps(x + 24);
+ const __m256 vx4 = _mm256_loadu_ps(x + 32);
+ const __m256 vx5 = _mm256_loadu_ps(x + 40);
+ const __m256 vx6 = _mm256_loadu_ps(x + 48);
+ const __m256 vx7 = _mm256_loadu_ps(x + 56);
+ const __m256 vx8 = _mm256_loadu_ps(x + 64);
+ const __m256 vx9 = _mm256_loadu_ps(x + 72);
+ x += 80;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
+ const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
+ const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
+ const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
+ const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
+ const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
+ const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
+ const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
+ const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
+ const __m256 vz9 = _mm256_or_ps(vx9, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
+ __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
+ __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
+ __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
+ __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
+ __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
+ __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
+ __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
+ __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
+ __m256 vn9 = _mm256_fmadd_ps(vz9, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
+ const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
+ const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
+ const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
+ const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
+ const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
+ const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
+ const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
+ const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
+ const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn9), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn0 = _mm256_sub_ps(vn0, vmagic_bias);
+ vn1 = _mm256_sub_ps(vn1, vmagic_bias);
+ vn2 = _mm256_sub_ps(vn2, vmagic_bias);
+ vn3 = _mm256_sub_ps(vn3, vmagic_bias);
+ vn4 = _mm256_sub_ps(vn4, vmagic_bias);
+ vn5 = _mm256_sub_ps(vn5, vmagic_bias);
+ vn6 = _mm256_sub_ps(vn6, vmagic_bias);
+ vn7 = _mm256_sub_ps(vn7, vmagic_bias);
+ vn8 = _mm256_sub_ps(vn8, vmagic_bias);
+ vn9 = _mm256_sub_ps(vn9, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
+ __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
+ __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
+ __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
+ __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
+ __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
+ __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
+ __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
+ __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
+ __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2, vz9);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
+ __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
+ __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
+ __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
+ __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
+ __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
+ __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
+ __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
+ __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
+ __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
+
+ vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
+ vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
+ vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
+ vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
+ vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
+ vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
+ vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
+ vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
+ vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
+ vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt0 = _mm256_mul_ps(vt0, vs0);
+ vt1 = _mm256_mul_ps(vt1, vs1);
+ vt2 = _mm256_mul_ps(vt2, vs2);
+ vt3 = _mm256_mul_ps(vt3, vs3);
+ vt4 = _mm256_mul_ps(vt4, vs4);
+ vt5 = _mm256_mul_ps(vt5, vs5);
+ vt6 = _mm256_mul_ps(vt6, vs6);
+ vt7 = _mm256_mul_ps(vt7, vs7);
+ vt8 = _mm256_mul_ps(vt8, vs8);
+ vt9 = _mm256_mul_ps(vt9, vs9);
+
+ const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
+ const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
+ const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
+ const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
+ const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
+ const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
+ const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
+ const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
+ const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
+ const __m256 ve9 = _mm256_fmadd_ps(vt9, vp9, vs9);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd0 = _mm256_add_ps(ve0, vone);
+ const __m256 vd1 = _mm256_add_ps(ve1, vone);
+ const __m256 vd2 = _mm256_add_ps(ve2, vone);
+ const __m256 vd3 = _mm256_add_ps(ve3, vone);
+ const __m256 vd4 = _mm256_add_ps(ve4, vone);
+ const __m256 vd5 = _mm256_add_ps(ve5, vone);
+ const __m256 vd6 = _mm256_add_ps(ve6, vone);
+ const __m256 vd7 = _mm256_add_ps(ve7, vone);
+ const __m256 vd8 = _mm256_add_ps(ve8, vone);
+ const __m256 vd9 = _mm256_add_ps(ve9, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr0 = _mm256_rcp_ps(vd0);
+ __m256 vr1 = _mm256_rcp_ps(vd1);
+ __m256 vr2 = _mm256_rcp_ps(vd2);
+ __m256 vr3 = _mm256_rcp_ps(vd3);
+ __m256 vr4 = _mm256_rcp_ps(vd4);
+ __m256 vr5 = _mm256_rcp_ps(vd5);
+ __m256 vr6 = _mm256_rcp_ps(vd6);
+ __m256 vr7 = _mm256_rcp_ps(vd7);
+ __m256 vr8 = _mm256_rcp_ps(vd8);
+ __m256 vr9 = _mm256_rcp_ps(vd9);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+ vr9 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr9, vd9, vone), vr9, vr9);
+
+ vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
+ vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
+ vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
+ vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
+ vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
+ vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
+ vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
+ vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
+ vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
+ vr9 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr9, vd9, vone), vr9, vr9);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf0 = _mm256_mul_ps(ve0, vr0);
+ __m256 vf1 = _mm256_mul_ps(ve1, vr1);
+ __m256 vf2 = _mm256_mul_ps(ve2, vr2);
+ __m256 vf3 = _mm256_mul_ps(ve3, vr3);
+ __m256 vf4 = _mm256_mul_ps(ve4, vr4);
+ __m256 vf5 = _mm256_mul_ps(ve5, vr5);
+ __m256 vf6 = _mm256_mul_ps(ve6, vr6);
+ __m256 vf7 = _mm256_mul_ps(ve7, vr7);
+ __m256 vf8 = _mm256_mul_ps(ve8, vr8);
+ __m256 vf9 = _mm256_mul_ps(ve9, vr9);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
+ vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
+ vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
+ vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
+ vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
+ vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
+ vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
+ vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
+ vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
+ vf9 = _mm256_andnot_ps(_mm256_cmp_ps(vz9, vdenorm_cutoff, _CMP_LT_OS), vf9);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
+ vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
+ vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
+ vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
+ vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
+ vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
+ vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
+ vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
+ vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
+ vf9 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf9), vf9, vx9);
+
+ _mm256_storeu_ps(y, vf0);
+ _mm256_storeu_ps(y + 8, vf1);
+ _mm256_storeu_ps(y + 16, vf2);
+ _mm256_storeu_ps(y + 24, vf3);
+ _mm256_storeu_ps(y + 32, vf4);
+ _mm256_storeu_ps(y + 40, vf5);
+ _mm256_storeu_ps(y + 48, vf6);
+ _mm256_storeu_ps(y + 56, vf7);
+ _mm256_storeu_ps(y + 64, vf8);
+ _mm256_storeu_ps(y + 72, vf9);
+ y += 80;
+ }
+ for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
+ const __m256 vx = _mm256_loadu_ps(x);
+ x += 8;
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_storeu_ps(y, vf);
+ y += 8;
+ }
+ if XNN_UNLIKELY(n != 0) {
+ assert(n >= 1 * sizeof(float));
+ assert(n <= 7 * sizeof(float));
+ __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
+
+ const __m256 vx = _mm256_maskload_ps(x, vmask);
+
+ // General structure of the algorithm:
+ // / exp(x) / (1 + exp(x)) if x <= 0
+ // f[x] :=
+ // \ 1 - f[-x] if x >= 0
+ //
+ // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
+ // then replace result with 1 - f[z] if x >= 0.
+ const __m256 vz = _mm256_or_ps(vx, vsign_mask);
+
+ // Compute reduced argument n := round(z / log(2)).
+ // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
+ // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
+ // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
+ // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
+ // the algorithm.
+ __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
+
+ // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
+ // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
+ const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
+
+ // Subtract the large number back to get final n := round(z / log(2)).
+ vn = _mm256_sub_ps(vn, vmagic_bias);
+
+ // Compute reduced argument t := z - n * log(2).
+ __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
+
+ // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
+ __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
+ vp = _mm256_fmadd_ps(vp, vt, vc3);
+ vp = _mm256_fmadd_ps(vp, vt, vc2);
+ vp = _mm256_fmadd_ps(vp, vt, vc1);
+
+ // Reconstruct the exp(z) value:
+ // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
+ // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
+ // = s + (t * s) * p
+ vt = _mm256_mul_ps(vt, vs);
+ const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
+
+ // Denominator of the sigmoid fraction: 1.0 + exp(z)
+ const __m256 vd = _mm256_add_ps(ve, vone);
+
+ // Use Newton-Raphson method to compute reciprocal of denominator.
+ // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
+ // Thus the reciprocal of the denominator never overflows.
+ __m256 vr = _mm256_rcp_ps(vd);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+ vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
+
+ // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
+ __m256 vf = _mm256_mul_ps(ve, vr);
+
+ // For inputs below denormal cutoff, replace output with +0.0f.
+ // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
+ vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
+
+ // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
+ vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
+
+ _mm256_maskstore_ps(y, vmask, vf);
+ }
+}
diff --git a/src/init.c b/src/init.c
index f0bf70f..2f9c728 100644
--- a/src/init.c
+++ b/src/init.c
@@ -901,7 +901,11 @@
} else {
xnn_params.f32.hswish = (xnn_univector_ukernel_function) xnn_f32_hswish_ukernel__sse_x8;
}
- xnn_params.f32.sigmoid = (xnn_univector_ukernel_function) xnn_f32_sigmoid_ukernel__sse2_p5_div_x16;
+ if (!XNN_PLATFORM_MOBILE && cpuinfo_has_x86_avx2()) {
+ xnn_params.f32.sigmoid = (xnn_univector_ukernel_function) xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x40;
+ } else {
+ xnn_params.f32.sigmoid = (xnn_univector_ukernel_function) xnn_f32_sigmoid_ukernel__sse2_p5_div_x16;
+ }
xnn_params.f32.prelu = (struct prelu_parameters) {
.ukernel = (xnn_prelu_ukernel_function) xnn_f32_prelu_ukernel__sse2_2x8,
.row_tile = 2,
diff --git a/src/xnnpack/vunary.h b/src/xnnpack/vunary.h
index ddbfb6a..78f1997 100644
--- a/src/xnnpack/vunary.h
+++ b/src/xnnpack/vunary.h
@@ -144,6 +144,39 @@
DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__sse41_p5_div_x20)
DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__sse41_p5_div_x24)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x8)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x16)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x24)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x32)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x40)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x48)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x56)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x64)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x72)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_div_x80)
+
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x8)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x16)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x24)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x32)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x40)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x48)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x56)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x64)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x72)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr1fma_x80)
+
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x8)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x16)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x24)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x32)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x40)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x48)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x56)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x64)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x72)
+DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x80)
+
DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__psimd_p5_div_x4)
DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__psimd_p5_div_x8)
DECLARE_F32_VUNARY_UKERNEL_FUNCTION(xnn_f32_sigmoid_ukernel__psimd_p5_div_x12)