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