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