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