Path: blob/master/src/java.base/share/native/libfdlibm/e_cosh.c
41152 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* __ieee754_cosh(x)26* Method :27* mathematically cosh(x) if defined to be (exp(x)+exp(-x))/228* 1. Replace x by |x| (cosh(x) = cosh(-x)).29* 2.30* [ exp(x) - 1 ]^231* 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------32* 2*exp(x)33*34* exp(x) + 1/exp(x)35* ln2/2 <= x <= 22 : cosh(x) := -------------------36* 237* 22 <= x <= lnovft : cosh(x) := exp(x)/238* lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)39* ln2ovft < x : cosh(x) := huge*huge (overflow)40*41* Special cases:42* cosh(x) is |x| if x is +INF, -INF, or NaN.43* only cosh(0)=1 is exact for finite x.44*/4546#include "fdlibm.h"4748#ifdef __STDC__49static const double one = 1.0, half=0.5, huge = 1.0e300;50#else51static double one = 1.0, half=0.5, huge = 1.0e300;52#endif5354#ifdef __STDC__55double __ieee754_cosh(double x)56#else57double __ieee754_cosh(x)58double x;59#endif60{61double t,w;62int ix;63unsigned lx;6465/* High word of |x|. */66ix = __HI(x);67ix &= 0x7fffffff;6869/* x is INF or NaN */70if(ix>=0x7ff00000) return x*x;7172/* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */73if(ix<0x3fd62e43) {74t = expm1(fabs(x));75w = one+t;76if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */77return one+(t*t)/(w+w);78}7980/* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */81if (ix < 0x40360000) {82t = __ieee754_exp(fabs(x));83return half*t+half/t;84}8586/* |x| in [22, log(maxdouble)] return half*exp(|x|) */87if (ix < 0x40862E42) return half*__ieee754_exp(fabs(x));8889/* |x| in [log(maxdouble), overflowthresold] */90lx = *( (((*(unsigned*)&one)>>29)) + (unsigned*)&x);91if (ix<0x408633CE ||92((ix==0x408633ce)&&(lx<=(unsigned)0x8fb9f87d))) {93w = __ieee754_exp(half*fabs(x));94t = half*w;95return t*w;96}9798/* |x| > overflowthresold, cosh(x) overflow */99return huge*huge;100}101102103