xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 34925)
134123Sbostic /*
224579Szliu  * Copyright (c) 1985 Regents of the University of California.
334123Sbostic  * All rights reserved.
434123Sbostic  *
534123Sbostic  * Redistribution and use in source and binary forms are permitted
6*34925Sbostic  * provided that the above copyright notice and this paragraph are
7*34925Sbostic  * duplicated in all such forms and that any documentation,
8*34925Sbostic  * advertising materials, and other materials related to such
9*34925Sbostic  * distribution and use acknowledge that the software was developed
10*34925Sbostic  * by the University of California, Berkeley.  The name of the
11*34925Sbostic  * University may not be used to endorse or promote products derived
12*34925Sbostic  * from this software without specific prior written permission.
13*34925Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34925Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34925Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634123Sbostic  *
1734123Sbostic  * All recipients should regard themselves as participants in an ongoing
1834123Sbostic  * research project and hence should feel obligated to report their
1934123Sbostic  * experiences (good or bad) with these elementary function codes, using
2034123Sbostic  * the sendbug(8) program, to the authors.
2124579Szliu  */
2224579Szliu 
2324579Szliu #ifndef lint
24*34925Sbostic static char sccsid[] = "@(#)cabs.c	5.3 (Berkeley) 06/30/88";
2534123Sbostic #endif /* not lint */
2624579Szliu 
2724579Szliu /* HYPOT(X,Y)
2824579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
2924579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
3024579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
3124579Szliu  * REVISED BY K.C. NG, 7/12/85.
3224579Szliu  *
3324579Szliu  * Required system supported functions :
3424579Szliu  *	copysign(x,y)
3524579Szliu  *	finite(x)
3624579Szliu  *	scalb(x,N)
3724579Szliu  *	sqrt(x)
3824579Szliu  *
3924579Szliu  * Method :
4024579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
4124579Szliu  *	   y if y > x (hence x is never smaller than y).
4224579Szliu  *	2. Hypot(x,y) is computed by:
4324579Szliu  *	   Case I, x/y > 2
4424579Szliu  *
4524579Szliu  *				       y
4624579Szliu  *		hypot = x + -----------------------------
4724579Szliu  *			 		    2
4824579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
4924579Szliu  *
5024579Szliu  *	   Case II, x/y <= 2
5124579Szliu  *				                   y
5224579Szliu  *		hypot = x + --------------------------------------------------
5324579Szliu  *				          		     2
5424579Szliu  *				     			[x/y]   -  2
5524579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
5624579Szliu  *			 		    			  2
5724579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
5824579Szliu  *
5924579Szliu  *
6024579Szliu  *
6124579Szliu  * Special cases:
6224579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
6324579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
6424579Szliu  *
6524579Szliu  * Accuracy:
6624579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
6724579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
6824579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
6924579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
7024579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
7124579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
7224579Szliu  *
7324579Szliu  * Constants:
7424579Szliu  * The hexadecimal values are the intended ones for the following constants.
7524579Szliu  * The decimal values may be used, provided that the compiler will convert
7624579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7724579Szliu  * shown.
7824579Szliu  */
7924579Szliu 
8031855Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
8131855Szliu #ifdef vax
8231814Szliu #define _0x(A,B)	0x/**/A/**/B
8331855Szliu #else	/* vax */
8431814Szliu #define _0x(A,B)	0x/**/B/**/A
8531855Szliu #endif	/* vax */
8624579Szliu /* static double */
8724579Szliu /* r2p1hi =  2.4142135623730950345E0     , Hex  2^  2   *  .9A827999FCEF32 */
8824579Szliu /* r2p1lo =  1.4349369327986523769E-17   , Hex  2^-55   *  .84597D89B3754B */
8924579Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
9031814Szliu static long    r2p1hix[] = { _0x(8279,411a), _0x(ef32,99fc)};
9131814Szliu static long    r2p1lox[] = { _0x(597d,2484), _0x(754b,89b3)};
9231814Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
9324579Szliu #define   r2p1hi    (*(double*)r2p1hix)
9424579Szliu #define   r2p1lo    (*(double*)r2p1lox)
9524579Szliu #define    sqrt2    (*(double*)sqrt2x)
9631855Szliu #else	/* defined(vax)||defined(tahoe)	*/
9724579Szliu static double
9824579Szliu r2p1hi =  2.4142135623730949234E0     , /*Hex  2^1     *  1.3504F333F9DE6 */
9924579Szliu r2p1lo =  1.2537167179050217666E-16   , /*Hex  2^-53   *  1.21165F626CDD5 */
10024579Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
10131855Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10224579Szliu 
10331991Szliu double
10431991Szliu hypot(x,y)
10524579Szliu double x, y;
10624579Szliu {
10724579Szliu 	static double zero=0, one=1,
10824579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
10924579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
11024579Szliu 	double copysign(),scalb(),logb(),sqrt(),t,r;
11124579Szliu 	int finite(), exp;
11224579Szliu 
11324579Szliu 	if(finite(x))
11424579Szliu 	    if(finite(y))
11524579Szliu 	    {
11624579Szliu 		x=copysign(x,one);
11724579Szliu 		y=copysign(y,one);
11824579Szliu 		if(y > x)
11924579Szliu 		    { t=x; x=y; y=t; }
12024579Szliu 		if(x == zero) return(zero);
12124579Szliu 		if(y == zero) return(x);
12224579Szliu 		exp= logb(x);
12324579Szliu 		if(exp-(int)logb(y) > ibig )
12424579Szliu 			/* raise inexact flag and return |x| */
12524579Szliu 		   { one+small; return(x); }
12624579Szliu 
12724579Szliu 	    /* start computing sqrt(x^2 + y^2) */
12824579Szliu 		r=x-y;
12924579Szliu 		if(r>y) { 	/* x/y > 2 */
13024579Szliu 		    r=x/y;
13124579Szliu 		    r=r+sqrt(one+r*r); }
13224579Szliu 		else {		/* 1 <= x/y <= 2 */
13324579Szliu 		    r/=y; t=r*(r+2.0);
13424579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
13524579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
13624579Szliu 
13724579Szliu 		r=y/r;
13824579Szliu 		return(x+r);
13924579Szliu 
14024579Szliu 	    }
14124579Szliu 
14224579Szliu 	    else if(y==y)   	   /* y is +-INF */
14324579Szliu 		     return(copysign(y,one));
14424579Szliu 	    else
14524579Szliu 		     return(y);	   /* y is NaN and x is finite */
14624579Szliu 
14724579Szliu 	else if(x==x) 		   /* x is +-INF */
14824579Szliu 	         return (copysign(x,one));
14924579Szliu 	else if(finite(y))
15024579Szliu 	         return(x);		   /* x is NaN, y is finite */
15131855Szliu #if !defined(vax)&&!defined(tahoe)
15224579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
15331855Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
15424579Szliu 	else return(copysign(y,one));   /* y is INF */
15524579Szliu }
15624579Szliu 
15731991Szliu /* CABS(Z)
15831991Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
15931991Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
16031991Szliu  * CODED IN C BY K.C. NG, 11/28/84.
16131991Szliu  * REVISED BY K.C. NG, 7/12/85.
16231991Szliu  *
16331991Szliu  * Required kernel function :
16431991Szliu  *	hypot(x,y)
16531991Szliu  *
16631991Szliu  * Method :
16731991Szliu  *	cabs(z) = hypot(x,y) .
16831991Szliu  */
16931991Szliu 
17031991Szliu double
17131991Szliu cabs(z)
17231991Szliu struct { double x, y;} z;
17331991Szliu {
17431991Szliu 	return hypot(z.x,z.y);
17531991Szliu }
17631991Szliu 
17731991Szliu double
17831991Szliu z_abs(z)
17931991Szliu struct { double x,y;} *z;
18031991Szliu {
18131991Szliu 	return hypot(z->x,z->y);
18231991Szliu }
18331991Szliu 
18424579Szliu /* A faster but less accurate version of cabs(x,y) */
18524579Szliu #if 0
18624579Szliu double hypot(x,y)
18724579Szliu double x, y;
18824579Szliu {
18924579Szliu 	static double zero=0, one=1;
19024579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
19124579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
19224579Szliu 	double copysign(),scalb(),logb(),sqrt(),temp;
19324579Szliu 	int finite(), exp;
19424579Szliu 
19524579Szliu 	if(finite(x))
19624579Szliu 	    if(finite(y))
19724579Szliu 	    {
19824579Szliu 		x=copysign(x,one);
19924579Szliu 		y=copysign(y,one);
20024579Szliu 		if(y > x)
20124579Szliu 		    { temp=x; x=y; y=temp; }
20224579Szliu 		if(x == zero) return(zero);
20324579Szliu 		if(y == zero) return(x);
20424579Szliu 		exp= logb(x);
20524579Szliu 		x=scalb(x,-exp);
20624579Szliu 		if(exp-(int)logb(y) > ibig )
20724579Szliu 			/* raise inexact flag and return |x| */
20824579Szliu 		   { one+small; return(scalb(x,exp)); }
20924579Szliu 		else y=scalb(y,-exp);
21024579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
21124579Szliu 	    }
21224579Szliu 
21324579Szliu 	    else if(y==y)   	   /* y is +-INF */
21424579Szliu 		     return(copysign(y,one));
21524579Szliu 	    else
21624579Szliu 		     return(y);	   /* y is NaN and x is finite */
21724579Szliu 
21824579Szliu 	else if(x==x) 		   /* x is +-INF */
21924579Szliu 	         return (copysign(x,one));
22024579Szliu 	else if(finite(y))
22124579Szliu 	         return(x);		   /* x is NaN, y is finite */
22224579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
22324579Szliu 	else return(copysign(y,one));   /* y is INF */
22424579Szliu }
22524579Szliu #endif
226