1141296Sdas
2141296Sdas/* @(#)e_log10.c 1.3 95/01/18 */
32116Sjkh/*
42116Sjkh * ====================================================
52116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
62116Sjkh *
7141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business.
82116Sjkh * Permission to use, copy, modify, and distribute this
9141296Sdas * software is freely granted, provided that this notice
102116Sjkh * is preserved.
112116Sjkh * ====================================================
122116Sjkh */
132116Sjkh
14176451Sdas#include <sys/cdefs.h>
15176451Sdas__FBSDID("$FreeBSD$");
162116Sjkh
17216247Sdas/*
18226375Sdas * Return the base 2 logarithm of x.  See e_log.c and k_log.h for most
19226375Sdas * comments.
20226376Sdas *
21226376Sdas * This reduces x to {k, 1+f} exactly as in e_log.c, then calls the kernel,
22226376Sdas * then does the combining and scaling steps
23226376Sdas *    log2(x) = (f - 0.5*f*f + k_log1p(f)) / ln2 + k
24226376Sdas * in not-quite-routine extra precision.
252116Sjkh */
262116Sjkh
27251404Sdas#include <float.h>
28251404Sdas
292116Sjkh#include "math.h"
302116Sjkh#include "math_private.h"
31216211Sdas#include "k_log.h"
322116Sjkh
332116Sjkhstatic const double
342116Sjkhtwo54      =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
35216247Sdasivln2hi    =  1.44269504072144627571e+00, /* 0x3ff71547, 0x65200000 */
36216247Sdasivln2lo    =  1.67517131648865118353e-10; /* 0x3de705fc, 0x2eefa200 */
372116Sjkh
382116Sjkhstatic const double zero   =  0.0;
39251024Sdasstatic volatile double vzero = 0.0;
402116Sjkh
4197413Salfreddouble
42216211Sdas__ieee754_log2(double x)
432116Sjkh{
44226376Sdas	double f,hfsq,hi,lo,r,val_hi,val_lo,w,y;
452116Sjkh	int32_t i,k,hx;
462116Sjkh	u_int32_t lx;
472116Sjkh
482116Sjkh	EXTRACT_WORDS(hx,lx,x);
492116Sjkh
50226375Sdas	k=0;
51226375Sdas	if (hx < 0x00100000) {			/* x < 2**-1022  */
52226375Sdas	    if (((hx&0x7fffffff)|lx)==0)
53251024Sdas		return -two54/vzero;		/* log(+-0)=-inf */
54226375Sdas	    if (hx<0) return (x-x)/zero;	/* log(-#) = NaN */
55226375Sdas	    k -= 54; x *= two54; /* subnormal number, scale up x */
562116Sjkh	    GET_HIGH_WORD(hx,x);
57226375Sdas	}
582116Sjkh	if (hx >= 0x7ff00000) return x+x;
59226376Sdas	if (hx == 0x3ff00000 && lx == 0)
60226376Sdas	    return zero;			/* log(1) = +0 */
612116Sjkh	k += (hx>>20)-1023;
62216211Sdas	hx &= 0x000fffff;
63216211Sdas	i = (hx+0x95f64)&0x100000;
64216211Sdas	SET_HIGH_WORD(x,hx|(i^0x3ff00000));	/* normalize x or x/2 */
65216211Sdas	k += (i>>20);
66226376Sdas	y = (double)k;
67226376Sdas	f = x - 1.0;
68226376Sdas	hfsq = 0.5*f*f;
69226376Sdas	r = k_log1p(f);
70226376Sdas
71226376Sdas	/*
72226376Sdas	 * f-hfsq must (for args near 1) be evaluated in extra precision
73226376Sdas	 * to avoid a large cancellation when x is near sqrt(2) or 1/sqrt(2).
74226376Sdas	 * This is fairly efficient since f-hfsq only depends on f, so can
75226376Sdas	 * be evaluated in parallel with R.  Not combining hfsq with R also
76226376Sdas	 * keeps R small (though not as small as a true `lo' term would be),
77226376Sdas	 * so that extra precision is not needed for terms involving R.
78226376Sdas	 *
79226376Sdas	 * Compiler bugs involving extra precision used to break Dekker's
80226376Sdas	 * theorem for spitting f-hfsq as hi+lo, unless double_t was used
81226376Sdas	 * or the multi-precision calculations were avoided when double_t
82226376Sdas	 * has extra precision.  These problems are now automatically
83226376Sdas	 * avoided as a side effect of the optimization of combining the
84226376Sdas	 * Dekker splitting step with the clear-low-bits step.
85226376Sdas	 *
86226376Sdas	 * y must (for args near sqrt(2) and 1/sqrt(2)) be added in extra
87226376Sdas	 * precision to avoid a very large cancellation when x is very near
88226376Sdas	 * these values.  Unlike the above cancellations, this problem is
89226376Sdas	 * specific to base 2.  It is strange that adding +-1 is so much
90226376Sdas	 * harder than adding +-ln2 or +-log10_2.
91226376Sdas	 *
92226376Sdas	 * This uses Dekker's theorem to normalize y+val_hi, so the
93226376Sdas	 * compiler bugs are back in some configurations, sigh.  And I
94226376Sdas	 * don't want to used double_t to avoid them, since that gives a
95226376Sdas	 * pessimization and the support for avoiding the pessimization
96226376Sdas	 * is not yet available.
97226376Sdas	 *
98226376Sdas	 * The multi-precision calculations for the multiplications are
99226376Sdas	 * routine.
100226376Sdas	 */
101226376Sdas	hi = f - hfsq;
102216211Sdas	SET_LOW_WORD(hi,0);
103226376Sdas	lo = (f - hi) - hfsq + r;
104226376Sdas	val_hi = hi*ivln2hi;
105226376Sdas	val_lo = (lo+hi)*ivln2lo + lo*ivln2hi;
106226376Sdas
107226376Sdas	/* spadd(val_hi, val_lo, y), except for not using double_t: */
108226376Sdas	w = y + val_hi;
109226376Sdas	val_lo += (y - w) + val_hi;
110226376Sdas	val_hi = w;
111226376Sdas
112226376Sdas	return val_lo + val_hi;
1132116Sjkh}
114251292Sdas
115251292Sdas#if (LDBL_MANT_DIG == 53)
116251292Sdas__weak_reference(log2, log2l);
117251292Sdas#endif
118