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