Path Tracer
MathFunctionsImpl.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com)
5 // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_MATHFUNCTIONSIMPL_H
12 #define EIGEN_MATHFUNCTIONSIMPL_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
28 template<typename T>
29 T generic_fast_tanh_float(const T& a_x)
30 {
31  // Clamp the inputs to the range [-c, c]
32 #ifdef EIGEN_VECTORIZE_FMA
33  const T plus_clamp = pset1<T>(7.99881172180175781f);
34  const T minus_clamp = pset1<T>(-7.99881172180175781f);
35 #else
36  const T plus_clamp = pset1<T>(7.90531110763549805f);
37  const T minus_clamp = pset1<T>(-7.90531110763549805f);
38 #endif
39  const T tiny = pset1<T>(0.0004f);
40  const T x = pmax(pmin(a_x, plus_clamp), minus_clamp);
41  const T tiny_mask = pcmp_lt(pabs(a_x), tiny);
42  // The monomial coefficients of the numerator polynomial (odd).
43  const T alpha_1 = pset1<T>(4.89352455891786e-03f);
44  const T alpha_3 = pset1<T>(6.37261928875436e-04f);
45  const T alpha_5 = pset1<T>(1.48572235717979e-05f);
46  const T alpha_7 = pset1<T>(5.12229709037114e-08f);
47  const T alpha_9 = pset1<T>(-8.60467152213735e-11f);
48  const T alpha_11 = pset1<T>(2.00018790482477e-13f);
49  const T alpha_13 = pset1<T>(-2.76076847742355e-16f);
50 
51  // The monomial coefficients of the denominator polynomial (even).
52  const T beta_0 = pset1<T>(4.89352518554385e-03f);
53  const T beta_2 = pset1<T>(2.26843463243900e-03f);
54  const T beta_4 = pset1<T>(1.18534705686654e-04f);
55  const T beta_6 = pset1<T>(1.19825839466702e-06f);
56 
57  // Since the polynomials are odd/even, we need x^2.
58  const T x2 = pmul(x, x);
59 
60  // Evaluate the numerator polynomial p.
61  T p = pmadd(x2, alpha_13, alpha_11);
62  p = pmadd(x2, p, alpha_9);
63  p = pmadd(x2, p, alpha_7);
64  p = pmadd(x2, p, alpha_5);
65  p = pmadd(x2, p, alpha_3);
66  p = pmadd(x2, p, alpha_1);
67  p = pmul(x, p);
68 
69  // Evaluate the denominator polynomial q.
70  T q = pmadd(x2, beta_6, beta_4);
71  q = pmadd(x2, q, beta_2);
72  q = pmadd(x2, q, beta_0);
73 
74  // Divide the numerator by the denominator.
75  return pselect(tiny_mask, x, pdiv(p, q));
76 }
77 
78 template<typename RealScalar>
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
80 RealScalar positive_real_hypot(const RealScalar& x, const RealScalar& y)
81 {
82  EIGEN_USING_STD(sqrt);
83  RealScalar p, qp;
84  p = numext::maxi(x,y);
85  if(p==RealScalar(0)) return RealScalar(0);
86  qp = numext::mini(y,x) / p;
87  return p * sqrt(RealScalar(1) + qp*qp);
88 }
89 
90 template<typename Scalar>
91 struct hypot_impl
92 {
93  typedef typename NumTraits<Scalar>::Real RealScalar;
94  static EIGEN_DEVICE_FUNC
95  inline RealScalar run(const Scalar& x, const Scalar& y)
96  {
97  EIGEN_USING_STD(abs);
98  return positive_real_hypot<RealScalar>(abs(x), abs(y));
99  }
100 };
101 
102 } // end namespace internal
103 
104 } // end namespace Eigen
105 
106 #endif // EIGEN_MATHFUNCTIONSIMPL_H
Eigen
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Eigen::internal::hypot_impl
Definition: MathFunctionsImpl.h:92
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:213