• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/avx-p5.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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__avx_rr2_p5_div_x64(size_t n,const float * x,float * y,const void * params)20 void xnn_f32_sigmoid_ukernel__avx_rr2_p5_div_x64(
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 vsign_mask = _mm256_set1_ps(-0.0f);
29   const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
30   const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f);
31   const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
32   const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
33   const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
34   const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
35   const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
36   const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
37   const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
38   const __m256 vone = _mm256_set1_ps(1.0f);
39   const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f);
40 
41   for (; n >= 64 * sizeof(float); n -= 64 * sizeof(float)) {
42     const __m256 vx0 = _mm256_loadu_ps(x);
43     const __m256 vx1 = _mm256_loadu_ps(x + 8);
44     const __m256 vx2 = _mm256_loadu_ps(x + 16);
45     const __m256 vx3 = _mm256_loadu_ps(x + 24);
46     const __m256 vx4 = _mm256_loadu_ps(x + 32);
47     const __m256 vx5 = _mm256_loadu_ps(x + 40);
48     const __m256 vx6 = _mm256_loadu_ps(x + 48);
49     const __m256 vx7 = _mm256_loadu_ps(x + 56);
50     x += 64;
51 
52     const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
53     const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
54     const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
55     const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
56     const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
57     const __m256 vz5 = _mm256_or_ps(vx5, vsign_mask);
58     const __m256 vz6 = _mm256_or_ps(vx6, vsign_mask);
59     const __m256 vz7 = _mm256_or_ps(vx7, vsign_mask);
60 
61     __m256 vn0 = _mm256_add_ps(_mm256_mul_ps(vz0, vlog2e), vmagic_bias);
62     __m256 vn1 = _mm256_add_ps(_mm256_mul_ps(vz1, vlog2e), vmagic_bias);
63     __m256 vn2 = _mm256_add_ps(_mm256_mul_ps(vz2, vlog2e), vmagic_bias);
64     __m256 vn3 = _mm256_add_ps(_mm256_mul_ps(vz3, vlog2e), vmagic_bias);
65     __m256 vn4 = _mm256_add_ps(_mm256_mul_ps(vz4, vlog2e), vmagic_bias);
66     __m256 vn5 = _mm256_add_ps(_mm256_mul_ps(vz5, vlog2e), vmagic_bias);
67     __m256 vn6 = _mm256_add_ps(_mm256_mul_ps(vz6, vlog2e), vmagic_bias);
68     __m256 vn7 = _mm256_add_ps(_mm256_mul_ps(vz7, vlog2e), vmagic_bias);
69 
70     const __m128 vs_lo0 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn0)), 23));
71     const __m128 vs_hi0 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn0, 1)), 23));
72     const __m256 vs0 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo0), vs_hi0, 1);
73     const __m128 vs_lo1 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn1)), 23));
74     const __m128 vs_hi1 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn1, 1)), 23));
75     const __m256 vs1 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo1), vs_hi1, 1);
76     const __m128 vs_lo2 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn2)), 23));
77     const __m128 vs_hi2 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn2, 1)), 23));
78     const __m256 vs2 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo2), vs_hi2, 1);
79     const __m128 vs_lo3 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn3)), 23));
80     const __m128 vs_hi3 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn3, 1)), 23));
81     const __m256 vs3 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo3), vs_hi3, 1);
82     const __m128 vs_lo4 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn4)), 23));
83     const __m128 vs_hi4 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn4, 1)), 23));
84     const __m256 vs4 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo4), vs_hi4, 1);
85     const __m128 vs_lo5 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn5)), 23));
86     const __m128 vs_hi5 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn5, 1)), 23));
87     const __m256 vs5 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo5), vs_hi5, 1);
88     const __m128 vs_lo6 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn6)), 23));
89     const __m128 vs_hi6 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn6, 1)), 23));
90     const __m256 vs6 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo6), vs_hi6, 1);
91     const __m128 vs_lo7 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn7)), 23));
92     const __m128 vs_hi7 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn7, 1)), 23));
93     const __m256 vs7 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo7), vs_hi7, 1);
94 
95     vn0 = _mm256_sub_ps(vn0, vmagic_bias);
96     vn1 = _mm256_sub_ps(vn1, vmagic_bias);
97     vn2 = _mm256_sub_ps(vn2, vmagic_bias);
98     vn3 = _mm256_sub_ps(vn3, vmagic_bias);
99     vn4 = _mm256_sub_ps(vn4, vmagic_bias);
100     vn5 = _mm256_sub_ps(vn5, vmagic_bias);
101     vn6 = _mm256_sub_ps(vn6, vmagic_bias);
102     vn7 = _mm256_sub_ps(vn7, vmagic_bias);
103 
104     __m256 vt0 = _mm256_add_ps(_mm256_mul_ps(vn0, vminus_ln2_hi), vz0);
105     __m256 vt1 = _mm256_add_ps(_mm256_mul_ps(vn1, vminus_ln2_hi), vz1);
106     __m256 vt2 = _mm256_add_ps(_mm256_mul_ps(vn2, vminus_ln2_hi), vz2);
107     __m256 vt3 = _mm256_add_ps(_mm256_mul_ps(vn3, vminus_ln2_hi), vz3);
108     __m256 vt4 = _mm256_add_ps(_mm256_mul_ps(vn4, vminus_ln2_hi), vz4);
109     __m256 vt5 = _mm256_add_ps(_mm256_mul_ps(vn5, vminus_ln2_hi), vz5);
110     __m256 vt6 = _mm256_add_ps(_mm256_mul_ps(vn6, vminus_ln2_hi), vz6);
111     __m256 vt7 = _mm256_add_ps(_mm256_mul_ps(vn7, vminus_ln2_hi), vz7);
112 
113     vt0 = _mm256_add_ps(_mm256_mul_ps(vn0, vminus_ln2_lo), vt0);
114     vt1 = _mm256_add_ps(_mm256_mul_ps(vn1, vminus_ln2_lo), vt1);
115     vt2 = _mm256_add_ps(_mm256_mul_ps(vn2, vminus_ln2_lo), vt2);
116     vt3 = _mm256_add_ps(_mm256_mul_ps(vn3, vminus_ln2_lo), vt3);
117     vt4 = _mm256_add_ps(_mm256_mul_ps(vn4, vminus_ln2_lo), vt4);
118     vt5 = _mm256_add_ps(_mm256_mul_ps(vn5, vminus_ln2_lo), vt5);
119     vt6 = _mm256_add_ps(_mm256_mul_ps(vn6, vminus_ln2_lo), vt6);
120     vt7 = _mm256_add_ps(_mm256_mul_ps(vn7, vminus_ln2_lo), vt7);
121 
122     __m256 vp0 = _mm256_add_ps(_mm256_mul_ps(vc5, vt0), vc4);
123     __m256 vp1 = _mm256_add_ps(_mm256_mul_ps(vc5, vt1), vc4);
124     __m256 vp2 = _mm256_add_ps(_mm256_mul_ps(vc5, vt2), vc4);
125     __m256 vp3 = _mm256_add_ps(_mm256_mul_ps(vc5, vt3), vc4);
126     __m256 vp4 = _mm256_add_ps(_mm256_mul_ps(vc5, vt4), vc4);
127     __m256 vp5 = _mm256_add_ps(_mm256_mul_ps(vc5, vt5), vc4);
128     __m256 vp6 = _mm256_add_ps(_mm256_mul_ps(vc5, vt6), vc4);
129     __m256 vp7 = _mm256_add_ps(_mm256_mul_ps(vc5, vt7), vc4);
130 
131     vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc3);
132     vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc3);
133     vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc3);
134     vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc3);
135     vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc3);
136     vp5 = _mm256_add_ps(_mm256_mul_ps(vp5, vt5), vc3);
137     vp6 = _mm256_add_ps(_mm256_mul_ps(vp6, vt6), vc3);
138     vp7 = _mm256_add_ps(_mm256_mul_ps(vp7, vt7), vc3);
139 
140     vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc2);
141     vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc2);
142     vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc2);
143     vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc2);
144     vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc2);
145     vp5 = _mm256_add_ps(_mm256_mul_ps(vp5, vt5), vc2);
146     vp6 = _mm256_add_ps(_mm256_mul_ps(vp6, vt6), vc2);
147     vp7 = _mm256_add_ps(_mm256_mul_ps(vp7, vt7), vc2);
148 
149     vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc1);
150     vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc1);
151     vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc1);
152     vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc1);
153     vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc1);
154     vp5 = _mm256_add_ps(_mm256_mul_ps(vp5, vt5), vc1);
155     vp6 = _mm256_add_ps(_mm256_mul_ps(vp6, vt6), vc1);
156     vp7 = _mm256_add_ps(_mm256_mul_ps(vp7, vt7), vc1);
157 
158     vt0 = _mm256_mul_ps(vt0, vs0);
159     vt1 = _mm256_mul_ps(vt1, vs1);
160     vt2 = _mm256_mul_ps(vt2, vs2);
161     vt3 = _mm256_mul_ps(vt3, vs3);
162     vt4 = _mm256_mul_ps(vt4, vs4);
163     vt5 = _mm256_mul_ps(vt5, vs5);
164     vt6 = _mm256_mul_ps(vt6, vs6);
165     vt7 = _mm256_mul_ps(vt7, vs7);
166 
167     const __m256 ve0 = _mm256_add_ps(_mm256_mul_ps(vt0, vp0), vs0);
168     const __m256 ve1 = _mm256_add_ps(_mm256_mul_ps(vt1, vp1), vs1);
169     const __m256 ve2 = _mm256_add_ps(_mm256_mul_ps(vt2, vp2), vs2);
170     const __m256 ve3 = _mm256_add_ps(_mm256_mul_ps(vt3, vp3), vs3);
171     const __m256 ve4 = _mm256_add_ps(_mm256_mul_ps(vt4, vp4), vs4);
172     const __m256 ve5 = _mm256_add_ps(_mm256_mul_ps(vt5, vp5), vs5);
173     const __m256 ve6 = _mm256_add_ps(_mm256_mul_ps(vt6, vp6), vs6);
174     const __m256 ve7 = _mm256_add_ps(_mm256_mul_ps(vt7, vp7), vs7);
175 
176     const __m256 vd0 = _mm256_add_ps(ve0, vone);
177     const __m256 vd1 = _mm256_add_ps(ve1, vone);
178     const __m256 vd2 = _mm256_add_ps(ve2, vone);
179     const __m256 vd3 = _mm256_add_ps(ve3, vone);
180     const __m256 vd4 = _mm256_add_ps(ve4, vone);
181     const __m256 vd5 = _mm256_add_ps(ve5, vone);
182     const __m256 vd6 = _mm256_add_ps(ve6, vone);
183     const __m256 vd7 = _mm256_add_ps(ve7, vone);
184 
185     __m256 vf0 = _mm256_div_ps(ve0, vd0);
186     __m256 vf1 = _mm256_div_ps(ve1, vd1);
187     __m256 vf2 = _mm256_div_ps(ve2, vd2);
188     __m256 vf3 = _mm256_div_ps(ve3, vd3);
189     __m256 vf4 = _mm256_div_ps(ve4, vd4);
190     __m256 vf5 = _mm256_div_ps(ve5, vd5);
191     __m256 vf6 = _mm256_div_ps(ve6, vd6);
192     __m256 vf7 = _mm256_div_ps(ve7, vd7);
193 
194     vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
195     vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
196     vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
197     vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
198     vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
199     vf5 = _mm256_andnot_ps(_mm256_cmp_ps(vz5, vdenorm_cutoff, _CMP_LT_OS), vf5);
200     vf6 = _mm256_andnot_ps(_mm256_cmp_ps(vz6, vdenorm_cutoff, _CMP_LT_OS), vf6);
201     vf7 = _mm256_andnot_ps(_mm256_cmp_ps(vz7, vdenorm_cutoff, _CMP_LT_OS), vf7);
202 
203     vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
204     vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
205     vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
206     vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
207     vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
208     vf5 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf5), vf5, vx5);
209     vf6 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf6), vf6, vx6);
210     vf7 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf7), vf7, vx7);
211 
212     _mm256_storeu_ps(y, vf0);
213     _mm256_storeu_ps(y + 8, vf1);
214     _mm256_storeu_ps(y + 16, vf2);
215     _mm256_storeu_ps(y + 24, vf3);
216     _mm256_storeu_ps(y + 32, vf4);
217     _mm256_storeu_ps(y + 40, vf5);
218     _mm256_storeu_ps(y + 48, vf6);
219     _mm256_storeu_ps(y + 56, vf7);
220     y += 64;
221   }
222   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
223     const __m256 vx = _mm256_loadu_ps(x);
224     x += 8;
225 
226     const __m256 vz = _mm256_or_ps(vx, vsign_mask);
227 
228     __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
229 
230     const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
231     const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
232     const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
233 
234     vn = _mm256_sub_ps(vn, vmagic_bias);
235 
236     __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
237     vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
238 
239     __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4);
240     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
241     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
242     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1);
243 
244     vt = _mm256_mul_ps(vt, vs);
245     const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs);
246 
247     const __m256 vd = _mm256_add_ps(ve, vone);
248     __m256 vf = _mm256_div_ps(ve, vd);
249 
250     vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
251     vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
252 
253     _mm256_storeu_ps(y, vf);
254     y += 8;
255   }
256   if XNN_UNLIKELY(n != 0) {
257     assert(n >= 1 * sizeof(float));
258     assert(n <= 7 * sizeof(float));
259     __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
260 
261     const __m256 vx = _mm256_maskload_ps(x, vmask);
262 
263     const __m256 vz = _mm256_or_ps(vx, vsign_mask);
264 
265     __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
266     const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
267     const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
268     const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
269 
270     vn = _mm256_sub_ps(vn, vmagic_bias);
271 
272     __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
273     vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
274 
275     __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4);
276     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
277     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
278     vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1);
279 
280     vt = _mm256_mul_ps(vt, vs);
281     const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs);
282 
283     const __m256 vd = _mm256_add_ps(ve, vone);
284     __m256 vf = _mm256_div_ps(ve, vd);
285 
286     vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
287     vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
288 
289     // _mm256_maskstore_ps(y, vmask, vf) could be used here, but triggers msan failures (probably an msan bug).
290     __m128 vf_lo = _mm256_castps256_ps128(vf);
291     if (n & (4 * sizeof(float))) {
292       _mm_storeu_ps(y, vf_lo);
293       vf_lo = _mm256_extractf128_ps(vf, 1);
294       y += 4;
295     }
296     if (n & (2 * sizeof(float))) {
297       _mm_storel_pi((__m64*) y, vf_lo);
298       vf_lo = _mm_movehl_ps(vf_lo, vf_lo);
299       y += 2;
300     }
301     if (n & (1 * sizeof(float))) {
302       _mm_store_ss(y, vf_lo);
303     }
304   }
305 }
306