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