Debian Codegraph @

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