1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/sse-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
12 #include <smmintrin.h>
13
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16
17
xnn_f32_sigmoid_ukernel__sse41_p5_div_x24(size_t n,const float * x,float * y,const void * params)18 void xnn_f32_sigmoid_ukernel__sse41_p5_div_x24(
19 size_t n,
20 const float* x,
21 float* y,
22 const void* params)
23 {
24 assert(n % sizeof(float) == 0);
25
26 const __m128 vmagic_bias = _mm_set1_ps(0x1.8000FEp23f);
27 // The smallest x for which sigmoidf(x) is normalized.
28 // This number is also the smallest x for which expf(x) is normalized.
29 const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f);
30 const __m128 vlog2e = _mm_set1_ps(0x1.715476p+0f);
31 // Last 7 bits are zeroes
32 const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.62E400p-1f);
33 const __m128 vminus_ln2_lo = _mm_set1_ps(-0x1.7F7D1Cp-20f);
34 const __m128 vone = _mm_set1_ps(1.0f);
35 const __m128 vsign_mask = _mm_set1_ps(-0.0f);
36
37 const __m128 vc1 = _mm_set1_ps(0x1.FFFFF6p-1f);
38 const __m128 vc2 = _mm_set1_ps(0x1.FFFDC6p-2f);
39 const __m128 vc3 = _mm_set1_ps(0x1.555A80p-3f);
40 const __m128 vc4 = _mm_set1_ps(0x1.573A1Ap-5f);
41 const __m128 vc5 = _mm_set1_ps(0x1.0F9F9Cp-7f);
42
43 for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
44 const __m128 vx0123 = _mm_loadu_ps(x);
45 const __m128 vx4567 = _mm_loadu_ps(x + 4);
46 const __m128 vx89AB = _mm_loadu_ps(x + 8);
47 const __m128 vxCDEF = _mm_loadu_ps(x + 12);
48 const __m128 vxGHIJ = _mm_loadu_ps(x + 16);
49 const __m128 vxKLMN = _mm_loadu_ps(x + 20);
50
51 // General structure of the algorithm:
52 // / exp(x) / (1 + exp(x)) if x <= 0
53 // f[x] :=
54 // \ 1 - f[-x] if x >= 0
55 //
56 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
57 // then replace result with 1 - f[z] if x >= 0.
58 const __m128 vz0123 = _mm_or_ps(vx0123, vsign_mask);
59 const __m128 vz4567 = _mm_or_ps(vx4567, vsign_mask);
60 const __m128 vz89AB = _mm_or_ps(vx89AB, vsign_mask);
61 const __m128 vzCDEF = _mm_or_ps(vxCDEF, vsign_mask);
62 const __m128 vzGHIJ = _mm_or_ps(vxGHIJ, vsign_mask);
63 const __m128 vzKLMN = _mm_or_ps(vxKLMN, vsign_mask);
64
65 // Compute reduced argument n := round(z / log(2)).
66 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
67 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
68 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
69 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
70 // the algorithm.
71 __m128 vn0123 = _mm_add_ps(_mm_mul_ps(vz0123, vlog2e), vmagic_bias);
72 __m128 vn4567 = _mm_add_ps(_mm_mul_ps(vz4567, vlog2e), vmagic_bias);
73 __m128 vn89AB = _mm_add_ps(_mm_mul_ps(vz89AB, vlog2e), vmagic_bias);
74 __m128 vnCDEF = _mm_add_ps(_mm_mul_ps(vzCDEF, vlog2e), vmagic_bias);
75 __m128 vnGHIJ = _mm_add_ps(_mm_mul_ps(vzGHIJ, vlog2e), vmagic_bias);
76 __m128 vnKLMN = _mm_add_ps(_mm_mul_ps(vzKLMN, vlog2e), vmagic_bias);
77
78 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
79 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
80 const __m128 vs0123 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn0123), 23));
81 const __m128 vs4567 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn4567), 23));
82 const __m128 vs89AB = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn89AB), 23));
83 const __m128 vsCDEF = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vnCDEF), 23));
84 const __m128 vsGHIJ = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vnGHIJ), 23));
85 const __m128 vsKLMN = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vnKLMN), 23));
86
87 // Subtract the large number back to get final n := round(z / log(2)).
88 vn0123 = _mm_sub_ps(vn0123, vmagic_bias);
89 vn4567 = _mm_sub_ps(vn4567, vmagic_bias);
90 vn89AB = _mm_sub_ps(vn89AB, vmagic_bias);
91 vnCDEF = _mm_sub_ps(vnCDEF, vmagic_bias);
92 vnGHIJ = _mm_sub_ps(vnGHIJ, vmagic_bias);
93 vnKLMN = _mm_sub_ps(vnKLMN, vmagic_bias);
94
95 // Compute reduced argument t := z - n * log(2).
96 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
97 __m128 vt0123 = _mm_add_ps(_mm_mul_ps(vn0123, vminus_ln2_hi), vz0123);
98 __m128 vt4567 = _mm_add_ps(_mm_mul_ps(vn4567, vminus_ln2_hi), vz4567);
99 __m128 vt89AB = _mm_add_ps(_mm_mul_ps(vn89AB, vminus_ln2_hi), vz89AB);
100 __m128 vtCDEF = _mm_add_ps(_mm_mul_ps(vnCDEF, vminus_ln2_hi), vzCDEF);
101 __m128 vtGHIJ = _mm_add_ps(_mm_mul_ps(vnGHIJ, vminus_ln2_hi), vzGHIJ);
102 __m128 vtKLMN = _mm_add_ps(_mm_mul_ps(vnKLMN, vminus_ln2_hi), vzKLMN);
103
104 vt0123 = _mm_add_ps(_mm_mul_ps(vn0123, vminus_ln2_lo), vt0123);
105 vt4567 = _mm_add_ps(_mm_mul_ps(vn4567, vminus_ln2_lo), vt4567);
106 vt89AB = _mm_add_ps(_mm_mul_ps(vn89AB, vminus_ln2_lo), vt89AB);
107 vtCDEF = _mm_add_ps(_mm_mul_ps(vnCDEF, vminus_ln2_lo), vtCDEF);
108 vtGHIJ = _mm_add_ps(_mm_mul_ps(vnGHIJ, vminus_ln2_lo), vtGHIJ);
109 vtKLMN = _mm_add_ps(_mm_mul_ps(vnKLMN, vminus_ln2_lo), vtKLMN);
110
111 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
112 __m128 vp0123 = _mm_add_ps(_mm_mul_ps(vc5, vt0123), vc4);
113 __m128 vp4567 = _mm_add_ps(_mm_mul_ps(vc5, vt4567), vc4);
114 __m128 vp89AB = _mm_add_ps(_mm_mul_ps(vc5, vt89AB), vc4);
115 __m128 vpCDEF = _mm_add_ps(_mm_mul_ps(vc5, vtCDEF), vc4);
116 __m128 vpGHIJ = _mm_add_ps(_mm_mul_ps(vc5, vtGHIJ), vc4);
117 __m128 vpKLMN = _mm_add_ps(_mm_mul_ps(vc5, vtKLMN), vc4);
118
119 vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc3);
120 vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc3);
121 vp89AB = _mm_add_ps(_mm_mul_ps(vp89AB, vt89AB), vc3);
122 vpCDEF = _mm_add_ps(_mm_mul_ps(vpCDEF, vtCDEF), vc3);
123 vpGHIJ = _mm_add_ps(_mm_mul_ps(vpGHIJ, vtGHIJ), vc3);
124 vpKLMN = _mm_add_ps(_mm_mul_ps(vpKLMN, vtKLMN), vc3);
125
126 vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc2);
127 vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc2);
128 vp89AB = _mm_add_ps(_mm_mul_ps(vp89AB, vt89AB), vc2);
129 vpCDEF = _mm_add_ps(_mm_mul_ps(vpCDEF, vtCDEF), vc2);
130 vpGHIJ = _mm_add_ps(_mm_mul_ps(vpGHIJ, vtGHIJ), vc2);
131 vpKLMN = _mm_add_ps(_mm_mul_ps(vpKLMN, vtKLMN), vc2);
132
133 vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc1);
134 vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc1);
135 vp89AB = _mm_add_ps(_mm_mul_ps(vp89AB, vt89AB), vc1);
136 vpCDEF = _mm_add_ps(_mm_mul_ps(vpCDEF, vtCDEF), vc1);
137 vpGHIJ = _mm_add_ps(_mm_mul_ps(vpGHIJ, vtGHIJ), vc1);
138 vpKLMN = _mm_add_ps(_mm_mul_ps(vpKLMN, vtKLMN), vc1);
139
140 // Reconstruct the exp(z) value:
141 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
142 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
143 // = s + (t * s) * p
144 vt0123 = _mm_mul_ps(vt0123, vs0123);
145 vt4567 = _mm_mul_ps(vt4567, vs4567);
146 vt89AB = _mm_mul_ps(vt89AB, vs89AB);
147 vtCDEF = _mm_mul_ps(vtCDEF, vsCDEF);
148 vtGHIJ = _mm_mul_ps(vtGHIJ, vsGHIJ);
149 vtKLMN = _mm_mul_ps(vtKLMN, vsKLMN);
150
151 __m128 ve0123 = _mm_add_ps(_mm_mul_ps(vt0123, vp0123), vs0123);
152 __m128 ve4567 = _mm_add_ps(_mm_mul_ps(vt4567, vp4567), vs4567);
153 __m128 ve89AB = _mm_add_ps(_mm_mul_ps(vt89AB, vp89AB), vs89AB);
154 __m128 veCDEF = _mm_add_ps(_mm_mul_ps(vtCDEF, vpCDEF), vsCDEF);
155 __m128 veGHIJ = _mm_add_ps(_mm_mul_ps(vtGHIJ, vpGHIJ), vsGHIJ);
156 __m128 veKLMN = _mm_add_ps(_mm_mul_ps(vtKLMN, vpKLMN), vsKLMN);
157
158 // Denominator of the sigmoid fraction: 1.0 + exp(z)
159 __m128 vd0123 = _mm_add_ps(ve0123, vone);
160 __m128 vd4567 = _mm_add_ps(ve4567, vone);
161 __m128 vd89AB = _mm_add_ps(ve89AB, vone);
162 __m128 vdCDEF = _mm_add_ps(veCDEF, vone);
163 __m128 vdGHIJ = _mm_add_ps(veGHIJ, vone);
164 __m128 vdKLMN = _mm_add_ps(veKLMN, vone);
165
166 // Reconstruct sigmoid(-z) = exp(z) / (1.0 + exp(z))
167 __m128 vf0123 = _mm_div_ps(ve0123, vd0123);
168 __m128 vf4567 = _mm_div_ps(ve4567, vd4567);
169 __m128 vf89AB = _mm_div_ps(ve89AB, vd89AB);
170 __m128 vfCDEF = _mm_div_ps(veCDEF, vdCDEF);
171 __m128 vfGHIJ = _mm_div_ps(veGHIJ, vdGHIJ);
172 __m128 vfKLMN = _mm_div_ps(veKLMN, vdKLMN);
173
174 // For inputs below denormal cutoff, replace output with +0.0f.
175 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
176 vf0123 = _mm_andnot_ps(_mm_cmplt_ps(vz0123, vdenorm_cutoff), vf0123);
177 vf4567 = _mm_andnot_ps(_mm_cmplt_ps(vz4567, vdenorm_cutoff), vf4567);
178 vf89AB = _mm_andnot_ps(_mm_cmplt_ps(vz89AB, vdenorm_cutoff), vf89AB);
179 vfCDEF = _mm_andnot_ps(_mm_cmplt_ps(vzCDEF, vdenorm_cutoff), vfCDEF);
180 vfGHIJ = _mm_andnot_ps(_mm_cmplt_ps(vzGHIJ, vdenorm_cutoff), vfGHIJ);
181 vfKLMN = _mm_andnot_ps(_mm_cmplt_ps(vzKLMN, vdenorm_cutoff), vfKLMN);
182
183 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
184 vf0123 = _mm_blendv_ps(_mm_sub_ps(vone, vf0123), vf0123, vx0123);
185 vf4567 = _mm_blendv_ps(_mm_sub_ps(vone, vf4567), vf4567, vx4567);
186 vf89AB = _mm_blendv_ps(_mm_sub_ps(vone, vf89AB), vf89AB, vx89AB);
187 vfCDEF = _mm_blendv_ps(_mm_sub_ps(vone, vfCDEF), vfCDEF, vxCDEF);
188 vfGHIJ = _mm_blendv_ps(_mm_sub_ps(vone, vfGHIJ), vfGHIJ, vxGHIJ);
189 vfKLMN = _mm_blendv_ps(_mm_sub_ps(vone, vfKLMN), vfKLMN, vxKLMN);
190
191 _mm_storeu_ps(y, vf0123);
192 _mm_storeu_ps(y + 4, vf4567);
193 _mm_storeu_ps(y + 8, vf89AB);
194 _mm_storeu_ps(y + 12, vfCDEF);
195 _mm_storeu_ps(y + 16, vfGHIJ);
196 _mm_storeu_ps(y + 20, vfKLMN);
197
198 x += 24;
199 y += 24;
200 }
201 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
202 const __m128 vx = _mm_loadu_ps(x);
203
204 // General structure of the algorithm:
205 // / exp(x) / (1 + exp(x)) if x <= 0
206 // f[x] :=
207 // \ 1 - f[-x] if x >= 0
208 //
209 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
210 // then replace result with 1 - f[z] if x >= 0.
211 const __m128 vz = _mm_or_ps(vx, vsign_mask);
212
213 // Compute reduced argument n := round(z / log(2)).
214 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
215 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
216 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
217 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
218 // the algorithm.
219 __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
220
221 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
222 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
223 const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23));
224
225 // Subtract the large number back to get final n := round(z / log(2)).
226 vn = _mm_sub_ps(vn, vmagic_bias);
227
228 // Compute reduced argument t := z - n * log(2).
229 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
230 __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz);
231 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt);
232
233 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
234 __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4);
235 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3);
236 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2);
237 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1);
238
239 // Reconstruct the exp(z) value:
240 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
241 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
242 // = s + (t * s) * p
243 vt = _mm_mul_ps(vt, vs);
244 __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs);
245
246 // Denominator of the sigmoid fraction: 1.0 + exp(z)
247 __m128 vd = _mm_add_ps(ve, vone);
248
249 // Reconstruct sigmoid(-z) = exp(z) / (1.0 + exp(z))
250 __m128 vf = _mm_div_ps(ve, vd);
251
252 // For inputs below denormal cutoff, replace output with +0.0f.
253 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
254 vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
255
256 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
257 vf = _mm_blendv_ps(_mm_sub_ps(vone, vf), vf, vx);
258
259 _mm_storeu_ps(y, vf);
260
261 x += 4;
262 y += 4;
263 }
264 if XNN_UNLIKELY(n != 0) {
265 const __m128 vx = _mm_loadu_ps(x);
266
267 // General structure of the algorithm:
268 // / exp(x) / (1 + exp(x)) if x <= 0
269 // f[x] :=
270 // \ 1 - f[-x] if x >= 0
271 //
272 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x),
273 // then replace result with 1 - f[z] if x >= 0.
274 const __m128 vz = _mm_or_ps(vx, vsign_mask);
275
276 // Compute reduced argument n := round(z / log(2)).
277 // We do it by adding a large number (magic bias) to the product z * (1/log(2)), which cause rounding of the result
278 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
279 // certain bounds (|x| <= 2**22), but thats ok, because inputs x outside of [-87.336544, 17.328678] (i.e. z outsize
280 // [0, 87.336544]) underflow or saturate sigmoidf(x) anyway. We fixup the result for such inputs at the very end of
281 // the algorithm.
282 __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
283
284 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
285 // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly.
286 const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23));
287
288 // Subtract the large number back to get final n := round(z / log(2)).
289 vn = _mm_sub_ps(vn, vmagic_bias);
290
291 // Compute reduced argument t := z - n * log(2).
292 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
293 __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz);
294 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt);
295
296 // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
297 __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4);
298 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3);
299 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2);
300 vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1);
301
302 // Reconstruct the exp(z) value:
303 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
304 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
305 // = s + (t * s) * p
306 vt = _mm_mul_ps(vt, vs);
307 __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs);
308
309 // Denominator of the sigmoid fraction: 1.0 + exp(z)
310 __m128 vd = _mm_add_ps(ve, vone);
311
312 // Reconstruct sigmoid(-z) = exp(z) / (1.0 + exp(z))
313 __m128 vf = _mm_div_ps(ve, vd);
314
315 // For inputs below denormal cutoff, replace output with +0.0f.
316 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
317 vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
318
319 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
320 vf = _mm_blendv_ps(_mm_sub_ps(vone, vf), vf, vx);
321
322 if (n & (2 * sizeof(float))) {
323 _mm_storel_pi((__m64*) y, vf);
324 vf = _mm_movehl_ps(vf, vf);
325 y += 2;
326 }
327 if (n & (1 * sizeof(float))) {
328 _mm_store_ss(y, vf);
329 }
330 }
331 }
332