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