xref: /aosp_15_r20/external/XNNPACK/src/math/sigmoid-f32-avx2-rr1-p5-div.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1*4bdc9457SAndroid Build Coastguard Worker // Copyright 2019 Google LLC
2*4bdc9457SAndroid Build Coastguard Worker //
3*4bdc9457SAndroid Build Coastguard Worker // This source code is licensed under the BSD-style license found in the
4*4bdc9457SAndroid Build Coastguard Worker // LICENSE file in the root directory of this source tree.
5*4bdc9457SAndroid Build Coastguard Worker 
6*4bdc9457SAndroid Build Coastguard Worker #include <assert.h>
7*4bdc9457SAndroid Build Coastguard Worker #include <stddef.h>
8*4bdc9457SAndroid Build Coastguard Worker 
9*4bdc9457SAndroid Build Coastguard Worker #include <immintrin.h>
10*4bdc9457SAndroid Build Coastguard Worker 
11*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/math-stubs.h>
12*4bdc9457SAndroid Build Coastguard Worker 
13*4bdc9457SAndroid Build Coastguard Worker 
xnn_math_f32_sigmoid__avx2_rr1_p5_div(size_t n,const float * input,float * output)14*4bdc9457SAndroid Build Coastguard Worker void xnn_math_f32_sigmoid__avx2_rr1_p5_div(
15*4bdc9457SAndroid Build Coastguard Worker     size_t n,
16*4bdc9457SAndroid Build Coastguard Worker     const float* input,
17*4bdc9457SAndroid Build Coastguard Worker     float* output)
18*4bdc9457SAndroid Build Coastguard Worker {
19*4bdc9457SAndroid Build Coastguard Worker   assert(n % (8 * sizeof(float)) == 0);
20*4bdc9457SAndroid Build Coastguard Worker 
21*4bdc9457SAndroid Build Coastguard Worker   // Floating-point mask with only the sign bit set
22*4bdc9457SAndroid Build Coastguard Worker   const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
23*4bdc9457SAndroid Build Coastguard Worker   // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22.
24*4bdc9457SAndroid Build Coastguard Worker   const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
25*4bdc9457SAndroid Build Coastguard Worker   const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f);
26*4bdc9457SAndroid Build Coastguard Worker   const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
27*4bdc9457SAndroid Build Coastguard Worker   // Coefficient of polynomial approximation of
28*4bdc9457SAndroid Build Coastguard Worker   // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2]
29*4bdc9457SAndroid Build Coastguard Worker   const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
30*4bdc9457SAndroid Build Coastguard Worker   const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
31*4bdc9457SAndroid Build Coastguard Worker   const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
32*4bdc9457SAndroid Build Coastguard Worker   const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
33*4bdc9457SAndroid Build Coastguard Worker   const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
34*4bdc9457SAndroid Build Coastguard Worker   const __m256 vone = _mm256_set1_ps(1.0f);
35*4bdc9457SAndroid Build Coastguard Worker   // The smallest x for which sigmoidf(x) is normalized.
36*4bdc9457SAndroid Build Coastguard Worker   // This number is also the smallest x for which expf(x) is normalized.
37*4bdc9457SAndroid Build Coastguard Worker   const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
38*4bdc9457SAndroid Build Coastguard Worker 
39*4bdc9457SAndroid Build Coastguard Worker   for (; n != 0; n -= 8 * sizeof(float)) {
40*4bdc9457SAndroid Build Coastguard Worker     const __m256 vx = _mm256_loadu_ps(input);
41*4bdc9457SAndroid Build Coastguard Worker 
42*4bdc9457SAndroid Build Coastguard Worker     // General structure of the algorithm:
43*4bdc9457SAndroid Build Coastguard Worker     //
44*4bdc9457SAndroid Build Coastguard Worker     //           / exp(x) / (1 + exp(x)) if x <= 0
45*4bdc9457SAndroid Build Coastguard Worker     //   f[x] :=
46*4bdc9457SAndroid Build Coastguard Worker     //           \ 1 - f[-x] if x >= 0
47*4bdc9457SAndroid Build Coastguard Worker     //
48*4bdc9457SAndroid Build Coastguard Worker     // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0.
49*4bdc9457SAndroid Build Coastguard Worker     const __m256 vz = _mm256_or_ps(vx, vsign_mask);
50*4bdc9457SAndroid Build Coastguard Worker 
51*4bdc9457SAndroid Build Coastguard Worker     // Compute reduced argument n := round(z / log(2)).
52*4bdc9457SAndroid Build Coastguard Worker     // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing
53*4bdc9457SAndroid Build Coastguard Worker     // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The
54*4bdc9457SAndroid Build Coastguard Worker     // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e.
55*4bdc9457SAndroid Build Coastguard Worker     // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678]
56*4bdc9457SAndroid Build Coastguard Worker     // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the
57*4bdc9457SAndroid Build Coastguard Worker     // very end of the algorithm.
58*4bdc9457SAndroid Build Coastguard Worker     __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
59*4bdc9457SAndroid Build Coastguard Worker 
60*4bdc9457SAndroid Build Coastguard Worker     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
61*4bdc9457SAndroid Build Coastguard Worker     // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
62*4bdc9457SAndroid Build Coastguard Worker     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
63*4bdc9457SAndroid Build Coastguard Worker 
64*4bdc9457SAndroid Build Coastguard Worker     // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number.
65*4bdc9457SAndroid Build Coastguard Worker     vn = _mm256_sub_ps(vn, vmagic_bias);
66*4bdc9457SAndroid Build Coastguard Worker 
67*4bdc9457SAndroid Build Coastguard Worker     // Compute reduced argument t := z - n * log(2).
68*4bdc9457SAndroid Build Coastguard Worker     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
69*4bdc9457SAndroid Build Coastguard Worker 
70*4bdc9457SAndroid Build Coastguard Worker     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
71*4bdc9457SAndroid Build Coastguard Worker     //   P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p
72*4bdc9457SAndroid Build Coastguard Worker     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
73*4bdc9457SAndroid Build Coastguard Worker     vp = _mm256_fmadd_ps(vp, vt, vc3);
74*4bdc9457SAndroid Build Coastguard Worker     vp = _mm256_fmadd_ps(vp, vt, vc2);
75*4bdc9457SAndroid Build Coastguard Worker     vp = _mm256_fmadd_ps(vp, vt, vc1);
76*4bdc9457SAndroid Build Coastguard Worker 
77*4bdc9457SAndroid Build Coastguard Worker     // Reconstruct the exp(z) value:
78*4bdc9457SAndroid Build Coastguard Worker     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
79*4bdc9457SAndroid Build Coastguard Worker     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
80*4bdc9457SAndroid Build Coastguard Worker     //     = s + (t * s) * p
81*4bdc9457SAndroid Build Coastguard Worker     vt = _mm256_mul_ps(vt, vs);
82*4bdc9457SAndroid Build Coastguard Worker     const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
83*4bdc9457SAndroid Build Coastguard Worker 
84*4bdc9457SAndroid Build Coastguard Worker     // Denominator of the sigmoid fraction: 1.0 + exp(z)
85*4bdc9457SAndroid Build Coastguard Worker     const __m256 vd = _mm256_add_ps(ve, vone);
86*4bdc9457SAndroid Build Coastguard Worker 
87*4bdc9457SAndroid Build Coastguard Worker     // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
88*4bdc9457SAndroid Build Coastguard Worker     __m256 vf = _mm256_div_ps(ve, vd);
89*4bdc9457SAndroid Build Coastguard Worker 
90*4bdc9457SAndroid Build Coastguard Worker     // For inputs below denormal cutoff, replace output with +0.0f.
91*4bdc9457SAndroid Build Coastguard Worker     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
92*4bdc9457SAndroid Build Coastguard Worker     vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
93*4bdc9457SAndroid Build Coastguard Worker 
94*4bdc9457SAndroid Build Coastguard Worker     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
95*4bdc9457SAndroid Build Coastguard Worker     vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
96*4bdc9457SAndroid Build Coastguard Worker 
97*4bdc9457SAndroid Build Coastguard Worker     _mm256_storeu_ps(output, vf);
98*4bdc9457SAndroid Build Coastguard Worker 
99*4bdc9457SAndroid Build Coastguard Worker     input += 8;
100*4bdc9457SAndroid Build Coastguard Worker     output += 8;
101*4bdc9457SAndroid Build Coastguard Worker   }
102*4bdc9457SAndroid Build Coastguard Worker }
103