xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 34123)
1*34123Sbostic /*
224579Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34123Sbostic  * All rights reserved.
4*34123Sbostic  *
5*34123Sbostic  * Redistribution and use in source and binary forms are permitted
6*34123Sbostic  * provided that this notice is preserved and that due credit is given
7*34123Sbostic  * to the University of California at Berkeley. The name of the University
8*34123Sbostic  * may not be used to endorse or promote products derived from this
9*34123Sbostic  * software without specific prior written permission. This software
10*34123Sbostic  * is provided ``as is'' without express or implied warranty.
11*34123Sbostic  *
12*34123Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34123Sbostic  * research project and hence should feel obligated to report their
14*34123Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34123Sbostic  * the sendbug(8) program, to the authors.
1624579Szliu  */
1724579Szliu 
1824579Szliu #ifndef lint
19*34123Sbostic static char sccsid[] = "@(#)cabs.c	5.2 (Berkeley) 04/29/88";
20*34123Sbostic #endif /* not lint */
2124579Szliu 
2224579Szliu /* HYPOT(X,Y)
2324579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
2424579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2524579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
2624579Szliu  * REVISED BY K.C. NG, 7/12/85.
2724579Szliu  *
2824579Szliu  * Required system supported functions :
2924579Szliu  *	copysign(x,y)
3024579Szliu  *	finite(x)
3124579Szliu  *	scalb(x,N)
3224579Szliu  *	sqrt(x)
3324579Szliu  *
3424579Szliu  * Method :
3524579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
3624579Szliu  *	   y if y > x (hence x is never smaller than y).
3724579Szliu  *	2. Hypot(x,y) is computed by:
3824579Szliu  *	   Case I, x/y > 2
3924579Szliu  *
4024579Szliu  *				       y
4124579Szliu  *		hypot = x + -----------------------------
4224579Szliu  *			 		    2
4324579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
4424579Szliu  *
4524579Szliu  *	   Case II, x/y <= 2
4624579Szliu  *				                   y
4724579Szliu  *		hypot = x + --------------------------------------------------
4824579Szliu  *				          		     2
4924579Szliu  *				     			[x/y]   -  2
5024579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
5124579Szliu  *			 		    			  2
5224579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
5324579Szliu  *
5424579Szliu  *
5524579Szliu  *
5624579Szliu  * Special cases:
5724579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
5824579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
5924579Szliu  *
6024579Szliu  * Accuracy:
6124579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
6224579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
6324579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
6424579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
6524579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
6624579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
6724579Szliu  *
6824579Szliu  * Constants:
6924579Szliu  * The hexadecimal values are the intended ones for the following constants.
7024579Szliu  * The decimal values may be used, provided that the compiler will convert
7124579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7224579Szliu  * shown.
7324579Szliu  */
7424579Szliu 
7531855Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7631855Szliu #ifdef vax
7731814Szliu #define _0x(A,B)	0x/**/A/**/B
7831855Szliu #else	/* vax */
7931814Szliu #define _0x(A,B)	0x/**/B/**/A
8031855Szliu #endif	/* vax */
8124579Szliu /* static double */
8224579Szliu /* r2p1hi =  2.4142135623730950345E0     , Hex  2^  2   *  .9A827999FCEF32 */
8324579Szliu /* r2p1lo =  1.4349369327986523769E-17   , Hex  2^-55   *  .84597D89B3754B */
8424579Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
8531814Szliu static long    r2p1hix[] = { _0x(8279,411a), _0x(ef32,99fc)};
8631814Szliu static long    r2p1lox[] = { _0x(597d,2484), _0x(754b,89b3)};
8731814Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
8824579Szliu #define   r2p1hi    (*(double*)r2p1hix)
8924579Szliu #define   r2p1lo    (*(double*)r2p1lox)
9024579Szliu #define    sqrt2    (*(double*)sqrt2x)
9131855Szliu #else	/* defined(vax)||defined(tahoe)	*/
9224579Szliu static double
9324579Szliu r2p1hi =  2.4142135623730949234E0     , /*Hex  2^1     *  1.3504F333F9DE6 */
9424579Szliu r2p1lo =  1.2537167179050217666E-16   , /*Hex  2^-53   *  1.21165F626CDD5 */
9524579Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
9631855Szliu #endif	/* defined(vax)||defined(tahoe)	*/
9724579Szliu 
9831991Szliu double
9931991Szliu hypot(x,y)
10024579Szliu double x, y;
10124579Szliu {
10224579Szliu 	static double zero=0, one=1,
10324579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
10424579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
10524579Szliu 	double copysign(),scalb(),logb(),sqrt(),t,r;
10624579Szliu 	int finite(), exp;
10724579Szliu 
10824579Szliu 	if(finite(x))
10924579Szliu 	    if(finite(y))
11024579Szliu 	    {
11124579Szliu 		x=copysign(x,one);
11224579Szliu 		y=copysign(y,one);
11324579Szliu 		if(y > x)
11424579Szliu 		    { t=x; x=y; y=t; }
11524579Szliu 		if(x == zero) return(zero);
11624579Szliu 		if(y == zero) return(x);
11724579Szliu 		exp= logb(x);
11824579Szliu 		if(exp-(int)logb(y) > ibig )
11924579Szliu 			/* raise inexact flag and return |x| */
12024579Szliu 		   { one+small; return(x); }
12124579Szliu 
12224579Szliu 	    /* start computing sqrt(x^2 + y^2) */
12324579Szliu 		r=x-y;
12424579Szliu 		if(r>y) { 	/* x/y > 2 */
12524579Szliu 		    r=x/y;
12624579Szliu 		    r=r+sqrt(one+r*r); }
12724579Szliu 		else {		/* 1 <= x/y <= 2 */
12824579Szliu 		    r/=y; t=r*(r+2.0);
12924579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
13024579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
13124579Szliu 
13224579Szliu 		r=y/r;
13324579Szliu 		return(x+r);
13424579Szliu 
13524579Szliu 	    }
13624579Szliu 
13724579Szliu 	    else if(y==y)   	   /* y is +-INF */
13824579Szliu 		     return(copysign(y,one));
13924579Szliu 	    else
14024579Szliu 		     return(y);	   /* y is NaN and x is finite */
14124579Szliu 
14224579Szliu 	else if(x==x) 		   /* x is +-INF */
14324579Szliu 	         return (copysign(x,one));
14424579Szliu 	else if(finite(y))
14524579Szliu 	         return(x);		   /* x is NaN, y is finite */
14631855Szliu #if !defined(vax)&&!defined(tahoe)
14724579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
14831855Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
14924579Szliu 	else return(copysign(y,one));   /* y is INF */
15024579Szliu }
15124579Szliu 
15231991Szliu /* CABS(Z)
15331991Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
15431991Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
15531991Szliu  * CODED IN C BY K.C. NG, 11/28/84.
15631991Szliu  * REVISED BY K.C. NG, 7/12/85.
15731991Szliu  *
15831991Szliu  * Required kernel function :
15931991Szliu  *	hypot(x,y)
16031991Szliu  *
16131991Szliu  * Method :
16231991Szliu  *	cabs(z) = hypot(x,y) .
16331991Szliu  */
16431991Szliu 
16531991Szliu double
16631991Szliu cabs(z)
16731991Szliu struct { double x, y;} z;
16831991Szliu {
16931991Szliu 	return hypot(z.x,z.y);
17031991Szliu }
17131991Szliu 
17231991Szliu double
17331991Szliu z_abs(z)
17431991Szliu struct { double x,y;} *z;
17531991Szliu {
17631991Szliu 	return hypot(z->x,z->y);
17731991Szliu }
17831991Szliu 
17924579Szliu /* A faster but less accurate version of cabs(x,y) */
18024579Szliu #if 0
18124579Szliu double hypot(x,y)
18224579Szliu double x, y;
18324579Szliu {
18424579Szliu 	static double zero=0, one=1;
18524579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
18624579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
18724579Szliu 	double copysign(),scalb(),logb(),sqrt(),temp;
18824579Szliu 	int finite(), exp;
18924579Szliu 
19024579Szliu 	if(finite(x))
19124579Szliu 	    if(finite(y))
19224579Szliu 	    {
19324579Szliu 		x=copysign(x,one);
19424579Szliu 		y=copysign(y,one);
19524579Szliu 		if(y > x)
19624579Szliu 		    { temp=x; x=y; y=temp; }
19724579Szliu 		if(x == zero) return(zero);
19824579Szliu 		if(y == zero) return(x);
19924579Szliu 		exp= logb(x);
20024579Szliu 		x=scalb(x,-exp);
20124579Szliu 		if(exp-(int)logb(y) > ibig )
20224579Szliu 			/* raise inexact flag and return |x| */
20324579Szliu 		   { one+small; return(scalb(x,exp)); }
20424579Szliu 		else y=scalb(y,-exp);
20524579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
20624579Szliu 	    }
20724579Szliu 
20824579Szliu 	    else if(y==y)   	   /* y is +-INF */
20924579Szliu 		     return(copysign(y,one));
21024579Szliu 	    else
21124579Szliu 		     return(y);	   /* y is NaN and x is finite */
21224579Szliu 
21324579Szliu 	else if(x==x) 		   /* x is +-INF */
21424579Szliu 	         return (copysign(x,one));
21524579Szliu 	else if(finite(y))
21624579Szliu 	         return(x);		   /* x is NaN, y is finite */
21724579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
21824579Szliu 	else return(copysign(y,one));   /* y is INF */
21924579Szliu }
22024579Szliu #endif
221