Path: blob/master/src/java.base/share/native/libfdlibm/e_log10.c
41149 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_log10(x)26* Return the base 10 logarithm of x27*28* Method :29* Let log10_2hi = leading 40 bits of log10(2) and30* log10_2lo = log10(2) - log10_2hi,31* ivln10 = 1/log(10) rounded.32* Then33* n = ilogb(x),34* if(n<0) n = n+1;35* x = scalbn(x,-n);36* log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))37*38* Note 1:39* To guarantee log10(10**n)=n, where 10**n is normal, the rounding40* mode must set to Round-to-Nearest.41* Note 2:42* [1/log(10)] rounded to 53 bits has error .198 ulps;43* log10 is monotonic at all binary break points.44*45* Special cases:46* log10(x) is NaN with signal if x < 0;47* log10(+INF) is +INF with no signal; log10(0) is -INF with signal;48* log10(NaN) is that NaN with no signal;49* log10(10**N) = N for N=0,1,...,22.50*51* Constants:52* The hexadecimal values are the intended ones for the following constants.53* The decimal values may be used, provided that the compiler will convert54* from decimal to binary accurately enough to produce the hexadecimal values55* shown.56*/5758#include "fdlibm.h"5960#ifdef __STDC__61static const double62#else63static double64#endif65two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */66ivln10 = 4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */67log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */68log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */6970static double zero = 0.0;7172#ifdef __STDC__73double __ieee754_log10(double x)74#else75double __ieee754_log10(x)76double x;77#endif78{79double y,z;80int i,k,hx;81unsigned lx;8283hx = __HI(x); /* high word of x */84lx = __LO(x); /* low word of x */8586k=0;87if (hx < 0x00100000) { /* x < 2**-1022 */88if (((hx&0x7fffffff)|lx)==0)89return -two54/zero; /* log(+-0)=-inf */90if (hx<0) return (x-x)/zero; /* log(-#) = NaN */91k -= 54; x *= two54; /* subnormal number, scale up x */92hx = __HI(x); /* high word of x */93}94if (hx >= 0x7ff00000) return x+x;95k += (hx>>20)-1023;96i = ((unsigned)k&0x80000000)>>31;97hx = (hx&0x000fffff)|((0x3ff-i)<<20);98y = (double)(k+i);99__HI(x) = hx;100z = y*log10_2lo + ivln10*__ieee754_log(x);101return z+y*log10_2hi;102}103104105