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; 992116Sjkh 10097413Salfreddouble 10197413Salfredlog1p(double x) 1022116Sjkh{ 1032116Sjkh double hfsq,f,c,s,z,R,u; 1042116Sjkh int32_t k,hx,hu,ax; 1052116Sjkh 1062116Sjkh GET_HIGH_WORD(hx,x); 1072116Sjkh ax = hx&0x7fffffff; 1082116Sjkh 1092116Sjkh k = 1; 110153086Sbde if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */ 1112116Sjkh if(ax>=0x3ff00000) { /* x <= -1.0 */ 1122116Sjkh if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */ 1132116Sjkh else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 1142116Sjkh } 1152116Sjkh if(ax<0x3e200000) { /* |x| < 2**-29 */ 1162116Sjkh if(two54+x>zero /* raise inexact */ 1172116Sjkh &&ax<0x3c900000) /* |x| < 2**-54 */ 1182116Sjkh return x; 1192116Sjkh else 1202116Sjkh return x - x*x*0.5; 1212116Sjkh } 122153086Sbde if(hx>0||hx<=((int32_t)0xbfd2bec4)) { 123153086Sbde k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 1248870Srgrimes } 1252116Sjkh if (hx >= 0x7ff00000) return x+x; 1262116Sjkh if(k!=0) { 1272116Sjkh if(hx<0x43400000) { 128175494Sbde STRICT_ASSIGN(double,u,1.0+x); 1292116Sjkh GET_HIGH_WORD(hu,u); 1302116Sjkh k = (hu>>20)-1023; 1312116Sjkh c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ 1322116Sjkh c /= u; 1332116Sjkh } else { 1342116Sjkh u = x; 1352116Sjkh GET_HIGH_WORD(hu,u); 1362116Sjkh k = (hu>>20)-1023; 1372116Sjkh c = 0; 1382116Sjkh } 1392116Sjkh hu &= 0x000fffff; 140153086Sbde /* 141153086Sbde * The approximation to sqrt(2) used in thresholds is not 142153086Sbde * critical. However, the ones used above must give less 143153086Sbde * strict bounds than the one here so that the k==0 case is 144153086Sbde * never reached from here, since here we have committed to 145153086Sbde * using the correction term but don't use it if k==0. 146153086Sbde */ 147153086Sbde if(hu<0x6a09e) { /* u ~< sqrt(2) */ 1482116Sjkh SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ 1492116Sjkh } else { 1508870Srgrimes k += 1; 1512116Sjkh SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ 1522116Sjkh hu = (0x00100000-hu)>>2; 1532116Sjkh } 1542116Sjkh f = u-1.0; 1552116Sjkh } 1562116Sjkh hfsq=0.5*f*f; 1572116Sjkh if(hu==0) { /* |f| < 2**-20 */ 158177711Sdas if(f==zero) { 159177711Sdas if(k==0) { 160177711Sdas return zero; 161177711Sdas } else { 162177711Sdas c += k*ln2_lo; 163177711Sdas return k*ln2_hi+c; 164177711Sdas } 165177711Sdas } 1662116Sjkh R = hfsq*(1.0-0.66666666666666666*f); 1672116Sjkh if(k==0) return f-R; else 1682116Sjkh return k*ln2_hi-((R-(k*ln2_lo+c))-f); 1692116Sjkh } 1708870Srgrimes s = f/(2.0+f); 1712116Sjkh z = s*s; 1722116Sjkh R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 1732116Sjkh if(k==0) return f-(hfsq-s*(hfsq+R)); else 1742116Sjkh return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 1752116Sjkh} 176