1c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/* from: FreeBSD: head/lib/msun/src/e_sinhl.c XXX */
2c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
3c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/*
4c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * ====================================================
5c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes *
7c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Developed at SunPro, a Sun Microsystems, Inc. business.
8c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Permission to use, copy, modify, and distribute this
9c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * software is freely granted, provided that this notice
10c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * is preserved.
11c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * ====================================================
12c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes */
13c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
14c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include <sys/cdefs.h>
15c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes__FBSDID("$FreeBSD$");
16c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
17c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/*
18c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * See e_sinh.c for complete comments.
19c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes *
20c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Converted to long double by Bruce D. Evans.
21c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes */
22c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
23c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include <float.h>
24c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#ifdef __i386__
25c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include <ieeefp.h>
26c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#endif
27c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
28c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include "fpmath.h"
29c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include "math.h"
30c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include "math_private.h"
31c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#include "k_expl.h"
32c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
33c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#if LDBL_MAX_EXP != 0x4000
34c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/* We also require the usual expsign encoding. */
35c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#error "Unsupported long double format"
36c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#endif
37c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
38c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#define	BIAS	(LDBL_MAX_EXP - 1)
39c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
40c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const long double shuge = 0x1p16383L;
41c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#if LDBL_MANT_DIG == 64
42c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/*
43c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Domain [-1, 1], range ~[-6.6749e-22, 6.6749e-22]:
44c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * |sinh(x)/x - s(x)| < 2**-70.3
45c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes */
46c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const union IEEEl2bits
47c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS3u = LD80C(0xaaaaaaaaaaaaaaaa, -3,  1.66666666666666666658e-1L);
48c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#define	S3	S3u.e
49c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const double
50c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS5  =  8.3333333333333332e-3,		/*  0x11111111111111.0p-59 */
51c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS7  =  1.9841269841270074e-4,		/*  0x1a01a01a01a070.0p-65 */
52c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS9  =  2.7557319223873889e-6,		/*  0x171de3a5565fe6.0p-71 */
53c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS11 =  2.5052108406704084e-8,		/*  0x1ae6456857530f.0p-78 */
54c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS13 =  1.6059042748655297e-10,		/*  0x161245fa910697.0p-85 */
55c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS15 =  7.6470006914396920e-13,		/*  0x1ae7ce4eff2792.0p-93 */
56c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS17 =  2.8346142308424267e-15;		/*  0x19882ce789ffc6.0p-101 */
57c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#elif LDBL_MANT_DIG == 113
58c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/*
59c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * Domain [-1, 1], range ~[-2.9673e-36, 2.9673e-36]:
60c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes * |sinh(x)/x - s(x)| < 2**-118.0
61c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes */
62c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const long double
63c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS3  =  1.66666666666666666666666666666666033e-1L,	/*  0x1555555555555555555555555553b.0p-115L */
64c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS5  =  8.33333333333333333333333333337643193e-3L,	/*  0x111111111111111111111111180f5.0p-119L */
65c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS7  =  1.98412698412698412698412697391263199e-4L,	/*  0x1a01a01a01a01a01a01a0176aad11.0p-125L */
66c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS9  =  2.75573192239858906525574406205464218e-6L,	/*  0x171de3a556c7338faac243aaa9592.0p-131L */
67c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS11 =  2.50521083854417187749675637460977997e-8L,	/*  0x1ae64567f544e38fe59b3380d7413.0p-138L */
68c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS13 =  1.60590438368216146368737762431552702e-10L,	/*  0x16124613a86d098059c7620850fc2.0p-145L */
69c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS15 =  7.64716373181980539786802470969096440e-13L,	/*  0x1ae7f3e733b814193af09ce723043.0p-153L */
70c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS17 =  2.81145725434775409870584280722701574e-15L;	/*  0x1952c77030c36898c3fd0b6dfc562.0p-161L */
71c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const double
72c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS19=  8.2206352435411005e-18,		/*  0x12f49b4662b86d.0p-109 */
73c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS21=  1.9572943931418891e-20,		/*  0x171b8f2fab9628.0p-118 */
74c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS23 =  3.8679983530666939e-23,		/*  0x17617002b73afc.0p-127 */
75c729d4f23771a01226c761423c6b35210dbb6ca7Elliott HughesS25 =  6.5067867911512749e-26;		/*  0x1423352626048a.0p-136 */
76c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#else
77c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#error "Unsupported long double format"
78c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#endif /* LDBL_MANT_DIG == 64 */
79c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
80c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes/* log(2**16385 - 0.5) rounded up: */
81c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughesstatic const float
82c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hugheso_threshold =  1.13572168e4;		/*  0xb174de.0p-10 */
83c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
84c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hugheslong double
85c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughessinhl(long double x)
86c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes{
87c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	long double hi,lo,x2,x4;
88c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	double dx2,s;
89c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	int16_t ix,jx;
90c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
91c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	GET_LDBL_EXPSIGN(jx,x);
92c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	ix = jx&0x7fff;
93c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
94c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes    /* x is INF or NaN */
95c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	if(ix>=0x7fff) return x+x;
96c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
97c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	ENTERI();
98c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
99c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	s = 1;
100c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	if (jx<0) s = -1;
101c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
102c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes    /* |x| < 64, return x, s(x), or accurate s*(exp(|x|)/2-1/exp(|x|)/2) */
103c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	if (ix<0x4005) {		/* |x|<64 */
104c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    if (ix<BIAS-(LDBL_MANT_DIG+1)/2) 	/* |x|<TINY */
105c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		if(shuge+x>1) RETURNI(x);  /* sinh(tiny) = tiny with inexact */
106c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    if (ix<0x3fff) {		/* |x|<1 */
107c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		x2 = x*x;
108c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#if LDBL_MANT_DIG == 64
109c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		x4 = x2*x2;
110c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		RETURNI(((S17*x2 + S15)*x4 + (S13*x2 + S11))*(x2*x*x4*x4) +
111c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		    ((S9*x2 + S7)*x2 + S5)*(x2*x*x2) + S3*(x2*x) + x);
112c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#elif LDBL_MANT_DIG == 113
113c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		dx2 = x2;
114c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		RETURNI(((((((((((S25*dx2 + S23)*dx2 +
115c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		    S21)*x2 + S19)*x2 +
116c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		    S17)*x2 + S15)*x2 + S13)*x2 + S11)*x2 + S9)*x2 + S7)*x2 +
117c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		    S5)* (x2*x*x2) +
118c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes		    S3*(x2*x) + x);
119c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes#endif
120c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    }
121c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    k_hexpl(fabsl(x), &hi, &lo);
122c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    RETURNI(s*(lo - 0.25/(hi + lo) + hi));
123c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	}
124c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
125c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes    /* |x| in [64, o_threshold], return correctly-overflowing s*exp(|x|)/2 */
126c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	if (fabsl(x) <= o_threshold)
127c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	    RETURNI(s*hexpl(fabsl(x)));
128c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes
129c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes    /* |x| > o_threshold, sinh(x) overflow */
130c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes	return x*shuge;
131c729d4f23771a01226c761423c6b35210dbb6ca7Elliott Hughes}
132