xref: /minix3/lib/libm/noieee_src/n_cabs.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*      $NetBSD: n_cabs.c,v 1.5 2003/08/07 16:44:50 agc Exp $ */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras  * Copyright (c) 1985, 1993
4*2fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras  * are met:
9*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
15*2fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
16*2fe8fb19SBen Gras  *    without specific prior written permission.
17*2fe8fb19SBen Gras  *
18*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*2fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*2fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*2fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*2fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*2fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*2fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*2fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*2fe8fb19SBen Gras  * SUCH DAMAGE.
29*2fe8fb19SBen Gras  */
30*2fe8fb19SBen Gras 
31*2fe8fb19SBen Gras #ifndef lint
32*2fe8fb19SBen Gras static char sccsid[] = "@(#)cabs.c	8.1 (Berkeley) 6/4/93";
33*2fe8fb19SBen Gras #endif /* not lint */
34*2fe8fb19SBen Gras 
35*2fe8fb19SBen Gras /* HYPOT(X,Y)
36*2fe8fb19SBen Gras  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
37*2fe8fb19SBen Gras  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
38*2fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 11/28/84;
39*2fe8fb19SBen Gras  * REVISED BY K.C. NG, 7/12/85.
40*2fe8fb19SBen Gras  *
41*2fe8fb19SBen Gras  * Required system supported functions :
42*2fe8fb19SBen Gras  *	copysign(x,y)
43*2fe8fb19SBen Gras  *	finite(x)
44*2fe8fb19SBen Gras  *	scalb(x,N)
45*2fe8fb19SBen Gras  *	sqrt(x)
46*2fe8fb19SBen Gras  *
47*2fe8fb19SBen Gras  * Method :
48*2fe8fb19SBen Gras  *	1. replace x by |x| and y by |y|, and swap x and
49*2fe8fb19SBen Gras  *	   y if y > x (hence x is never smaller than y).
50*2fe8fb19SBen Gras  *	2. Hypot(x,y) is computed by:
51*2fe8fb19SBen Gras  *	   Case I, x/y > 2
52*2fe8fb19SBen Gras  *
53*2fe8fb19SBen Gras  *				       y
54*2fe8fb19SBen Gras  *		hypot = x + -----------------------------
55*2fe8fb19SBen Gras  *			 		    2
56*2fe8fb19SBen Gras  *			    sqrt ( 1 + [x/y]  )  +  x/y
57*2fe8fb19SBen Gras  *
58*2fe8fb19SBen Gras  *	   Case II, x/y <= 2
59*2fe8fb19SBen Gras  *				                   y
60*2fe8fb19SBen Gras  *		hypot = x + --------------------------------------------------
61*2fe8fb19SBen Gras  *				          		     2
62*2fe8fb19SBen Gras  *				     			[x/y]   -  2
63*2fe8fb19SBen Gras  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
64*2fe8fb19SBen Gras  *			 		    			  2
65*2fe8fb19SBen Gras  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
66*2fe8fb19SBen Gras  *
67*2fe8fb19SBen Gras  *
68*2fe8fb19SBen Gras  *
69*2fe8fb19SBen Gras  * Special cases:
70*2fe8fb19SBen Gras  *	hypot(x,y) is INF if x or y is +INF or -INF; else
71*2fe8fb19SBen Gras  *	hypot(x,y) is NAN if x or y is NAN.
72*2fe8fb19SBen Gras  *
73*2fe8fb19SBen Gras  * Accuracy:
74*2fe8fb19SBen Gras  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
75*2fe8fb19SBen Gras  *	in the last place). See Kahan's "Interval Arithmetic Options in the
76*2fe8fb19SBen Gras  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
77*2fe8fb19SBen Gras  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
78*2fe8fb19SBen Gras  *	code follows in	comments.) In a test run with 500,000 random arguments
79*2fe8fb19SBen Gras  *	on a VAX, the maximum observed error was .959 ulps.
80*2fe8fb19SBen Gras  *
81*2fe8fb19SBen Gras  * Constants:
82*2fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
83*2fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
84*2fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
85*2fe8fb19SBen Gras  * shown.
86*2fe8fb19SBen Gras  */
87*2fe8fb19SBen Gras #define _LIBM_STATIC
88*2fe8fb19SBen Gras #include "mathimpl.h"
89*2fe8fb19SBen Gras 
90*2fe8fb19SBen Gras vc(r2p1hi, 2.4142135623730950345E0   ,8279,411a,ef32,99fc,   2, .9A827999FCEF32)
91*2fe8fb19SBen Gras vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B)
92*2fe8fb19SBen Gras vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
93*2fe8fb19SBen Gras 
94*2fe8fb19SBen Gras ic(r2p1hi, 2.4142135623730949234E0   ,   1, 1.3504F333F9DE6)
95*2fe8fb19SBen Gras ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5)
96*2fe8fb19SBen Gras ic(sqrt2,  1.4142135623730951455E0   ,   0, 1.6A09E667F3BCD)
97*2fe8fb19SBen Gras 
98*2fe8fb19SBen Gras #ifdef vccast
99*2fe8fb19SBen Gras #define	r2p1hi	vccast(r2p1hi)
100*2fe8fb19SBen Gras #define	r2p1lo	vccast(r2p1lo)
101*2fe8fb19SBen Gras #define	sqrt2	vccast(sqrt2)
102*2fe8fb19SBen Gras #endif
103*2fe8fb19SBen Gras 
104*2fe8fb19SBen Gras double
hypot(double x,double y)105*2fe8fb19SBen Gras hypot(double x, double y)
106*2fe8fb19SBen Gras {
107*2fe8fb19SBen Gras 	static const double zero=0, one=1,
108*2fe8fb19SBen Gras 		      small=1.0E-18;	/* fl(1+small)==1 */
109*2fe8fb19SBen Gras 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
110*2fe8fb19SBen Gras 	double t,r;
111*2fe8fb19SBen Gras 	int exp;
112*2fe8fb19SBen Gras 
113*2fe8fb19SBen Gras 	if(finite(x))
114*2fe8fb19SBen Gras 	    if(finite(y))
115*2fe8fb19SBen Gras 	    {
116*2fe8fb19SBen Gras 		x=copysign(x,one);
117*2fe8fb19SBen Gras 		y=copysign(y,one);
118*2fe8fb19SBen Gras 		if(y > x)
119*2fe8fb19SBen Gras 		    { t=x; x=y; y=t; }
120*2fe8fb19SBen Gras 		if(x == zero) return(zero);
121*2fe8fb19SBen Gras 		if(y == zero) return(x);
122*2fe8fb19SBen Gras 		exp= logb(x);
123*2fe8fb19SBen Gras 		if(exp-(int)logb(y) > ibig )
124*2fe8fb19SBen Gras 			/* raise inexact flag and return |x| */
125*2fe8fb19SBen Gras 		   { one+small; return(x); }
126*2fe8fb19SBen Gras 
127*2fe8fb19SBen Gras 	    /* start computing sqrt(x^2 + y^2) */
128*2fe8fb19SBen Gras 		r=x-y;
129*2fe8fb19SBen Gras 		if(r>y) { 	/* x/y > 2 */
130*2fe8fb19SBen Gras 		    r=x/y;
131*2fe8fb19SBen Gras 		    r=r+sqrt(one+r*r); }
132*2fe8fb19SBen Gras 		else {		/* 1 <= x/y <= 2 */
133*2fe8fb19SBen Gras 		    r/=y; t=r*(r+2.0);
134*2fe8fb19SBen Gras 		    r+=t/(sqrt2+sqrt(2.0+t));
135*2fe8fb19SBen Gras 		    r+=r2p1lo; r+=r2p1hi; }
136*2fe8fb19SBen Gras 
137*2fe8fb19SBen Gras 		r=y/r;
138*2fe8fb19SBen Gras 		return(x+r);
139*2fe8fb19SBen Gras 
140*2fe8fb19SBen Gras 	    }
141*2fe8fb19SBen Gras 
142*2fe8fb19SBen Gras 	    else if(y==y)   	   /* y is +-INF */
143*2fe8fb19SBen Gras 		     return(copysign(y,one));
144*2fe8fb19SBen Gras 	    else
145*2fe8fb19SBen Gras 		     return(y);	   /* y is NaN and x is finite */
146*2fe8fb19SBen Gras 
147*2fe8fb19SBen Gras 	else if(x==x) 		   /* x is +-INF */
148*2fe8fb19SBen Gras 	         return (copysign(x,one));
149*2fe8fb19SBen Gras 	else if(finite(y))
150*2fe8fb19SBen Gras 	         return(x);		   /* x is NaN, y is finite */
151*2fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
152*2fe8fb19SBen Gras 	else if(y!=y) return(y);  /* x and y is NaN */
153*2fe8fb19SBen Gras #endif	/* !defined(__vax__)&&!defined(tahoe) */
154*2fe8fb19SBen Gras 	else return(copysign(y,one));   /* y is INF */
155*2fe8fb19SBen Gras }
156*2fe8fb19SBen Gras 
157*2fe8fb19SBen Gras /* CABS(Z)
158*2fe8fb19SBen Gras  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
159*2fe8fb19SBen Gras  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
160*2fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 11/28/84.
161*2fe8fb19SBen Gras  * REVISED BY K.C. NG, 7/12/85.
162*2fe8fb19SBen Gras  *
163*2fe8fb19SBen Gras  * Required kernel function :
164*2fe8fb19SBen Gras  *	hypot(x,y)
165*2fe8fb19SBen Gras  *
166*2fe8fb19SBen Gras  * Method :
167*2fe8fb19SBen Gras  *	cabs(z) = hypot(x,y) .
168*2fe8fb19SBen Gras  */
169*2fe8fb19SBen Gras 
170*2fe8fb19SBen Gras struct complex { double x, y; };
171*2fe8fb19SBen Gras 
172*2fe8fb19SBen Gras double
cabs(z)173*2fe8fb19SBen Gras cabs(z)
174*2fe8fb19SBen Gras struct complex z;
175*2fe8fb19SBen Gras {
176*2fe8fb19SBen Gras 	return hypot(z.x,z.y);
177*2fe8fb19SBen Gras }
178*2fe8fb19SBen Gras 
179*2fe8fb19SBen Gras double
z_abs(z)180*2fe8fb19SBen Gras z_abs(z)
181*2fe8fb19SBen Gras struct complex *z;
182*2fe8fb19SBen Gras {
183*2fe8fb19SBen Gras 	return hypot(z->x,z->y);
184*2fe8fb19SBen Gras }
185*2fe8fb19SBen Gras 
186*2fe8fb19SBen Gras /* A faster but less accurate version of cabs(x,y) */
187*2fe8fb19SBen Gras #if 0
188*2fe8fb19SBen Gras double hypot(x,y)
189*2fe8fb19SBen Gras double x, y;
190*2fe8fb19SBen Gras {
191*2fe8fb19SBen Gras 	static const double zero=0, one=1;
192*2fe8fb19SBen Gras 		      small=1.0E-18;	/* fl(1+small)==1 */
193*2fe8fb19SBen Gras 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
194*2fe8fb19SBen Gras 	double temp;
195*2fe8fb19SBen Gras 	int exp;
196*2fe8fb19SBen Gras 
197*2fe8fb19SBen Gras 	if(finite(x))
198*2fe8fb19SBen Gras 	    if(finite(y))
199*2fe8fb19SBen Gras 	    {
200*2fe8fb19SBen Gras 		x=copysign(x,one);
201*2fe8fb19SBen Gras 		y=copysign(y,one);
202*2fe8fb19SBen Gras 		if(y > x)
203*2fe8fb19SBen Gras 		    { temp=x; x=y; y=temp; }
204*2fe8fb19SBen Gras 		if(x == zero) return(zero);
205*2fe8fb19SBen Gras 		if(y == zero) return(x);
206*2fe8fb19SBen Gras 		exp= logb(x);
207*2fe8fb19SBen Gras 		x=scalb(x,-exp);
208*2fe8fb19SBen Gras 		if(exp-(int)logb(y) > ibig )
209*2fe8fb19SBen Gras 			/* raise inexact flag and return |x| */
210*2fe8fb19SBen Gras 		   { one+small; return(scalb(x,exp)); }
211*2fe8fb19SBen Gras 		else y=scalb(y,-exp);
212*2fe8fb19SBen Gras 		return(scalb(sqrt(x*x+y*y),exp));
213*2fe8fb19SBen Gras 	    }
214*2fe8fb19SBen Gras 
215*2fe8fb19SBen Gras 	    else if(y==y)   	   /* y is +-INF */
216*2fe8fb19SBen Gras 		     return(copysign(y,one));
217*2fe8fb19SBen Gras 	    else
218*2fe8fb19SBen Gras 		     return(y);	   /* y is NaN and x is finite */
219*2fe8fb19SBen Gras 
220*2fe8fb19SBen Gras 	else if(x==x) 		   /* x is +-INF */
221*2fe8fb19SBen Gras 	         return (copysign(x,one));
222*2fe8fb19SBen Gras 	else if(finite(y))
223*2fe8fb19SBen Gras 	         return(x);		   /* x is NaN, y is finite */
224*2fe8fb19SBen Gras 	else if(y!=y) return(y);  	/* x and y is NaN */
225*2fe8fb19SBen Gras 	else return(copysign(y,one));   /* y is INF */
226*2fe8fb19SBen Gras }
227*2fe8fb19SBen Gras #endif
228