Path: blob/master/src/java.base/share/native/libfdlibm/e_log.c
41152 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* __ieee754_log(x)26* Return the logrithm of x27*28* Method :29* 1. Argument Reduction: find k and f such that30* x = 2^k * (1+f),31* where sqrt(2)/2 < 1+f < sqrt(2) .32*33* 2. Approximation of log(1+f).34* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)35* = 2s + 2/3 s**3 + 2/5 s**5 + .....,36* = 2s + s*R37* We use a special Reme algorithm on [0,0.1716] to generate38* a polynomial of degree 14 to approximate R The maximum error39* of this polynomial approximation is bounded by 2**-58.45. In40* other words,41* 2 4 6 8 10 12 1442* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s43* (the values of Lg1 to Lg7 are listed in the program)44* and45* | 2 14 | -58.4546* | Lg1*s +...+Lg7*s - R(z) | <= 247* | |48* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.49* In order to guarantee error in log below 1ulp, we compute log50* by51* log(1+f) = f - s*(f - R) (if f is not too large)52* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)53*54* 3. Finally, log(x) = k*ln2 + log(1+f).55* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))56* Here ln2 is split into two floating point number:57* ln2_hi + ln2_lo,58* where n*ln2_hi is always exact for |n| < 2000.59*60* Special cases:61* log(x) is NaN with signal if x < 0 (including -INF) ;62* log(+INF) is +INF; log(0) is -INF with signal;63* log(NaN) is that NaN with no signal.64*65* Accuracy:66* according to an error analysis, the error is always less than67* 1 ulp (unit in the last place).68*69* Constants:70* The hexadecimal values are the intended ones for the following71* constants. The decimal values may be used, provided that the72* compiler will convert from decimal to binary accurately enough73* to produce the hexadecimal values shown.74*/7576#include "fdlibm.h"7778#ifdef __STDC__79static const double80#else81static double82#endif83ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */84ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */85two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */86Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */87Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */88Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */89Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */90Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */91Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */92Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */9394static double zero = 0.0;9596#ifdef __STDC__97double __ieee754_log(double x)98#else99double __ieee754_log(x)100double x;101#endif102{103double hfsq,f,s,z,R,w,t1,t2,dk;104int k,hx,i,j;105unsigned lx;106107hx = __HI(x); /* high word of x */108lx = __LO(x); /* low word of x */109110k=0;111if (hx < 0x00100000) { /* x < 2**-1022 */112if (((hx&0x7fffffff)|lx)==0)113return -two54/zero; /* log(+-0)=-inf */114if (hx<0) return (x-x)/zero; /* log(-#) = NaN */115k -= 54; x *= two54; /* subnormal number, scale up x */116hx = __HI(x); /* high word of x */117}118if (hx >= 0x7ff00000) return x+x;119k += (hx>>20)-1023;120hx &= 0x000fffff;121i = (hx+0x95f64)&0x100000;122__HI(x) = hx|(i^0x3ff00000); /* normalize x or x/2 */123k += (i>>20);124f = x-1.0;125if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */126if(f==zero) {127if (k==0) return zero;128else {dk=(double)k; return dk*ln2_hi+dk*ln2_lo;}129}130R = f*f*(0.5-0.33333333333333333*f);131if(k==0) return f-R; else {dk=(double)k;132return dk*ln2_hi-((R-dk*ln2_lo)-f);}133}134s = f/(2.0+f);135dk = (double)k;136z = s*s;137i = hx-0x6147a;138w = z*z;139j = 0x6b851-hx;140t1= w*(Lg2+w*(Lg4+w*Lg6));141t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));142i |= j;143R = t2+t1;144if(i>0) {145hfsq=0.5*f*f;146if(k==0) return f-(hfsq-s*(hfsq+R)); else147return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);148} else {149if(k==0) return f-s*(f-R); else150return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);151}152}153154155