xref: /netbsd-src/lib/libm/src/s_tanhl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /* from: FreeBSD: head/lib/msun/src/s_tanhl.c XXX */
2 
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 #include <sys/cdefs.h>
15 
16 #include "namespace.h"
17 #include <float.h>
18 #include <machine/ieee.h>
19 
20 #include "math.h"
21 #include "math_private.h"
22 
23 __weak_alias(tanhl, _tanhl)
24 
25 #ifdef __HAVE_LONG_DOUBLE
26 /*
27  * See s_tanh.c for complete comments.
28  *
29  * Converted to long double by Bruce D. Evans.
30  */
31 
32 #ifdef __i386__
33 #include <ieeefp.h>
34 #endif
35 
36 #if LDBL_MANT_DIG == 64
37 #include "../ld80/k_expl.h"
38 #elif LDBL_MANT_DIG == 113
39 #include "../ld128/k_expl.h"
40 #else
41 #error "Unsupported long double format"
42 #endif
43 
44 #if LDBL_MAX_EXP != 0x4000
45 /* We also require the usual expsign encoding. */
46 #error "Unsupported long double format"
47 #endif
48 
49 #define	BIAS	(LDBL_MAX_EXP - 1)
50 
51 static const volatile double tiny = 1.0e-300;
52 static const double one = 1.0;
53 #if LDBL_MANT_DIG == 64
54 /*
55  * Domain [-0.25, 0.25], range ~[-1.6304e-22, 1.6304e-22]:
56  * |tanh(x)/x - t(x)| < 2**-72.3
57  */
58 static const union ieee_ext_u
59 T3u = LD80C(0xaaaaaaaaaaaaaa9f, -2, -3.33333333333333333017e-1L);
60 #define	T3	T3u.extu_ld
61 static const double
62 T5  =  1.3333333333333314e-1,		/*  0x1111111111110a.0p-55 */
63 T7  = -5.3968253968210485e-2,		/* -0x1ba1ba1ba1a1a1.0p-57 */
64 T9  =  2.1869488531393817e-2,		/*  0x1664f488172022.0p-58 */
65 T11 = -8.8632352345964591e-3,		/* -0x1226e34bc138d5.0p-59 */
66 T13 =  3.5921169709993771e-3,		/*  0x1d6d371d3e400f.0p-61 */
67 T15 = -1.4555786415756001e-3,		/* -0x17d923aa63814d.0p-62 */
68 T17 =  5.8645267876296793e-4,		/*  0x13378589b85aa7.0p-63 */
69 T19 = -2.1121033571392224e-4;		/* -0x1baf0af80c4090.0p-65 */
70 #elif LDBL_MANT_DIG == 113
71 /*
72  * Domain [-0.25, 0.25], range ~[-2.4211e-37, 2.4211e-37]:
73  * |tanh(x)/x - t(x)| < 2**121.6
74  */
75 static const long double
76 T3 = -3.33333333333333333333333333333332980e-1L,	/* -0x1555555555555555555555555554e.0p-114L */
77 T5  =  1.33333333333333333333333333332707260e-1L,	/*  0x1111111111111111111111110ab7b.0p-115L */
78 T7  = -5.39682539682539682539682535723482314e-2L,	/* -0x1ba1ba1ba1ba1ba1ba1ba17b5fc98.0p-117L */
79 T9  =  2.18694885361552028218693591149061717e-2L,	/*  0x1664f4882c10f9f32d6b1a12a25e5.0p-118L */
80 T11 = -8.86323552990219656883762347736381851e-3L,	/* -0x1226e355e6c23c8f5a5a0f386cb4d.0p-119L */
81 T13 =  3.59212803657248101358314398220822722e-3L,	/*  0x1d6d3d0e157ddfb403ad3637442c6.0p-121L */
82 T15 = -1.45583438705131796512568010348874662e-3L;	/* -0x17da36452b75e150c44cc34253b34.0p-122L */
83 static const double
84 T17 =  5.9002744094556621e-4,		/*  0x1355824803668e.0p-63 */
85 T19 = -2.3912911424260516e-4,		/* -0x1f57d7734c8dde.0p-65 */
86 T21 =  9.6915379535512898e-5,		/*  0x1967e18ad6a6ca.0p-66 */
87 T23 = -3.9278322983156353e-5,		/* -0x1497d8e6b75729.0p-67 */
88 T25 =  1.5918887220143869e-5,		/*  0x10b1319998cafa.0p-68 */
89 T27 = -6.4514295231630956e-6,		/* -0x1b0f2b71b218eb.0p-70 */
90 T29 =  2.6120754043964365e-6,		/*  0x15e963a3cf3a39.0p-71 */
91 T31 = -1.0407567231003314e-6,		/* -0x1176041e656869.0p-72 */
92 T33 =  3.4744117554063574e-7;		/*  0x1750fe732cab9c.0p-74 */
93 #endif /* LDBL_MANT_DIG == 64 */
94 
95 static inline long double
96 divl(long double a, long double b, long double c, long double d,
97     long double e, long double f)
98 {
99 	long double inv, r;
100 	float fr, fw;
101 
102 	_2sumF(a, c);
103 	b = b + c;
104 	_2sumF(d, f);
105 	e = e + f;
106 
107 	inv = 1 / (d + e);
108 
109 	r = (a + b) * inv;
110 	fr = r;
111 	r = fr;
112 
113 	fw = d + e;
114 	e = d - fw + e;
115 	d = fw;
116 
117 	r = r + (a - d * r + b - e * r) * inv;
118 
119 	return r;
120 }
121 
122 long double
123 tanhl(long double x)
124 {
125 	long double hi,lo,s,x2,z;
126 #if LDBL_MANT_DIG == 113
127 	double dx2;
128 #endif
129 	int16_t jx,ix;
130 
131 	GET_LDBL_EXPSIGN(jx,x);
132 	ix = jx&0x7fff;
133 
134     /* x is INF or NaN */
135 	if(ix>=0x7fff) {
136 	    if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
137 	    else       return one/x-one;    /* tanh(NaN) = NaN */
138 	}
139 
140 	ENTERI();
141 
142     /* |x| < 40 */
143 	if (ix < 0x4004 || fabsl(x) < 40) {	/* |x|<40 */
144 	    if (__predict_false(ix<BIAS-(LDBL_MANT_DIG+1)/2)) {	/* |x|<TINY */
145 		/* tanh(+-0) = +0; tanh(tiny) = tiny(-+) with inexact: */
146 		return (x == 0 ? x : (0x1p200 * x - x) * 0x1p-200);
147 	    }
148 	    if (ix<0x3ffd) {		/* |x|<0.25 */
149 		x2 = x*x;
150 #if LDBL_MANT_DIG == 64
151 		long double x4 = x2*x2;
152 		RETURNI(((T19*x2 + T17)*x4 + (T15*x2 + T13))*(x2*x*x2*x4*x4) +
153 		    ((T11*x2 + T9)*x4 + (T7*x2 + T5))*(x2*x*x2) +
154 		    T3*(x2*x) + x);
155 #elif LDBL_MANT_DIG == 113
156 		dx2 = x2;
157 #if 0
158 		RETURNI(((((((((((((((T33*dx2 + T31)*dx2 + T29)*dx2 + T27)*dx2 +
159 		    T25)*x2 + T23)*x2 + T21)*x2 + T19)*x2 + T17)*x2 +
160 		    T15)*x2 + T13)*x2 + T11)*x2 + T9)*x2 + T7)*x2 + T5)*
161 		    (x2*x*x2) +
162 		    T3*(x2*x) + x);
163 #else
164 		long double q = ((((((((((((((T33*dx2 + T31)*dx2 + T29)*dx2 + T27)*dx2 +
165 		    T25)*x2 + T23)*x2 + T21)*x2 + T19)*x2 + T17)*x2 +
166 		    T15)*x2 + T13)*x2 + T11)*x2 + T9)*x2 + T7)*x2 + T5)*
167 		    (x2*x*x2);
168 		RETURNI(q + T3*(x2*x) + x);
169 #endif
170 #endif
171 	    }
172 	    k_hexpl(2*fabsl(x), &hi, &lo);
173 	    if (ix<0x4001 && fabsl(x) < 1.5)	/* |x|<1.5 */
174 		z = divl(hi, lo, -0.5, hi, lo, 0.5);
175 	    else
176 		z = one - one/(lo+0.5+hi);
177     /* |x| >= 40, return +-1 */
178 	} else {
179 	    z = one - tiny;		/* raise inexact flag */
180 	}
181 	s = 1;
182 	if (jx<0) s = -1;
183 	RETURNI(s*z);
184 }
185 #else
186 
187 long double
188 tanhl(long double x)
189 {
190 	return tanh(x);
191 }
192 #endif
193