Eigen/src/Core/arch/AVX512/MathFunctions.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016 Pedro Gonnet (pedro.gonnet@gmail.com)
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef THIRD_PARTY_EIGEN3_EIGEN_SRC_CORE_ARCH_AVX512_MATHFUNCTIONS_H_
11 #define THIRD_PARTY_EIGEN3_EIGEN_SRC_CORE_ARCH_AVX512_MATHFUNCTIONS_H_
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 // Disable the code for older versions of gcc that don't support many of the required avx512 instrinsics.
18 #if EIGEN_GNUC_AT_LEAST(5, 3)
19 
20 #define _EIGEN_DECLARE_CONST_Packet16f(NAME, X) \
21  const Packet16f p16f_##NAME = pset1<Packet16f>(X)
22 
23 #define _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(NAME, X) \
24  const Packet16f p16f_##NAME = (__m512)pset1<Packet16i>(X)
25 
26 #define _EIGEN_DECLARE_CONST_Packet8d(NAME, X) \
27  const Packet8d p8d_##NAME = pset1<Packet8d>(X)
28 
29 #define _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(NAME, X) \
30  const Packet8d p8d_##NAME = _mm512_castsi512_pd(_mm512_set1_epi64(X))
31 
32 // Natural logarithm
33 // Computes log(x) as log(2^e * m) = C*e + log(m), where the constant C =log(2)
34 // and m is in the range [sqrt(1/2),sqrt(2)). In this range, the logarithm can
35 // be easily approximated by a polynomial centered on m=1 for stability.
36 #if defined(EIGEN_VECTORIZE_AVX512DQ)
37 template <>
39 plog<Packet16f>(const Packet16f& _x) {
40  Packet16f x = _x;
41  _EIGEN_DECLARE_CONST_Packet16f(1, 1.0f);
42  _EIGEN_DECLARE_CONST_Packet16f(half, 0.5f);
43  _EIGEN_DECLARE_CONST_Packet16f(126f, 126.0f);
44 
45  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(inv_mant_mask, ~0x7f800000);
46 
47  // The smallest non denormalized float number.
48  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(min_norm_pos, 0x00800000);
49  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(minus_inf, 0xff800000);
50  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(nan, 0x7fc00000);
51 
52  // Polynomial coefficients.
53  _EIGEN_DECLARE_CONST_Packet16f(cephes_SQRTHF, 0.707106781186547524f);
54  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p0, 7.0376836292E-2f);
55  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p1, -1.1514610310E-1f);
56  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p2, 1.1676998740E-1f);
57  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p3, -1.2420140846E-1f);
58  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p4, +1.4249322787E-1f);
59  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p5, -1.6668057665E-1f);
60  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p6, +2.0000714765E-1f);
61  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p7, -2.4999993993E-1f);
62  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_p8, +3.3333331174E-1f);
63  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_q1, -2.12194440e-4f);
64  _EIGEN_DECLARE_CONST_Packet16f(cephes_log_q2, 0.693359375f);
65 
66  // invalid_mask is set to true when x is NaN
67  __mmask16 invalid_mask =
68  _mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_NGE_UQ);
69  __mmask16 iszero_mask =
70  _mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_EQ_UQ);
71 
72  // Truncate input values to the minimum positive normal.
73  x = pmax(x, p16f_min_norm_pos);
74 
75  // Extract the shifted exponents.
76  Packet16f emm0 = _mm512_cvtepi32_ps(_mm512_srli_epi32((__m512i)x, 23));
77  Packet16f e = _mm512_sub_ps(emm0, p16f_126f);
78 
79  // Set the exponents to -1, i.e. x are in the range [0.5,1).
80  x = _mm512_and_ps(x, p16f_inv_mant_mask);
81  x = _mm512_or_ps(x, p16f_half);
82 
83  // part2: Shift the inputs from the range [0.5,1) to [sqrt(1/2),sqrt(2))
84  // and shift by -1. The values are then centered around 0, which improves
85  // the stability of the polynomial evaluation.
86  // if( x < SQRTHF ) {
87  // e -= 1;
88  // x = x + x - 1.0;
89  // } else { x = x - 1.0; }
90  __mmask16 mask = _mm512_cmp_ps_mask(x, p16f_cephes_SQRTHF, _CMP_LT_OQ);
91  Packet16f tmp = _mm512_mask_blend_ps(mask, x, _mm512_setzero_ps());
92  x = psub(x, p16f_1);
93  e = psub(e, _mm512_mask_blend_ps(mask, p16f_1, _mm512_setzero_ps()));
94  x = padd(x, tmp);
95 
96  Packet16f x2 = pmul(x, x);
97  Packet16f x3 = pmul(x2, x);
98 
99  // Evaluate the polynomial approximant of degree 8 in three parts, probably
100  // to improve instruction-level parallelism.
101  Packet16f y, y1, y2;
102  y = pmadd(p16f_cephes_log_p0, x, p16f_cephes_log_p1);
103  y1 = pmadd(p16f_cephes_log_p3, x, p16f_cephes_log_p4);
104  y2 = pmadd(p16f_cephes_log_p6, x, p16f_cephes_log_p7);
105  y = pmadd(y, x, p16f_cephes_log_p2);
106  y1 = pmadd(y1, x, p16f_cephes_log_p5);
107  y2 = pmadd(y2, x, p16f_cephes_log_p8);
108  y = pmadd(y, x3, y1);
109  y = pmadd(y, x3, y2);
110  y = pmul(y, x3);
111 
112  // Add the logarithm of the exponent back to the result of the interpolation.
113  y1 = pmul(e, p16f_cephes_log_q1);
114  tmp = pmul(x2, p16f_half);
115  y = padd(y, y1);
116  x = psub(x, tmp);
117  y2 = pmul(e, p16f_cephes_log_q2);
118  x = padd(x, y);
119  x = padd(x, y2);
120 
121  // Filter out invalid inputs, i.e. negative arg will be NAN, 0 will be -INF.
122  return _mm512_mask_blend_ps(iszero_mask, p16f_minus_inf,
123  _mm512_mask_blend_ps(invalid_mask, p16f_nan, x));
124 }
125 #endif
126 
127 // Exponential function. Works by writing "x = m*log(2) + r" where
128 // "m = floor(x/log(2)+1/2)" and "r" is the remainder. The result is then
129 // "exp(x) = 2^m*exp(r)" where exp(r) is in the range [-1,1).
130 template <>
132 pexp<Packet16f>(const Packet16f& _x) {
133  _EIGEN_DECLARE_CONST_Packet16f(1, 1.0f);
134  _EIGEN_DECLARE_CONST_Packet16f(half, 0.5f);
135  _EIGEN_DECLARE_CONST_Packet16f(127, 127.0f);
136 
137  _EIGEN_DECLARE_CONST_Packet16f(exp_hi, 88.3762626647950f);
138  _EIGEN_DECLARE_CONST_Packet16f(exp_lo, -88.3762626647949f);
139 
140  _EIGEN_DECLARE_CONST_Packet16f(cephes_LOG2EF, 1.44269504088896341f);
141 
142  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p0, 1.9875691500E-4f);
143  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p1, 1.3981999507E-3f);
144  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p2, 8.3334519073E-3f);
145  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p3, 4.1665795894E-2f);
146  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p4, 1.6666665459E-1f);
147  _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p5, 5.0000001201E-1f);
148 
149  // Clamp x.
150  Packet16f x = pmax(pmin(_x, p16f_exp_hi), p16f_exp_lo);
151 
152  // Express exp(x) as exp(m*ln(2) + r), start by extracting
153  // m = floor(x/ln(2) + 0.5).
154  Packet16f m = _mm512_floor_ps(pmadd(x, p16f_cephes_LOG2EF, p16f_half));
155 
156  // Get r = x - m*ln(2). Note that we can do this without losing more than one
157  // ulp precision due to the FMA instruction.
158  _EIGEN_DECLARE_CONST_Packet16f(nln2, -0.6931471805599453f);
159  Packet16f r = _mm512_fmadd_ps(m, p16f_nln2, x);
160  Packet16f r2 = pmul(r, r);
161 
162  // TODO(gonnet): Split into odd/even polynomials and try to exploit
163  // instruction-level parallelism.
164  Packet16f y = p16f_cephes_exp_p0;
165  y = pmadd(y, r, p16f_cephes_exp_p1);
166  y = pmadd(y, r, p16f_cephes_exp_p2);
167  y = pmadd(y, r, p16f_cephes_exp_p3);
168  y = pmadd(y, r, p16f_cephes_exp_p4);
169  y = pmadd(y, r, p16f_cephes_exp_p5);
170  y = pmadd(y, r2, r);
171  y = padd(y, p16f_1);
172 
173  // Build emm0 = 2^m.
174  Packet16i emm0 = _mm512_cvttps_epi32(padd(m, p16f_127));
175  emm0 = _mm512_slli_epi32(emm0, 23);
176 
177  // Return 2^m * exp(r).
178  return pmax(pmul(y, _mm512_castsi512_ps(emm0)), _x);
179 }
180 
181 /*template <>
182 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8d
183 pexp<Packet8d>(const Packet8d& _x) {
184  Packet8d x = _x;
185 
186  _EIGEN_DECLARE_CONST_Packet8d(1, 1.0);
187  _EIGEN_DECLARE_CONST_Packet8d(2, 2.0);
188 
189  _EIGEN_DECLARE_CONST_Packet8d(exp_hi, 709.437);
190  _EIGEN_DECLARE_CONST_Packet8d(exp_lo, -709.436139303);
191 
192  _EIGEN_DECLARE_CONST_Packet8d(cephes_LOG2EF, 1.4426950408889634073599);
193 
194  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p0, 1.26177193074810590878e-4);
195  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p1, 3.02994407707441961300e-2);
196  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p2, 9.99999999999999999910e-1);
197 
198  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q0, 3.00198505138664455042e-6);
199  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q1, 2.52448340349684104192e-3);
200  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q2, 2.27265548208155028766e-1);
201  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q3, 2.00000000000000000009e0);
202 
203  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_C1, 0.693145751953125);
204  _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_C2, 1.42860682030941723212e-6);
205 
206  // clamp x
207  x = pmax(pmin(x, p8d_exp_hi), p8d_exp_lo);
208 
209  // Express exp(x) as exp(g + n*log(2)).
210  const Packet8d n =
211  _mm512_mul_round_pd(p8d_cephes_LOG2EF, x, _MM_FROUND_TO_NEAREST_INT);
212 
213  // Get the remainder modulo log(2), i.e. the "g" described above. Subtract
214  // n*log(2) out in two steps, i.e. n*C1 + n*C2, C1+C2=log2 to get the last
215  // digits right.
216  const Packet8d nC1 = pmul(n, p8d_cephes_exp_C1);
217  const Packet8d nC2 = pmul(n, p8d_cephes_exp_C2);
218  x = psub(x, nC1);
219  x = psub(x, nC2);
220 
221  const Packet8d x2 = pmul(x, x);
222 
223  // Evaluate the numerator polynomial of the rational interpolant.
224  Packet8d px = p8d_cephes_exp_p0;
225  px = pmadd(px, x2, p8d_cephes_exp_p1);
226  px = pmadd(px, x2, p8d_cephes_exp_p2);
227  px = pmul(px, x);
228 
229  // Evaluate the denominator polynomial of the rational interpolant.
230  Packet8d qx = p8d_cephes_exp_q0;
231  qx = pmadd(qx, x2, p8d_cephes_exp_q1);
232  qx = pmadd(qx, x2, p8d_cephes_exp_q2);
233  qx = pmadd(qx, x2, p8d_cephes_exp_q3);
234 
235  // I don't really get this bit, copied from the SSE2 routines, so...
236  // TODO(gonnet): Figure out what is going on here, perhaps find a better
237  // rational interpolant?
238  x = _mm512_div_pd(px, psub(qx, px));
239  x = pmadd(p8d_2, x, p8d_1);
240 
241  // Build e=2^n.
242  const Packet8d e = _mm512_castsi512_pd(_mm512_slli_epi64(
243  _mm512_add_epi64(_mm512_cvtpd_epi64(n), _mm512_set1_epi64(1023)), 52));
244 
245  // Construct the result 2^n * exp(g) = e * x. The max is used to catch
246  // non-finite values in the input.
247  return pmax(pmul(x, e), _x);
248  }*/
249 
250 // Functions for sqrt.
251 // The EIGEN_FAST_MATH version uses the _mm_rsqrt_ps approximation and one step
252 // of Newton's method, at a cost of 1-2 bits of precision as opposed to the
253 // exact solution. The main advantage of this approach is not just speed, but
254 // also the fact that it can be inlined and pipelined with other computations,
255 // further reducing its effective latency.
256 #if EIGEN_FAST_MATH
257 template <>
259 psqrt<Packet16f>(const Packet16f& _x) {
260  _EIGEN_DECLARE_CONST_Packet16f(one_point_five, 1.5f);
261  _EIGEN_DECLARE_CONST_Packet16f(minus_half, -0.5f);
262  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(flt_min, 0x00800000);
263 
264  Packet16f neg_half = pmul(_x, p16f_minus_half);
265 
266  // select only the inverse sqrt of positive normal inputs (denormals are
267  // flushed to zero and cause infs as well).
268  __mmask16 non_zero_mask = _mm512_cmp_ps_mask(_x, p16f_flt_min, _CMP_GE_OQ);
269  Packet16f x = _mm512_mask_blend_ps(non_zero_mask, _mm512_rsqrt14_ps(_x),
270  _mm512_setzero_ps());
271 
272  // Do a single step of Newton's iteration.
273  x = pmul(x, pmadd(neg_half, pmul(x, x), p16f_one_point_five));
274 
275  // Multiply the original _x by it's reciprocal square root to extract the
276  // square root.
277  return pmul(_x, x);
278 }
279 
280 template <>
282 psqrt<Packet8d>(const Packet8d& _x) {
283  _EIGEN_DECLARE_CONST_Packet8d(one_point_five, 1.5);
284  _EIGEN_DECLARE_CONST_Packet8d(minus_half, -0.5);
285  _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(dbl_min, 0x0010000000000000LL);
286 
287  Packet8d neg_half = pmul(_x, p8d_minus_half);
288 
289  // select only the inverse sqrt of positive normal inputs (denormals are
290  // flushed to zero and cause infs as well).
291  __mmask8 non_zero_mask = _mm512_cmp_pd_mask(_x, p8d_dbl_min, _CMP_GE_OQ);
292  Packet8d x = _mm512_mask_blend_pd(non_zero_mask, _mm512_rsqrt14_pd(_x),
293  _mm512_setzero_pd());
294 
295  // Do a first step of Newton's iteration.
296  x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
297 
298  // Do a second step of Newton's iteration.
299  x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
300 
301  // Multiply the original _x by it's reciprocal square root to extract the
302  // square root.
303  return pmul(_x, x);
304 }
305 #else
306 template <>
307 EIGEN_STRONG_INLINE Packet16f psqrt<Packet16f>(const Packet16f& x) {
308  return _mm512_sqrt_ps(x);
309 }
310 template <>
311 EIGEN_STRONG_INLINE Packet8d psqrt<Packet8d>(const Packet8d& x) {
312  return _mm512_sqrt_pd(x);
313 }
314 #endif
315 
316 // Functions for rsqrt.
317 // Almost identical to the sqrt routine, just leave out the last multiplication
318 // and fill in NaN/Inf where needed. Note that this function only exists as an
319 // iterative version for doubles since there is no instruction for diretly
320 // computing the reciprocal square root in AVX-512.
321 #ifdef EIGEN_FAST_MATH
322 template <>
324 prsqrt<Packet16f>(const Packet16f& _x) {
325  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(inf, 0x7f800000);
326  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(nan, 0x7fc00000);
327  _EIGEN_DECLARE_CONST_Packet16f(one_point_five, 1.5f);
328  _EIGEN_DECLARE_CONST_Packet16f(minus_half, -0.5f);
329  _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(flt_min, 0x00800000);
330 
331  Packet16f neg_half = pmul(_x, p16f_minus_half);
332 
333  // select only the inverse sqrt of positive normal inputs (denormals are
334  // flushed to zero and cause infs as well).
335  __mmask16 le_zero_mask = _mm512_cmp_ps_mask(_x, p16f_flt_min, _CMP_LT_OQ);
336  Packet16f x = _mm512_mask_blend_ps(le_zero_mask, _mm512_setzero_ps(),
337  _mm512_rsqrt14_ps(_x));
338 
339  // Fill in NaNs and Infs for the negative/zero entries.
340  __mmask16 neg_mask = _mm512_cmp_ps_mask(_x, _mm512_setzero_ps(), _CMP_LT_OQ);
341  Packet16f infs_and_nans = _mm512_mask_blend_ps(
342  neg_mask, p16f_nan,
343  _mm512_mask_blend_ps(le_zero_mask, p16f_inf, _mm512_setzero_ps()));
344 
345  // Do a single step of Newton's iteration.
346  x = pmul(x, pmadd(neg_half, pmul(x, x), p16f_one_point_five));
347 
348  // Insert NaNs and Infs in all the right places.
349  return _mm512_mask_blend_ps(le_zero_mask, infs_and_nans, x);
350 }
351 
352 template <>
354 prsqrt<Packet8d>(const Packet8d& _x) {
355  _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(inf, 0x7ff0000000000000LL);
356  _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(nan, 0x7ff1000000000000LL);
357  _EIGEN_DECLARE_CONST_Packet8d(one_point_five, 1.5);
358  _EIGEN_DECLARE_CONST_Packet8d(minus_half, -0.5);
359  _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(dbl_min, 0x0010000000000000LL);
360 
361  Packet8d neg_half = pmul(_x, p8d_minus_half);
362 
363  // select only the inverse sqrt of positive normal inputs (denormals are
364  // flushed to zero and cause infs as well).
365  __mmask8 le_zero_mask = _mm512_cmp_pd_mask(_x, p8d_dbl_min, _CMP_LT_OQ);
366  Packet8d x = _mm512_mask_blend_pd(le_zero_mask, _mm512_setzero_pd(),
367  _mm512_rsqrt14_pd(_x));
368 
369  // Fill in NaNs and Infs for the negative/zero entries.
370  __mmask8 neg_mask = _mm512_cmp_pd_mask(_x, _mm512_setzero_pd(), _CMP_LT_OQ);
371  Packet8d infs_and_nans = _mm512_mask_blend_pd(
372  neg_mask, p8d_nan,
373  _mm512_mask_blend_pd(le_zero_mask, p8d_inf, _mm512_setzero_pd()));
374 
375  // Do a first step of Newton's iteration.
376  x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
377 
378  // Do a second step of Newton's iteration.
379  x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
380 
381  // Insert NaNs and Infs in all the right places.
382  return _mm512_mask_blend_pd(le_zero_mask, infs_and_nans, x);
383 }
384 #else
385 template <>
386 EIGEN_STRONG_INLINE Packet16f prsqrt<Packet16f>(const Packet16f& x) {
387  return _mm512_rsqrt28_ps(x);
388 }
389 #endif
390 #endif
391 
392 } // end namespace internal
393 
394 } // end namespace Eigen
395 
396 #endif // THIRD_PARTY_EIGEN3_EIGEN_SRC_CORE_ARCH_AVX512_MATHFUNCTIONS_H_
#define EIGEN_STRONG_INLINE
Definition: Macros.h:493
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
EIGEN_DEVICE_FUNC const Scalar & x
Definition: LDLT.h:16
#define EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
Definition: Macros.h:529
EIGEN_DEVICE_FUNC Packet padd(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC Packet pmin(const Packet &a, const Packet &b)
#define EIGEN_UNUSED
Definition: Macros.h:607
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
EIGEN_DEVICE_FUNC Packet psub(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC Packet pmul(const Packet &a, const Packet &b)
EIGEN_DEVICE_FUNC Packet pmax(const Packet &a, const Packet &b)
const T & y


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:24