1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci * 15425bb815Sopenharmony_ci * This file is based on work under the following copyright and permission 16425bb815Sopenharmony_ci * notice: 17425bb815Sopenharmony_ci * 18425bb815Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 19425bb815Sopenharmony_ci * 20425bb815Sopenharmony_ci * Developed at SunSoft, a Sun Microsystems, Inc. business. 21425bb815Sopenharmony_ci * Permission to use, copy, modify, and distribute this 22425bb815Sopenharmony_ci * software is freely granted, provided that this notice 23425bb815Sopenharmony_ci * is preserved. 24425bb815Sopenharmony_ci * 25425bb815Sopenharmony_ci * @(#)e_cosh.c 1.3 95/01/18 26425bb815Sopenharmony_ci */ 27425bb815Sopenharmony_ci 28425bb815Sopenharmony_ci#include "jerry-libm-internal.h" 29425bb815Sopenharmony_ci 30425bb815Sopenharmony_ci/* cosh(x) 31425bb815Sopenharmony_ci * Method: 32425bb815Sopenharmony_ci * mathematically cosh(x) if defined to be (exp(x) + exp(-x)) / 2 33425bb815Sopenharmony_ci * 1. Replace x by |x| (cosh(x) = cosh(-x)). 34425bb815Sopenharmony_ci * 2. 35425bb815Sopenharmony_ci * [ exp(x) - 1 ]^2 36425bb815Sopenharmony_ci * 0 <= x <= ln2/2 : cosh(x) := 1 + ------------------- 37425bb815Sopenharmony_ci * 2*exp(x) 38425bb815Sopenharmony_ci * 39425bb815Sopenharmony_ci * exp(x) + 1/exp(x) 40425bb815Sopenharmony_ci * ln2/2 <= x <= 22 : cosh(x) := ------------------- 41425bb815Sopenharmony_ci * 2 42425bb815Sopenharmony_ci * 43425bb815Sopenharmony_ci * 22 <= x <= lnovft : cosh(x) := exp(x)/2 44425bb815Sopenharmony_ci * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2) 45425bb815Sopenharmony_ci * ln2ovft < x : cosh(x) := huge * huge (overflow) 46425bb815Sopenharmony_ci * 47425bb815Sopenharmony_ci * Special cases: 48425bb815Sopenharmony_ci * cosh(x) is |x| if x is +INF, -INF, or NaN. 49425bb815Sopenharmony_ci * only cosh(0) = 1 is exact for finite x. 50425bb815Sopenharmony_ci */ 51425bb815Sopenharmony_ci 52425bb815Sopenharmony_ci#define one 1.0 53425bb815Sopenharmony_ci#define half 0.5 54425bb815Sopenharmony_ci#define huge 1.0e300 55425bb815Sopenharmony_ci 56425bb815Sopenharmony_cidouble 57425bb815Sopenharmony_cicosh (double x) 58425bb815Sopenharmony_ci{ 59425bb815Sopenharmony_ci double t, w; 60425bb815Sopenharmony_ci int ix; 61425bb815Sopenharmony_ci unsigned lx; 62425bb815Sopenharmony_ci 63425bb815Sopenharmony_ci /* High word of |x|. */ 64425bb815Sopenharmony_ci ix = __HI (x); 65425bb815Sopenharmony_ci ix &= 0x7fffffff; 66425bb815Sopenharmony_ci 67425bb815Sopenharmony_ci /* x is INF or NaN */ 68425bb815Sopenharmony_ci if (ix >= 0x7ff00000) 69425bb815Sopenharmony_ci { 70425bb815Sopenharmony_ci return x * x; 71425bb815Sopenharmony_ci } 72425bb815Sopenharmony_ci /* |x| in [0, 0.5 * ln2], return 1 + expm1(|x|)^2 / (2 * exp(|x|)) */ 73425bb815Sopenharmony_ci if (ix < 0x3fd62e43) 74425bb815Sopenharmony_ci { 75425bb815Sopenharmony_ci t = expm1 (fabs (x)); 76425bb815Sopenharmony_ci w = one + t; 77425bb815Sopenharmony_ci if (ix < 0x3c800000) 78425bb815Sopenharmony_ci { 79425bb815Sopenharmony_ci /* cosh(tiny) = 1 */ 80425bb815Sopenharmony_ci return w; 81425bb815Sopenharmony_ci } 82425bb815Sopenharmony_ci return one + (t * t) / (w + w); 83425bb815Sopenharmony_ci } 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci /* |x| in [0.5 * ln2, 22], return (exp(|x|) + 1 / exp(|x|) / 2; */ 86425bb815Sopenharmony_ci if (ix < 0x40360000) 87425bb815Sopenharmony_ci { 88425bb815Sopenharmony_ci t = exp (fabs (x)); 89425bb815Sopenharmony_ci return half * t + half / t; 90425bb815Sopenharmony_ci } 91425bb815Sopenharmony_ci 92425bb815Sopenharmony_ci /* |x| in [22, log(maxdouble)] return half * exp(|x|) */ 93425bb815Sopenharmony_ci if (ix < 0x40862E42) 94425bb815Sopenharmony_ci { 95425bb815Sopenharmony_ci return half * exp (fabs (x)); 96425bb815Sopenharmony_ci } 97425bb815Sopenharmony_ci /* |x| in [log(maxdouble), overflowthresold] */ 98425bb815Sopenharmony_ci lx = ((1 >> 29) + (unsigned int) x); 99425bb815Sopenharmony_ci if ((ix < 0x408633CE) || 100425bb815Sopenharmony_ci ((ix == 0x408633ce) && (lx <= (unsigned) 0x8fb9f87d))) 101425bb815Sopenharmony_ci { 102425bb815Sopenharmony_ci w = exp (half * fabs (x)); 103425bb815Sopenharmony_ci t = half * w; 104425bb815Sopenharmony_ci return t * w; 105425bb815Sopenharmony_ci } 106425bb815Sopenharmony_ci 107425bb815Sopenharmony_ci /* |x| > overflowthresold, cosh(x) overflow */ 108425bb815Sopenharmony_ci return huge * huge; 109425bb815Sopenharmony_ci} /* cosh */ 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci#undef one 112425bb815Sopenharmony_ci#undef half 113425bb815Sopenharmony_ci#undef huge 114