1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/scalar-p5-div.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2019 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11 #include <math.h>
12
13 #include <xnnpack/common.h>
14 #include <xnnpack/vunary.h>
15
16 #include <fp16/bitcasts.h>
17
18
xnn_f32_sigmoid_ukernel__scalar_p5_div_x2(size_t n,const float * x,float * y,const void * params)19 void xnn_f32_sigmoid_ukernel__scalar_p5_div_x2(
20 size_t n,
21 const float* x,
22 float* y,
23 const void* params)
24 {
25 assert(n % sizeof(float) == 0);
26
27 const float vmagic_bias = 0x1.8000FEp23f;
28 // The largest z for which sigmoidf(-z) is normalized.
29 // This number is also the largest z for which expf(-z) is normalized.
30 const float vdenorm_cutoff = 0x1.5D589Ep+6f;
31 const float vminus_log2e = -0x1.715476p+0f;
32 // Last 7 bits are zeroes
33 const float vln2_hi = 0x1.62E400p-1f;
34 const float vln2_lo = 0x1.7F7D1Cp-20f;
35 const float vone = 1.0f;
36
37 const float vc1 = -0x1.FFFFF6p-1f;
38 const float vc2 = 0x1.FFFDC6p-2f;
39 const float vc3 = -0x1.555A80p-3f;
40 const float vc4 = 0x1.573A1Ap-5f;
41 const float vc5 = -0x1.0F9F9Cp-7f;
42
43 for (; n >= 2 * sizeof(float); n -= 2 * sizeof(float)) {
44 const float vx0 = x[0];
45 const float vx1 = x[1];
46 x += 2;
47
48 // General structure of the algorithm:
49 // / exp(x) / (1 + exp(x)) if x <= 0
50 // f[x] :=
51 // \ 1 - f[-x] if x >= 0
52 //
53 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
54 // then replace result with 1 - f[-z] if x >= 0.
55 const float vz0 = fabsf(vx0);
56 const float vz1 = fabsf(vx1);
57
58 // Compute reduced argument n := round(-z / log(2)).
59 // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
60 // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
61 // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
62 // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
63 // anyway. We fixup the result for such inputs at the very end of the algorithm.
64 float vn0 = vz0 * vminus_log2e + vmagic_bias;
65 float vn1 = vz1 * vminus_log2e + vmagic_bias;
66
67 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
68 // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
69 const float vs0 = fp32_from_bits(fp32_to_bits(vn0) << 23);
70 const float vs1 = fp32_from_bits(fp32_to_bits(vn1) << 23);
71
72 // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
73 vn0 -= vmagic_bias;
74 vn1 -= vmagic_bias;
75
76 // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
77 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
78 float vt0 = vn0 * vln2_hi + vz0;
79 float vt1 = vn1 * vln2_hi + vz1;
80
81 vt0 = vn0 * vln2_lo + vt0;
82 vt1 = vn1 * vln2_lo + vt1;
83
84 // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
85 // P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
86 float vp0 = vt0 * vc5 + vc4;
87 float vp1 = vt1 * vc5 + vc4;
88
89 vp0 = vt0 * vp0 + vc3;
90 vp1 = vt1 * vp1 + vc3;
91
92 vp0 = vt0 * vp0 + vc2;
93 vp1 = vt1 * vp1 + vc2;
94
95 vp0 = vt0 * vp0 + vc1;
96 vp1 = vt1 * vp1 + vc1;
97
98 // Reconstruct the exp(-z) value:
99 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
100 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
101 // = s + (t * s) * p
102 vt0 *= vs0;
103 vt1 *= vs1;
104
105 const float ve0 = vt0 * vp0 + vs0;
106 const float ve1 = vt1 * vp1 + vs1;
107
108 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
109 float vf0 = ve0 / (ve0 + vone);
110 float vf1 = ve1 / (ve1 + vone);
111
112 // For inputs below denormal cutoff, replace output with +0.0f.
113 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
114 if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
115 vf0 = 0.0f;
116 }
117 if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
118 vf1 = 0.0f;
119 }
120
121 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
122 if XNN_UNPREDICTABLE(vx0 > 0.0f) {
123 vf0 = vone - vf0;
124 }
125 if XNN_UNPREDICTABLE(vx1 > 0.0f) {
126 vf1 = vone - vf1;
127 }
128
129 y[0] = vf0;
130 y[1] = vf1;
131 y += 2;
132 }
133 if XNN_UNLIKELY(n != 0) {
134 const float vx = *x;
135
136 // General structure of the algorithm:
137 // / exp(x) / (1 + exp(x)) if x <= 0
138 // f[x] :=
139 // \ 1 - f[-x] if x >= 0
140 //
141 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
142 // then replace result with 1 - f[-z] if x >= 0.
143 const float vz = fabsf(vx);
144
145 // Compute reduced argument n := round(-z / log(2)).
146 // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
147 // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
148 // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
149 // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
150 // anyway. We fixup the result for such inputs at the very end of the algorithm.
151 float vn = vz * vminus_log2e + vmagic_bias;
152
153 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
154 // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
155 const float vs = fp32_from_bits(fp32_to_bits(vn) << 23);
156
157 // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
158 vn -= vmagic_bias;
159
160 // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
161 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
162 float vt = vn * vln2_hi + vz;
163 vt = vn * vln2_lo + vt;
164
165 // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
166 // P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
167 float vp = vt * vc5 + vc4;
168 vp = vt * vp + vc3;
169 vp = vt * vp + vc2;
170 vp = vt * vp + vc1;
171
172 // Reconstruct the exp(-z) value:
173 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
174 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
175 // = s + (t * s) * p
176 vt *= vs;
177 const float ve = vt * vp + vs;
178
179 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
180 float vf = ve / (ve + vone);
181
182 // For inputs above denormal cutoff, replace output with +0.0f.
183 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
184 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
185 vf = 0.0f;
186 }
187
188 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
189 if XNN_UNPREDICTABLE(vx > 0.0f) {
190 vf = vone - vf;
191 }
192
193 *y = vf;
194 }
195 }
196