• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
19 
xnn_f32_vscaleextexp_ukernel__avx2_p5_x88(size_t elements,const float * x,float * y,float scale_value,float scale_exp)20 void xnn_f32_vscaleextexp_ukernel__avx2_p5_x88(
21     size_t elements,
22     const float* x,
23     float* y,
24     float scale_value,
25     float scale_exp)
26 {
27   assert(elements % sizeof(float) == 0);
28 
29   const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
30   const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
31   const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
32 
33   // The smallest elements such that 2**elements is considered non-negligible.
34   // For smaller elements, 2**elements is replaced with zero.
35   const __m256 vmin_exponent = _mm256_set1_ps(-127.0f);
36   const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
37 
38   const __m256 vc0 = _mm256_set1_ps(1.0f);
39   const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f);
40   const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f);
41   const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f);
42   const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f);
43   const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f);
44 
45   const __m256 vscalev = _mm256_set1_ps(scale_value);
46   const __m256 vscalee = _mm256_set1_ps(scale_exp);
47 
48   for (; elements >= 88 * sizeof(float); elements -= 88 * sizeof(float)) {
49     // Load 88 (11x8) inputs at a time.
50     const __m256 vx0 = _mm256_loadu_ps(x);
51     const __m256 vx1 = _mm256_loadu_ps(x + 8);
52     const __m256 vx2 = _mm256_loadu_ps(x + 16);
53     const __m256 vx3 = _mm256_loadu_ps(x + 24);
54     const __m256 vx4 = _mm256_loadu_ps(x + 32);
55     const __m256 vx5 = _mm256_loadu_ps(x + 40);
56     const __m256 vx6 = _mm256_loadu_ps(x + 48);
57     const __m256 vx7 = _mm256_loadu_ps(x + 56);
58     const __m256 vx8 = _mm256_loadu_ps(x + 64);
59     const __m256 vx9 = _mm256_loadu_ps(x + 72);
60     const __m256 vx10 = _mm256_loadu_ps(x + 80);
61     x += 88;
62 
63     // Compute reduced argument elements := round(x / log(2)).
64     const __m256 vn0 = _mm256_round_ps(_mm256_mul_ps(vx0, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
65     const __m256 vn1 = _mm256_round_ps(_mm256_mul_ps(vx1, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
66     const __m256 vn2 = _mm256_round_ps(_mm256_mul_ps(vx2, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
67     const __m256 vn3 = _mm256_round_ps(_mm256_mul_ps(vx3, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
68     const __m256 vn4 = _mm256_round_ps(_mm256_mul_ps(vx4, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
69     const __m256 vn5 = _mm256_round_ps(_mm256_mul_ps(vx5, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
70     const __m256 vn6 = _mm256_round_ps(_mm256_mul_ps(vx6, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
71     const __m256 vn7 = _mm256_round_ps(_mm256_mul_ps(vx7, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
72     const __m256 vn8 = _mm256_round_ps(_mm256_mul_ps(vx8, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
73     const __m256 vn9 = _mm256_round_ps(_mm256_mul_ps(vx9, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
74     const __m256 vn10 = _mm256_round_ps(_mm256_mul_ps(vx10, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
75 
76     // Compute reduced argument t := x - elements * log(2).
77     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
78     __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0);
79     __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_hi, vx1);
80     __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_hi, vx2);
81     __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_hi, vx3);
82     __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_hi, vx4);
83     __m256 vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_hi, vx5);
84     __m256 vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_hi, vx6);
85     __m256 vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_hi, vx7);
86     __m256 vt8 = _mm256_fmadd_ps(vn8, vminus_ln2_hi, vx8);
87     __m256 vt9 = _mm256_fmadd_ps(vn9, vminus_ln2_hi, vx9);
88     __m256 vt10 = _mm256_fmadd_ps(vn10, vminus_ln2_hi, vx10);
89 
90     vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
91     vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
92     vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_lo, vt2);
93     vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_lo, vt3);
94     vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_lo, vt4);
95     vt5 = _mm256_fmadd_ps(vn5, vminus_ln2_lo, vt5);
96     vt6 = _mm256_fmadd_ps(vn6, vminus_ln2_lo, vt6);
97     vt7 = _mm256_fmadd_ps(vn7, vminus_ln2_lo, vt7);
98     vt8 = _mm256_fmadd_ps(vn8, vminus_ln2_lo, vt8);
99     vt9 = _mm256_fmadd_ps(vn9, vminus_ln2_lo, vt9);
100     vt10 = _mm256_fmadd_ps(vn10, vminus_ln2_lo, vt10);
101 
102     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
103     __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
104     __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
105     __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
106     __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
107     __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
108     __m256 vp5 = _mm256_fmadd_ps(vc5, vt5, vc4);
109     __m256 vp6 = _mm256_fmadd_ps(vc5, vt6, vc4);
110     __m256 vp7 = _mm256_fmadd_ps(vc5, vt7, vc4);
111     __m256 vp8 = _mm256_fmadd_ps(vc5, vt8, vc4);
112     __m256 vp9 = _mm256_fmadd_ps(vc5, vt9, vc4);
113     __m256 vp10 = _mm256_fmadd_ps(vc5, vt10, vc4);
114 
115     vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
116     vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
117     vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
118     vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
119     vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
120     vp5 = _mm256_fmadd_ps(vp5, vt5, vc3);
121     vp6 = _mm256_fmadd_ps(vp6, vt6, vc3);
122     vp7 = _mm256_fmadd_ps(vp7, vt7, vc3);
123     vp8 = _mm256_fmadd_ps(vp8, vt8, vc3);
124     vp9 = _mm256_fmadd_ps(vp9, vt9, vc3);
125     vp10 = _mm256_fmadd_ps(vp10, vt10, vc3);
126 
127     vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
128     vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
129     vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
130     vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
131     vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
132     vp5 = _mm256_fmadd_ps(vp5, vt5, vc2);
133     vp6 = _mm256_fmadd_ps(vp6, vt6, vc2);
134     vp7 = _mm256_fmadd_ps(vp7, vt7, vc2);
135     vp8 = _mm256_fmadd_ps(vp8, vt8, vc2);
136     vp9 = _mm256_fmadd_ps(vp9, vt9, vc2);
137     vp10 = _mm256_fmadd_ps(vp10, vt10, vc2);
138 
139     vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
140     vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
141     vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
142     vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
143     vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
144     vp5 = _mm256_fmadd_ps(vp5, vt5, vc1);
145     vp6 = _mm256_fmadd_ps(vp6, vt6, vc1);
146     vp7 = _mm256_fmadd_ps(vp7, vt7, vc1);
147     vp8 = _mm256_fmadd_ps(vp8, vt8, vc1);
148     vp9 = _mm256_fmadd_ps(vp9, vt9, vc1);
149     vp10 = _mm256_fmadd_ps(vp10, vt10, vc1);
150 
151     vp0 = _mm256_fmadd_ps(vp0, vt0, vc0);
152     vp1 = _mm256_fmadd_ps(vp1, vt1, vc0);
153     vp2 = _mm256_fmadd_ps(vp2, vt2, vc0);
154     vp3 = _mm256_fmadd_ps(vp3, vt3, vc0);
155     vp4 = _mm256_fmadd_ps(vp4, vt4, vc0);
156     vp5 = _mm256_fmadd_ps(vp5, vt5, vc0);
157     vp6 = _mm256_fmadd_ps(vp6, vt6, vc0);
158     vp7 = _mm256_fmadd_ps(vp7, vt7, vc0);
159     vp8 = _mm256_fmadd_ps(vp8, vt8, vc0);
160     vp9 = _mm256_fmadd_ps(vp9, vt9, vc0);
161     vp10 = _mm256_fmadd_ps(vp10, vt10, vc0);
162 
163     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where
164     //  - vnX is "exponent"
165     //  - vpX is "mantissa"
166     //
167     // exp2(ae) * av * exp2(be) * bv =
168     //   = exp2(ae + be) * (av * bv)
169     __m256 vf0 = _mm256_mul_ps(vp0, vscalev);
170     __m256 vf1 = _mm256_mul_ps(vp1, vscalev);
171     __m256 vf2 = _mm256_mul_ps(vp2, vscalev);
172     __m256 vf3 = _mm256_mul_ps(vp3, vscalev);
173     __m256 vf4 = _mm256_mul_ps(vp4, vscalev);
174     __m256 vf5 = _mm256_mul_ps(vp5, vscalev);
175     __m256 vf6 = _mm256_mul_ps(vp6, vscalev);
176     __m256 vf7 = _mm256_mul_ps(vp7, vscalev);
177     __m256 vf8 = _mm256_mul_ps(vp8, vscalev);
178     __m256 vf9 = _mm256_mul_ps(vp9, vscalev);
179     __m256 vf10 = _mm256_mul_ps(vp10, vscalev);
180 
181     __m256 ve0 = _mm256_add_ps(vn0, vscalee);
182     __m256 ve1 = _mm256_add_ps(vn1, vscalee);
183     __m256 ve2 = _mm256_add_ps(vn2, vscalee);
184     __m256 ve3 = _mm256_add_ps(vn3, vscalee);
185     __m256 ve4 = _mm256_add_ps(vn4, vscalee);
186     __m256 ve5 = _mm256_add_ps(vn5, vscalee);
187     __m256 ve6 = _mm256_add_ps(vn6, vscalee);
188     __m256 ve7 = _mm256_add_ps(vn7, vscalee);
189     __m256 ve8 = _mm256_add_ps(vn8, vscalee);
190     __m256 ve9 = _mm256_add_ps(vn9, vscalee);
191     __m256 ve10 = _mm256_add_ps(vn10, vscalee);
192 
193     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
194     // This replacement is done in two steps:
195     // 1. Clamp minimum e at -127.0.
196     // 2. Map e to scale factor 0.0 when e == -127.0
197     ve0 = _mm256_max_ps(ve0, vmin_exponent);
198     ve1 = _mm256_max_ps(ve1, vmin_exponent);
199     ve2 = _mm256_max_ps(ve2, vmin_exponent);
200     ve3 = _mm256_max_ps(ve3, vmin_exponent);
201     ve4 = _mm256_max_ps(ve4, vmin_exponent);
202     ve5 = _mm256_max_ps(ve5, vmin_exponent);
203     ve6 = _mm256_max_ps(ve6, vmin_exponent);
204     ve7 = _mm256_max_ps(ve7, vmin_exponent);
205     ve8 = _mm256_max_ps(ve8, vmin_exponent);
206     ve9 = _mm256_max_ps(ve9, vmin_exponent);
207     ve10 = _mm256_max_ps(ve10, vmin_exponent);
208 
209     // Convert exponents into scale factors:
210     // - s = exp2(e) when e > -127.0
211     // - s = 0.0 when e <= -127.0
212     const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve0, vmagic_bias)), 23));
213     const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve1, vmagic_bias)), 23));
214     const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve2, vmagic_bias)), 23));
215     const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve3, vmagic_bias)), 23));
216     const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve4, vmagic_bias)), 23));
217     const __m256 vs5 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve5, vmagic_bias)), 23));
218     const __m256 vs6 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve6, vmagic_bias)), 23));
219     const __m256 vs7 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve7, vmagic_bias)), 23));
220     const __m256 vs8 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve8, vmagic_bias)), 23));
221     const __m256 vs9 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve9, vmagic_bias)), 23));
222     const __m256 vs10 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve10, vmagic_bias)), 23));
223 
224     // Multiply "mantissa" by the scale factor.
225     vf0 = _mm256_mul_ps(vf0, vs0);
226     vf1 = _mm256_mul_ps(vf1, vs1);
227     vf2 = _mm256_mul_ps(vf2, vs2);
228     vf3 = _mm256_mul_ps(vf3, vs3);
229     vf4 = _mm256_mul_ps(vf4, vs4);
230     vf5 = _mm256_mul_ps(vf5, vs5);
231     vf6 = _mm256_mul_ps(vf6, vs6);
232     vf7 = _mm256_mul_ps(vf7, vs7);
233     vf8 = _mm256_mul_ps(vf8, vs8);
234     vf9 = _mm256_mul_ps(vf9, vs9);
235     vf10 = _mm256_mul_ps(vf10, vs10);
236 
237     // Store 88 (11x8) outputs at a time.
238     _mm256_storeu_ps(y, vf0);
239     _mm256_storeu_ps(y + 8, vf1);
240     _mm256_storeu_ps(y + 16, vf2);
241     _mm256_storeu_ps(y + 24, vf3);
242     _mm256_storeu_ps(y + 32, vf4);
243     _mm256_storeu_ps(y + 40, vf5);
244     _mm256_storeu_ps(y + 48, vf6);
245     _mm256_storeu_ps(y + 56, vf7);
246     _mm256_storeu_ps(y + 64, vf8);
247     _mm256_storeu_ps(y + 72, vf9);
248     _mm256_storeu_ps(y + 80, vf10);
249     y += 88;
250   }
251 
252   for (; elements >= 8 * sizeof(float); elements -= 8 * sizeof(float)) {
253     // Load 8 inputs at a time.
254     const __m256 vx = _mm256_loadu_ps(x);
255     x += 8;
256 
257     // Compute reduced argument elements := round(x / log(2)).
258     const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
259 
260     // Compute reduced argument t := x - elements * log(2).
261     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
262     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
263     vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
264 
265     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
266     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
267     vp = _mm256_fmadd_ps(vp, vt, vc3);
268     vp = _mm256_fmadd_ps(vp, vt, vc2);
269     vp = _mm256_fmadd_ps(vp, vt, vc1);
270     vp = _mm256_fmadd_ps(vp, vt, vc0);
271 
272     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
273     __m256 vf = _mm256_mul_ps(vp, vscalev);
274     __m256 ve = _mm256_add_ps(vn, vscalee);
275 
276     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
277     ve = _mm256_max_ps(ve, vmin_exponent);
278 
279     // Convert exponents into scale factors.
280     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
281 
282     // Multiply "mantissa" by the scale factor.
283     vf = _mm256_mul_ps(vf, vs);
284 
285     // Store 8 results at a time.
286     _mm256_storeu_ps(y, vf);
287     y += 8;
288   }
289   if XNN_UNLIKELY(elements != 0) {
290     assert(elements >= 1 * sizeof(float));
291     assert(elements <= 7 * sizeof(float));
292     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - elements));
293 
294     // Load up to 7 inputs at a time.
295     const __m256 vx = _mm256_maskload_ps(x, vmask);
296 
297     // Compute reduced argument elements := round(x / log(2)).
298     const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
299 
300     // Compute reduced argument t := x - elements * log(2).
301     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
302     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
303     vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
304 
305     // Compute degree-5 polynomial approxiatmion for exp(t) on [-log(2)/2, log(2)/2].
306     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
307     vp = _mm256_fmadd_ps(vp, vt, vc3);
308     vp = _mm256_fmadd_ps(vp, vt, vc2);
309     vp = _mm256_fmadd_ps(vp, vt, vc1);
310     vp = _mm256_fmadd_ps(vp, vt, vc0);
311 
312     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
313     __m256 vf = _mm256_mul_ps(vp, vscalev);
314     __m256 ve = _mm256_add_ps(vn, vscalee);
315 
316     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
317     ve = _mm256_max_ps(ve, vmin_exponent);
318 
319     // Convert exponents into scale factors.
320     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
321 
322     // Multiply "mantissa" by the scale factor.
323     vf = _mm256_mul_ps(vf, vs);
324 
325     // Store up to 7 inputs at a time.
326     _mm256_maskstore_ps(y, vmask, vf);
327   }
328   _mm256_zeroupper();
329 }
330