math: raise invalid flag in ilogb*.c on +-0, +-inf and nan
[musl] / src / math / sinh.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_sinh.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /* sinh(x)
13  * Method :
14  * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
15  *      1. Replace x by |x| (sinh(-x) = -sinh(x)).
16  *      2.
17  *                                                  E + E/(E+1)
18  *          0        <= x <= 22     :  sinh(x) := --------------, E=expm1(x)
19  *                                                      2
20  *
21  *          22       <= x <= lnovft :  sinh(x) := exp(x)/2
22  *          lnovft   <= x <= ln2ovft:  sinh(x) := exp(x/2)/2 * exp(x/2)
23  *          ln2ovft  <  x           :  sinh(x) := x*shuge (overflow)
24  *
25  * Special cases:
26  *      sinh(x) is |x| if x is +INF, -INF, or NaN.
27  *      only sinh(0)=0 is exact for finite x.
28  */
29
30 #include "libm.h"
31
32 static const double huge = 1.0e307;
33
34 double sinh(double x)
35 {
36         double t, h;
37         int32_t ix, jx;
38
39         /* High word of |x|. */
40         GET_HIGH_WORD(jx, x);
41         ix = jx & 0x7fffffff;
42
43         /* x is INF or NaN */
44         if (ix >= 0x7ff00000)
45                 return x + x;
46
47         h = 0.5;
48         if (jx < 0) h = -h;
49         /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */
50         if (ix < 0x40360000) {  /* |x|<22 */
51                 if (ix < 0x3e300000)  /* |x|<2**-28 */
52                         /* raise inexact, return x */
53                         if (huge+x > 1.0)
54                                 return x;
55                 t = expm1(fabs(x));
56                 if (ix < 0x3ff00000)
57                         return h*(2.0*t - t*t/(t+1.0));
58                 return h*(t + t/(t+1.0));
59         }
60
61         /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */
62         if (ix < 0x40862E42)
63                 return h*exp(fabs(x));
64
65         /* |x| in [log(maxdouble), overflowthresold] */
66         if (ix <= 0x408633CE)
67                 return h * 2.0 * __expo2(fabs(x)); /* h is for sign only */
68
69         /* |x| > overflowthresold, sinh(x) overflow */
70         return x*huge;
71 }