1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vsigmoid/avx-rr2-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
xnn_f32_vsigmoid_ukernel__avx_rr2_p5_div_x40(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f32_vsigmoid_ukernel__avx_rr2_p5_div_x40(
19 size_t n,
20 const float* x,
21 float* y,
22 const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24 assert(n % sizeof(float) == 0);
25
26 const __m256 vsign_mask = _mm256_load_ps(params->avx_rr2_p5.sign_mask);
27 const __m256 vmagic_bias = _mm256_load_ps(params->avx_rr2_p5.magic_bias);
28 const __m256 vlog2e = _mm256_load_ps(params->avx_rr2_p5.log2e);
29 const __m256 vminus_ln2_hi = _mm256_load_ps(params->avx_rr2_p5.minus_ln2_hi);
30 const __m256 vminus_ln2_lo = _mm256_load_ps(params->avx_rr2_p5.minus_ln2_lo);
31 const __m256 vc5 = _mm256_load_ps(params->avx_rr2_p5.c5);
32 const __m256 vc4 = _mm256_load_ps(params->avx_rr2_p5.c4);
33 const __m256 vc3 = _mm256_load_ps(params->avx_rr2_p5.c3);
34 const __m256 vc2 = _mm256_load_ps(params->avx_rr2_p5.c2);
35 const __m256 vc1 = _mm256_load_ps(params->avx_rr2_p5.c1);
36 const __m256 vone = _mm256_load_ps(params->avx_rr2_p5.one);
37 const __m256 vdenorm_cutoff = _mm256_load_ps(params->avx_rr2_p5.denorm_cutoff);
38
39 for (; n >= 40 * sizeof(float); n -= 40 * sizeof(float)) {
40 const __m256 vx0 = _mm256_loadu_ps(x);
41 const __m256 vx1 = _mm256_loadu_ps(x + 8);
42 const __m256 vx2 = _mm256_loadu_ps(x + 16);
43 const __m256 vx3 = _mm256_loadu_ps(x + 24);
44 const __m256 vx4 = _mm256_loadu_ps(x + 32);
45 x += 40;
46
47 const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
48 const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
49 const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
50 const __m256 vz3 = _mm256_or_ps(vx3, vsign_mask);
51 const __m256 vz4 = _mm256_or_ps(vx4, vsign_mask);
52
53 __m256 vn0 = _mm256_add_ps(_mm256_mul_ps(vz0, vlog2e), vmagic_bias);
54 __m256 vn1 = _mm256_add_ps(_mm256_mul_ps(vz1, vlog2e), vmagic_bias);
55 __m256 vn2 = _mm256_add_ps(_mm256_mul_ps(vz2, vlog2e), vmagic_bias);
56 __m256 vn3 = _mm256_add_ps(_mm256_mul_ps(vz3, vlog2e), vmagic_bias);
57 __m256 vn4 = _mm256_add_ps(_mm256_mul_ps(vz4, vlog2e), vmagic_bias);
58
59 const __m128 vs_lo0 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn0)), 23));
60 const __m128 vs_hi0 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn0, 1)), 23));
61 const __m256 vs0 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo0), vs_hi0, 1);
62 const __m128 vs_lo1 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn1)), 23));
63 const __m128 vs_hi1 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn1, 1)), 23));
64 const __m256 vs1 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo1), vs_hi1, 1);
65 const __m128 vs_lo2 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn2)), 23));
66 const __m128 vs_hi2 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn2, 1)), 23));
67 const __m256 vs2 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo2), vs_hi2, 1);
68 const __m128 vs_lo3 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn3)), 23));
69 const __m128 vs_hi3 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn3, 1)), 23));
70 const __m256 vs3 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo3), vs_hi3, 1);
71 const __m128 vs_lo4 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn4)), 23));
72 const __m128 vs_hi4 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn4, 1)), 23));
73 const __m256 vs4 = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo4), vs_hi4, 1);
74
75 vn0 = _mm256_sub_ps(vn0, vmagic_bias);
76 vn1 = _mm256_sub_ps(vn1, vmagic_bias);
77 vn2 = _mm256_sub_ps(vn2, vmagic_bias);
78 vn3 = _mm256_sub_ps(vn3, vmagic_bias);
79 vn4 = _mm256_sub_ps(vn4, vmagic_bias);
80
81 __m256 vt0 = _mm256_add_ps(_mm256_mul_ps(vn0, vminus_ln2_hi), vz0);
82 __m256 vt1 = _mm256_add_ps(_mm256_mul_ps(vn1, vminus_ln2_hi), vz1);
83 __m256 vt2 = _mm256_add_ps(_mm256_mul_ps(vn2, vminus_ln2_hi), vz2);
84 __m256 vt3 = _mm256_add_ps(_mm256_mul_ps(vn3, vminus_ln2_hi), vz3);
85 __m256 vt4 = _mm256_add_ps(_mm256_mul_ps(vn4, vminus_ln2_hi), vz4);
86
87 vt0 = _mm256_add_ps(_mm256_mul_ps(vn0, vminus_ln2_lo), vt0);
88 vt1 = _mm256_add_ps(_mm256_mul_ps(vn1, vminus_ln2_lo), vt1);
89 vt2 = _mm256_add_ps(_mm256_mul_ps(vn2, vminus_ln2_lo), vt2);
90 vt3 = _mm256_add_ps(_mm256_mul_ps(vn3, vminus_ln2_lo), vt3);
91 vt4 = _mm256_add_ps(_mm256_mul_ps(vn4, vminus_ln2_lo), vt4);
92
93 __m256 vp0 = _mm256_add_ps(_mm256_mul_ps(vc5, vt0), vc4);
94 __m256 vp1 = _mm256_add_ps(_mm256_mul_ps(vc5, vt1), vc4);
95 __m256 vp2 = _mm256_add_ps(_mm256_mul_ps(vc5, vt2), vc4);
96 __m256 vp3 = _mm256_add_ps(_mm256_mul_ps(vc5, vt3), vc4);
97 __m256 vp4 = _mm256_add_ps(_mm256_mul_ps(vc5, vt4), vc4);
98
99 vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc3);
100 vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc3);
101 vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc3);
102 vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc3);
103 vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc3);
104
105 vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc2);
106 vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc2);
107 vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc2);
108 vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc2);
109 vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc2);
110
111 vp0 = _mm256_add_ps(_mm256_mul_ps(vp0, vt0), vc1);
112 vp1 = _mm256_add_ps(_mm256_mul_ps(vp1, vt1), vc1);
113 vp2 = _mm256_add_ps(_mm256_mul_ps(vp2, vt2), vc1);
114 vp3 = _mm256_add_ps(_mm256_mul_ps(vp3, vt3), vc1);
115 vp4 = _mm256_add_ps(_mm256_mul_ps(vp4, vt4), vc1);
116
117 vt0 = _mm256_mul_ps(vt0, vs0);
118 vt1 = _mm256_mul_ps(vt1, vs1);
119 vt2 = _mm256_mul_ps(vt2, vs2);
120 vt3 = _mm256_mul_ps(vt3, vs3);
121 vt4 = _mm256_mul_ps(vt4, vs4);
122
123 const __m256 ve0 = _mm256_add_ps(_mm256_mul_ps(vt0, vp0), vs0);
124 const __m256 ve1 = _mm256_add_ps(_mm256_mul_ps(vt1, vp1), vs1);
125 const __m256 ve2 = _mm256_add_ps(_mm256_mul_ps(vt2, vp2), vs2);
126 const __m256 ve3 = _mm256_add_ps(_mm256_mul_ps(vt3, vp3), vs3);
127 const __m256 ve4 = _mm256_add_ps(_mm256_mul_ps(vt4, vp4), vs4);
128
129 const __m256 vd0 = _mm256_add_ps(ve0, vone);
130 const __m256 vd1 = _mm256_add_ps(ve1, vone);
131 const __m256 vd2 = _mm256_add_ps(ve2, vone);
132 const __m256 vd3 = _mm256_add_ps(ve3, vone);
133 const __m256 vd4 = _mm256_add_ps(ve4, vone);
134
135 __m256 vf0 = _mm256_div_ps(ve0, vd0);
136 __m256 vf1 = _mm256_div_ps(ve1, vd1);
137 __m256 vf2 = _mm256_div_ps(ve2, vd2);
138 __m256 vf3 = _mm256_div_ps(ve3, vd3);
139 __m256 vf4 = _mm256_div_ps(ve4, vd4);
140
141 vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
142 vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
143 vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
144 vf3 = _mm256_andnot_ps(_mm256_cmp_ps(vz3, vdenorm_cutoff, _CMP_LT_OS), vf3);
145 vf4 = _mm256_andnot_ps(_mm256_cmp_ps(vz4, vdenorm_cutoff, _CMP_LT_OS), vf4);
146
147 vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
148 vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
149 vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
150 vf3 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf3), vf3, vx3);
151 vf4 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf4), vf4, vx4);
152
153 _mm256_storeu_ps(y, vf0);
154 _mm256_storeu_ps(y + 8, vf1);
155 _mm256_storeu_ps(y + 16, vf2);
156 _mm256_storeu_ps(y + 24, vf3);
157 _mm256_storeu_ps(y + 32, vf4);
158 y += 40;
159 }
160 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
161 const __m256 vx = _mm256_loadu_ps(x);
162 x += 8;
163
164 const __m256 vz = _mm256_or_ps(vx, vsign_mask);
165
166 __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
167
168 const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
169 const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
170 const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
171
172 vn = _mm256_sub_ps(vn, vmagic_bias);
173
174 __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
175 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
176
177 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4);
178 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
179 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
180 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1);
181
182 vt = _mm256_mul_ps(vt, vs);
183 const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs);
184
185 const __m256 vd = _mm256_add_ps(ve, vone);
186 __m256 vf = _mm256_div_ps(ve, vd);
187
188 vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
189 vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
190
191 _mm256_storeu_ps(y, vf);
192 y += 8;
193 }
194 if XNN_UNLIKELY(n != 0) {
195 assert(n >= 1 * sizeof(float));
196 assert(n <= 7 * sizeof(float));
197 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) ¶ms->avx_rr2_p5.mask_table[7] - n));
198
199 const __m256 vx = _mm256_maskload_ps(x, vmask);
200
201 const __m256 vz = _mm256_or_ps(vx, vsign_mask);
202
203 __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
204 const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
205 const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
206 const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
207
208 vn = _mm256_sub_ps(vn, vmagic_bias);
209
210 __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
211 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
212
213 __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4);
214 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
215 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
216 vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1);
217
218 vt = _mm256_mul_ps(vt, vs);
219 const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs);
220
221 const __m256 vd = _mm256_add_ps(ve, vone);
222 __m256 vf = _mm256_div_ps(ve, vd);
223
224 vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
225 vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
226
227 __m128 vf_lo = _mm256_castps256_ps128(vf);
228 if (n & (4 * sizeof(float))) {
229 _mm_storeu_ps(y, vf_lo);
230 vf_lo = _mm256_extractf128_ps(vf, 1);
231 y += 4;
232 }
233 if (n & (2 * sizeof(float))) {
234 _mm_storel_pi((__m64*) y, vf_lo);
235 vf_lo = _mm_movehl_ps(vf_lo, vf_lo);
236 y += 2;
237 }
238 if (n & (1 * sizeof(float))) {
239 _mm_store_ss(y, vf_lo);
240 }
241 }
242 }
243