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