12116Sjkh/* @(#)s_log1p.c 5.1 93/09/24 */ 22116Sjkh/* 32116Sjkh * ==================================================== 42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 52116Sjkh * 62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 72116Sjkh * Permission to use, copy, modify, and distribute this 88870Srgrimes * software is freely granted, provided that this notice 92116Sjkh * is preserved. 102116Sjkh * ==================================================== 112116Sjkh */ 122116Sjkh 13175494Sbde#include <sys/cdefs.h> 14175494Sbde__FBSDID("$FreeBSD$"); 152116Sjkh 162116Sjkh/* double log1p(double x) 172116Sjkh * 188870Srgrimes * Method : 198870Srgrimes * 1. Argument Reduction: find k and f such that 208870Srgrimes * 1+x = 2^k * (1+f), 212116Sjkh * where sqrt(2)/2 < 1+f < sqrt(2) . 222116Sjkh * 232116Sjkh * Note. If k=0, then f=x is exact. However, if k!=0, then f 242116Sjkh * may not be representable exactly. In that case, a correction 252116Sjkh * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 262116Sjkh * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 272116Sjkh * and add back the correction term c/u. 282116Sjkh * (Note: when x > 2**53, one can simply return log(x)) 292116Sjkh * 302116Sjkh * 2. Approximation of log1p(f). 312116Sjkh * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 322116Sjkh * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 332116Sjkh * = 2s + s*R 348870Srgrimes * We use a special Reme algorithm on [0,0.1716] to generate 358870Srgrimes * a polynomial of degree 14 to approximate R The maximum error 362116Sjkh * of this polynomial approximation is bounded by 2**-58.45. In 372116Sjkh * other words, 382116Sjkh * 2 4 6 8 10 12 14 392116Sjkh * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s 402116Sjkh * (the values of Lp1 to Lp7 are listed in the program) 412116Sjkh * and 422116Sjkh * | 2 14 | -58.45 438870Srgrimes * | Lp1*s +...+Lp7*s - R(z) | <= 2 442116Sjkh * | | 452116Sjkh * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 462116Sjkh * In order to guarantee error in log below 1ulp, we compute log 472116Sjkh * by 482116Sjkh * log1p(f) = f - (hfsq - s*(hfsq+R)). 498870Srgrimes * 508870Srgrimes * 3. Finally, log1p(x) = k*ln2 + log1p(f). 512116Sjkh * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 528870Srgrimes * Here ln2 is split into two floating point number: 532116Sjkh * ln2_hi + ln2_lo, 542116Sjkh * where n*ln2_hi is always exact for |n| < 2000. 552116Sjkh * 562116Sjkh * Special cases: 578870Srgrimes * log1p(x) is NaN with signal if x < -1 (including -INF) ; 582116Sjkh * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 592116Sjkh * log1p(NaN) is that NaN with no signal. 602116Sjkh * 612116Sjkh * Accuracy: 622116Sjkh * according to an error analysis, the error is always less than 632116Sjkh * 1 ulp (unit in the last place). 642116Sjkh * 652116Sjkh * Constants: 668870Srgrimes * The hexadecimal values are the intended ones for the following 678870Srgrimes * constants. The decimal values may be used, provided that the 688870Srgrimes * compiler will convert from decimal to binary accurately enough 692116Sjkh * to produce the hexadecimal values shown. 702116Sjkh * 712116Sjkh * Note: Assuming log() return accurate answer, the following 722116Sjkh * algorithm can be used to compute log1p(x) to within a few ULP: 738870Srgrimes * 742116Sjkh * u = 1+x; 752116Sjkh * if(u==1.0) return x ; else 762116Sjkh * return log(u)*(x/(u-1.0)); 772116Sjkh * 782116Sjkh * See HP-15C Advanced Functions Handbook, p.193. 792116Sjkh */ 802116Sjkh 81175494Sbde#include <float.h> 82175494Sbde 832116Sjkh#include "math.h" 842116Sjkh#include "math_private.h" 852116Sjkh 862116Sjkhstatic const double 872116Sjkhln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 882116Sjkhln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 892116Sjkhtwo54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ 902116SjkhLp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 912116SjkhLp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 922116SjkhLp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 932116SjkhLp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 942116SjkhLp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 952116SjkhLp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 962116SjkhLp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 972116Sjkh 982116Sjkhstatic const double zero = 0.0; 99251024Sdasstatic volatile double vzero = 0.0; 1002116Sjkh 10197413Salfreddouble 10297413Salfredlog1p(double x) 1032116Sjkh{ 1042116Sjkh double hfsq,f,c,s,z,R,u; 1052116Sjkh int32_t k,hx,hu,ax; 1062116Sjkh 1072116Sjkh GET_HIGH_WORD(hx,x); 1082116Sjkh ax = hx&0x7fffffff; 1092116Sjkh 1102116Sjkh k = 1; 111153086Sbde if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */ 1122116Sjkh if(ax>=0x3ff00000) { /* x <= -1.0 */ 113251024Sdas if(x==-1.0) return -two54/vzero; /* log1p(-1)=+inf */ 1142116Sjkh else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 1152116Sjkh } 1162116Sjkh if(ax<0x3e200000) { /* |x| < 2**-29 */ 1172116Sjkh if(two54+x>zero /* raise inexact */ 1182116Sjkh &&ax<0x3c900000) /* |x| < 2**-54 */ 1192116Sjkh return x; 1202116Sjkh else 1212116Sjkh return x - x*x*0.5; 1222116Sjkh } 123153086Sbde if(hx>0||hx<=((int32_t)0xbfd2bec4)) { 124153086Sbde k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 1258870Srgrimes } 1262116Sjkh if (hx >= 0x7ff00000) return x+x; 1272116Sjkh if(k!=0) { 1282116Sjkh if(hx<0x43400000) { 129175494Sbde STRICT_ASSIGN(double,u,1.0+x); 1302116Sjkh GET_HIGH_WORD(hu,u); 1312116Sjkh k = (hu>>20)-1023; 1322116Sjkh c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ 1332116Sjkh c /= u; 1342116Sjkh } else { 1352116Sjkh u = x; 1362116Sjkh GET_HIGH_WORD(hu,u); 1372116Sjkh k = (hu>>20)-1023; 1382116Sjkh c = 0; 1392116Sjkh } 1402116Sjkh hu &= 0x000fffff; 141153086Sbde /* 142153086Sbde * The approximation to sqrt(2) used in thresholds is not 143153086Sbde * critical. However, the ones used above must give less 144153086Sbde * strict bounds than the one here so that the k==0 case is 145153086Sbde * never reached from here, since here we have committed to 146153086Sbde * using the correction term but don't use it if k==0. 147153086Sbde */ 148153086Sbde if(hu<0x6a09e) { /* u ~< sqrt(2) */ 1492116Sjkh SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ 1502116Sjkh } else { 1518870Srgrimes k += 1; 1522116Sjkh SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ 1532116Sjkh hu = (0x00100000-hu)>>2; 1542116Sjkh } 1552116Sjkh f = u-1.0; 1562116Sjkh } 1572116Sjkh hfsq=0.5*f*f; 1582116Sjkh if(hu==0) { /* |f| < 2**-20 */ 159177711Sdas if(f==zero) { 160177711Sdas if(k==0) { 161177711Sdas return zero; 162177711Sdas } else { 163177711Sdas c += k*ln2_lo; 164177711Sdas return k*ln2_hi+c; 165177711Sdas } 166177711Sdas } 1672116Sjkh R = hfsq*(1.0-0.66666666666666666*f); 1682116Sjkh if(k==0) return f-R; else 1692116Sjkh return k*ln2_hi-((R-(k*ln2_lo+c))-f); 1702116Sjkh } 1718870Srgrimes s = f/(2.0+f); 1722116Sjkh z = s*s; 1732116Sjkh R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 1742116Sjkh if(k==0) return f-(hfsq-s*(hfsq+R)); else 1752116Sjkh return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 1762116Sjkh} 177251292Sdas 178251292Sdas#if (LDBL_MANT_DIG == 53) 179251292Sdas__weak_reference(log1p, log1pl); 180251292Sdas#endif 181