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