1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/avx2-p5.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
12 #include <immintrin.h>
13
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16
17
18 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
19
xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x80(size_t n,const float * x,float * y,const void * params)20 void xnn_f32_sigmoid_ukernel__avx2_rr1_p5_nr2fma_x80(
21 size_t n,
22 const float* x,
23 float* y,
24 const void* params)
25 {
26 assert(n % sizeof(float) == 0);
27
28 const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
29 // The smallest x for which sigmoidf(x) is normalized.
30 // This number is also the smallest x for which expf(x) is normalized.
31 const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
32 const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
33 const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f);
34 const __m256 vone = _mm256_set1_ps(1.0f);
35 const __m256 vsign_mask = _mm256_set1_ps(-0.0f);
36
37 const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
38 const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
39 const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
40 const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
41 const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
42
43 for (; n >= 80 * sizeof(float); n -= 80 * sizeof(float)) {
44 const __m256 vx0 = _mm256_loadu_ps(x);
45 const __m256 vx1 = _mm256_loadu_ps(x + 8);
46 const __m256 vx2 = _mm256_loadu_ps(x + 16);
47 const __m256 vx3 = _mm256_loadu_ps(x + 24);
48 const __m256 vx4 = _mm256_loadu_ps(x + 32);
49 const __m256 vx5 = _mm256_loadu_ps(x + 40);
50 const __m256 vx6 = _mm256_loadu_ps(x + 48);
51 const __m256 vx7 = _mm256_loadu_ps(x + 56);
52 const __m256 vx8 = _mm256_loadu_ps(x + 64);
53 const __m256 vx9 = _mm256_loadu_ps(x + 72);
54 x += 80;
55
56 // General structure of the algorithm:
57 // / exp(x) / (1 + exp(x)) if x <= 0
58 // f[x] :=
59 // \ 1 - f[-x] if x >= 0
60 //
61 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
62 // then replace result with 1 - f[z] if x >= 0.
63 const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
64 const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
65 const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
66 const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
67 const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
68 const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
69 const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
70 const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
71 const __m256 vz8 = _mm256_or_ps(vx8, vsign_mask);
72 const __m256 vz9 = _mm256_or_ps(vx9, vsign_mask);
73
74 // Compute reduced argument n := round(z / log(2)).
75 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
76 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
77 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
78 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
79 // the algorithm.
80 __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
81 __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
82 __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
83 __m256 vn3 = _mm256_fmadd_ps(vz3, vlog2e, vmagic_bias);
84 __m256 vn4 = _mm256_fmadd_ps(vz4, vlog2e, vmagic_bias);
85 __m256 vn5 = _mm256_fmadd_ps(vz5, vlog2e, vmagic_bias);
86 __m256 vn6 = _mm256_fmadd_ps(vz6, vlog2e, vmagic_bias);
87 __m256 vn7 = _mm256_fmadd_ps(vz7, vlog2e, vmagic_bias);
88 __m256 vn8 = _mm256_fmadd_ps(vz8, vlog2e, vmagic_bias);
89 __m256 vn9 = _mm256_fmadd_ps(vz9, vlog2e, vmagic_bias);
90
91 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
92 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
93 const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
94 const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
95 const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
96 const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn3), 23));
97 const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn4), 23));
98 const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn5), 23));
99 const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn6), 23));
100 const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn7), 23));
101 const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn8), 23));
102 const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn9), 23));
103
104 // Subtract the large number back to get final n := round(z / log(2)).
105 vn0 = _mm256_sub_ps(vn0, vmagic_bias);
106 vn1 = _mm256_sub_ps(vn1, vmagic_bias);
107 vn2 = _mm256_sub_ps(vn2, vmagic_bias);
108 vn3 = _mm256_sub_ps(vn3, vmagic_bias);
109 vn4 = _mm256_sub_ps(vn4, vmagic_bias);
110 vn5 = _mm256_sub_ps(vn5, vmagic_bias);
111 vn6 = _mm256_sub_ps(vn6, vmagic_bias);
112 vn7 = _mm256_sub_ps(vn7, vmagic_bias);
113 vn8 = _mm256_sub_ps(vn8, vmagic_bias);
114 vn9 = _mm256_sub_ps(vn9, vmagic_bias);
115
116 // Compute reduced argument t := z - n * log(2).
117 __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
118 __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
119 __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
120 __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2, vz3);
121 __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2, vz4);
122 __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2, vz5);
123 __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2, vz6);
124 __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2, vz7);
125 __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2, vz8);
126 __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2, vz9);
127
128 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
129 __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
130 __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
131 __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
132 __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
133 __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
134 __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
135 __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
136 __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
137 __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
138 __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
139
140 vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
141 vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
142 vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
143 vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
144 vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
145 vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
146 vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
147 vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
148 vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
149 vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
150
151 vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
152 vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
153 vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
154 vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
155 vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
156 vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
157 vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
158 vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
159 vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
160 vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
161
162 vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
163 vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
164 vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
165 vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
166 vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
167 vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
168 vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
169 vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
170 vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
171 vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
172
173 // Reconstruct the exp(z) value:
174 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
175 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
176 // = s + (t * s) * p
177 vt0 = _mm256_mul_ps(vt0, vs0);
178 vt1 = _mm256_mul_ps(vt1, vs1);
179 vt2 = _mm256_mul_ps(vt2, vs2);
180 vt3 = _mm256_mul_ps(vt3, vs3);
181 vt4 = _mm256_mul_ps(vt4, vs4);
182 vt5 = _mm256_mul_ps(vt5, vs5);
183 vt6 = _mm256_mul_ps(vt6, vs6);
184 vt7 = _mm256_mul_ps(vt7, vs7);
185 vt8 = _mm256_mul_ps(vt8, vs8);
186 vt9 = _mm256_mul_ps(vt9, vs9);
187
188 const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
189 const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
190 const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
191 const __m256 ve3 = _mm256_fmadd_ps(vt3, vp3, vs3);
192 const __m256 ve4 = _mm256_fmadd_ps(vt4, vp4, vs4);
193 const __m256 ve5 = _mm256_fmadd_ps(vt5, vp5, vs5);
194 const __m256 ve6 = _mm256_fmadd_ps(vt6, vp6, vs6);
195 const __m256 ve7 = _mm256_fmadd_ps(vt7, vp7, vs7);
196 const __m256 ve8 = _mm256_fmadd_ps(vt8, vp8, vs8);
197 const __m256 ve9 = _mm256_fmadd_ps(vt9, vp9, vs9);
198
199 // Denominator of the sigmoid fraction: 1.0 + exp(z)
200 const __m256 vd0 = _mm256_add_ps(ve0, vone);
201 const __m256 vd1 = _mm256_add_ps(ve1, vone);
202 const __m256 vd2 = _mm256_add_ps(ve2, vone);
203 const __m256 vd3 = _mm256_add_ps(ve3, vone);
204 const __m256 vd4 = _mm256_add_ps(ve4, vone);
205 const __m256 vd5 = _mm256_add_ps(ve5, vone);
206 const __m256 vd6 = _mm256_add_ps(ve6, vone);
207 const __m256 vd7 = _mm256_add_ps(ve7, vone);
208 const __m256 vd8 = _mm256_add_ps(ve8, vone);
209 const __m256 vd9 = _mm256_add_ps(ve9, vone);
210
211 // Use Newton-Raphson method to compute reciprocal of denominator.
212 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
213 // Thus the reciprocal of the denominator never overflows.
214 __m256 vr0 = _mm256_rcp_ps(vd0);
215 __m256 vr1 = _mm256_rcp_ps(vd1);
216 __m256 vr2 = _mm256_rcp_ps(vd2);
217 __m256 vr3 = _mm256_rcp_ps(vd3);
218 __m256 vr4 = _mm256_rcp_ps(vd4);
219 __m256 vr5 = _mm256_rcp_ps(vd5);
220 __m256 vr6 = _mm256_rcp_ps(vd6);
221 __m256 vr7 = _mm256_rcp_ps(vd7);
222 __m256 vr8 = _mm256_rcp_ps(vd8);
223 __m256 vr9 = _mm256_rcp_ps(vd9);
224
225 vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
226 vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
227 vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
228 vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
229 vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
230 vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
231 vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
232 vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
233 vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
234 vr9 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr9, vd9, vone), vr9, vr9);
235
236 vr0 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr0, vd0, vone), vr0, vr0);
237 vr1 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr1, vd1, vone), vr1, vr1);
238 vr2 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr2, vd2, vone), vr2, vr2);
239 vr3 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr3, vd3, vone), vr3, vr3);
240 vr4 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr4, vd4, vone), vr4, vr4);
241 vr5 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr5, vd5, vone), vr5, vr5);
242 vr6 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr6, vd6, vone), vr6, vr6);
243 vr7 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr7, vd7, vone), vr7, vr7);
244 vr8 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr8, vd8, vone), vr8, vr8);
245 vr9 = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr9, vd9, vone), vr9, vr9);
246
247 // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
248 __m256 vf0 = _mm256_mul_ps(ve0, vr0);
249 __m256 vf1 = _mm256_mul_ps(ve1, vr1);
250 __m256 vf2 = _mm256_mul_ps(ve2, vr2);
251 __m256 vf3 = _mm256_mul_ps(ve3, vr3);
252 __m256 vf4 = _mm256_mul_ps(ve4, vr4);
253 __m256 vf5 = _mm256_mul_ps(ve5, vr5);
254 __m256 vf6 = _mm256_mul_ps(ve6, vr6);
255 __m256 vf7 = _mm256_mul_ps(ve7, vr7);
256 __m256 vf8 = _mm256_mul_ps(ve8, vr8);
257 __m256 vf9 = _mm256_mul_ps(ve9, vr9);
258
259 // For inputs below denormal cutoff, replace output with +0.0f.
260 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
261 vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
262 vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
263 vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
264 vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
265 vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
266 vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
267 vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
268 vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
269 vf8 = _mm256_andnot_ps(_mm256_cmp_ps(vz8, vdenorm_cutoff, _CMP_LT_OS), vf8);
270 vf9 = _mm256_andnot_ps(_mm256_cmp_ps(vz9, vdenorm_cutoff, _CMP_LT_OS), vf9);
271
272 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
273 vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
274 vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
275 vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
276 vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
277 vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
278 vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
279 vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
280 vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
281 vf8 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf8), vf8, vx8);
282 vf9 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf9), vf9, vx9);
283
284 _mm256_storeu_ps(y, vf0);
285 _mm256_storeu_ps(y + 8, vf1);
286 _mm256_storeu_ps(y + 16, vf2);
287 _mm256_storeu_ps(y + 24, vf3);
288 _mm256_storeu_ps(y + 32, vf4);
289 _mm256_storeu_ps(y + 40, vf5);
290 _mm256_storeu_ps(y + 48, vf6);
291 _mm256_storeu_ps(y + 56, vf7);
292 _mm256_storeu_ps(y + 64, vf8);
293 _mm256_storeu_ps(y + 72, vf9);
294 y += 80;
295 }
296 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
297 const __m256 vx = _mm256_loadu_ps(x);
298 x += 8;
299
300 // General structure of the algorithm:
301 // / exp(x) / (1 + exp(x)) if x <= 0
302 // f[x] :=
303 // \ 1 - f[-x] if x >= 0
304 //
305 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
306 // then replace result with 1 - f[z] if x >= 0.
307 const __m256 vz = _mm256_or_ps(vx, vsign_mask);
308
309 // Compute reduced argument n := round(z / log(2)).
310 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
311 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
312 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
313 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
314 // the algorithm.
315 __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
316
317 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
318 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
319 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
320
321 // Subtract the large number back to get final n := round(z / log(2)).
322 vn = _mm256_sub_ps(vn, vmagic_bias);
323
324 // Compute reduced argument t := z - n * log(2).
325 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
326
327 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
328 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
329 vp = _mm256_fmadd_ps(vp, vt, vc3);
330 vp = _mm256_fmadd_ps(vp, vt, vc2);
331 vp = _mm256_fmadd_ps(vp, vt, vc1);
332
333 // Reconstruct the exp(z) value:
334 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
335 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
336 // = s + (t * s) * p
337 vt = _mm256_mul_ps(vt, vs);
338 const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
339
340 // Denominator of the sigmoid fraction: 1.0 + exp(z)
341 const __m256 vd = _mm256_add_ps(ve, vone);
342
343 // Use Newton-Raphson method to compute reciprocal of denominator.
344 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
345 // Thus the reciprocal of the denominator never overflows.
346 __m256 vr = _mm256_rcp_ps(vd);
347 vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
348 vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
349
350 // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
351 __m256 vf = _mm256_mul_ps(ve, vr);
352
353 // For inputs below denormal cutoff, replace output with +0.0f.
354 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
355 vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
356
357 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
358 vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
359
360 _mm256_storeu_ps(y, vf);
361 y += 8;
362 }
363 if XNN_UNLIKELY(n != 0) {
364 assert(n >= 1 * sizeof(float));
365 assert(n <= 7 * sizeof(float));
366 __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
367
368 const __m256 vx = _mm256_maskload_ps(x, vmask);
369
370 // General structure of the algorithm:
371 // / exp(x) / (1 + exp(x)) if x <= 0
372 // f[x] :=
373 // \ 1 - f[-x] if x >= 0
374 //
375 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
376 // then replace result with 1 - f[z] if x >= 0.
377 const __m256 vz = _mm256_or_ps(vx, vsign_mask);
378
379 // Compute reduced argument n := round(z / log(2)).
380 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
381 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
382 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
383 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
384 // the algorithm.
385 __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
386
387 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
388 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
389 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
390
391 // Subtract the large number back to get final n := round(z / log(2)).
392 vn = _mm256_sub_ps(vn, vmagic_bias);
393
394 // Compute reduced argument t := z - n * log(2).
395 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
396
397 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
398 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
399 vp = _mm256_fmadd_ps(vp, vt, vc3);
400 vp = _mm256_fmadd_ps(vp, vt, vc2);
401 vp = _mm256_fmadd_ps(vp, vt, vc1);
402
403 // Reconstruct the exp(z) value:
404 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
405 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
406 // = s + (t * s) * p
407 vt = _mm256_mul_ps(vt, vs);
408 const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
409
410 // Denominator of the sigmoid fraction: 1.0 + exp(z)
411 const __m256 vd = _mm256_add_ps(ve, vone);
412
413 // Use Newton-Raphson method to compute reciprocal of denominator.
414 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
415 // Thus the reciprocal of the denominator never overflows.
416 __m256 vr = _mm256_rcp_ps(vd);
417 vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
418 vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr);
419
420 // Reconstruct sigmoid(z) = exp(z) * recip(1.0 + exp(z))
421 __m256 vf = _mm256_mul_ps(ve, vr);
422
423 // For inputs below denormal cutoff, replace output with +0.0f.
424 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
425 vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
426
427 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
428 vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
429
430 // _mm256_maskstore_ps(y, vmask, vf) could be used here, but triggers msan failures (probably an msan bug).
431 __m128 vf_lo = _mm256_castps256_ps128(vf);
432 if (n & (4 * sizeof(float))) {
433 _mm_storeu_ps(y, vf_lo);
434 vf_lo = _mm256_extractf128_ps(vf, 1);
435 y += 4;
436 }
437 if (n & (2 * sizeof(float))) {
438 _mm_storel_pi((__m64*) y, vf_lo);
439 vf_lo = _mm_movehl_ps(vf_lo, vf_lo);
440 y += 2;
441 }
442 if (n & (1 * sizeof(float))) {
443 _mm_store_ss(y, vf_lo);
444 }
445 }
446 }
447