Debian Codegraph @

src/f32-vscaleextexp/gen/f32-vscaleextexp-avx2-p5-u56.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vscaleextexp/avx2-p5.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 <immintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/vscaleextexp.h"
16
17
18 void xnn_f32_vscaleextexp_ukernel__avx2_p5_u56(
19 size_t batch,
20 const float* input,
21 float* output,
22 float scale_value,
23 float scale_exp)
24 {
25 assert(batch != 0);
26 assert(batch % sizeof(float) == 0);
27 assert(input != NULL);
28 assert(output != NULL);
29
30 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
31
32 const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
33 const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
34 const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
35
36 // The smallest batch such that 2**batch is considered non-negligible.
37 // For smaller batch, 2**batch is replaced with zero.
38 const __m256 vmin_exponent = _mm256_set1_ps(-127.0f);
39 const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
40
41 const __m256 vc0 = _mm256_set1_ps(1.0f);
42 const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
43 const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
44 const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
45 const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
46 const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
47
48 const __m256 vscalev = _mm256_set1_ps(scale_value);
49 const __m256 vscalee = _mm256_set1_ps(scale_exp);
50
51 for (; batch >= 56 * sizeof(float); batch -= 56 * sizeof(float)) {
52 // Load 56 (7x8) inputs at a time.
53 const __m256 vx0 = _mm256_loadu_ps(input);
54 const __m256 vx1 = _mm256_loadu_ps(input + 8);
55 const __m256 vx2 = _mm256_loadu_ps(input + 16);
56 const __m256 vx3 = _mm256_loadu_ps(input + 24);
57 const __m256 vx4 = _mm256_loadu_ps(input + 32);
58 const __m256 vx5 = _mm256_loadu_ps(input + 40);
59 const __m256 vx6 = _mm256_loadu_ps(input + 48);
60 input += 56;
61
62 // Compute reduced argument batch := round(input / log(2)).
63 const __m256 vn0 = _mm256_round_ps(_mm256_mul_ps(vx0, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
64 const __m256 vn1 = _mm256_round_ps(_mm256_mul_ps(vx1, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
65 const __m256 vn2 = _mm256_round_ps(_mm256_mul_ps(vx2, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
66 const __m256 vn3 = _mm256_round_ps(_mm256_mul_ps(vx3, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
67 const __m256 vn4 = _mm256_round_ps(_mm256_mul_ps(vx4, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
68 const __m256 vn5 = _mm256_round_ps(_mm256_mul_ps(vx5, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
69 const __m256 vn6 = _mm256_round_ps(_mm256_mul_ps(vx6, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
70
71 // Compute reduced argument t := input - batch * log(2).
72 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
73 __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0);
74 __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_hi, vx1);
75 __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_hi, vx2);
76 __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_hi, vx3);
77 __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_hi, vx4);
78 __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_hi, vx5);
79 __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_hi, vx6);
80
81 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
82 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
83 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_lo, vt2);
84 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_lo, vt3);
85 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_lo, vt4);
86 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_lo, vt5);
87 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_lo, vt6);
88
89 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
90 __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
91 __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
92 __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
93 __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
94 __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
95 __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
96 __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
97
98 vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
99 vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
100 vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
101 vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
102 vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
103 vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
104 vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
105
106 vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
107 vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
108 vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
109 vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
110 vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
111 vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
112 vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
113
114 vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
115 vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
116 vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
117 vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
118 vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
119 vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
120 vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
121
122 vp0 = _mm256_fmadd_ps(vp0, vt0, vc0);
123 vp1 = _mm256_fmadd_ps(vp1, vt1, vc0);
124 vp2 = _mm256_fmadd_ps(vp2, vt2, vc0);
125 vp3 = _mm256_fmadd_ps(vp3, vt3, vc0);
126 vp4 = _mm256_fmadd_ps(vp4, vt4, vc0);
127 vp5 = _mm256_fmadd_ps(vp5, vt5, vc0);
128 vp6 = _mm256_fmadd_ps(vp6, vt6, vc0);
129
130 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where
131 // - vnX is "exponent"
132 // - vpX is "mantissa"
133 //
134 // exp2(ae) * av * exp2(be) * bv =
135 // = exp2(ae + be) * (av * bv)
136 __m256 vf0 = _mm256_mul_ps(vp0, vscalev);
137 __m256 vf1 = _mm256_mul_ps(vp1, vscalev);
138 __m256 vf2 = _mm256_mul_ps(vp2, vscalev);
139 __m256 vf3 = _mm256_mul_ps(vp3, vscalev);
140 __m256 vf4 = _mm256_mul_ps(vp4, vscalev);
141 __m256 vf5 = _mm256_mul_ps(vp5, vscalev);
142 __m256 vf6 = _mm256_mul_ps(vp6, vscalev);
143
144 __m256 ve0 = _mm256_add_ps(vn0, vscalee);
145 __m256 ve1 = _mm256_add_ps(vn1, vscalee);
146 __m256 ve2 = _mm256_add_ps(vn2, vscalee);
147 __m256 ve3 = _mm256_add_ps(vn3, vscalee);
148 __m256 ve4 = _mm256_add_ps(vn4, vscalee);
149 __m256 ve5 = _mm256_add_ps(vn5, vscalee);
150 __m256 ve6 = _mm256_add_ps(vn6, vscalee);
151
152 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
153 // This replacement is done in two steps:
154 // 1. Clamp minimum e at -127.0.
155 // 2. Map e to scale factor 0.0 when e == -127.0
156 ve0 = _mm256_max_ps(ve0, vmin_exponent);
157 ve1 = _mm256_max_ps(ve1, vmin_exponent);
158 ve2 = _mm256_max_ps(ve2, vmin_exponent);
159 ve3 = _mm256_max_ps(ve3, vmin_exponent);
160 ve4 = _mm256_max_ps(ve4, vmin_exponent);
161 ve5 = _mm256_max_ps(ve5, vmin_exponent);
162 ve6 = _mm256_max_ps(ve6, vmin_exponent);
163
164 // Convert exponents into scale factors:
165 // - s = exp2(e) when e > -127.0
166 // - s = 0.0 when e <= -127.0
167 const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve0, vmagic_bias)), 23));
168 const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve1, vmagic_bias)), 23));
169 const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve2, vmagic_bias)), 23));
170 const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve3, vmagic_bias)), 23));
171 const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve4, vmagic_bias)), 23));
172 const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve5, vmagic_bias)), 23));
173 const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve6, vmagic_bias)), 23));
174
175 // Multiply "mantissa" by the scale factor.
176 vf0 = _mm256_mul_ps(vf0, vs0);
177 vf1 = _mm256_mul_ps(vf1, vs1);
178 vf2 = _mm256_mul_ps(vf2, vs2);
179 vf3 = _mm256_mul_ps(vf3, vs3);
180 vf4 = _mm256_mul_ps(vf4, vs4);
181 vf5 = _mm256_mul_ps(vf5, vs5);
182 vf6 = _mm256_mul_ps(vf6, vs6);
183
184 // Store 56 (7x8) outputs at a time.
185 _mm256_storeu_ps(output, vf0);
186 _mm256_storeu_ps(output + 8, vf1);
187 _mm256_storeu_ps(output + 16, vf2);
188 _mm256_storeu_ps(output + 24, vf3);
189 _mm256_storeu_ps(output + 32, vf4);
190 _mm256_storeu_ps(output + 40, vf5);
191 _mm256_storeu_ps(output + 48, vf6);
192 output += 56;
193 }
194
195 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
196 // Load 8 inputs at a time.
197 const __m256 vx = _mm256_loadu_ps(input);
198 input += 8;
199
200 // Compute reduced argument batch := round(input / log(2)).
201 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
202
203 // Compute reduced argument t := input - batch * log(2).
204 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
205 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
206 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
207
208 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
209 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
210 vp = _mm256_fmadd_ps(vp, vt, vc3);
211 vp = _mm256_fmadd_ps(vp, vt, vc2);
212 vp = _mm256_fmadd_ps(vp, vt, vc1);
213 vp = _mm256_fmadd_ps(vp, vt, vc0);
214
215 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
216 __m256 vf = _mm256_mul_ps(vp, vscalev);
217 __m256 ve = _mm256_add_ps(vn, vscalee);
218
219 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
220 ve = _mm256_max_ps(ve, vmin_exponent);
221
222 // Convert exponents into scale factors.
223 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
224
225 // Multiply "mantissa" by the scale factor.
226 vf = _mm256_mul_ps(vf, vs);
227
228 // Store 8 results at a time.
229 _mm256_storeu_ps(output, vf);
230 output += 8;
231 }
232 if XNN_UNLIKELY(batch != 0) {
233 assert(batch >= 1 * sizeof(float));
234 assert(batch <= 7 * sizeof(float));
235 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - batch));
236
237 // Load up to 7 inputs at a time.
238 const __m256 vx = _mm256_maskload_ps(input, vmask);
239
240 // Compute reduced argument batch := round(input / log(2)).
241 const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
242
243 // Compute reduced argument t := input - batch * log(2).
244 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
245 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
246 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
247
248 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
249 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
250 vp = _mm256_fmadd_ps(vp, vt, vc3);
251 vp = _mm256_fmadd_ps(vp, vt, vc2);
252 vp = _mm256_fmadd_ps(vp, vt, vc1);
253 vp = _mm256_fmadd_ps(vp, vt, vc0);
254
255 // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
256 __m256 vf = _mm256_mul_ps(vp, vscalev);
257 __m256 ve = _mm256_add_ps(vn, vscalee);
258
259 // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
260 ve = _mm256_max_ps(ve, vmin_exponent);
261
262 // Convert exponents into scale factors.
263 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
264
265 // Multiply "mantissa" by the scale factor.
266 vf = _mm256_mul_ps(vf, vs);
267
268 // Store up to 7 inputs at a time.
269 _mm256_maskstore_ps(output, vmask, vf);
270 }
271 }