s_log1p.c revision 175494
1250003Sadrian/* @(#)s_log1p.c 5.1 93/09/24 */ 2250003Sadrian/* 3250003Sadrian * ==================================================== 4250003Sadrian * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5250003Sadrian * 6250003Sadrian * Developed at SunPro, a Sun Microsystems, Inc. business. 7250003Sadrian * Permission to use, copy, modify, and distribute this 8250003Sadrian * software is freely granted, provided that this notice 9250003Sadrian * is preserved. 10250003Sadrian * ==================================================== 11250003Sadrian */ 12250003Sadrian 13250003Sadrian#include <sys/cdefs.h> 14250003Sadrian__FBSDID("$FreeBSD: head/lib/msun/src/s_log1p.c 175494 2008-01-19 18:13:21Z bde $"); 15250003Sadrian 16250003Sadrian/* double log1p(double x) 17250003Sadrian * 18250003Sadrian * Method : 19250003Sadrian * 1. Argument Reduction: find k and f such that 20250003Sadrian * 1+x = 2^k * (1+f), 21250003Sadrian * where sqrt(2)/2 < 1+f < sqrt(2) . 22250003Sadrian * 23250003Sadrian * Note. If k=0, then f=x is exact. However, if k!=0, then f 24250003Sadrian * may not be representable exactly. In that case, a correction 25250003Sadrian * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 26250003Sadrian * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 27250003Sadrian * and add back the correction term c/u. 28250003Sadrian * (Note: when x > 2**53, one can simply return log(x)) 29250003Sadrian * 30250003Sadrian * 2. Approximation of log1p(f). 31250003Sadrian * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 32250003Sadrian * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 33250003Sadrian * = 2s + s*R 34250003Sadrian * We use a special Reme algorithm on [0,0.1716] to generate 35250003Sadrian * a polynomial of degree 14 to approximate R The maximum error 36250003Sadrian * of this polynomial approximation is bounded by 2**-58.45. In 37250003Sadrian * other words, 38250003Sadrian * 2 4 6 8 10 12 14 39250003Sadrian * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s 40250003Sadrian * (the values of Lp1 to Lp7 are listed in the program) 41250003Sadrian * and 42250003Sadrian * | 2 14 | -58.45 43250003Sadrian * | Lp1*s +...+Lp7*s - R(z) | <= 2 44250003Sadrian * | | 45250003Sadrian * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 46250003Sadrian * In order to guarantee error in log below 1ulp, we compute log 47250003Sadrian * by 48250003Sadrian * log1p(f) = f - (hfsq - s*(hfsq+R)). 49250003Sadrian * 50250003Sadrian * 3. Finally, log1p(x) = k*ln2 + log1p(f). 51250003Sadrian * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 52250003Sadrian * Here ln2 is split into two floating point number: 53250003Sadrian * ln2_hi + ln2_lo, 54250003Sadrian * where n*ln2_hi is always exact for |n| < 2000. 55250003Sadrian * 56250003Sadrian * Special cases: 57250003Sadrian * log1p(x) is NaN with signal if x < -1 (including -INF) ; 58250003Sadrian * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 59250003Sadrian * log1p(NaN) is that NaN with no signal. 60250003Sadrian * 61250003Sadrian * Accuracy: 62250003Sadrian * according to an error analysis, the error is always less than 63250003Sadrian * 1 ulp (unit in the last place). 64250003Sadrian * 65250003Sadrian * Constants: 66250003Sadrian * The hexadecimal values are the intended ones for the following 67250003Sadrian * constants. The decimal values may be used, provided that the 68250003Sadrian * compiler will convert from decimal to binary accurately enough 69250003Sadrian * to produce the hexadecimal values shown. 70250003Sadrian * 71250003Sadrian * Note: Assuming log() return accurate answer, the following 72250003Sadrian * algorithm can be used to compute log1p(x) to within a few ULP: 73250003Sadrian * 74250003Sadrian * u = 1+x; 75250003Sadrian * if(u==1.0) return x ; else 76250003Sadrian * return log(u)*(x/(u-1.0)); 77250003Sadrian * 78250003Sadrian * See HP-15C Advanced Functions Handbook, p.193. 79250003Sadrian */ 80250003Sadrian 81250003Sadrian#include <float.h> 82250003Sadrian 83250003Sadrian#include "math.h" 84250003Sadrian#include "math_private.h" 85250003Sadrian 86250003Sadrianstatic const double 87250003Sadrianln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 88250003Sadrianln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 89250003Sadriantwo54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ 90250003SadrianLp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 91250003SadrianLp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 92250003SadrianLp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 93250003SadrianLp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 94250003SadrianLp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 95250003SadrianLp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 96250003SadrianLp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 97250003Sadrian 98250003Sadrianstatic const double zero = 0.0; 99250003Sadrian 100250003Sadriandouble 101250003Sadrianlog1p(double x) 102250003Sadrian{ 103250003Sadrian double hfsq,f,c,s,z,R,u; 104250003Sadrian int32_t k,hx,hu,ax; 105250003Sadrian 106250003Sadrian GET_HIGH_WORD(hx,x); 107250003Sadrian ax = hx&0x7fffffff; 108250003Sadrian 109250003Sadrian k = 1; 110250003Sadrian if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */ 111250003Sadrian if(ax>=0x3ff00000) { /* x <= -1.0 */ 112250003Sadrian if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */ 113250003Sadrian else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 114250003Sadrian } 115250003Sadrian if(ax<0x3e200000) { /* |x| < 2**-29 */ 116250003Sadrian if(two54+x>zero /* raise inexact */ 117250003Sadrian &&ax<0x3c900000) /* |x| < 2**-54 */ 118250003Sadrian return x; 119250003Sadrian else 120250003Sadrian return x - x*x*0.5; 121250003Sadrian } 122250003Sadrian if(hx>0||hx<=((int32_t)0xbfd2bec4)) { 123250003Sadrian k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 124250003Sadrian } 125250003Sadrian if (hx >= 0x7ff00000) return x+x; 126250003Sadrian if(k!=0) { 127250003Sadrian if(hx<0x43400000) { 128250003Sadrian STRICT_ASSIGN(double,u,1.0+x); 129250003Sadrian GET_HIGH_WORD(hu,u); 130250003Sadrian k = (hu>>20)-1023; 131250003Sadrian c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ 132250003Sadrian c /= u; 133250003Sadrian } else { 134250003Sadrian u = x; 135250003Sadrian GET_HIGH_WORD(hu,u); 136250003Sadrian k = (hu>>20)-1023; 137250003Sadrian c = 0; 138250003Sadrian } 139250003Sadrian hu &= 0x000fffff; 140250003Sadrian /* 141250003Sadrian * The approximation to sqrt(2) used in thresholds is not 142250003Sadrian * critical. However, the ones used above must give less 143250003Sadrian * strict bounds than the one here so that the k==0 case is 144250003Sadrian * never reached from here, since here we have committed to 145250003Sadrian * using the correction term but don't use it if k==0. 146250003Sadrian */ 147250003Sadrian if(hu<0x6a09e) { /* u ~< sqrt(2) */ 148250003Sadrian SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ 149250003Sadrian } else { 150250003Sadrian k += 1; 151250003Sadrian SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ 152250003Sadrian hu = (0x00100000-hu)>>2; 153250003Sadrian } 154250003Sadrian f = u-1.0; 155250003Sadrian } 156250003Sadrian hfsq=0.5*f*f; 157250003Sadrian if(hu==0) { /* |f| < 2**-20 */ 158250003Sadrian if(f==zero) if(k==0) return zero; 159250003Sadrian else {c += k*ln2_lo; return k*ln2_hi+c;} 160250003Sadrian R = hfsq*(1.0-0.66666666666666666*f); 161250003Sadrian if(k==0) return f-R; else 162250003Sadrian return k*ln2_hi-((R-(k*ln2_lo+c))-f); 163250003Sadrian } 164250003Sadrian s = f/(2.0+f); 165250003Sadrian z = s*s; 166250003Sadrian R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 167250003Sadrian if(k==0) return f-(hfsq-s*(hfsq+R)); else 168250003Sadrian return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 169250003Sadrian} 170250003Sadrian