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 * Permission to use, copy, modify, and distribute this 21425bb815Sopenharmony_ci * software is freely granted, provided that this notice 22425bb815Sopenharmony_ci * is preserved. 23425bb815Sopenharmony_ci * 24425bb815Sopenharmony_ci * @(#)s_log1p.c 5.1 93/09/24 25425bb815Sopenharmony_ci */ 26425bb815Sopenharmony_ci 27425bb815Sopenharmony_ci#include "jerry-libm-internal.h" 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_ci/* log1p(x) 30425bb815Sopenharmony_ci * Method : 31425bb815Sopenharmony_ci * 1. Argument Reduction: find k and f such that 32425bb815Sopenharmony_ci * 1+x = 2^k * (1+f), 33425bb815Sopenharmony_ci * where sqrt(2)/2 < 1+f < sqrt(2) . 34425bb815Sopenharmony_ci * 35425bb815Sopenharmony_ci * Note. If k=0, then f=x is exact. However, if k!=0, then f 36425bb815Sopenharmony_ci * may not be representable exactly. In that case, a correction 37425bb815Sopenharmony_ci * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 38425bb815Sopenharmony_ci * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 39425bb815Sopenharmony_ci * and add back the correction term c/u. 40425bb815Sopenharmony_ci * (Note: when x > 2**53, one can simply return log(x)) 41425bb815Sopenharmony_ci * 42425bb815Sopenharmony_ci * 2. Approximation of log1p(f). 43425bb815Sopenharmony_ci * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 44425bb815Sopenharmony_ci * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 45425bb815Sopenharmony_ci * = 2s + s*R 46425bb815Sopenharmony_ci * We use a special Reme algorithm on [0,0.1716] to generate 47425bb815Sopenharmony_ci * a polynomial of degree 14 to approximate R The maximum error 48425bb815Sopenharmony_ci * of this polynomial approximation is bounded by 2**-58.45. In 49425bb815Sopenharmony_ci * other words, 50425bb815Sopenharmony_ci * 2 4 6 8 10 12 14 51425bb815Sopenharmony_ci * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s 52425bb815Sopenharmony_ci * (the values of Lp1 to Lp7 are listed in the program) 53425bb815Sopenharmony_ci * and 54425bb815Sopenharmony_ci * | 2 14 | -58.45 55425bb815Sopenharmony_ci * | Lp1*s +...+Lp7*s - R(z) | <= 2 56425bb815Sopenharmony_ci * | | 57425bb815Sopenharmony_ci * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 58425bb815Sopenharmony_ci * In order to guarantee error in log below 1ulp, we compute log 59425bb815Sopenharmony_ci * by 60425bb815Sopenharmony_ci * log1p(f) = f - (hfsq - s*(hfsq+R)). 61425bb815Sopenharmony_ci * 62425bb815Sopenharmony_ci * 3. Finally, log1p(x) = k*ln2 + log1p(f). 63425bb815Sopenharmony_ci * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 64425bb815Sopenharmony_ci * Here ln2 is split into two floating point number: 65425bb815Sopenharmony_ci * ln2_hi + ln2_lo, 66425bb815Sopenharmony_ci * where n*ln2_hi is always exact for |n| < 2000. 67425bb815Sopenharmony_ci * 68425bb815Sopenharmony_ci * Special cases: 69425bb815Sopenharmony_ci * log1p(x) is NaN with signal if x < -1 (including -INF) ; 70425bb815Sopenharmony_ci * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 71425bb815Sopenharmony_ci * log1p(NaN) is that NaN with no signal. 72425bb815Sopenharmony_ci * 73425bb815Sopenharmony_ci * Accuracy: 74425bb815Sopenharmony_ci * according to an error analysis, the error is always less than 75425bb815Sopenharmony_ci * 1 ulp (unit in the last place). 76425bb815Sopenharmony_ci * 77425bb815Sopenharmony_ci * Constants: 78425bb815Sopenharmony_ci * The hexadecimal values are the intended ones for the following 79425bb815Sopenharmony_ci * constants. The decimal values may be used, provided that the 80425bb815Sopenharmony_ci * compiler will convert from decimal to binary accurately enough 81425bb815Sopenharmony_ci * to produce the hexadecimal values shown. 82425bb815Sopenharmony_ci * 83425bb815Sopenharmony_ci * Note: Assuming log() return accurate answer, the following 84425bb815Sopenharmony_ci * algorithm can be used to compute log1p(x) to within a few ULP: 85425bb815Sopenharmony_ci * 86425bb815Sopenharmony_ci * u = 1+x; 87425bb815Sopenharmony_ci * if(u==1.0) return x ; else 88425bb815Sopenharmony_ci * return log(u)*(x/(u-1.0)); 89425bb815Sopenharmony_ci * 90425bb815Sopenharmony_ci * See HP-15C Advanced Functions Handbook, p.193. 91425bb815Sopenharmony_ci */ 92425bb815Sopenharmony_ci 93425bb815Sopenharmony_ci#define zero 0.0 94425bb815Sopenharmony_ci#define ln2_hi 6.93147180369123816490e-01 /* 3fe62e42 fee00000 */ 95425bb815Sopenharmony_ci#define ln2_lo 1.90821492927058770002e-10 /* 3dea39ef 35793c76 */ 96425bb815Sopenharmony_ci#define two54 1.80143985094819840000e+16 /* 43500000 00000000 */ 97425bb815Sopenharmony_ci#define Lp1 6.666666666666735130e-01 /* 3FE55555 55555593 */ 98425bb815Sopenharmony_ci#define Lp2 3.999999999940941908e-01 /* 3FD99999 9997FA04 */ 99425bb815Sopenharmony_ci#define Lp3 2.857142874366239149e-01 /* 3FD24924 94229359 */ 100425bb815Sopenharmony_ci#define Lp4 2.222219843214978396e-01 /* 3FCC71C5 1D8E78AF */ 101425bb815Sopenharmony_ci#define Lp5 1.818357216161805012e-01 /* 3FC74664 96CB03DE */ 102425bb815Sopenharmony_ci#define Lp6 1.531383769920937332e-01 /* 3FC39A09 D078C69F */ 103425bb815Sopenharmony_ci#define Lp7 1.479819860511658591e-01 /* 3FC2F112 DF3E5244 */ 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_cidouble 106425bb815Sopenharmony_cilog1p (double x) 107425bb815Sopenharmony_ci{ 108425bb815Sopenharmony_ci double hfsq, f, c, s, z, R; 109425bb815Sopenharmony_ci double_accessor u; 110425bb815Sopenharmony_ci int k, hx, hu, ax; 111425bb815Sopenharmony_ci 112425bb815Sopenharmony_ci hx = __HI (x); 113425bb815Sopenharmony_ci ax = hx & 0x7fffffff; 114425bb815Sopenharmony_ci c = 0; 115425bb815Sopenharmony_ci k = 1; 116425bb815Sopenharmony_ci if (hx < 0x3FDA827A) 117425bb815Sopenharmony_ci { 118425bb815Sopenharmony_ci /* 1+x < sqrt(2)+ */ 119425bb815Sopenharmony_ci if (ax >= 0x3ff00000) 120425bb815Sopenharmony_ci { 121425bb815Sopenharmony_ci /* x <= -1.0 */ 122425bb815Sopenharmony_ci if (x == -1.0) 123425bb815Sopenharmony_ci { 124425bb815Sopenharmony_ci /* log1p(-1) = +inf */ 125425bb815Sopenharmony_ci return -two54 / zero; 126425bb815Sopenharmony_ci } 127425bb815Sopenharmony_ci else 128425bb815Sopenharmony_ci { 129425bb815Sopenharmony_ci /* log1p(x<-1) = NaN */ 130425bb815Sopenharmony_ci return NAN; 131425bb815Sopenharmony_ci } 132425bb815Sopenharmony_ci } 133425bb815Sopenharmony_ci if (ax < 0x3e200000) 134425bb815Sopenharmony_ci { /* |x| < 2**-29 */ 135425bb815Sopenharmony_ci if ((two54 + x > zero) /* raise inexact */ 136425bb815Sopenharmony_ci && (ax < 0x3c900000)) /* |x| < 2**-54 */ 137425bb815Sopenharmony_ci { 138425bb815Sopenharmony_ci return x; 139425bb815Sopenharmony_ci } 140425bb815Sopenharmony_ci else 141425bb815Sopenharmony_ci { 142425bb815Sopenharmony_ci return x - x * x * 0.5; 143425bb815Sopenharmony_ci } 144425bb815Sopenharmony_ci } 145425bb815Sopenharmony_ci if ((hx > 0) || hx <= ((int) 0xbfd2bec4)) 146425bb815Sopenharmony_ci { 147425bb815Sopenharmony_ci /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 148425bb815Sopenharmony_ci k = 0; 149425bb815Sopenharmony_ci f = x; 150425bb815Sopenharmony_ci hu = 1; 151425bb815Sopenharmony_ci } 152425bb815Sopenharmony_ci } 153425bb815Sopenharmony_ci if (hx >= 0x7ff00000) 154425bb815Sopenharmony_ci { 155425bb815Sopenharmony_ci return x + x; 156425bb815Sopenharmony_ci } 157425bb815Sopenharmony_ci if (k != 0) 158425bb815Sopenharmony_ci { 159425bb815Sopenharmony_ci if (hx < 0x43400000) 160425bb815Sopenharmony_ci { 161425bb815Sopenharmony_ci u.dbl = 1.0 + x; 162425bb815Sopenharmony_ci hu = u.as_int.hi; 163425bb815Sopenharmony_ci k = (hu >> 20) - 1023; 164425bb815Sopenharmony_ci c = (k > 0) ? 1.0 - (u.dbl - x) : x - (u.dbl - 1.0); /* correction term */ 165425bb815Sopenharmony_ci c /= u.dbl; 166425bb815Sopenharmony_ci } 167425bb815Sopenharmony_ci else 168425bb815Sopenharmony_ci { 169425bb815Sopenharmony_ci u.dbl = x; 170425bb815Sopenharmony_ci hu = u.as_int.hi; 171425bb815Sopenharmony_ci k = (hu >> 20) - 1023; 172425bb815Sopenharmony_ci c = 0; 173425bb815Sopenharmony_ci } 174425bb815Sopenharmony_ci hu &= 0x000fffff; 175425bb815Sopenharmony_ci /* 176425bb815Sopenharmony_ci * The approximation to sqrt(2) used in thresholds is not 177425bb815Sopenharmony_ci * critical. However, the ones used above must give less 178425bb815Sopenharmony_ci * strict bounds than the one here so that the k==0 case is 179425bb815Sopenharmony_ci * never reached from here, since here we have committed to 180425bb815Sopenharmony_ci * using the correction term but don't use it if k==0. 181425bb815Sopenharmony_ci */ 182425bb815Sopenharmony_ci if (hu < 0x6a09e) 183425bb815Sopenharmony_ci { 184425bb815Sopenharmony_ci /* u ~< sqrt(2) */ 185425bb815Sopenharmony_ci u.as_int.hi = hu | 0x3ff00000; /* normalize u */ 186425bb815Sopenharmony_ci } 187425bb815Sopenharmony_ci else 188425bb815Sopenharmony_ci { 189425bb815Sopenharmony_ci k += 1; 190425bb815Sopenharmony_ci u.as_int.hi = hu | 0x3fe00000; /* normalize u/2 */ 191425bb815Sopenharmony_ci hu = (0x00100000 - hu) >> 2; 192425bb815Sopenharmony_ci } 193425bb815Sopenharmony_ci f = u.dbl - 1.0; 194425bb815Sopenharmony_ci } 195425bb815Sopenharmony_ci hfsq = 0.5 * f * f; 196425bb815Sopenharmony_ci if (hu == 0) 197425bb815Sopenharmony_ci { 198425bb815Sopenharmony_ci /* |f| < 2**-20 */ 199425bb815Sopenharmony_ci if (f == zero) 200425bb815Sopenharmony_ci { 201425bb815Sopenharmony_ci if (k == 0) 202425bb815Sopenharmony_ci { 203425bb815Sopenharmony_ci return zero; 204425bb815Sopenharmony_ci } 205425bb815Sopenharmony_ci else 206425bb815Sopenharmony_ci { 207425bb815Sopenharmony_ci c += k * ln2_lo; 208425bb815Sopenharmony_ci return k * ln2_hi + c; 209425bb815Sopenharmony_ci } 210425bb815Sopenharmony_ci } 211425bb815Sopenharmony_ci R = hfsq * (1.0 - 0.66666666666666666 * f); 212425bb815Sopenharmony_ci if (k == 0) 213425bb815Sopenharmony_ci { 214425bb815Sopenharmony_ci return f - R; 215425bb815Sopenharmony_ci } 216425bb815Sopenharmony_ci else 217425bb815Sopenharmony_ci { 218425bb815Sopenharmony_ci return k * ln2_hi - ((R - (k * ln2_lo + c)) - f); 219425bb815Sopenharmony_ci } 220425bb815Sopenharmony_ci } 221425bb815Sopenharmony_ci s = f / (2.0 + f); 222425bb815Sopenharmony_ci z = s * s; 223425bb815Sopenharmony_ci R = z * (Lp1 + 224425bb815Sopenharmony_ci z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7)))))); 225425bb815Sopenharmony_ci if (k == 0) 226425bb815Sopenharmony_ci { 227425bb815Sopenharmony_ci return f - (hfsq - s * (hfsq + R)); 228425bb815Sopenharmony_ci } 229425bb815Sopenharmony_ci else 230425bb815Sopenharmony_ci { 231425bb815Sopenharmony_ci return k * ln2_hi - ((hfsq - (s * (hfsq + R) + (k * ln2_lo + c))) - f); 232425bb815Sopenharmony_ci } 233425bb815Sopenharmony_ci} /* log1p */ 234425bb815Sopenharmony_ci 235425bb815Sopenharmony_ci#undef zero 236425bb815Sopenharmony_ci#undef ln2_hi 237425bb815Sopenharmony_ci#undef ln2_lo 238425bb815Sopenharmony_ci#undef two54 239425bb815Sopenharmony_ci#undef Lp1 240425bb815Sopenharmony_ci#undef Lp2 241425bb815Sopenharmony_ci#undef Lp3 242425bb815Sopenharmony_ci#undef Lp4 243425bb815Sopenharmony_ci#undef Lp5 244425bb815Sopenharmony_ci#undef Lp6 245425bb815Sopenharmony_ci#undef Lp7 246