xref: /csrg-svn/lib/libm/ieee/cabs.c (revision 24579)
1*24579Szliu /*
2*24579Szliu  * Copyright (c) 1985 Regents of the University of California.
3*24579Szliu  *
4*24579Szliu  * Use and reproduction of this software are granted  in  accordance  with
5*24579Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
6*24579Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
7*24579Szliu  * source, and inclusion of this notice) with the additional understanding
8*24579Szliu  * that  all  recipients  should regard themselves as participants  in  an
9*24579Szliu  * ongoing  research  project and hence should  feel  obligated  to report
10*24579Szliu  * their  experiences (good or bad) with these elementary function  codes,
11*24579Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12*24579Szliu  */
13*24579Szliu 
14*24579Szliu #ifndef lint
15*24579Szliu static char sccsid[] = "@(#)cabs.c	1.1 (ELEFUNT) 09/06/85";
16*24579Szliu #endif not lint
17*24579Szliu 
18*24579Szliu /* CABS(Z)
19*24579Szliu  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
20*24579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
21*24579Szliu  * CODED IN C BY K.C. NG, 11/28/84.
22*24579Szliu  * REVISED BY K.C. NG, 7/12/85.
23*24579Szliu  *
24*24579Szliu  * Required kernel function :
25*24579Szliu  *	hypot(x,y)
26*24579Szliu  *
27*24579Szliu  * Method :
28*24579Szliu  *	cabs(z) = hypot(x,y) .
29*24579Szliu  */
30*24579Szliu 
31*24579Szliu double cabs(z)
32*24579Szliu struct { double x, y;} z;
33*24579Szliu {
34*24579Szliu 	double hypot();
35*24579Szliu 	return(hypot(z.x,z.y));
36*24579Szliu }
37*24579Szliu 
38*24579Szliu 
39*24579Szliu /* HYPOT(X,Y)
40*24579Szliu  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
41*24579Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
42*24579Szliu  * CODED IN C BY K.C. NG, 11/28/84;
43*24579Szliu  * REVISED BY K.C. NG, 7/12/85.
44*24579Szliu  *
45*24579Szliu  * Required system supported functions :
46*24579Szliu  *	copysign(x,y)
47*24579Szliu  *	finite(x)
48*24579Szliu  *	scalb(x,N)
49*24579Szliu  *	sqrt(x)
50*24579Szliu  *
51*24579Szliu  * Method :
52*24579Szliu  *	1. replace x by |x| and y by |y|, and swap x and
53*24579Szliu  *	   y if y > x (hence x is never smaller than y).
54*24579Szliu  *	2. Hypot(x,y) is computed by:
55*24579Szliu  *	   Case I, x/y > 2
56*24579Szliu  *
57*24579Szliu  *				       y
58*24579Szliu  *		hypot = x + -----------------------------
59*24579Szliu  *			 		    2
60*24579Szliu  *			    sqrt ( 1 + [x/y]  )  +  x/y
61*24579Szliu  *
62*24579Szliu  *	   Case II, x/y <= 2
63*24579Szliu  *				                   y
64*24579Szliu  *		hypot = x + --------------------------------------------------
65*24579Szliu  *				          		     2
66*24579Szliu  *				     			[x/y]   -  2
67*24579Szliu  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
68*24579Szliu  *			 		    			  2
69*24579Szliu  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
70*24579Szliu  *
71*24579Szliu  *
72*24579Szliu  *
73*24579Szliu  * Special cases:
74*24579Szliu  *	hypot(x,y) is INF if x or y is +INF or -INF; else
75*24579Szliu  *	hypot(x,y) is NAN if x or y is NAN.
76*24579Szliu  *
77*24579Szliu  * Accuracy:
78*24579Szliu  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
79*24579Szliu  *	in the last place). See Kahan's "Interval Arithmetic Options in the
80*24579Szliu  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
81*24579Szliu  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
82*24579Szliu  *	code follows in	comments.) In a test run with 500,000 random arguments
83*24579Szliu  *	on a VAX, the maximum observed error was .959 ulps.
84*24579Szliu  *
85*24579Szliu  * Constants:
86*24579Szliu  * The hexadecimal values are the intended ones for the following constants.
87*24579Szliu  * The decimal values may be used, provided that the compiler will convert
88*24579Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
89*24579Szliu  * shown.
90*24579Szliu  */
91*24579Szliu 
92*24579Szliu #ifdef VAX	/* VAX D format */
93*24579Szliu /* static double */
94*24579Szliu /* r2p1hi =  2.4142135623730950345E0     , Hex  2^  2   *  .9A827999FCEF32 */
95*24579Szliu /* r2p1lo =  1.4349369327986523769E-17   , Hex  2^-55   *  .84597D89B3754B */
96*24579Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
97*24579Szliu static long    r2p1hix[] = { 0x8279411a, 0xef3299fc};
98*24579Szliu static long    r2p1lox[] = { 0x597d2484, 0x754b89b3};
99*24579Szliu static long     sqrt2x[] = { 0x04f340b5, 0xde6533f9};
100*24579Szliu #define   r2p1hi    (*(double*)r2p1hix)
101*24579Szliu #define   r2p1lo    (*(double*)r2p1lox)
102*24579Szliu #define    sqrt2    (*(double*)sqrt2x)
103*24579Szliu #else		/* IEEE double format */
104*24579Szliu static double
105*24579Szliu r2p1hi =  2.4142135623730949234E0     , /*Hex  2^1     *  1.3504F333F9DE6 */
106*24579Szliu r2p1lo =  1.2537167179050217666E-16   , /*Hex  2^-53   *  1.21165F626CDD5 */
107*24579Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
108*24579Szliu #endif
109*24579Szliu 
110*24579Szliu double hypot(x,y)
111*24579Szliu double x, y;
112*24579Szliu {
113*24579Szliu 	static double zero=0, one=1,
114*24579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
115*24579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
116*24579Szliu 	double copysign(),scalb(),logb(),sqrt(),t,r;
117*24579Szliu 	int finite(), exp;
118*24579Szliu 
119*24579Szliu 	if(finite(x))
120*24579Szliu 	    if(finite(y))
121*24579Szliu 	    {
122*24579Szliu 		x=copysign(x,one);
123*24579Szliu 		y=copysign(y,one);
124*24579Szliu 		if(y > x)
125*24579Szliu 		    { t=x; x=y; y=t; }
126*24579Szliu 		if(x == zero) return(zero);
127*24579Szliu 		if(y == zero) return(x);
128*24579Szliu 		exp= logb(x);
129*24579Szliu 		if(exp-(int)logb(y) > ibig )
130*24579Szliu 			/* raise inexact flag and return |x| */
131*24579Szliu 		   { one+small; return(x); }
132*24579Szliu 
133*24579Szliu 	    /* start computing sqrt(x^2 + y^2) */
134*24579Szliu 		r=x-y;
135*24579Szliu 		if(r>y) { 	/* x/y > 2 */
136*24579Szliu 		    r=x/y;
137*24579Szliu 		    r=r+sqrt(one+r*r); }
138*24579Szliu 		else {		/* 1 <= x/y <= 2 */
139*24579Szliu 		    r/=y; t=r*(r+2.0);
140*24579Szliu 		    r+=t/(sqrt2+sqrt(2.0+t));
141*24579Szliu 		    r+=r2p1lo; r+=r2p1hi; }
142*24579Szliu 
143*24579Szliu 		r=y/r;
144*24579Szliu 		return(x+r);
145*24579Szliu 
146*24579Szliu 	    }
147*24579Szliu 
148*24579Szliu 	    else if(y==y)   	   /* y is +-INF */
149*24579Szliu 		     return(copysign(y,one));
150*24579Szliu 	    else
151*24579Szliu 		     return(y);	   /* y is NaN and x is finite */
152*24579Szliu 
153*24579Szliu 	else if(x==x) 		   /* x is +-INF */
154*24579Szliu 	         return (copysign(x,one));
155*24579Szliu 	else if(finite(y))
156*24579Szliu 	         return(x);		   /* x is NaN, y is finite */
157*24579Szliu 	else if(y!=y) return(y);  /* x and y is NaN */
158*24579Szliu 	else return(copysign(y,one));   /* y is INF */
159*24579Szliu }
160*24579Szliu 
161*24579Szliu /* A faster but less accurate version of cabs(x,y) */
162*24579Szliu #if 0
163*24579Szliu double hypot(x,y)
164*24579Szliu double x, y;
165*24579Szliu {
166*24579Szliu 	static double zero=0, one=1;
167*24579Szliu 		      small=1.0E-18;	/* fl(1+small)==1 */
168*24579Szliu 	static ibig=30;	/* fl(1+2**(2*ibig))==1 */
169*24579Szliu 	double copysign(),scalb(),logb(),sqrt(),temp;
170*24579Szliu 	int finite(), exp;
171*24579Szliu 
172*24579Szliu 	if(finite(x))
173*24579Szliu 	    if(finite(y))
174*24579Szliu 	    {
175*24579Szliu 		x=copysign(x,one);
176*24579Szliu 		y=copysign(y,one);
177*24579Szliu 		if(y > x)
178*24579Szliu 		    { temp=x; x=y; y=temp; }
179*24579Szliu 		if(x == zero) return(zero);
180*24579Szliu 		if(y == zero) return(x);
181*24579Szliu 		exp= logb(x);
182*24579Szliu 		x=scalb(x,-exp);
183*24579Szliu 		if(exp-(int)logb(y) > ibig )
184*24579Szliu 			/* raise inexact flag and return |x| */
185*24579Szliu 		   { one+small; return(scalb(x,exp)); }
186*24579Szliu 		else y=scalb(y,-exp);
187*24579Szliu 		return(scalb(sqrt(x*x+y*y),exp));
188*24579Szliu 	    }
189*24579Szliu 
190*24579Szliu 	    else if(y==y)   	   /* y is +-INF */
191*24579Szliu 		     return(copysign(y,one));
192*24579Szliu 	    else
193*24579Szliu 		     return(y);	   /* y is NaN and x is finite */
194*24579Szliu 
195*24579Szliu 	else if(x==x) 		   /* x is +-INF */
196*24579Szliu 	         return (copysign(x,one));
197*24579Szliu 	else if(finite(y))
198*24579Szliu 	         return(x);		   /* x is NaN, y is finite */
199*24579Szliu 	else if(y!=y) return(y);  	/* x and y is NaN */
200*24579Szliu 	else return(copysign(y,one));   /* y is INF */
201*24579Szliu }
202*24579Szliu #endif
203