![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com) 00005 // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr> 00006 // 00007 // This Source Code Form is subject to the terms of the Mozilla 00008 // Public License v. 2.0. If a copy of the MPL was not distributed 00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00010 00011 #ifndef EIGEN_MATHFUNCTIONSIMPL_H 00012 #define EIGEN_MATHFUNCTIONSIMPL_H 00013 00014 namespace Eigen { 00015 00016 namespace internal { 00017 00025 template<typename T> 00026 T generic_fast_tanh_float(const T& a_x) 00027 { 00028 // Clamp the inputs to the range [-9, 9] since anything outside 00029 // this range is +/-1.0f in single-precision. 00030 const T plus_9 = pset1<T>(9.f); 00031 const T minus_9 = pset1<T>(-9.f); 00032 // NOTE GCC prior to 6.3 might improperly optimize this max/min 00033 // step such that if a_x is nan, x will be either 9 or -9, 00034 // and tanh will return 1 or -1 instead of nan. 00035 // This is supposed to be fixed in gcc6.3, 00036 // see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867 00037 const T x = pmax(minus_9,pmin(plus_9,a_x)); 00038 // The monomial coefficients of the numerator polynomial (odd). 00039 const T alpha_1 = pset1<T>(4.89352455891786e-03f); 00040 const T alpha_3 = pset1<T>(6.37261928875436e-04f); 00041 const T alpha_5 = pset1<T>(1.48572235717979e-05f); 00042 const T alpha_7 = pset1<T>(5.12229709037114e-08f); 00043 const T alpha_9 = pset1<T>(-8.60467152213735e-11f); 00044 const T alpha_11 = pset1<T>(2.00018790482477e-13f); 00045 const T alpha_13 = pset1<T>(-2.76076847742355e-16f); 00046 00047 // The monomial coefficients of the denominator polynomial (even). 00048 const T beta_0 = pset1<T>(4.89352518554385e-03f); 00049 const T beta_2 = pset1<T>(2.26843463243900e-03f); 00050 const T beta_4 = pset1<T>(1.18534705686654e-04f); 00051 const T beta_6 = pset1<T>(1.19825839466702e-06f); 00052 00053 // Since the polynomials are odd/even, we need x^2. 00054 const T x2 = pmul(x, x); 00055 00056 // Evaluate the numerator polynomial p. 00057 T p = pmadd(x2, alpha_13, alpha_11); 00058 p = pmadd(x2, p, alpha_9); 00059 p = pmadd(x2, p, alpha_7); 00060 p = pmadd(x2, p, alpha_5); 00061 p = pmadd(x2, p, alpha_3); 00062 p = pmadd(x2, p, alpha_1); 00063 p = pmul(x, p); 00064 00065 // Evaluate the denominator polynomial p. 00066 T q = pmadd(x2, beta_6, beta_4); 00067 q = pmadd(x2, q, beta_2); 00068 q = pmadd(x2, q, beta_0); 00069 00070 // Divide the numerator by the denominator. 00071 return pdiv(p, q); 00072 } 00073 00074 } // end namespace internal 00075 00076 } // end namespace Eigen 00077 00078 #endif // EIGEN_MATHFUNCTIONSIMPL_H