1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __tanhl = tanhl
  32 
  33 /*
  34  * tanhl(x) returns the hyperbolic tangent of x
  35  *
  36  * Method :
  37  *      1. reduce x to non-negative:  tanhl(-x) = - tanhl(x).
  38  *      2.
  39  *        0      <  x <=  small    :  tanhl(x) := x
  40  *                                                -expm1l(-2x)
  41  *        small  <  x <=  1        :  tanhl(x) := --------------
  42  *                                               expm1l(-2x) + 2
  43  *                                                        2
  44  *        1      <= x <= threshold :  tanhl(x) := 1 -  ---------------
  45  *                                                    expm1l(2x) + 2
  46  *     threshold <  x <= INF       :  tanhl(x) := 1.
  47  *
  48  * where
  49  *      single :        small = 1.e-5           threshold = 11.0
  50  *      double :        small = 1.e-10          threshold = 22.0
  51  *      quad   :        small = 1.e-20          threshold = 45.0
  52  *
  53  * Note: threshold was chosen so that
  54  *              fl(1.0+2/(expm1(2*threshold)+2)) == 1.
  55  *
  56  * Special cases:
  57  *      tanhl(NaN) is NaN;
  58  *      only tanhl(0.0)=0.0 is exact for finite argument.
  59  */
  60 
  61 #include "libm.h"
  62 #include "longdouble.h"
  63 
  64 static const long double small = 1.0e-20L,
  65         one = 1.0,
  66         two = 2.0,
  67 #ifndef lint
  68         big = 1.0e+20L,
  69 #endif
  70         threshold = 45.0L;
  71 
  72 long double
  73 tanhl(long double x)
  74 {
  75         long double t, y, z;
  76         int signx;
  77 
  78 #ifndef lint
  79         volatile long double dummy;
  80 #endif
  81 
  82         if (isnanl(x))
  83                 return (x + x);         /* x is NaN */
  84 
  85         signx = signbitl(x);
  86         t = fabsl(x);
  87         z = one;
  88 
  89         if (t <= threshold) {
  90                 if (t > one) {
  91                         z = one - two / (expm1l(t + t) + two);
  92                 } else if (t > small) {
  93                         y = expm1l(-t - t);
  94                         z = -y / (y + two);
  95                 } else {
  96 #ifndef lint
  97                         dummy = t + big;
  98                         /* inexact if t != 0 */
  99 #endif
 100                         return (x);
 101                 }
 102         } else if (!finitel(t)) {
 103                 return (copysignl(one, x));
 104         } else {
 105                 return (signx ? -z + small * small : z - small * small);
 106         }
 107 
 108         return (signx ? -z : z);
 109 }