Back to index

lightning-sunbird  0.9+nobinonly
e_log10.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)e_log10.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  */
00051 
00052 /* __ieee754_log10(x)
00053  * Return the base 10 logarithm of x
00054  * 
00055  * Method :
00056  *     Let log10_2hi = leading 40 bits of log10(2) and
00057  *         log10_2lo = log10(2) - log10_2hi,
00058  *         ivln10   = 1/log(10) rounded.
00059  *     Then
00060  *            n = ilogb(x), 
00061  *            if(n<0)  n = n+1;
00062  *            x = scalbn(x,-n);
00063  *            log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
00064  *
00065  * Note 1:
00066  *     To guarantee log10(10**n)=n, where 10**n is normal, the rounding 
00067  *     mode must set to Round-to-Nearest.
00068  * Note 2:
00069  *     [1/log(10)] rounded to 53 bits has error  .198   ulps;
00070  *     log10 is monotonic at all binary break points.
00071  *
00072  * Special cases:
00073  *     log10(x) is NaN with signal if x < 0; 
00074  *     log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
00075  *     log10(NaN) is that NaN with no signal;
00076  *     log10(10**N) = N  for N=0,1,...,22.
00077  *
00078  * Constants:
00079  * The hexadecimal values are the intended ones for the following constants.
00080  * The decimal values may be used, provided that the compiler will convert
00081  * from decimal to binary accurately enough to produce the hexadecimal values
00082  * shown.
00083  */
00084 
00085 #include "fdlibm.h"
00086 
00087 #ifdef __STDC__
00088 static const double
00089 #else
00090 static double
00091 #endif
00092 two54      =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
00093 ivln10     =  4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */
00094 log10_2hi  =  3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
00095 log10_2lo  =  3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
00096 
00097 static double zero   =  0.0;
00098 
00099 #ifdef __STDC__
00100        double __ieee754_log10(double x)
00101 #else
00102        double __ieee754_log10(x)
00103        double x;
00104 #endif
00105 {
00106         fd_twoints u;
00107        double y,z;
00108        int i,k,hx;
00109        unsigned lx;
00110 
00111         u.d = x;
00112        hx = __HI(u); /* high word of x */
00113        lx = __LO(u); /* low word of x */
00114 
00115         k=0;
00116         if (hx < 0x00100000) {                  /* x < 2**-1022  */
00117             if (((hx&0x7fffffff)|lx)==0)
00118                 return -two54/zero;             /* log(+-0)=-inf */
00119             if (hx<0) return (x-x)/zero;        /* log(-#) = NaN */
00120             k -= 54; x *= two54; /* subnormal number, scale up x */
00121             u.d = x;
00122             hx = __HI(u);                /* high word of x */
00123         }
00124        if (hx >= 0x7ff00000) return x+x;
00125        k += (hx>>20)-1023;
00126        i  = ((unsigned)k&0x80000000)>>31;
00127         hx = (hx&0x000fffff)|((0x3ff-i)<<20);
00128         y  = (double)(k+i);
00129         u.d = x;
00130         __HI(u) = hx;
00131         x = u.d;
00132        z  = y*log10_2lo + ivln10*__ieee754_log(x);
00133        return  z+y*log10_2hi;
00134 }