da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "FEATURE/uwin"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if !_UWIN || _lib_log1p
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvoid _STUB_log1p(){}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Copyright (c) 1985, 1993
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The Regents of the University of California. All rights reserved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Redistribution and use in source and binary forms, with or without
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * modification, are permitted provided that the following conditions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * are met:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 1. Redistributions of source code must retain the above copyright
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * notice, this list of conditions and the following disclaimer.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 2. Redistributions in binary form must reproduce the above copyright
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * notice, this list of conditions and the following disclaimer in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * documentation and/or other materials provided with the distribution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 3. Neither the name of the University nor the names of its contributors
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * may be used to endorse or promote products derived from this software
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * without specific prior written permission.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * SUCH DAMAGE.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifndef lint
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic char sccsid[] = "@(#)log1p.c 8.1 (Berkeley) 6/4/93";
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* not lint */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* LOG1P(x)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * RETURN THE LOGARITHM OF 1+x
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * CODED IN C BY K.C. NG, 1/19/85;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Required system supported functions:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * scalb(x,n)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * copysign(x,y)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * logb(x)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * finite(x)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Required kernel function:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log__L(z)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Method :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 1. Argument Reduction: find k and f such that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 1+x = 2^k * (1+f),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * where sqrt(2)/2 < 1+f < sqrt(2) .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log(1+f) is computed by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log(1+f) = 2s + s*log__L(s*s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * where
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * See log__L() for the values of the coefficients.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 3. Finally, log(1+x) = k*ln2 + log(1+f).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 20 bits (for VAX D format), or the last 21 bits ( for IEEE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * double) is 0. This ensures n*ln2hi is exactly representable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * 2. In step 1, f may not be representable. A correction term c
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * for f is computed. It follows that the correction term for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * f - t (the leading term of log(1+f) in step 2) is c-c*x. We
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * add this correction term to n*ln2lo to attenuate the error.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Special cases:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log1p(INF) is +INF; log1p(-1) is -INF with signal;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * only log1p(0)=0 is exact for finite argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Accuracy:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * log1p(x) returns the exact log(1+x) nearly rounded. In a test run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * with 1,536,000 random arguments on a VAX, the maximum observed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * error was .846 ulps (units in the last place).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Constants:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The hexadecimal values are the intended ones for the following constants.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * The decimal values may be used, provided that the compiler will convert
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * from decimal to binary accurately enough to produce the hexadecimal values
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * shown.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include <errno.h>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "mathimpl.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#ifdef vccast
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ln2hi vccast(ln2hi)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define ln2lo vccast(ln2lo)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define sqrt2 vccast(sqrt2)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern double log1p(x)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindouble x;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin const static double zero=0.0, negone= -1.0, one=1.0,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin half=1.0/2.0, small=1.0E-20; /* 1+small == 1 */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin double z,s,t,c;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int k;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if !defined(vax)&&!defined(tahoe)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(x!=x) return(x); /* x is NaN */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* !defined(vax)&&!defined(tahoe) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(finite(x)) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if( x > negone ) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* argument reduction */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(copysign(x,one)<small) return(x);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin k=(int)logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(z+t >= sqrt2 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { k += 1 ; z *= half; t *= half; }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin t += negone; x = z + t;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin c = (t-x)+z ; /* correction term for x */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* compute log(1+x) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin s = x/(2+x); t = x*x*half;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin c += (k*ln2lo-c*x);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin z = c+s*(t+__log__L(s*s));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin x += (z - t) ;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(k*ln2hi+x);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* end of if (x > negone) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if defined(vax)||defined(tahoe)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ( x == negone )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (infnan(-ERANGE)); /* -INF */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (infnan(EDOM)); /* NaN */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else /* defined(vax)||defined(tahoe) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* x = -1, return -INF with signal */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ( x == negone ) return( negone/zero );
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* negative argument for log, return NaN with signal */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else return ( zero / zero );
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* defined(vax)||defined(tahoe) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* end of if (finite(x)) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* log(-INF) is NaN */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(x<0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return(zero/zero);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* log(+INF) is INF */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else return(x);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif