Blame sysdeps/ieee754/flt-32/e_jnf.c

Packit 6c4009
/* e_jnf.c -- float version of e_jn.c.
Packit 6c4009
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math-narrow-eval.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math-underflow.h>
Packit 6c4009
Packit 6c4009
static const float
Packit 6c4009
two   =  2.0000000000e+00, /* 0x40000000 */
Packit 6c4009
one   =  1.0000000000e+00; /* 0x3F800000 */
Packit 6c4009
Packit 6c4009
static const float zero  =  0.0000000000e+00;
Packit 6c4009
Packit 6c4009
float
Packit 6c4009
__ieee754_jnf(int n, float x)
Packit 6c4009
{
Packit 6c4009
    float ret;
Packit 6c4009
    {
Packit 6c4009
	int32_t i,hx,ix, sgn;
Packit 6c4009
	float a, b, temp, di;
Packit 6c4009
	float z, w;
Packit 6c4009
Packit 6c4009
    /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
Packit 6c4009
     * Thus, J(-n,x) = J(n,-x)
Packit 6c4009
     */
Packit 6c4009
	GET_FLOAT_WORD(hx,x);
Packit 6c4009
	ix = 0x7fffffff&hx;
Packit 6c4009
    /* if J(n,NaN) is NaN */
Packit 6c4009
	if(__builtin_expect(ix>0x7f800000, 0)) return x+x;
Packit 6c4009
	if(n<0){
Packit 6c4009
		n = -n;
Packit 6c4009
		x = -x;
Packit 6c4009
		hx ^= 0x80000000;
Packit 6c4009
	}
Packit 6c4009
	if(n==0) return(__ieee754_j0f(x));
Packit 6c4009
	if(n==1) return(__ieee754_j1f(x));
Packit 6c4009
	sgn = (n&1)&(hx>>31);	/* even n -- 0, odd n -- sign(x) */
Packit 6c4009
	x = fabsf(x);
Packit 6c4009
	SET_RESTORE_ROUNDF (FE_TONEAREST);
Packit 6c4009
	if(__builtin_expect(ix==0||ix>=0x7f800000, 0))	/* if x is 0 or inf */
Packit 6c4009
	    return sgn == 1 ? -zero : zero;
Packit 6c4009
	else if((float)n<=x) {
Packit 6c4009
		/* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
Packit 6c4009
	    a = __ieee754_j0f(x);
Packit 6c4009
	    b = __ieee754_j1f(x);
Packit 6c4009
	    for(i=1;i
Packit 6c4009
		temp = b;
Packit 6c4009
		b = b*((double)(i+i)/x) - a; /* avoid underflow */
Packit 6c4009
		a = temp;
Packit 6c4009
	    }
Packit 6c4009
	} else {
Packit 6c4009
	    if(ix<0x30800000) {	/* x < 2**-29 */
Packit 6c4009
    /* x is tiny, return the first Taylor expansion of J(n,x)
Packit 6c4009
     * J(n,x) = 1/n!*(x/2)^n  - ...
Packit 6c4009
     */
Packit 6c4009
		if(n>33)	/* underflow */
Packit 6c4009
		    b = zero;
Packit 6c4009
		else {
Packit 6c4009
		    temp = x*(float)0.5; b = temp;
Packit 6c4009
		    for (a=one,i=2;i<=n;i++) {
Packit 6c4009
			a *= (float)i;		/* a = n! */
Packit 6c4009
			b *= temp;		/* b = (x/2)^n */
Packit 6c4009
		    }
Packit 6c4009
		    b = b/a;
Packit 6c4009
		}
Packit 6c4009
	    } else {
Packit 6c4009
		/* use backward recurrence */
Packit 6c4009
		/*			x      x^2      x^2
Packit 6c4009
		 *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
Packit 6c4009
		 *			2n  - 2(n+1) - 2(n+2)
Packit 6c4009
		 *
Packit 6c4009
		 *			1      1        1
Packit 6c4009
		 *  (for large x)   =  ----  ------   ------   .....
Packit 6c4009
		 *			2n   2(n+1)   2(n+2)
Packit 6c4009
		 *			-- - ------ - ------ -
Packit 6c4009
		 *			 x     x         x
Packit 6c4009
		 *
Packit 6c4009
		 * Let w = 2n/x and h=2/x, then the above quotient
Packit 6c4009
		 * is equal to the continued fraction:
Packit 6c4009
		 *		    1
Packit 6c4009
		 *	= -----------------------
Packit 6c4009
		 *		       1
Packit 6c4009
		 *	   w - -----------------
Packit 6c4009
		 *			  1
Packit 6c4009
		 *		w+h - ---------
Packit 6c4009
		 *		       w+2h - ...
Packit 6c4009
		 *
Packit 6c4009
		 * To determine how many terms needed, let
Packit 6c4009
		 * Q(0) = w, Q(1) = w(w+h) - 1,
Packit 6c4009
		 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
Packit 6c4009
		 * When Q(k) > 1e4	good for single
Packit 6c4009
		 * When Q(k) > 1e9	good for double
Packit 6c4009
		 * When Q(k) > 1e17	good for quadruple
Packit 6c4009
		 */
Packit 6c4009
	    /* determine k */
Packit 6c4009
		float t,v;
Packit 6c4009
		float q0,q1,h,tmp; int32_t k,m;
Packit 6c4009
		w  = (n+n)/(float)x; h = (float)2.0/(float)x;
Packit 6c4009
		q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
Packit 6c4009
		while(q1<(float)1.0e9) {
Packit 6c4009
			k += 1; z += h;
Packit 6c4009
			tmp = z*q1 - q0;
Packit 6c4009
			q0 = q1;
Packit 6c4009
			q1 = tmp;
Packit 6c4009
		}
Packit 6c4009
		m = n+n;
Packit 6c4009
		for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
Packit 6c4009
		a = t;
Packit 6c4009
		b = one;
Packit 6c4009
		/*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
Packit 6c4009
		 *  Hence, if n*(log(2n/x)) > ...
Packit 6c4009
		 *  single 8.8722839355e+01
Packit 6c4009
		 *  double 7.09782712893383973096e+02
Packit 6c4009
		 *  long double 1.1356523406294143949491931077970765006170e+04
Packit 6c4009
		 *  then recurrent value may overflow and the result is
Packit 6c4009
		 *  likely underflow to zero
Packit 6c4009
		 */
Packit 6c4009
		tmp = n;
Packit 6c4009
		v = two/x;
Packit 6c4009
		tmp = tmp*__ieee754_logf(fabsf(v*tmp));
Packit 6c4009
		if(tmp<(float)8.8721679688e+01) {
Packit 6c4009
		    for(i=n-1,di=(float)(i+i);i>0;i--){
Packit 6c4009
			temp = b;
Packit 6c4009
			b *= di;
Packit 6c4009
			b  = b/x - a;
Packit 6c4009
			a = temp;
Packit 6c4009
			di -= two;
Packit 6c4009
		    }
Packit 6c4009
		} else {
Packit 6c4009
		    for(i=n-1,di=(float)(i+i);i>0;i--){
Packit 6c4009
			temp = b;
Packit 6c4009
			b *= di;
Packit 6c4009
			b  = b/x - a;
Packit 6c4009
			a = temp;
Packit 6c4009
			di -= two;
Packit 6c4009
		    /* scale b to avoid spurious overflow */
Packit 6c4009
			if(b>(float)1e10) {
Packit 6c4009
			    a /= b;
Packit 6c4009
			    t /= b;
Packit 6c4009
			    b  = one;
Packit 6c4009
			}
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
		/* j0() and j1() suffer enormous loss of precision at and
Packit 6c4009
		 * near zero; however, we know that their zero points never
Packit 6c4009
		 * coincide, so just choose the one further away from zero.
Packit 6c4009
		 */
Packit 6c4009
		z = __ieee754_j0f (x);
Packit 6c4009
		w = __ieee754_j1f (x);
Packit 6c4009
		if (fabsf (z) >= fabsf (w))
Packit 6c4009
		  b = (t * z / b);
Packit 6c4009
		else
Packit 6c4009
		  b = (t * w / a);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
	if(sgn==1) ret = -b; else ret = b;
Packit 6c4009
	ret = math_narrow_eval (ret);
Packit 6c4009
    }
Packit 6c4009
    if (ret == 0)
Packit 6c4009
      {
Packit 6c4009
	ret = math_narrow_eval (__copysignf (FLT_MIN, ret) * FLT_MIN);
Packit 6c4009
	__set_errno (ERANGE);
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
	math_check_force_underflow (ret);
Packit 6c4009
    return ret;
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_jnf, __jnf_finite)
Packit 6c4009
Packit 6c4009
float
Packit 6c4009
__ieee754_ynf(int n, float x)
Packit 6c4009
{
Packit 6c4009
    float ret;
Packit 6c4009
    {
Packit 6c4009
	int32_t i,hx,ix;
Packit 6c4009
	uint32_t ib;
Packit 6c4009
	int32_t sign;
Packit 6c4009
	float a, b, temp;
Packit 6c4009
Packit 6c4009
	GET_FLOAT_WORD(hx,x);
Packit 6c4009
	ix = 0x7fffffff&hx;
Packit 6c4009
    /* if Y(n,NaN) is NaN */
Packit 6c4009
	if(__builtin_expect(ix>0x7f800000, 0)) return x+x;
Packit 6c4009
	sign = 1;
Packit 6c4009
	if(n<0){
Packit 6c4009
		n = -n;
Packit 6c4009
		sign = 1 - ((n&1)<<1);
Packit 6c4009
	}
Packit 6c4009
	if(n==0) return(__ieee754_y0f(x));
Packit 6c4009
	if(__builtin_expect(ix==0, 0))
Packit 6c4009
		return -sign/zero;
Packit 6c4009
	if(__builtin_expect(hx<0, 0)) return zero/(zero*x);
Packit 6c4009
	SET_RESTORE_ROUNDF (FE_TONEAREST);
Packit 6c4009
	if(n==1) {
Packit 6c4009
	    ret = sign*__ieee754_y1f(x);
Packit 6c4009
	    goto out;
Packit 6c4009
	}
Packit 6c4009
	if(__builtin_expect(ix==0x7f800000, 0)) return zero;
Packit 6c4009
Packit 6c4009
	a = __ieee754_y0f(x);
Packit 6c4009
	b = __ieee754_y1f(x);
Packit 6c4009
	/* quit if b is -inf */
Packit 6c4009
	GET_FLOAT_WORD(ib,b);
Packit 6c4009
	for(i=1;i
Packit 6c4009
	    temp = b;
Packit 6c4009
	    b = ((double)(i+i)/x)*b - a;
Packit 6c4009
	    GET_FLOAT_WORD(ib,b);
Packit 6c4009
	    a = temp;
Packit 6c4009
	}
Packit 6c4009
	/* If B is +-Inf, set up errno accordingly.  */
Packit 6c4009
	if (! isfinite (b))
Packit 6c4009
	  __set_errno (ERANGE);
Packit 6c4009
	if(sign>0) ret = b; else ret = -b;
Packit 6c4009
    }
Packit 6c4009
 out:
Packit 6c4009
    if (isinf (ret))
Packit 6c4009
	ret = __copysignf (FLT_MAX, ret) * FLT_MAX;
Packit 6c4009
    return ret;
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_ynf, __ynf_finite)