xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 42657)
134123Sbostic /*
224579Szliu  * Copyright (c) 1985 Regents of the University of California.
334123Sbostic  * All rights reserved.
434123Sbostic  *
5*42657Sbostic  * %sccs.include.redist.c%
634123Sbostic  *
734123Sbostic  * All recipients should regard themselves as participants in an ongoing
834123Sbostic  * research project and hence should feel obligated to report their
934123Sbostic  * experiences (good or bad) with these elementary function codes, using
1034123Sbostic  * the sendbug(8) program, to the authors.
1124579Szliu  */
1224579Szliu 
1324579Szliu #ifndef lint
14*42657Sbostic static char sccsid[] = "@(#)cabs.c	5.5 (Berkeley) 06/01/90";
1534123Sbostic #endif /* not lint */
1624579Szliu 
1724579Szliu /* HYPOT(X,Y)
1824579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
1924579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2024579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
2124579Szliu  * REVISED BY K.C. NG, 7/12/85.
2224579Szliu  *
2324579Szliu  * Required system supported functions :
2424579Szliu  *	copysign(x,y)
2524579Szliu  *	finite(x)
2624579Szliu  *	scalb(x,N)
2724579Szliu  *	sqrt(x)
2824579Szliu  *
2924579Szliu  * Method :
3024579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
3124579Szliu  *	   y if y > x (hence x is never smaller than y).
3224579Szliu  *	2. Hypot(x,y) is computed by:
3324579Szliu  *	   Case I, x/y > 2
3424579Szliu  *
3524579Szliu  *				       y
3624579Szliu  *		hypot = x + -----------------------------
3724579Szliu  *			 		    2
3824579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
3924579Szliu  *
4024579Szliu  *	   Case II, x/y <= 2
4124579Szliu  *				                   y
4224579Szliu  *		hypot = x + --------------------------------------------------
4324579Szliu  *				          		     2
4424579Szliu  *				     			[x/y]   -  2
4524579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
4624579Szliu  *			 		    			  2
4724579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
4824579Szliu  *
4924579Szliu  *
5024579Szliu  *
5124579Szliu  * Special cases:
5224579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
5324579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
5424579Szliu  *
5524579Szliu  * Accuracy:
5624579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
5724579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
5824579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
5924579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
6024579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
6124579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
6224579Szliu  *
6324579Szliu  * Constants:
6424579Szliu  * The hexadecimal values are the intended ones for the following constants.
6524579Szliu  * The decimal values may be used, provided that the compiler will convert
6624579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6724579Szliu  * shown.
6824579Szliu  */
6935681Sbostic #include "mathimpl.h"
7024579Szliu 
7135681Sbostic vc(r2p1hi, 2.4142135623730950345E0   ,8279,411a,ef32,99fc,   2, .9A827999FCEF32)
7235681Sbostic vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B)
7335681Sbostic vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
7424579Szliu 
7535681Sbostic ic(r2p1hi, 2.4142135623730949234E0   ,   1, 1.3504F333F9DE6)
7635681Sbostic ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5)
7735681Sbostic ic(sqrt2,  1.4142135623730951455E0   ,   0, 1.6A09E667F3BCD)
7835681Sbostic 
7935681Sbostic #ifdef vccast
8035681Sbostic #define	r2p1hi	vccast(r2p1hi)
8135681Sbostic #define	r2p1lo	vccast(r2p1lo)
8235681Sbostic #define	sqrt2	vccast(sqrt2)
8335681Sbostic #endif
8435681Sbostic 
8531991Szliu double
8631991Szliu hypot(x,y)
8724579Szliu double x, y;
8824579Szliu {
8935681Sbostic 	static const double zero=0, one=1,
9024579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
9135681Sbostic 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
9235681Sbostic 	double t,r;
9335681Sbostic 	int exp;
9424579Szliu 
9524579Szliu 	if(finite(x))
9624579Szliu 	    if(finite(y))
9724579Szliu 	    {
9824579Szliu 		x=copysign(x,one);
9924579Szliu 		y=copysign(y,one);
10024579Szliu 		if(y > x)
10124579Szliu 		    { t=x; x=y; y=t; }
10224579Szliu 		if(x == zero) return(zero);
10324579Szliu 		if(y == zero) return(x);
10424579Szliu 		exp= logb(x);
10524579Szliu 		if(exp-(int)logb(y) > ibig )
10624579Szliu 			/* raise inexact flag and return |x| */
10724579Szliu 		   { one+small; return(x); }
10824579Szliu 
10924579Szliu 	    /* start computing sqrt(x^2 + y^2) */
11024579Szliu 		r=x-y;
11124579Szliu 		if(r>y) { 	/* x/y > 2 */
11224579Szliu 		    r=x/y;
11324579Szliu 		    r=r+sqrt(one+r*r); }
11424579Szliu 		else {		/* 1 <= x/y <= 2 */
11524579Szliu 		    r/=y; t=r*(r+2.0);
11624579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
11724579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
11824579Szliu 
11924579Szliu 		r=y/r;
12024579Szliu 		return(x+r);
12124579Szliu 
12224579Szliu 	    }
12324579Szliu 
12424579Szliu 	    else if(y==y)   	   /* y is +-INF */
12524579Szliu 		     return(copysign(y,one));
12624579Szliu 	    else
12724579Szliu 		     return(y);	   /* y is NaN and x is finite */
12824579Szliu 
12924579Szliu 	else if(x==x) 		   /* x is +-INF */
13024579Szliu 	         return (copysign(x,one));
13124579Szliu 	else if(finite(y))
13224579Szliu 	         return(x);		   /* x is NaN, y is finite */
13331855Szliu #if !defined(vax)&&!defined(tahoe)
13424579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
13531855Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
13624579Szliu 	else return(copysign(y,one));   /* y is INF */
13724579Szliu }
13824579Szliu 
13931991Szliu /* CABS(Z)
14031991Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
14131991Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
14231991Szliu  * CODED IN C BY K.C. NG, 11/28/84.
14331991Szliu  * REVISED BY K.C. NG, 7/12/85.
14431991Szliu  *
14531991Szliu  * Required kernel function :
14631991Szliu  *	hypot(x,y)
14731991Szliu  *
14831991Szliu  * Method :
14931991Szliu  *	cabs(z) = hypot(x,y) .
15031991Szliu  */
15131991Szliu 
15231991Szliu double
15331991Szliu cabs(z)
15431991Szliu struct { double x, y;} z;
15531991Szliu {
15631991Szliu 	return hypot(z.x,z.y);
15731991Szliu }
15831991Szliu 
15931991Szliu double
16031991Szliu z_abs(z)
16131991Szliu struct { double x,y;} *z;
16231991Szliu {
16331991Szliu 	return hypot(z->x,z->y);
16431991Szliu }
16531991Szliu 
16624579Szliu /* A faster but less accurate version of cabs(x,y) */
16724579Szliu #if 0
16824579Szliu double hypot(x,y)
16924579Szliu double x, y;
17024579Szliu {
17135681Sbostic 	static const double zero=0, one=1;
17224579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
17335681Sbostic 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
17435681Sbostic 	double temp;
17535681Sbostic 	int exp;
17624579Szliu 
17724579Szliu 	if(finite(x))
17824579Szliu 	    if(finite(y))
17924579Szliu 	    {
18024579Szliu 		x=copysign(x,one);
18124579Szliu 		y=copysign(y,one);
18224579Szliu 		if(y > x)
18324579Szliu 		    { temp=x; x=y; y=temp; }
18424579Szliu 		if(x == zero) return(zero);
18524579Szliu 		if(y == zero) return(x);
18624579Szliu 		exp= logb(x);
18724579Szliu 		x=scalb(x,-exp);
18824579Szliu 		if(exp-(int)logb(y) > ibig )
18924579Szliu 			/* raise inexact flag and return |x| */
19024579Szliu 		   { one+small; return(scalb(x,exp)); }
19124579Szliu 		else y=scalb(y,-exp);
19224579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
19324579Szliu 	    }
19424579Szliu 
19524579Szliu 	    else if(y==y)   	   /* y is +-INF */
19624579Szliu 		     return(copysign(y,one));
19724579Szliu 	    else
19824579Szliu 		     return(y);	   /* y is NaN and x is finite */
19924579Szliu 
20024579Szliu 	else if(x==x) 		   /* x is +-INF */
20124579Szliu 	         return (copysign(x,one));
20224579Szliu 	else if(finite(y))
20324579Szliu 	         return(x);		   /* x is NaN, y is finite */
20424579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
20524579Szliu 	else return(copysign(y,one));   /* y is INF */
20624579Szliu }
20724579Szliu #endif
208