xref: /netbsd-src/lib/libm/src/e_sinhl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /* from: FreeBSD: head/lib/msun/src/e_sinhl.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  * See e_sinh.c for complete comments.
17  *
18  * Converted to long double by Bruce D. Evans.
19  */
20 
21 #include "namespace.h"
22 
23 __weak_alias(sinhl, _sinhl)
24 
25 #include <float.h>
26 
27 #ifdef __HAVE_LONG_DOUBLE
28 
29 #ifdef __i386__
30 #include <ieeefp.h>
31 #endif
32 
33 #include "math.h"
34 #include "math_private.h"
35 #if LDBL_MANT_DIG == 64
36 #include "../ld80/k_expl.h"
37 #elif LDBL_MANT_DIG == 113
38 #include "../ld128/k_expl.h"
39 #else
40 #error "Unsupported long double format"
41 #endif
42 
43 #if LDBL_MAX_EXP != 0x4000
44 /* We also require the usual expsign encoding. */
45 #error "Unsupported long double format"
46 #endif
47 
48 #define	BIAS	(LDBL_MAX_EXP - 1)
49 
50 static const long double shuge = 0x1p16383L;
51 #if LDBL_MANT_DIG == 64
52 /*
53  * Domain [-1, 1], range ~[-6.6749e-22, 6.6749e-22]:
54  * |sinh(x)/x - s(x)| < 2**-70.3
55  */
56 static const union ieee_ext_u
57 S3u = LD80C(0xaaaaaaaaaaaaaaaa, -3,  1.66666666666666666658e-1L);
58 #define	S3	S3u.extu_ld
59 static const double
60 S5  =  8.3333333333333332e-3,		/*  0x11111111111111.0p-59 */
61 S7  =  1.9841269841270074e-4,		/*  0x1a01a01a01a070.0p-65 */
62 S9  =  2.7557319223873889e-6,		/*  0x171de3a5565fe6.0p-71 */
63 S11 =  2.5052108406704084e-8,		/*  0x1ae6456857530f.0p-78 */
64 S13 =  1.6059042748655297e-10,		/*  0x161245fa910697.0p-85 */
65 S15 =  7.6470006914396920e-13,		/*  0x1ae7ce4eff2792.0p-93 */
66 S17 =  2.8346142308424267e-15;		/*  0x19882ce789ffc6.0p-101 */
67 #elif LDBL_MANT_DIG == 113
68 /*
69  * Domain [-1, 1], range ~[-2.9673e-36, 2.9673e-36]:
70  * |sinh(x)/x - s(x)| < 2**-118.0
71  */
72 static const long double
73 S3  =  1.66666666666666666666666666666666033e-1L,	/*  0x1555555555555555555555555553b.0p-115L */
74 S5  =  8.33333333333333333333333333337643193e-3L,	/*  0x111111111111111111111111180f5.0p-119L */
75 S7  =  1.98412698412698412698412697391263199e-4L,	/*  0x1a01a01a01a01a01a01a0176aad11.0p-125L */
76 S9  =  2.75573192239858906525574406205464218e-6L,	/*  0x171de3a556c7338faac243aaa9592.0p-131L */
77 S11 =  2.50521083854417187749675637460977997e-8L,	/*  0x1ae64567f544e38fe59b3380d7413.0p-138L */
78 S13 =  1.60590438368216146368737762431552702e-10L,	/*  0x16124613a86d098059c7620850fc2.0p-145L */
79 S15 =  7.64716373181980539786802470969096440e-13L,	/*  0x1ae7f3e733b814193af09ce723043.0p-153L */
80 S17 =  2.81145725434775409870584280722701574e-15L;	/*  0x1952c77030c36898c3fd0b6dfc562.0p-161L */
81 static const double
82 S19=  8.2206352435411005e-18,		/*  0x12f49b4662b86d.0p-109 */
83 S21=  1.9572943931418891e-20,		/*  0x171b8f2fab9628.0p-118 */
84 S23 =  3.8679983530666939e-23,		/*  0x17617002b73afc.0p-127 */
85 S25 =  6.5067867911512749e-26;		/*  0x1423352626048a.0p-136 */
86 #else
87 #error "Unsupported long double format"
88 #endif /* LDBL_MANT_DIG == 64 */
89 
90 /* log(2**16385 - 0.5) rounded up: */
91 static const float
92 o_threshold =  1.13572168e4;		/*  0xb174de.0p-10 */
93 
94 long double
95 sinhl(long double x)
96 {
97 	long double hi,lo,x2;
98 #if LDBL_MANT_DIG == 113
99 	double dx2;
100 #endif
101 	double s;
102 	int16_t ix,jx;
103 
104 	GET_LDBL_EXPSIGN(jx,x);
105 	ix = jx&0x7fff;
106 
107     /* x is INF or NaN */
108 	if(ix>=0x7fff) return x+x;
109 
110 	ENTERI();
111 
112 	s = 1;
113 	if (jx<0) s = -1;
114 
115     /* |x| < 64, return x, s(x), or accurate s*(exp(|x|)/2-1/exp(|x|)/2) */
116 	if (ix<0x4005) {		/* |x|<64 */
117 	    if (ix<BIAS-(LDBL_MANT_DIG+1)/2) 	/* |x|<TINY */
118 		if(shuge+x>1) RETURNI(x);  /* sinh(tiny) = tiny with inexact */
119 	    if (ix<0x3fff) {		/* |x|<1 */
120 		x2 = x*x;
121 #if LDBL_MANT_DIG == 64
122 		long double x4 = x2*x2;
123 		RETURNI(((S17*x2 + S15)*x4 + (S13*x2 + S11))*(x2*x*x4*x4) +
124 		    ((S9*x2 + S7)*x2 + S5)*(x2*x*x2) + S3*(x2*x) + x);
125 #elif LDBL_MANT_DIG == 113
126 		dx2 = x2;
127 		RETURNI(((((((((((S25*dx2 + S23)*dx2 +
128 		    S21)*x2 + S19)*x2 +
129 		    S17)*x2 + S15)*x2 + S13)*x2 + S11)*x2 + S9)*x2 + S7)*x2 +
130 		    S5)* (x2*x*x2) +
131 		    S3*(x2*x) + x);
132 #endif
133 	    }
134 	    k_hexpl(fabsl(x), &hi, &lo);
135 	    RETURNI(s*(lo - 0.25/(hi + lo) + hi));
136 	}
137 
138     /* |x| in [64, o_threshold], return correctly-overflowing s*exp(|x|)/2 */
139 	if (fabsl(x) <= o_threshold)
140 	    RETURNI(s*hexpl(fabsl(x)));
141 
142     /* |x| > o_threshold, sinh(x) overflow */
143 	return x*shuge;
144 }
145 #else
146 
147 #include "math.h"
148 
149 long double
150 sinhl(long double x)
151 {
152 	return sinh(x);
153 }
154 
155 #endif
156