Debian Codegraph @

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