1 /*
2  * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /* __ieee754_cosh(x)
27  * Method :
28  * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
29  *      1. Replace x by |x| (cosh(x) = cosh(-x)).
30  *      2.
31  *                                                      [ exp(x) - 1 ]^2
32  *          0        <= x <= ln2/2  :  cosh(x) := 1 + -------------------
33  *                                                         2*exp(x)
34  *
35  *                                                exp(x) +  1/exp(x)
36  *          ln2/2    <= x <= 22     :  cosh(x) := -------------------
37  *                                                        2
38  *          22       <= x <= lnovft :  cosh(x) := exp(x)/2
39  *          lnovft   <= x <= ln2ovft:  cosh(x) := exp(x/2)/2 * exp(x/2)
40  *          ln2ovft  <  x           :  cosh(x) := huge*huge (overflow)
41  *
42  * Special cases:
43  *      cosh(x) is |x| if x is +INF, -INF, or NaN.
44  *      only cosh(0)=1 is exact for finite x.
45  */
46 
47 #include "fdlibm.h"
48 
49 #ifdef __STDC__
50 static const double one = 1.0, half=0.5, huge = 1.0e300;
51 #else
52 static double one = 1.0, half=0.5, huge = 1.0e300;
53 #endif
54 
55 #ifdef __STDC__
__ieee754_cosh(double x)56         double __ieee754_cosh(double x)
57 #else
58         double __ieee754_cosh(x)
59         double x;
60 #endif
61 {
62         double t,w;
63         int ix;
64         unsigned lx;
65 
66     /* High word of |x|. */
67         ix = __HI(x);
68         ix &= 0x7fffffff;
69 
70     /* x is INF or NaN */
71         if(ix>=0x7ff00000) return x*x;
72 
73     /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */
74         if(ix<0x3fd62e43) {
75             t = expm1(fabs(x));
76             w = one+t;
77             if (ix<0x3c800000) return w;        /* cosh(tiny) = 1 */
78             return one+(t*t)/(w+w);
79         }
80 
81     /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */
82         if (ix < 0x40360000) {
83                 t = __ieee754_exp(fabs(x));
84                 return half*t+half/t;
85         }
86 
87     /* |x| in [22, log(maxdouble)] return half*exp(|x|) */
88         if (ix < 0x40862E42)  return half*__ieee754_exp(fabs(x));
89 
90     /* |x| in [log(maxdouble), overflowthresold] */
91         lx = *( (((*(unsigned*)&one)>>29)) + (unsigned*)&x);
92         if (ix<0x408633CE ||
93               ((ix==0x408633ce)&&(lx<=(unsigned)0x8fb9f87d))) {
94             w = __ieee754_exp(half*fabs(x));
95             t = half*w;
96             return t*w;
97         }
98 
99     /* |x| > overflowthresold, cosh(x) overflow */
100         return huge*huge;
101 }
102