Debian Codegraph @

src/f32-vscaleexpminusmax/gen/f32-vscaleexpminusmax-avx2-p5-u16.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vscaleexpminusmax/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/vscaleexpminusmax.h"
16
17
18 void xnn_f32_vscaleexpminusmax_ukernel__avx2_p5_u16(
19 size_t batch,
20 const float* input,
21 float* output,
22 float scale,
23 float max)
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 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
33 // The smallest x for which expf(x) is normalized.
34 const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep6f);
35 const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
36 const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f);
37 const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f);
38
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 vscale = _mm256_set1_ps(scale);
46 const __m256 vi_max = _mm256_set1_ps(max);
47
48 for (; batch >= 16 * sizeof(float); batch -= 16 * sizeof(float)) {
49 // Load 16 (2x8) inputs at a time.
50 const __m256 vi0 = _mm256_loadu_ps(input);
51 const __m256 vi1 = _mm256_loadu_ps(input + 8);
52 input += 16;
53
54 // Subtract maximum input x := i - i_max. This implies x <= 0.
55 const __m256 vx0 = _mm256_sub_ps(vi0, vi_max);
56 const __m256 vx1 = _mm256_sub_ps(vi1, vi_max);
57
58 // Compute reduced argument batch := round(x / log(2)).
59 __m256 vn0 = _mm256_fmadd_ps(vx0, vlog2e, vmagic_bias);
60 __m256 vn1 = _mm256_fmadd_ps(vx1, vlog2e, vmagic_bias);
61
62 // Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e.
63 // -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly.
64 const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
65 const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
66
67 // Subtract the large number back to get final batch := round(x / log(2)).
68 vn0 = _mm256_sub_ps(vn0, vmagic_bias);
69 vn1 = _mm256_sub_ps(vn1, vmagic_bias);
70
71 // Compute reduced argument t := x - 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
76 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2_lo, vt0);
77 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2_lo, vt1);
78
79 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
80 __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
81 __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
82
83 vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
84 vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
85
86 vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
87 vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
88
89 vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
90 vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
91
92 // Reconstruct the final f value:
93 // f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
94 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
95 // = s + (t * s) * p
96 vt0 = _mm256_mul_ps(vt0, vs0);
97 vt1 = _mm256_mul_ps(vt1, vs1);
98
99 __m256 vf0 = _mm256_fmadd_ps(vt0, vp0, vs0);
100 __m256 vf1 = _mm256_fmadd_ps(vt1, vp1, vs1);
101
102 // For inputs below zero cutoff, replace output with +0.0f.
103 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
104 vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vx0, vdenorm_cutoff, _CMP_LT_OS), vf0);
105 vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vx1, vdenorm_cutoff, _CMP_LT_OS), vf1);
106
107 // Multiply by scale.
108 vf0 = _mm256_mul_ps(vf0, vscale);
109 vf1 = _mm256_mul_ps(vf1, vscale);
110
111 // Store 16 (2x8) outputs at a time.
112 _mm256_storeu_ps(output, vf0);
113 _mm256_storeu_ps(output + 8, vf1);
114 output += 16;
115 }
116 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
117 // Load 8 inputs at a time.
118 const __m256 vi = _mm256_loadu_ps(input);
119 input += 8;
120
121 // Subtract maximum input x := i - i_max. This implies x <= 0.
122 const __m256 vx = _mm256_sub_ps(vi, vi_max);
123
124 // Compute reduced argument batch := round(x / log(2)).
125 __m256 vn = _mm256_fmadd_ps(vx, vlog2e, vmagic_bias);
126
127 // Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e.
128 // -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly.
129 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
130
131 // Subtract the large number back to get final batch := round(x / log(2)).
132 vn = _mm256_sub_ps(vn, vmagic_bias);
133
134 // Compute reduced argument t := x - batch * log(2).
135 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
136 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
137 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
138
139 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
140 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
141 vp = _mm256_fmadd_ps(vp, vt, vc3);
142 vp = _mm256_fmadd_ps(vp, vt, vc2);
143 vp = _mm256_fmadd_ps(vp, vt, vc1);
144
145 // Reconstruct the final f value:
146 // f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
147 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
148 // = s + (t * s) * p
149 vt = _mm256_mul_ps(vt, vs);
150 __m256 vf = _mm256_fmadd_ps(vt, vp, vs);
151
152 // For inputs below zero cutoff, replace output with +0.0f.
153 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
154 vf = _mm256_andnot_ps(_mm256_cmp_ps(vx, vdenorm_cutoff, _CMP_LT_OS), vf);
155
156 // Multiply by scale.
157 vf = _mm256_mul_ps(vf, vscale);
158
159 // Store 64 (8x8) outputs at a time.
160 _mm256_storeu_ps(output, vf);
161 output += 8;
162 }
163 if (batch != 0) {
164 assert(batch >= 1 * sizeof(float));
165 assert(batch <= 7 * sizeof(float));
166 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - batch));
167
168 // Load up to 7 inputs at a time.
169 const __m256 vi = _mm256_maskload_ps(input, vmask);
170
171 // Subtract maximum input x := i - i_max. This implies x <= 0.
172 const __m256 vx = _mm256_sub_ps(vi, vi_max);
173
174 // Compute reduced argument batch := round(x / log(2)).
175 __m256 vn = _mm256_fmadd_ps(vx, vlog2e, vmagic_bias);
176
177 // Create a floating-point number s (scale) such that s == 2**batch for inputs which don't cause underflow, i.e.
178 // -87.33642 <= x <= 0.0, and -126 <= batch <= 0 accordingly.
179 const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
180
181 // Subtract the large number back to get final batch := round(x / log(2)).
182 vn = _mm256_sub_ps(vn, vmagic_bias);
183
184 // Compute reduced argument t := x - batch * log(2).
185 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
186 __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vx);
187 vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt);
188
189 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
190 __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
191 vp = _mm256_fmadd_ps(vp, vt, vc3);
192 vp = _mm256_fmadd_ps(vp, vt, vc2);
193 vp = _mm256_fmadd_ps(vp, vt, vc1);
194
195 // Reconstruct the final f value:
196 // f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
197 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
198 // = s + (t * s) * p
199 vt = _mm256_mul_ps(vt, vs);
200 __m256 vf = _mm256_fmadd_ps(vt, vp, vs);
201
202 // For inputs below zero cutoff, replace output with +0.0f.
203 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
204 vf = _mm256_andnot_ps(_mm256_cmp_ps(vx, vdenorm_cutoff, _CMP_LT_OS), vf);
205
206 // Multiply by scale.
207 vf = _mm256_mul_ps(vf, vscale);
208
209 // Store up to 7 outputs at a time.
210 _mm256_maskstore_ps(output, vmask, vf);
211 }
212 }