xref: /aosp_15_r20/external/XNNPACK/src/f32-vscaleextexp/gen/avx2-p5-x40.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_x40(size_t elements,const float * x,float * y,float scale_value,float scale_exp)20 void xnn_f32_vscaleextexp_ukernel__avx2_p5_x40(
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 >= 40 * sizeof(float); elements -= 40 * sizeof(float)) {
49     // Load 40 (5x8) 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     x += 40;
56 
57     // Compute reduced argument elements := round(x / log(2)).
58     const __m256 vn0 = _mm256_round_ps(_mm256_mul_ps(vx0, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
59     const __m256 vn1 = _mm256_round_ps(_mm256_mul_ps(vx1, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
60     const __m256 vn2 = _mm256_round_ps(_mm256_mul_ps(vx2, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
61     const __m256 vn3 = _mm256_round_ps(_mm256_mul_ps(vx3, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
62     const __m256 vn4 = _mm256_round_ps(_mm256_mul_ps(vx4, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
63 
64     // Compute reduced argument t := x - elements * log(2).
65     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
66     __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_hi, vx0);
67     __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_hi, vx1);
68     __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_hi, vx2);
69     __m256 vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_hi, vx3);
70     __m256 vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_hi, vx4);
71 
72     vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
73     vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
74     vt2 = _mm256_fmadd_ps(vn2, vminus_ln2_lo, vt2);
75     vt3 = _mm256_fmadd_ps(vn3, vminus_ln2_lo, vt3);
76     vt4 = _mm256_fmadd_ps(vn4, vminus_ln2_lo, vt4);
77 
78     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
79     __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
80     __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
81     __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
82     __m256 vp3 = _mm256_fmadd_ps(vc5, vt3, vc4);
83     __m256 vp4 = _mm256_fmadd_ps(vc5, vt4, vc4);
84 
85     vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
86     vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
87     vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
88     vp3 = _mm256_fmadd_ps(vp3, vt3, vc3);
89     vp4 = _mm256_fmadd_ps(vp4, vt4, vc3);
90 
91     vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
92     vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
93     vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
94     vp3 = _mm256_fmadd_ps(vp3, vt3, vc2);
95     vp4 = _mm256_fmadd_ps(vp4, vt4, vc2);
96 
97     vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
98     vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
99     vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
100     vp3 = _mm256_fmadd_ps(vp3, vt3, vc1);
101     vp4 = _mm256_fmadd_ps(vp4, vt4, vc1);
102 
103     vp0 = _mm256_fmadd_ps(vp0, vt0, vc0);
104     vp1 = _mm256_fmadd_ps(vp1, vt1, vc0);
105     vp2 = _mm256_fmadd_ps(vp2, vt2, vc0);
106     vp3 = _mm256_fmadd_ps(vp3, vt3, vc0);
107     vp4 = _mm256_fmadd_ps(vp4, vt4, vc0);
108 
109     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation where
110     //  - vnX is "exponent"
111     //  - vpX is "mantissa"
112     //
113     // exp2(ae) * av * exp2(be) * bv =
114     //   = exp2(ae + be) * (av * bv)
115     __m256 vf0 = _mm256_mul_ps(vp0, vscalev);
116     __m256 vf1 = _mm256_mul_ps(vp1, vscalev);
117     __m256 vf2 = _mm256_mul_ps(vp2, vscalev);
118     __m256 vf3 = _mm256_mul_ps(vp3, vscalev);
119     __m256 vf4 = _mm256_mul_ps(vp4, vscalev);
120 
121     __m256 ve0 = _mm256_add_ps(vn0, vscalee);
122     __m256 ve1 = _mm256_add_ps(vn1, vscalee);
123     __m256 ve2 = _mm256_add_ps(vn2, vscalee);
124     __m256 ve3 = _mm256_add_ps(vn3, vscalee);
125     __m256 ve4 = _mm256_add_ps(vn4, vscalee);
126 
127     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
128     // This replacement is done in two steps:
129     // 1. Clamp minimum e at -127.0.
130     // 2. Map e to scale factor 0.0 when e == -127.0
131     ve0 = _mm256_max_ps(ve0, vmin_exponent);
132     ve1 = _mm256_max_ps(ve1, vmin_exponent);
133     ve2 = _mm256_max_ps(ve2, vmin_exponent);
134     ve3 = _mm256_max_ps(ve3, vmin_exponent);
135     ve4 = _mm256_max_ps(ve4, vmin_exponent);
136 
137     // Convert exponents into scale factors:
138     // - s = exp2(e) when e > -127.0
139     // - s = 0.0 when e <= -127.0
140     const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve0, vmagic_bias)), 23));
141     const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve1, vmagic_bias)), 23));
142     const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve2, vmagic_bias)), 23));
143     const __m256 vs3 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve3, vmagic_bias)), 23));
144     const __m256 vs4 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve4, vmagic_bias)), 23));
145 
146     // Multiply "mantissa" by the scale factor.
147     vf0 = _mm256_mul_ps(vf0, vs0);
148     vf1 = _mm256_mul_ps(vf1, vs1);
149     vf2 = _mm256_mul_ps(vf2, vs2);
150     vf3 = _mm256_mul_ps(vf3, vs3);
151     vf4 = _mm256_mul_ps(vf4, vs4);
152 
153     // Store 40 (5x8) outputs at a time.
154     _mm256_storeu_ps(y, vf0);
155     _mm256_storeu_ps(y + 8, vf1);
156     _mm256_storeu_ps(y + 16, vf2);
157     _mm256_storeu_ps(y + 24, vf3);
158     _mm256_storeu_ps(y + 32, vf4);
159     y += 40;
160   }
161 
162   for (; elements >= 8 * sizeof(float); elements -= 8 * sizeof(float)) {
163     // Load 8 inputs at a time.
164     const __m256 vx = _mm256_loadu_ps(x);
165     x += 8;
166 
167     // Compute reduced argument elements := round(x / log(2)).
168     const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
169 
170     // Compute reduced argument t := x - elements * log(2).
171     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
172     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
173     vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
174 
175     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
176     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
177     vp = _mm256_fmadd_ps(vp, vt, vc3);
178     vp = _mm256_fmadd_ps(vp, vt, vc2);
179     vp = _mm256_fmadd_ps(vp, vt, vc1);
180     vp = _mm256_fmadd_ps(vp, vt, vc0);
181 
182     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
183     __m256 vf = _mm256_mul_ps(vp, vscalev);
184     __m256 ve = _mm256_add_ps(vn, vscalee);
185 
186     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
187     ve = _mm256_max_ps(ve, vmin_exponent);
188 
189     // Convert exponents into scale factors.
190     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
191 
192     // Multiply "mantissa" by the scale factor.
193     vf = _mm256_mul_ps(vf, vs);
194 
195     // Store 8 results at a time.
196     _mm256_storeu_ps(y, vf);
197     y += 8;
198   }
199   if XNN_UNLIKELY(elements != 0) {
200     assert(elements >= 1 * sizeof(float));
201     assert(elements <= 7 * sizeof(float));
202     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - elements));
203 
204     // Load up to 7 inputs at a time.
205     const __m256 vx = _mm256_maskload_ps(x, vmask);
206 
207     // Compute reduced argument elements := round(x / log(2)).
208     const __m256 vn = _mm256_round_ps(_mm256_mul_ps(vx, vlog2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
209 
210     // Compute reduced argument t := x - elements * log(2).
211     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
212     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
213     vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
214 
215     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
216     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
217     vp = _mm256_fmadd_ps(vp, vt, vc3);
218     vp = _mm256_fmadd_ps(vp, vt, vc2);
219     vp = _mm256_fmadd_ps(vp, vt, vc1);
220     vp = _mm256_fmadd_ps(vp, vt, vc0);
221 
222     // Multiply "extended" floating-point numbers in ("mantissa", "exponent") representation.
223     __m256 vf = _mm256_mul_ps(vp, vscalev);
224     __m256 ve = _mm256_add_ps(vn, vscalee);
225 
226     // For computational efficiency, replace exp2(e) with 0.0f when e <= -127.0.
227     ve = _mm256_max_ps(ve, vmin_exponent);
228 
229     // Convert exponents into scale factors.
230     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(_mm256_add_ps(ve, vmagic_bias)), 23));
231 
232     // Multiply "mantissa" by the scale factor.
233     vf = _mm256_mul_ps(vf, vs);
234 
235     // Store up to 7 inputs at a time.
236     _mm256_maskstore_ps(y, vmask, vf);
237   }
238   _mm256_zeroupper();
239 }
240