xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 31855)
124579Szliu /*
224579Szliu  * Copyright (c) 1985 Regents of the University of California.
324579Szliu  *
424579Szliu  * Use and reproduction of this software are granted  in  accordance  with
524579Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624579Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724579Szliu  * source, and inclusion of this notice) with the additional understanding
824579Szliu  * that  all  recipients  should regard themselves as participants  in  an
924579Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024579Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124579Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224579Szliu  */
1324579Szliu 
1424579Szliu #ifndef lint
1524719Selefunt static char sccsid[] =
16*31855Szliu "@(#)cabs.c	1.2 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87";
17*31855Szliu #endif	/* not lint */
1824579Szliu 
1924579Szliu /* CABS(Z)
2024579Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
2124579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2224579Szliu  * CODED IN C BY K.C. NG, 11/28/84.
2324579Szliu  * REVISED BY K.C. NG, 7/12/85.
2424579Szliu  *
2524579Szliu  * Required kernel function :
2624579Szliu  *	hypot(x,y)
2724579Szliu  *
2824579Szliu  * Method :
2924579Szliu  *	cabs(z) = hypot(x,y) .
3024579Szliu  */
3124579Szliu 
3224579Szliu double cabs(z)
3324579Szliu struct { double x, y;} z;
3424579Szliu {
3524579Szliu 	double hypot();
3624579Szliu 	return(hypot(z.x,z.y));
3724579Szliu }
3824579Szliu 
3924579Szliu 
4024579Szliu /* HYPOT(X,Y)
4124579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
4224579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
4324579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
4424579Szliu  * REVISED BY K.C. NG, 7/12/85.
4524579Szliu  *
4624579Szliu  * Required system supported functions :
4724579Szliu  *	copysign(x,y)
4824579Szliu  *	finite(x)
4924579Szliu  *	scalb(x,N)
5024579Szliu  *	sqrt(x)
5124579Szliu  *
5224579Szliu  * Method :
5324579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
5424579Szliu  *	   y if y > x (hence x is never smaller than y).
5524579Szliu  *	2. Hypot(x,y) is computed by:
5624579Szliu  *	   Case I, x/y > 2
5724579Szliu  *
5824579Szliu  *				       y
5924579Szliu  *		hypot = x + -----------------------------
6024579Szliu  *			 		    2
6124579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
6224579Szliu  *
6324579Szliu  *	   Case II, x/y <= 2
6424579Szliu  *				                   y
6524579Szliu  *		hypot = x + --------------------------------------------------
6624579Szliu  *				          		     2
6724579Szliu  *				     			[x/y]   -  2
6824579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
6924579Szliu  *			 		    			  2
7024579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
7124579Szliu  *
7224579Szliu  *
7324579Szliu  *
7424579Szliu  * Special cases:
7524579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
7624579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
7724579Szliu  *
7824579Szliu  * Accuracy:
7924579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
8024579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
8124579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
8224579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
8324579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
8424579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
8524579Szliu  *
8624579Szliu  * Constants:
8724579Szliu  * The hexadecimal values are the intended ones for the following constants.
8824579Szliu  * The decimal values may be used, provided that the compiler will convert
8924579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
9024579Szliu  * shown.
9124579Szliu  */
9224579Szliu 
93*31855Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
94*31855Szliu #ifdef vax
9531814Szliu #define _0x(A,B)	0x/**/A/**/B
96*31855Szliu #else	/* vax */
9731814Szliu #define _0x(A,B)	0x/**/B/**/A
98*31855Szliu #endif	/* vax */
9924579Szliu /* static double */
10024579Szliu /* r2p1hi =  2.4142135623730950345E0     , Hex  2^  2   *  .9A827999FCEF32 */
10124579Szliu /* r2p1lo =  1.4349369327986523769E-17   , Hex  2^-55   *  .84597D89B3754B */
10224579Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
10331814Szliu static long    r2p1hix[] = { _0x(8279,411a), _0x(ef32,99fc)};
10431814Szliu static long    r2p1lox[] = { _0x(597d,2484), _0x(754b,89b3)};
10531814Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
10624579Szliu #define   r2p1hi    (*(double*)r2p1hix)
10724579Szliu #define   r2p1lo    (*(double*)r2p1lox)
10824579Szliu #define    sqrt2    (*(double*)sqrt2x)
109*31855Szliu #else	/* defined(vax)||defined(tahoe)	*/
11024579Szliu static double
11124579Szliu r2p1hi =  2.4142135623730949234E0     , /*Hex  2^1     *  1.3504F333F9DE6 */
11224579Szliu r2p1lo =  1.2537167179050217666E-16   , /*Hex  2^-53   *  1.21165F626CDD5 */
11324579Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
114*31855Szliu #endif	/* defined(vax)||defined(tahoe)	*/
11524579Szliu 
11624579Szliu double hypot(x,y)
11724579Szliu double x, y;
11824579Szliu {
11924579Szliu 	static double zero=0, one=1,
12024579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
12124579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
12224579Szliu 	double copysign(),scalb(),logb(),sqrt(),t,r;
12324579Szliu 	int finite(), exp;
12424579Szliu 
12524579Szliu 	if(finite(x))
12624579Szliu 	    if(finite(y))
12724579Szliu 	    {
12824579Szliu 		x=copysign(x,one);
12924579Szliu 		y=copysign(y,one);
13024579Szliu 		if(y > x)
13124579Szliu 		    { t=x; x=y; y=t; }
13224579Szliu 		if(x == zero) return(zero);
13324579Szliu 		if(y == zero) return(x);
13424579Szliu 		exp= logb(x);
13524579Szliu 		if(exp-(int)logb(y) > ibig )
13624579Szliu 			/* raise inexact flag and return |x| */
13724579Szliu 		   { one+small; return(x); }
13824579Szliu 
13924579Szliu 	    /* start computing sqrt(x^2 + y^2) */
14024579Szliu 		r=x-y;
14124579Szliu 		if(r>y) { 	/* x/y > 2 */
14224579Szliu 		    r=x/y;
14324579Szliu 		    r=r+sqrt(one+r*r); }
14424579Szliu 		else {		/* 1 <= x/y <= 2 */
14524579Szliu 		    r/=y; t=r*(r+2.0);
14624579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
14724579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
14824579Szliu 
14924579Szliu 		r=y/r;
15024579Szliu 		return(x+r);
15124579Szliu 
15224579Szliu 	    }
15324579Szliu 
15424579Szliu 	    else if(y==y)   	   /* y is +-INF */
15524579Szliu 		     return(copysign(y,one));
15624579Szliu 	    else
15724579Szliu 		     return(y);	   /* y is NaN and x is finite */
15824579Szliu 
15924579Szliu 	else if(x==x) 		   /* x is +-INF */
16024579Szliu 	         return (copysign(x,one));
16124579Szliu 	else if(finite(y))
16224579Szliu 	         return(x);		   /* x is NaN, y is finite */
163*31855Szliu #if !defined(vax)&&!defined(tahoe)
16424579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
165*31855Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
16624579Szliu 	else return(copysign(y,one));   /* y is INF */
16724579Szliu }
16824579Szliu 
16924579Szliu /* A faster but less accurate version of cabs(x,y) */
17024579Szliu #if 0
17124579Szliu double hypot(x,y)
17224579Szliu double x, y;
17324579Szliu {
17424579Szliu 	static double zero=0, one=1;
17524579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
17624579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
17724579Szliu 	double copysign(),scalb(),logb(),sqrt(),temp;
17824579Szliu 	int finite(), exp;
17924579Szliu 
18024579Szliu 	if(finite(x))
18124579Szliu 	    if(finite(y))
18224579Szliu 	    {
18324579Szliu 		x=copysign(x,one);
18424579Szliu 		y=copysign(y,one);
18524579Szliu 		if(y > x)
18624579Szliu 		    { temp=x; x=y; y=temp; }
18724579Szliu 		if(x == zero) return(zero);
18824579Szliu 		if(y == zero) return(x);
18924579Szliu 		exp= logb(x);
19024579Szliu 		x=scalb(x,-exp);
19124579Szliu 		if(exp-(int)logb(y) > ibig )
19224579Szliu 			/* raise inexact flag and return |x| */
19324579Szliu 		   { one+small; return(scalb(x,exp)); }
19424579Szliu 		else y=scalb(y,-exp);
19524579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
19624579Szliu 	    }
19724579Szliu 
19824579Szliu 	    else if(y==y)   	   /* y is +-INF */
19924579Szliu 		     return(copysign(y,one));
20024579Szliu 	    else
20124579Szliu 		     return(y);	   /* y is NaN and x is finite */
20224579Szliu 
20324579Szliu 	else if(x==x) 		   /* x is +-INF */
20424579Szliu 	         return (copysign(x,one));
20524579Szliu 	else if(finite(y))
20624579Szliu 	         return(x);		   /* x is NaN, y is finite */
20724579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
20824579Szliu 	else return(copysign(y,one));   /* y is INF */
20924579Szliu }
21024579Szliu #endif
211