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 <>
38 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet16f
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, _mm512_setzero_ps(), x);
92   x = psub(x, p16f_1);
93   e = psub(e, _mm512_mask_blend_ps(mask, _mm512_setzero_ps(), p16f_1));
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,
123                               _mm512_mask_blend_ps(invalid_mask, x, p16f_nan),
124                               p16f_minus_inf);
125 }
126 #endif
127 
128 // Exponential function. Works by writing "x = m*log(2) + r" where
129 // "m = floor(x/log(2)+1/2)" and "r" is the remainder. The result is then
130 // "exp(x) = 2^m*exp(r)" where exp(r) is in the range [-1,1).
131 template <>
132 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet16f
133 pexp<Packet16f>(const Packet16f& _x) {
134   _EIGEN_DECLARE_CONST_Packet16f(1, 1.0f);
135   _EIGEN_DECLARE_CONST_Packet16f(half, 0.5f);
136   _EIGEN_DECLARE_CONST_Packet16f(127, 127.0f);
137 
138   _EIGEN_DECLARE_CONST_Packet16f(exp_hi, 88.3762626647950f);
139   _EIGEN_DECLARE_CONST_Packet16f(exp_lo, -88.3762626647949f);
140 
141   _EIGEN_DECLARE_CONST_Packet16f(cephes_LOG2EF, 1.44269504088896341f);
142 
143   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p0, 1.9875691500E-4f);
144   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p1, 1.3981999507E-3f);
145   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p2, 8.3334519073E-3f);
146   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p3, 4.1665795894E-2f);
147   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p4, 1.6666665459E-1f);
148   _EIGEN_DECLARE_CONST_Packet16f(cephes_exp_p5, 5.0000001201E-1f);
149 
150   // Clamp x.
151   Packet16f x = pmax(pmin(_x, p16f_exp_hi), p16f_exp_lo);
152 
153   // Express exp(x) as exp(m*ln(2) + r), start by extracting
154   // m = floor(x/ln(2) + 0.5).
155   Packet16f m = _mm512_floor_ps(pmadd(x, p16f_cephes_LOG2EF, p16f_half));
156 
157   // Get r = x - m*ln(2). Note that we can do this without losing more than one
158   // ulp precision due to the FMA instruction.
159   _EIGEN_DECLARE_CONST_Packet16f(nln2, -0.6931471805599453f);
160   Packet16f r = _mm512_fmadd_ps(m, p16f_nln2, x);
161   Packet16f r2 = pmul(r, r);
162 
163   // TODO(gonnet): Split into odd/even polynomials and try to exploit
164   //               instruction-level parallelism.
165   Packet16f y = p16f_cephes_exp_p0;
166   y = pmadd(y, r, p16f_cephes_exp_p1);
167   y = pmadd(y, r, p16f_cephes_exp_p2);
168   y = pmadd(y, r, p16f_cephes_exp_p3);
169   y = pmadd(y, r, p16f_cephes_exp_p4);
170   y = pmadd(y, r, p16f_cephes_exp_p5);
171   y = pmadd(y, r2, r);
172   y = padd(y, p16f_1);
173 
174   // Build emm0 = 2^m.
175   Packet16i emm0 = _mm512_cvttps_epi32(padd(m, p16f_127));
176   emm0 = _mm512_slli_epi32(emm0, 23);
177 
178   // Return 2^m * exp(r).
179   return pmax(pmul(y, _mm512_castsi512_ps(emm0)), _x);
180 }
181 
182 /*template <>
183 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8d
184 pexp<Packet8d>(const Packet8d& _x) {
185   Packet8d x = _x;
186 
187   _EIGEN_DECLARE_CONST_Packet8d(1, 1.0);
188   _EIGEN_DECLARE_CONST_Packet8d(2, 2.0);
189 
190   _EIGEN_DECLARE_CONST_Packet8d(exp_hi, 709.437);
191   _EIGEN_DECLARE_CONST_Packet8d(exp_lo, -709.436139303);
192 
193   _EIGEN_DECLARE_CONST_Packet8d(cephes_LOG2EF, 1.4426950408889634073599);
194 
195   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p0, 1.26177193074810590878e-4);
196   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p1, 3.02994407707441961300e-2);
197   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_p2, 9.99999999999999999910e-1);
198 
199   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q0, 3.00198505138664455042e-6);
200   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q1, 2.52448340349684104192e-3);
201   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q2, 2.27265548208155028766e-1);
202   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_q3, 2.00000000000000000009e0);
203 
204   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_C1, 0.693145751953125);
205   _EIGEN_DECLARE_CONST_Packet8d(cephes_exp_C2, 1.42860682030941723212e-6);
206 
207   // clamp x
208   x = pmax(pmin(x, p8d_exp_hi), p8d_exp_lo);
209 
210   // Express exp(x) as exp(g + n*log(2)).
211   const Packet8d n =
212       _mm512_mul_round_pd(p8d_cephes_LOG2EF, x, _MM_FROUND_TO_NEAREST_INT);
213 
214   // Get the remainder modulo log(2), i.e. the "g" described above. Subtract
215   // n*log(2) out in two steps, i.e. n*C1 + n*C2, C1+C2=log2 to get the last
216   // digits right.
217   const Packet8d nC1 = pmul(n, p8d_cephes_exp_C1);
218   const Packet8d nC2 = pmul(n, p8d_cephes_exp_C2);
219   x = psub(x, nC1);
220   x = psub(x, nC2);
221 
222   const Packet8d x2 = pmul(x, x);
223 
224   // Evaluate the numerator polynomial of the rational interpolant.
225   Packet8d px = p8d_cephes_exp_p0;
226   px = pmadd(px, x2, p8d_cephes_exp_p1);
227   px = pmadd(px, x2, p8d_cephes_exp_p2);
228   px = pmul(px, x);
229 
230   // Evaluate the denominator polynomial of the rational interpolant.
231   Packet8d qx = p8d_cephes_exp_q0;
232   qx = pmadd(qx, x2, p8d_cephes_exp_q1);
233   qx = pmadd(qx, x2, p8d_cephes_exp_q2);
234   qx = pmadd(qx, x2, p8d_cephes_exp_q3);
235 
236   // I don't really get this bit, copied from the SSE2 routines, so...
237   // TODO(gonnet): Figure out what is going on here, perhaps find a better
238   // rational interpolant?
239   x = _mm512_div_pd(px, psub(qx, px));
240   x = pmadd(p8d_2, x, p8d_1);
241 
242   // Build e=2^n.
243   const Packet8d e = _mm512_castsi512_pd(_mm512_slli_epi64(
244       _mm512_add_epi64(_mm512_cvtpd_epi64(n), _mm512_set1_epi64(1023)), 52));
245 
246   // Construct the result 2^n * exp(g) = e * x. The max is used to catch
247   // non-finite values in the input.
248   return pmax(pmul(x, e), _x);
249   }*/
250 
251 // Functions for sqrt.
252 // The EIGEN_FAST_MATH version uses the _mm_rsqrt_ps approximation and one step
253 // of Newton's method, at a cost of 1-2 bits of precision as opposed to the
254 // exact solution. The main advantage of this approach is not just speed, but
255 // also the fact that it can be inlined and pipelined with other computations,
256 // further reducing its effective latency.
257 #if EIGEN_FAST_MATH
258 template <>
259 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet16f
260 psqrt<Packet16f>(const Packet16f& _x) {
261   _EIGEN_DECLARE_CONST_Packet16f(one_point_five, 1.5f);
262   _EIGEN_DECLARE_CONST_Packet16f(minus_half, -0.5f);
263   _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(flt_min, 0x00800000);
264 
265   Packet16f neg_half = pmul(_x, p16f_minus_half);
266 
267   // select only the inverse sqrt of positive normal inputs (denormals are
268   // flushed to zero and cause infs as well).
269   __mmask16 non_zero_mask = _mm512_cmp_ps_mask(_x, p16f_flt_min, _CMP_GE_OQ);
270   Packet16f x = _mm512_mask_blend_ps(non_zero_mask, _mm512_setzero_ps(), _mm512_rsqrt14_ps(_x));
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 <>
281 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8d
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_setzero_pd(), _mm512_rsqrt14_pd(_x));
293 
294   // Do a first step of Newton's iteration.
295   x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
296 
297   // Do a second step of Newton's iteration.
298   x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
299 
300   // Multiply the original _x by it's reciprocal square root to extract the
301   // square root.
302   return pmul(_x, x);
303 }
304 #else
305 template <>
306 EIGEN_STRONG_INLINE Packet16f psqrt<Packet16f>(const Packet16f& x) {
307   return _mm512_sqrt_ps(x);
308 }
309 template <>
310 EIGEN_STRONG_INLINE Packet8d psqrt<Packet8d>(const Packet8d& x) {
311   return _mm512_sqrt_pd(x);
312 }
313 #endif
314 
315 // Functions for rsqrt.
316 // Almost identical to the sqrt routine, just leave out the last multiplication
317 // and fill in NaN/Inf where needed. Note that this function only exists as an
318 // iterative version for doubles since there is no instruction for diretly
319 // computing the reciprocal square root in AVX-512.
320 #ifdef EIGEN_FAST_MATH
321 template <>
322 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet16f
323 prsqrt<Packet16f>(const Packet16f& _x) {
324   _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(inf, 0x7f800000);
325   _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(nan, 0x7fc00000);
326   _EIGEN_DECLARE_CONST_Packet16f(one_point_five, 1.5f);
327   _EIGEN_DECLARE_CONST_Packet16f(minus_half, -0.5f);
328   _EIGEN_DECLARE_CONST_Packet16f_FROM_INT(flt_min, 0x00800000);
329 
330   Packet16f neg_half = pmul(_x, p16f_minus_half);
331 
332   // select only the inverse sqrt of positive normal inputs (denormals are
333   // flushed to zero and cause infs as well).
334   __mmask16 le_zero_mask = _mm512_cmp_ps_mask(_x, p16f_flt_min, _CMP_LT_OQ);
335   Packet16f x = _mm512_mask_blend_ps(le_zero_mask, _mm512_rsqrt14_ps(_x), _mm512_setzero_ps());
336 
337   // Fill in NaNs and Infs for the negative/zero entries.
338   __mmask16 neg_mask = _mm512_cmp_ps_mask(_x, _mm512_setzero_ps(), _CMP_LT_OQ);
339   Packet16f infs_and_nans = _mm512_mask_blend_ps(
340       neg_mask, _mm512_mask_blend_ps(le_zero_mask, _mm512_setzero_ps(), p16f_inf), p16f_nan);
341 
342   // Do a single step of Newton's iteration.
343   x = pmul(x, pmadd(neg_half, pmul(x, x), p16f_one_point_five));
344 
345   // Insert NaNs and Infs in all the right places.
346   return _mm512_mask_blend_ps(le_zero_mask, x, infs_and_nans);
347 }
348 
349 template <>
350 EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_UNUSED Packet8d
351 prsqrt<Packet8d>(const Packet8d& _x) {
352   _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(inf, 0x7ff0000000000000LL);
353   _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(nan, 0x7ff1000000000000LL);
354   _EIGEN_DECLARE_CONST_Packet8d(one_point_five, 1.5);
355   _EIGEN_DECLARE_CONST_Packet8d(minus_half, -0.5);
356   _EIGEN_DECLARE_CONST_Packet8d_FROM_INT64(dbl_min, 0x0010000000000000LL);
357 
358   Packet8d neg_half = pmul(_x, p8d_minus_half);
359 
360   // select only the inverse sqrt of positive normal inputs (denormals are
361   // flushed to zero and cause infs as well).
362   __mmask8 le_zero_mask = _mm512_cmp_pd_mask(_x, p8d_dbl_min, _CMP_LT_OQ);
363   Packet8d x = _mm512_mask_blend_pd(le_zero_mask, _mm512_rsqrt14_pd(_x), _mm512_setzero_pd());
364 
365   // Fill in NaNs and Infs for the negative/zero entries.
366   __mmask8 neg_mask = _mm512_cmp_pd_mask(_x, _mm512_setzero_pd(), _CMP_LT_OQ);
367   Packet8d infs_and_nans = _mm512_mask_blend_pd(
368       neg_mask, _mm512_mask_blend_pd(le_zero_mask, _mm512_setzero_pd(), p8d_inf), p8d_nan);
369 
370   // Do a first step of Newton's iteration.
371   x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
372 
373   // Do a second step of Newton's iteration.
374   x = pmul(x, pmadd(neg_half, pmul(x, x), p8d_one_point_five));
375 
376   // Insert NaNs and Infs in all the right places.
377   return _mm512_mask_blend_pd(le_zero_mask, x, infs_and_nans);
378 }
379 #elif defined(EIGEN_VECTORIZE_AVX512ER)
380 template <>
381 EIGEN_STRONG_INLINE Packet16f prsqrt<Packet16f>(const Packet16f& x) {
382   return _mm512_rsqrt28_ps(x);
383 }
384 #endif
385 #endif
386 
387 }  // end namespace internal
388 
389 }  // end namespace Eigen
390 
391 #endif  // THIRD_PARTY_EIGEN3_EIGEN_SRC_CORE_ARCH_AVX512_MATHFUNCTIONS_H_
392