e_sinhl.c revision 260067
1152219Simp/* from: FreeBSD: head/lib/msun/src/e_sinhl.c XXX */
2156243Srink
3152219Simp/*
4152219Simp * ====================================================
5152219Simp * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6152219Simp *
7152219Simp * Developed at SunPro, a Sun Microsystems, Inc. business.
8152219Simp * Permission to use, copy, modify, and distribute this
9152219Simp * software is freely granted, provided that this notice
10152219Simp * is preserved.
11152219Simp * ====================================================
12152219Simp */
13152219Simp
14152219Simp#include <sys/cdefs.h>
15152219Simp__FBSDID("$FreeBSD: head/lib/msun/src/e_sinhl.c 260067 2013-12-30 01:06:21Z kargl $");
16152219Simp
17152219Simp/*
18152219Simp * See e_sinh.c for complete comments.
19152219Simp *
20152219Simp * Converted to long double by Bruce D. Evans.
21152219Simp */
22152219Simp
23152219Simp#include <float.h>
24152219Simp#ifdef __i386__
25152219Simp#include <ieeefp.h>
26152219Simp#endif
27152219Simp
28170837Smarius#include "fpmath.h"
29170837Smarius#include "math.h"
30170837Smarius#include "math_private.h"
31152219Simp#include "k_expl.h"
32156243Srink
33156243Srink#if LDBL_MAX_EXP != 0x4000
34152219Simp/* We also require the usual expsign encoding. */
35156243Srink#error "Unsupported long double format"
36156243Srink#endif
37156243Srink
38152219Simp#define	BIAS	(LDBL_MAX_EXP - 1)
39152219Simp
40152219Simpstatic const long double shuge = 0x1p16383L;
41152219Simp#if LDBL_MANT_DIG == 64
42152219Simp/*
43156243Srink * Domain [-1, 1], range ~[-6.6749e-22, 6.6749e-22]:
44152219Simp * |sinh(x)/x - s(x)| < 2**-70.3
45156243Srink */
46152219Simpstatic const union IEEEl2bits
47152219SimpS3u = LD80C(0xaaaaaaaaaaaaaaaa, -3,  1.66666666666666666658e-1L);
48156243Srink#define	S3	S3u.e
49152219Simpstatic const double
50152219SimpS5  =  8.3333333333333332e-3,		/*  0x11111111111111.0p-59 */
51152219SimpS7  =  1.9841269841270074e-4,		/*  0x1a01a01a01a070.0p-65 */
52152219SimpS9  =  2.7557319223873889e-6,		/*  0x171de3a5565fe6.0p-71 */
53152219SimpS11 =  2.5052108406704084e-8,		/*  0x1ae6456857530f.0p-78 */
54152219SimpS13 =  1.6059042748655297e-10,		/*  0x161245fa910697.0p-85 */
55152219SimpS15 =  7.6470006914396920e-13,		/*  0x1ae7ce4eff2792.0p-93 */
56152219SimpS17 =  2.8346142308424267e-15;		/*  0x19882ce789ffc6.0p-101 */
57156243Srink#elif LDBL_MANT_DIG == 113
58152219Simp/*
59152219Simp * Domain [-1, 1], range ~[-2.9673e-36, 2.9673e-36]:
60156243Srink * |sinh(x)/x - s(x)| < 2**-118.0
61152219Simp */
62156243Srinkstatic const long double
63156243SrinkS3  =  1.66666666666666666666666666666666033e-1L,	/*  0x1555555555555555555555555553b.0p-115L */
64156243SrinkS5  =  8.33333333333333333333333333337643193e-3L,	/*  0x111111111111111111111111180f5.0p-119L */
65156243SrinkS7  =  1.98412698412698412698412697391263199e-4L,	/*  0x1a01a01a01a01a01a01a0176aad11.0p-125L */
66156243SrinkS9  =  2.75573192239858906525574406205464218e-6L,	/*  0x171de3a556c7338faac243aaa9592.0p-131L */
67156243SrinkS11 =  2.50521083854417187749675637460977997e-8L,	/*  0x1ae64567f544e38fe59b3380d7413.0p-138L */
68156243SrinkS13 =  1.60590438368216146368737762431552702e-10L,	/*  0x16124613a86d098059c7620850fc2.0p-145L */
69156243SrinkS15 =  7.64716373181980539786802470969096440e-13L,	/*  0x1ae7f3e733b814193af09ce723043.0p-153L */
70156243SrinkS17 =  2.81145725434775409870584280722701574e-15L;	/*  0x1952c77030c36898c3fd0b6dfc562.0p-161L */
71156243Srinkstatic const double
72156243SrinkS19=  8.2206352435411005e-18,		/*  0x12f49b4662b86d.0p-109 */
73156243SrinkS21=  1.9572943931418891e-20,		/*  0x171b8f2fab9628.0p-118 */
74156243SrinkS23 =  3.8679983530666939e-23,		/*  0x17617002b73afc.0p-127 */
75170837SmariusS25 =  6.5067867911512749e-26;		/*  0x1423352626048a.0p-136 */
76156243Srink#else
77156243Srink#error "Unsupported long double format"
78152219Simp#endif /* LDBL_MANT_DIG == 64 */
79152219Simp
80152219Simp/* log(2**16385 - 0.5) rounded towards up: */
81156243Srinkstatic const float
82152219Simpo_threshold =  1.13572168e4;		/*  0xb174de.0p-10 */
83170837Smarius
84152219Simplong double
85156243Srinksinhl(long double x)
86156243Srink{
87156243Srink	long double hi,lo,x2,x4;
88156243Srink	double dx2,s;
89156243Srink	int16_t ix,jx;
90156243Srink
91156243Srink	GET_LDBL_EXPSIGN(jx,x);
92156243Srink	ix = jx&0x7fff;
93156243Srink
94156243Srink    /* x is INF or NaN */
95156243Srink	if(ix>=0x7fff) return x+x;
96156243Srink
97156243Srink	ENTERI();
98156243Srink
99156243Srink	s = 1;
100156243Srink	if (jx<0) s = -1;
101156243Srink
102156243Srink    /* |x| < 64, return x, s(x), or accurate s*(exp(|x|)/2-1/exp(|x|)/2) */
103156243Srink	if (ix<0x4005) {		/* |x|<64 */
104156243Srink	    if (ix<BIAS-(LDBL_MANT_DIG+1)/2) 	/* |x|<TINY */
105156243Srink		if(shuge+x>1) RETURNI(x);  /* sinh(tiny) = tiny with inexact */
106156243Srink	    if (ix<0x3fff) {		/* |x|<1 */
107156243Srink		x2 = x*x;
108156243Srink#if LDBL_MANT_DIG == 64
109156243Srink		x4 = x2*x2;
110156243Srink		RETURNI(((S17*x2 + S15)*x4 + (S13*x2 + S11))*(x2*x*x4*x4) +
111156243Srink		    ((S9*x2 + S7)*x2 + S5)*(x2*x*x2) + S3*(x2*x) + x);
112156243Srink#elif LDBL_MANT_DIG == 113
113156243Srink		dx2 = x2;
114156243Srink		RETURNI(((((((((((S25*dx2 + S23)*dx2 +
115156243Srink		    S21)*x2 + S19)*x2 +
116152219Simp		    S17)*x2 + S15)*x2 + S13)*x2 + S11)*x2 + S9)*x2 + S7)*x2 +
117156243Srink		    S5)* (x2*x*x2) +
118156243Srink		    S3*(x2*x) + x);
119156243Srink#endif
120156243Srink	    }
121156243Srink	    k_hexpl(fabsl(x), &hi, &lo);
122156243Srink	    RETURNI(s*(lo - 0.25/(hi + lo) + hi));
123156243Srink	}
124156243Srink
125156243Srink    /* |x| in [64, o_threshold], return correctly-overflowing s*exp(|x|)/2 */
126156243Srink	if (fabsl(x) <= o_threshold)
127156243Srink	    RETURNI(s*hexpl(fabsl(x)));
128156243Srink
129156243Srink    /* |x| > o_threshold, sinh(x) overflow */
130156243Srink	return x*shuge;
131156243Srink}
132156243Srink