xref: /freebsd/lib/msun/src/e_coshl.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1a48e1f22SSteve Kargl /* from: FreeBSD: head/lib/msun/src/e_coshl.c XXX */
2a48e1f22SSteve Kargl 
3a48e1f22SSteve Kargl /*
4a48e1f22SSteve Kargl  * ====================================================
5a48e1f22SSteve Kargl  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6a48e1f22SSteve Kargl  *
7a48e1f22SSteve Kargl  * Developed at SunPro, a Sun Microsystems, Inc. business.
8a48e1f22SSteve Kargl  * Permission to use, copy, modify, and distribute this
9a48e1f22SSteve Kargl  * software is freely granted, provided that this notice
10a48e1f22SSteve Kargl  * is preserved.
11a48e1f22SSteve Kargl  * ====================================================
12a48e1f22SSteve Kargl  */
13a48e1f22SSteve Kargl 
14a48e1f22SSteve Kargl /*
15a48e1f22SSteve Kargl  * See e_cosh.c for complete comments.
16a48e1f22SSteve Kargl  *
17a48e1f22SSteve Kargl  * Converted to long double by Bruce D. Evans.
18a48e1f22SSteve Kargl  */
19a48e1f22SSteve Kargl 
20a48e1f22SSteve Kargl #include <float.h>
21a48e1f22SSteve Kargl #ifdef __i386__
22a48e1f22SSteve Kargl #include <ieeefp.h>
23a48e1f22SSteve Kargl #endif
24a48e1f22SSteve Kargl 
25a48e1f22SSteve Kargl #include "fpmath.h"
26a48e1f22SSteve Kargl #include "math.h"
27a48e1f22SSteve Kargl #include "math_private.h"
28a48e1f22SSteve Kargl #include "k_expl.h"
29a48e1f22SSteve Kargl 
30a48e1f22SSteve Kargl #if LDBL_MAX_EXP != 0x4000
31a48e1f22SSteve Kargl /* We also require the usual expsign encoding. */
32a48e1f22SSteve Kargl #error "Unsupported long double format"
33a48e1f22SSteve Kargl #endif
34a48e1f22SSteve Kargl 
35a48e1f22SSteve Kargl #define	BIAS	(LDBL_MAX_EXP - 1)
36a48e1f22SSteve Kargl 
37a48e1f22SSteve Kargl static const volatile long double huge = 0x1p10000L, tiny = 0x1p-10000L;
38a48e1f22SSteve Kargl #if LDBL_MANT_DIG == 64
39a48e1f22SSteve Kargl /*
40a48e1f22SSteve Kargl  * Domain [-1, 1], range ~[-1.8211e-21, 1.8211e-21]:
41a48e1f22SSteve Kargl  * |cosh(x) - c(x)| < 2**-68.8
42a48e1f22SSteve Kargl  */
43a48e1f22SSteve Kargl static const union IEEEl2bits
44a48e1f22SSteve Kargl C4u = LD80C(0xaaaaaaaaaaaaac78, -5,  4.16666666666666682297e-2L);
45a48e1f22SSteve Kargl #define	C4	C4u.e
46a48e1f22SSteve Kargl static const double
47a48e1f22SSteve Kargl C2  =  0.5,
48a48e1f22SSteve Kargl C6  =  1.3888888888888616e-3,		/*  0x16c16c16c16b99.0p-62 */
49a48e1f22SSteve Kargl C8  =  2.4801587301767953e-5,		/*  0x1a01a01a027061.0p-68 */
50a48e1f22SSteve Kargl C10 =  2.7557319163300398e-7,		/*  0x127e4fb6c9b55f.0p-74 */
51a48e1f22SSteve Kargl C12 =  2.0876768371393075e-9,		/*  0x11eed99406a3f4.0p-81 */
52a48e1f22SSteve Kargl C14 =  1.1469537039374480e-11,		/*  0x1938c67cd18c48.0p-89 */
53a48e1f22SSteve Kargl C16 =  4.8473490896852041e-14;		/*  0x1b49c429701e45.0p-97 */
54a48e1f22SSteve Kargl #elif LDBL_MANT_DIG == 113
55a48e1f22SSteve Kargl /*
56a48e1f22SSteve Kargl  * Domain [-1, 1], range ~[-2.3194e-37, 2.3194e-37]:
57a48e1f22SSteve Kargl  * |cosh(x) - c(x)| < 2**-121.69
58a48e1f22SSteve Kargl  */
59a48e1f22SSteve Kargl static const long double
60a48e1f22SSteve Kargl C4  =  4.16666666666666666666666666666666225e-2L,	/*  0x1555555555555555555555555554e.0p-117L */
61a48e1f22SSteve Kargl C6  =  1.38888888888888888888888888889434831e-3L,	/*  0x16c16c16c16c16c16c16c16c1dd7a.0p-122L */
62a48e1f22SSteve Kargl C8  =  2.48015873015873015873015871870962089e-5L,	/*  0x1a01a01a01a01a01a01a017af2756.0p-128L */
63a48e1f22SSteve Kargl C10 =  2.75573192239858906525574318600800201e-7L,	/*  0x127e4fb7789f5c72ef01c8a040640.0p-134L */
64a48e1f22SSteve Kargl C12 =  2.08767569878680989791444691755468269e-9L,	/*  0x11eed8eff8d897b543d0679607399.0p-141L */
65a48e1f22SSteve Kargl C14=  1.14707455977297247387801189650495351e-11L,	/*  0x193974a8c07c9d24ae169a7fa9b54.0p-149L */
66a48e1f22SSteve Kargl C16 =  4.77947733238737883626416876486279985e-14L;	/*  0x1ae7f3e733b814d4e1b90f5727fe4.0p-157L */
67a48e1f22SSteve Kargl static const double
68a48e1f22SSteve Kargl C2  =  0.5,
69a48e1f22SSteve Kargl C18 =  1.5619206968597871e-16,		/*  0x16827863b9900b.0p-105 */
70a48e1f22SSteve Kargl C20 =  4.1103176218528049e-19,		/*  0x1e542ba3d3c269.0p-114 */
71a48e1f22SSteve Kargl C22 =  8.8967926401641701e-22,		/*  0x10ce399542a014.0p-122 */
72a48e1f22SSteve Kargl C24 =  1.6116681626523904e-24,		/*  0x1f2c981d1f0cb7.0p-132 */
73a48e1f22SSteve Kargl C26 =  2.5022374732804632e-27;		/*  0x18c7ecf8b2c4a0.0p-141 */
74a48e1f22SSteve Kargl #else
75a48e1f22SSteve Kargl #error "Unsupported long double format"
76a48e1f22SSteve Kargl #endif /* LDBL_MANT_DIG == 64 */
77a48e1f22SSteve Kargl 
781531aa5fSSteve Kargl /* log(2**16385 - 0.5) rounded up: */
79a48e1f22SSteve Kargl static const float
80a48e1f22SSteve Kargl o_threshold =  1.13572168e4;		/*  0xb174de.0p-10 */
81a48e1f22SSteve Kargl 
82a48e1f22SSteve Kargl long double
coshl(long double x)83a48e1f22SSteve Kargl coshl(long double x)
84a48e1f22SSteve Kargl {
85a48e1f22SSteve Kargl 	long double hi,lo,x2,x4;
86*cf825f93SEd Maste #if LDBL_MANT_DIG == 113
87a48e1f22SSteve Kargl 	double dx2;
88*cf825f93SEd Maste #endif
89a48e1f22SSteve Kargl 	uint16_t ix;
90a48e1f22SSteve Kargl 
91a48e1f22SSteve Kargl 	GET_LDBL_EXPSIGN(ix,x);
92a48e1f22SSteve Kargl 	ix &= 0x7fff;
93a48e1f22SSteve Kargl 
94a48e1f22SSteve Kargl     /* x is INF or NaN */
95a48e1f22SSteve Kargl 	if(ix>=0x7fff) return x*x;
96a48e1f22SSteve Kargl 
97a48e1f22SSteve Kargl 	ENTERI();
98a48e1f22SSteve Kargl 
99a48e1f22SSteve Kargl     /* |x| < 1, return 1 or c(x) */
100a48e1f22SSteve Kargl 	if(ix<0x3fff) {
101a48e1f22SSteve Kargl 	    if (ix<BIAS-(LDBL_MANT_DIG+1)/2) 	/* |x| < TINY */
102a48e1f22SSteve Kargl 		RETURNI(1+tiny);	/* cosh(tiny) = 1(+) with inexact */
103a48e1f22SSteve Kargl 	    x2 = x*x;
104a48e1f22SSteve Kargl #if LDBL_MANT_DIG == 64
105a48e1f22SSteve Kargl 	    x4 = x2*x2;
106a48e1f22SSteve Kargl 	    RETURNI(((C16*x2 + C14)*x4 + (C12*x2 + C10))*(x4*x4*x2) +
107a48e1f22SSteve Kargl 		((C8*x2 + C6)*x2 + C4)*x4 + C2*x2 + 1);
108a48e1f22SSteve Kargl #elif LDBL_MANT_DIG == 113
109a48e1f22SSteve Kargl 	    dx2 = x2;
110a48e1f22SSteve Kargl 	    RETURNI((((((((((((C26*dx2 + C24)*dx2 + C22)*dx2 +
111a48e1f22SSteve Kargl 		C20)*x2 + C18)*x2 +
112a48e1f22SSteve Kargl 		C16)*x2 + C14)*x2 + C12)*x2 + C10)*x2 + C8)*x2 + C6)*x2 +
113a48e1f22SSteve Kargl 		C4)*(x2*x2) + C2*x2 + 1);
114a48e1f22SSteve Kargl #endif
115a48e1f22SSteve Kargl 	}
116a48e1f22SSteve Kargl 
117a48e1f22SSteve Kargl     /* |x| in [1, 64), return accurate exp(|x|)/2+1/exp(|x|)/2 */
118a48e1f22SSteve Kargl 	if (ix < 0x4005) {
119a48e1f22SSteve Kargl 	    k_hexpl(fabsl(x), &hi, &lo);
120a48e1f22SSteve Kargl 	    RETURNI(lo + 0.25/(hi + lo) + hi);
121a48e1f22SSteve Kargl 	}
122a48e1f22SSteve Kargl 
123a48e1f22SSteve Kargl     /* |x| in [64, o_threshold], return correctly-overflowing exp(|x|)/2 */
124a48e1f22SSteve Kargl 	if (fabsl(x) <= o_threshold)
125a48e1f22SSteve Kargl 	    RETURNI(hexpl(fabsl(x)));
126a48e1f22SSteve Kargl 
127a48e1f22SSteve Kargl     /* |x| > o_threshold, cosh(x) overflow */
128a48e1f22SSteve Kargl 	RETURNI(huge*huge);
129a48e1f22SSteve Kargl }
130