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