1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/s_log1p.c */ 2570af302Sopenharmony_ci/* 3570af302Sopenharmony_ci * ==================================================== 4570af302Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5570af302Sopenharmony_ci * 6570af302Sopenharmony_ci * Developed at SunPro, a Sun Microsystems, Inc. business. 7570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this 8570af302Sopenharmony_ci * software is freely granted, provided that this notice 9570af302Sopenharmony_ci * is preserved. 10570af302Sopenharmony_ci * ==================================================== 11570af302Sopenharmony_ci */ 12570af302Sopenharmony_ci/* double log1p(double x) 13570af302Sopenharmony_ci * Return the natural logarithm of 1+x. 14570af302Sopenharmony_ci * 15570af302Sopenharmony_ci * Method : 16570af302Sopenharmony_ci * 1. Argument Reduction: find k and f such that 17570af302Sopenharmony_ci * 1+x = 2^k * (1+f), 18570af302Sopenharmony_ci * where sqrt(2)/2 < 1+f < sqrt(2) . 19570af302Sopenharmony_ci * 20570af302Sopenharmony_ci * Note. If k=0, then f=x is exact. However, if k!=0, then f 21570af302Sopenharmony_ci * may not be representable exactly. In that case, a correction 22570af302Sopenharmony_ci * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 23570af302Sopenharmony_ci * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 24570af302Sopenharmony_ci * and add back the correction term c/u. 25570af302Sopenharmony_ci * (Note: when x > 2**53, one can simply return log(x)) 26570af302Sopenharmony_ci * 27570af302Sopenharmony_ci * 2. Approximation of log(1+f): See log.c 28570af302Sopenharmony_ci * 29570af302Sopenharmony_ci * 3. Finally, log1p(x) = k*ln2 + log(1+f) + c/u. See log.c 30570af302Sopenharmony_ci * 31570af302Sopenharmony_ci * Special cases: 32570af302Sopenharmony_ci * log1p(x) is NaN with signal if x < -1 (including -INF) ; 33570af302Sopenharmony_ci * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 34570af302Sopenharmony_ci * log1p(NaN) is that NaN with no signal. 35570af302Sopenharmony_ci * 36570af302Sopenharmony_ci * Accuracy: 37570af302Sopenharmony_ci * according to an error analysis, the error is always less than 38570af302Sopenharmony_ci * 1 ulp (unit in the last place). 39570af302Sopenharmony_ci * 40570af302Sopenharmony_ci * Constants: 41570af302Sopenharmony_ci * The hexadecimal values are the intended ones for the following 42570af302Sopenharmony_ci * constants. The decimal values may be used, provided that the 43570af302Sopenharmony_ci * compiler will convert from decimal to binary accurately enough 44570af302Sopenharmony_ci * to produce the hexadecimal values shown. 45570af302Sopenharmony_ci * 46570af302Sopenharmony_ci * Note: Assuming log() return accurate answer, the following 47570af302Sopenharmony_ci * algorithm can be used to compute log1p(x) to within a few ULP: 48570af302Sopenharmony_ci * 49570af302Sopenharmony_ci * u = 1+x; 50570af302Sopenharmony_ci * if(u==1.0) return x ; else 51570af302Sopenharmony_ci * return log(u)*(x/(u-1.0)); 52570af302Sopenharmony_ci * 53570af302Sopenharmony_ci * See HP-15C Advanced Functions Handbook, p.193. 54570af302Sopenharmony_ci */ 55570af302Sopenharmony_ci 56570af302Sopenharmony_ci#include "libm.h" 57570af302Sopenharmony_ci 58570af302Sopenharmony_cistatic const double 59570af302Sopenharmony_ciln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 60570af302Sopenharmony_ciln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 61570af302Sopenharmony_ciLg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 62570af302Sopenharmony_ciLg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 63570af302Sopenharmony_ciLg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 64570af302Sopenharmony_ciLg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 65570af302Sopenharmony_ciLg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 66570af302Sopenharmony_ciLg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 67570af302Sopenharmony_ciLg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 68570af302Sopenharmony_ci 69570af302Sopenharmony_cidouble log1p(double x) 70570af302Sopenharmony_ci{ 71570af302Sopenharmony_ci union {double f; uint64_t i;} u = {x}; 72570af302Sopenharmony_ci double_t hfsq,f,c,s,z,R,w,t1,t2,dk; 73570af302Sopenharmony_ci uint32_t hx,hu; 74570af302Sopenharmony_ci int k; 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci hx = u.i>>32; 77570af302Sopenharmony_ci k = 1; 78570af302Sopenharmony_ci if (hx < 0x3fda827a || hx>>31) { /* 1+x < sqrt(2)+ */ 79570af302Sopenharmony_ci if (hx >= 0xbff00000) { /* x <= -1.0 */ 80570af302Sopenharmony_ci if (x == -1) 81570af302Sopenharmony_ci return x/0.0; /* log1p(-1) = -inf */ 82570af302Sopenharmony_ci return (x-x)/0.0; /* log1p(x<-1) = NaN */ 83570af302Sopenharmony_ci } 84570af302Sopenharmony_ci if (hx<<1 < 0x3ca00000<<1) { /* |x| < 2**-53 */ 85570af302Sopenharmony_ci /* underflow if subnormal */ 86570af302Sopenharmony_ci if ((hx&0x7ff00000) == 0) 87570af302Sopenharmony_ci FORCE_EVAL((float)x); 88570af302Sopenharmony_ci return x; 89570af302Sopenharmony_ci } 90570af302Sopenharmony_ci if (hx <= 0xbfd2bec4) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 91570af302Sopenharmony_ci k = 0; 92570af302Sopenharmony_ci c = 0; 93570af302Sopenharmony_ci f = x; 94570af302Sopenharmony_ci } 95570af302Sopenharmony_ci } else if (hx >= 0x7ff00000) 96570af302Sopenharmony_ci return x; 97570af302Sopenharmony_ci if (k) { 98570af302Sopenharmony_ci u.f = 1 + x; 99570af302Sopenharmony_ci hu = u.i>>32; 100570af302Sopenharmony_ci hu += 0x3ff00000 - 0x3fe6a09e; 101570af302Sopenharmony_ci k = (int)(hu>>20) - 0x3ff; 102570af302Sopenharmony_ci /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */ 103570af302Sopenharmony_ci if (k < 54) { 104570af302Sopenharmony_ci c = k >= 2 ? 1-(u.f-x) : x-(u.f-1); 105570af302Sopenharmony_ci c /= u.f; 106570af302Sopenharmony_ci } else 107570af302Sopenharmony_ci c = 0; 108570af302Sopenharmony_ci /* reduce u into [sqrt(2)/2, sqrt(2)] */ 109570af302Sopenharmony_ci hu = (hu&0x000fffff) + 0x3fe6a09e; 110570af302Sopenharmony_ci u.i = (uint64_t)hu<<32 | (u.i&0xffffffff); 111570af302Sopenharmony_ci f = u.f - 1; 112570af302Sopenharmony_ci } 113570af302Sopenharmony_ci hfsq = 0.5*f*f; 114570af302Sopenharmony_ci s = f/(2.0+f); 115570af302Sopenharmony_ci z = s*s; 116570af302Sopenharmony_ci w = z*z; 117570af302Sopenharmony_ci t1 = w*(Lg2+w*(Lg4+w*Lg6)); 118570af302Sopenharmony_ci t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); 119570af302Sopenharmony_ci R = t2 + t1; 120570af302Sopenharmony_ci dk = k; 121570af302Sopenharmony_ci return s*(hfsq+R) + (dk*ln2_lo+c) - hfsq + f + dk*ln2_hi; 122570af302Sopenharmony_ci} 123