xref: /netbsd-src/lib/libm/noieee_src/n_atan2.c (revision 3f7199ab7e77eaa6d6792460369f88d1f31d7939)
1*3f7199abSmartin /*      $NetBSD: n_atan2.c,v 1.7 2014/10/10 20:58:09 martin Exp $        */
225d43745Sragge /*
325d43745Sragge  * Copyright (c) 1985, 1993
425d43745Sragge  *	The Regents of the University of California.  All rights reserved.
525d43745Sragge  *
625d43745Sragge  * Redistribution and use in source and binary forms, with or without
725d43745Sragge  * modification, are permitted provided that the following conditions
825d43745Sragge  * are met:
925d43745Sragge  * 1. Redistributions of source code must retain the above copyright
1025d43745Sragge  *    notice, this list of conditions and the following disclaimer.
1125d43745Sragge  * 2. Redistributions in binary form must reproduce the above copyright
1225d43745Sragge  *    notice, this list of conditions and the following disclaimer in the
1325d43745Sragge  *    documentation and/or other materials provided with the distribution.
14eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1525d43745Sragge  *    may be used to endorse or promote products derived from this software
1625d43745Sragge  *    without specific prior written permission.
1725d43745Sragge  *
1825d43745Sragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1925d43745Sragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2025d43745Sragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2125d43745Sragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2225d43745Sragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2325d43745Sragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2425d43745Sragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2525d43745Sragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2625d43745Sragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2725d43745Sragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2825d43745Sragge  * SUCH DAMAGE.
2925d43745Sragge  */
3025d43745Sragge 
3125d43745Sragge #ifndef lint
3225d43745Sragge static char sccsid[] = "@(#)atan2.c	8.1 (Berkeley) 6/4/93";
3325d43745Sragge #endif /* not lint */
3425d43745Sragge 
3525d43745Sragge /* ATAN2(Y,X)
3625d43745Sragge  * RETURN ARG (X+iY)
3725d43745Sragge  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
3825d43745Sragge  * CODED IN C BY K.C. NG, 1/8/85;
3925d43745Sragge  * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
4025d43745Sragge  *
4125d43745Sragge  * Required system supported functions :
4225d43745Sragge  *	copysign(x,y)
4325d43745Sragge  *	scalb(x,y)
4425d43745Sragge  *	logb(x)
4525d43745Sragge  *
4625d43745Sragge  * Method :
4725d43745Sragge  *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
4825d43745Sragge  *	2. Reduce x to positive by (if x and y are unexceptional):
4925d43745Sragge  *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
5025d43745Sragge  *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
5125d43745Sragge  *	3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
5225d43745Sragge  *	   is further reduced to one of the following intervals and the
5325d43745Sragge  *	   arctangent of y/x is evaluated by the corresponding formula:
5425d43745Sragge  *
5525d43745Sragge  *         [0,7/16]	   atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
5625d43745Sragge  *	   [7/16,11/16]    atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
5725d43745Sragge  *	   [11/16.19/16]   atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
5825d43745Sragge  *	   [19/16,39/16]   atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
5925d43745Sragge  *	   [39/16,INF]     atan(y/x) = atan(INF) + atan( -x/y )
6025d43745Sragge  *
6125d43745Sragge  * Special cases:
6225d43745Sragge  * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
6325d43745Sragge  *
6425d43745Sragge  *	ARG( NAN , (anything) ) is NaN;
6525d43745Sragge  *	ARG( (anything), NaN ) is NaN;
6625d43745Sragge  *	ARG(+(anything but NaN), +-0) is +-0  ;
6725d43745Sragge  *	ARG(-(anything but NaN), +-0) is +-PI ;
6825d43745Sragge  *	ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
6925d43745Sragge  *	ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
7025d43745Sragge  *	ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
7125d43745Sragge  *	ARG( +INF,+-INF ) is +-PI/4 ;
7225d43745Sragge  *	ARG( -INF,+-INF ) is +-3PI/4;
7325d43745Sragge  *	ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
7425d43745Sragge  *
7525d43745Sragge  * Accuracy:
7625d43745Sragge  *	atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
7725d43745Sragge  *	where
7825d43745Sragge  *
7925d43745Sragge  *	in decimal:
8025d43745Sragge  *		pi = 3.141592653589793 23846264338327 .....
8125d43745Sragge  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
8225d43745Sragge  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
8325d43745Sragge  *
8425d43745Sragge  *	in hexadecimal:
8525d43745Sragge  *		pi = 3.243F6A8885A308D313198A2E....
8625d43745Sragge  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
8725d43745Sragge  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
8825d43745Sragge  *
8925d43745Sragge  *	In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
9025d43745Sragge  *	VAX, the maximum observed error was 1.41 ulps (units of the last place)
9125d43745Sragge  *	compared with (PI/pi)*(the exact ARG(x+iy)).
9225d43745Sragge  *
9325d43745Sragge  * Note:
9425d43745Sragge  *	We use machine PI (the true pi rounded) in place of the actual
9525d43745Sragge  *	value of pi for all the trig and inverse trig functions. In general,
9625d43745Sragge  *	if trig is one of sin, cos, tan, then computed trig(y) returns the
9725d43745Sragge  *	exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
9825d43745Sragge  *	returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
9925d43745Sragge  *	trig functions have period PI, and trig(arctrig(x)) returns x for
10025d43745Sragge  *	all critical values x.
10125d43745Sragge  *
10225d43745Sragge  * Constants:
10325d43745Sragge  * The hexadecimal values are the intended ones for the following constants.
10425d43745Sragge  * The decimal values may be used, provided that the compiler will convert
10525d43745Sragge  * from decimal to binary accurately enough to produce the hexadecimal values
10625d43745Sragge  * shown.
10725d43745Sragge  */
10825d43745Sragge 
109f1009ecdSmatt #define _LIBM_STATIC
11025d43745Sragge #include "mathimpl.h"
11125d43745Sragge 
11225d43745Sragge vc(athfhi, 4.6364760900080611433E-1  ,6338,3fed,da7b,2b0d,  -1, .ED63382B0DDA7B)
11325d43745Sragge vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
11425d43745Sragge vc(PIo4,   7.8539816339744830676E-1  ,0fda,4049,68c2,a221,   0, .C90FDAA22168C2)
11525d43745Sragge vc(at1fhi, 9.8279372324732906796E-1  ,985e,407b,b4d9,940f,   0, .FB985E940FB4D9)
11625d43745Sragge vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
11725d43745Sragge vc(PIo2,   1.5707963267948966135E0   ,0fda,40c9,68c2,a221,   1, .C90FDAA22168C2)
11825d43745Sragge vc(PI,     3.1415926535897932270E0   ,0fda,4149,68c2,a221,   2, .C90FDAA22168C2)
11925d43745Sragge vc(a1,     3.3333333333333473730E-1  ,aaaa,3faa,ab75,aaaa,  -1, .AAAAAAAAAAAB75)
12025d43745Sragge vc(a2,    -2.0000000000017730678E-1  ,cccc,bf4c,946e,cccd,  -2,-.CCCCCCCCCD946E)
12125d43745Sragge vc(a3,     1.4285714286694640301E-1  ,4924,3f12,4262,9274,  -2, .92492492744262)
12225d43745Sragge vc(a4,    -1.1111111135032672795E-1  ,8e38,bee3,6292,ebc6,  -3,-.E38E38EBC66292)
12325d43745Sragge vc(a5,     9.0909091380563043783E-2  ,2e8b,3eba,d70c,b31b,  -3, .BA2E8BB31BD70C)
12425d43745Sragge vc(a6,    -7.6922954286089459397E-2  ,89c8,be9d,7f18,27c3,  -3,-.9D89C827C37F18)
12525d43745Sragge vc(a7,     6.6663180891693915586E-2  ,86b4,3e88,9e58,ae37,  -3, .8886B4AE379E58)
12625d43745Sragge vc(a8,    -5.8772703698290408927E-2  ,bba5,be70,a942,8481,  -4,-.F0BBA58481A942)
12725d43745Sragge vc(a9,     5.2170707402812969804E-2  ,b0f3,3e55,13ab,a1ab,  -4, .D5B0F3A1AB13AB)
12825d43745Sragge vc(a10,   -4.4895863157820361210E-2  ,e4b9,be37,048f,7fd1,  -4,-.B7E4B97FD1048F)
12925d43745Sragge vc(a11,    3.3006147437343875094E-2  ,3174,3e07,2d87,3cf7,  -4, .8731743CF72D87)
13025d43745Sragge vc(a12,   -1.4614844866464185439E-2  ,731a,bd6f,76d9,2f34,  -6,-.EF731A2F3476D9)
13125d43745Sragge 
13225d43745Sragge ic(athfhi, 4.6364760900080609352E-1  ,  -2,  1.DAC670561BB4F)
13325d43745Sragge ic(athflo, 4.6249969567426939759E-18 , -58,  1.5543B8F253271)
13425d43745Sragge ic(PIo4,   7.8539816339744827900E-1  ,  -1,  1.921FB54442D18)
13525d43745Sragge ic(at1fhi, 9.8279372324732905408E-1  ,  -1,  1.F730BD281F69B)
13625d43745Sragge ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
13725d43745Sragge ic(PIo2,   1.5707963267948965580E0   ,   0,  1.921FB54442D18)
13825d43745Sragge ic(PI,     3.1415926535897931160E0   ,   1,  1.921FB54442D18)
13925d43745Sragge ic(a1,     3.3333333333333942106E-1  ,  -2,  1.55555555555C3)
14025d43745Sragge ic(a2,    -1.9999999999979536924E-1  ,  -3, -1.9999999997CCD)
14125d43745Sragge ic(a3,     1.4285714278004377209E-1  ,  -3,  1.24924921EC1D7)
14225d43745Sragge ic(a4,    -1.1111110579344973814E-1  ,  -4, -1.C71C7059AF280)
14325d43745Sragge ic(a5,     9.0908906105474668324E-2  ,  -4,  1.745CE5AA35DB2)
14425d43745Sragge ic(a6,    -7.6919217767468239799E-2  ,  -4, -1.3B0FA54BEC400)
14525d43745Sragge ic(a7,     6.6614695906082474486E-2  ,  -4,  1.10DA924597FFF)
14625d43745Sragge ic(a8,    -5.8358371008508623523E-2  ,  -5, -1.DE125FDDBD793)
14725d43745Sragge ic(a9,     4.9850617156082015213E-2  ,  -5,  1.9860524BDD807)
14825d43745Sragge ic(a10,   -3.6700606902093604877E-2  ,  -5, -1.2CA6C04C6937A)
14925d43745Sragge ic(a11,    1.6438029044759730479E-2  ,  -6,  1.0D52174A1BB54)
15025d43745Sragge 
15125d43745Sragge #ifdef vccast
15225d43745Sragge #define	athfhi	vccast(athfhi)
15325d43745Sragge #define	athflo	vccast(athflo)
15425d43745Sragge #define	PIo4	vccast(PIo4)
15525d43745Sragge #define	at1fhi	vccast(at1fhi)
15625d43745Sragge #define	at1flo	vccast(at1flo)
15725d43745Sragge #define	PIo2	vccast(PIo2)
15825d43745Sragge #define	PI	vccast(PI)
15925d43745Sragge #define	a1	vccast(a1)
16025d43745Sragge #define	a2	vccast(a2)
16125d43745Sragge #define	a3	vccast(a3)
16225d43745Sragge #define	a4	vccast(a4)
16325d43745Sragge #define	a5	vccast(a5)
16425d43745Sragge #define	a6	vccast(a6)
16525d43745Sragge #define	a7	vccast(a7)
16625d43745Sragge #define	a8	vccast(a8)
16725d43745Sragge #define	a9	vccast(a9)
16825d43745Sragge #define	a10	vccast(a10)
16925d43745Sragge #define	a11	vccast(a11)
17025d43745Sragge #define	a12	vccast(a12)
17125d43745Sragge #endif
17225d43745Sragge 
173*3f7199abSmartin #ifdef __weak_alias
174*3f7199abSmartin __weak_alias(_atan2l, atan2);
175*3f7199abSmartin #endif
176*3f7199abSmartin 
177f1009ecdSmatt double
atan2(double y,double x)178f1009ecdSmatt atan2(double y, double x)
17925d43745Sragge {
18025d43745Sragge 	static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
18125d43745Sragge 	double t,z,signy,signx,hi,lo;
18225d43745Sragge 	int k,m;
18325d43745Sragge 
184c34b7ca1Smatt #if !defined(__vax__)&&!defined(tahoe)
18525d43745Sragge     /* if x or y is NAN */
18625d43745Sragge 	if(x!=x) return(x); if(y!=y) return(y);
187c34b7ca1Smatt #endif	/* !defined(__vax__)&&!defined(tahoe) */
18825d43745Sragge 
18925d43745Sragge     /* copy down the sign of y and x */
19025d43745Sragge 	signy = copysign(one,y) ;
19125d43745Sragge 	signx = copysign(one,x) ;
19225d43745Sragge 
19325d43745Sragge     /* if x is 1.0, goto begin */
19425d43745Sragge 	if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
19525d43745Sragge 
19625d43745Sragge     /* when y = 0 */
19725d43745Sragge 	if(y==zero) return((signx==one)?y:copysign(PI,signy));
19825d43745Sragge 
19925d43745Sragge     /* when x = 0 */
20025d43745Sragge 	if(x==zero) return(copysign(PIo2,signy));
20125d43745Sragge 
20225d43745Sragge     /* when x is INF */
20325d43745Sragge 	if(!finite(x))
20425d43745Sragge 	    if(!finite(y))
20525d43745Sragge 		return(copysign((signx==one)?PIo4:3*PIo4,signy));
20625d43745Sragge 	    else
20725d43745Sragge 		return(copysign((signx==one)?zero:PI,signy));
20825d43745Sragge 
20925d43745Sragge     /* when y is INF */
21025d43745Sragge 	if(!finite(y)) return(copysign(PIo2,signy));
21125d43745Sragge 
21225d43745Sragge     /* compute y/x */
21325d43745Sragge 	x=copysign(x,one);
21425d43745Sragge 	y=copysign(y,one);
21525d43745Sragge 	if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
21625d43745Sragge 	    else if(m < -80 ) t=y/x;
21725d43745Sragge 	    else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
21825d43745Sragge 
21925d43745Sragge     /* begin argument reduction */
22025d43745Sragge begin:
22125d43745Sragge 	if (t < 2.4375) {
22225d43745Sragge 
22325d43745Sragge 	/* truncate 4(t+1/16) to integer for branching */
22425d43745Sragge 	    k = 4 * (t+0.0625);
22525d43745Sragge 	    switch (k) {
22625d43745Sragge 
22725d43745Sragge 	    /* t is in [0,7/16] */
22825d43745Sragge 	    case 0:
22925d43745Sragge 	    case 1:
23025d43745Sragge 		if (t < small)
23125d43745Sragge 		    { big + small ;  /* raise inexact flag */
23225d43745Sragge 		      return (copysign((signx>zero)?t:PI-t,signy)); }
23325d43745Sragge 
23425d43745Sragge 		hi = zero;  lo = zero;  break;
23525d43745Sragge 
23625d43745Sragge 	    /* t is in [7/16,11/16] */
23725d43745Sragge 	    case 2:
23825d43745Sragge 		hi = athfhi; lo = athflo;
23925d43745Sragge 		z = x+x;
24025d43745Sragge 		t = ( (y+y) - x ) / ( z +  y ); break;
24125d43745Sragge 
24225d43745Sragge 	    /* t is in [11/16,19/16] */
24325d43745Sragge 	    case 3:
24425d43745Sragge 	    case 4:
24525d43745Sragge 		hi = PIo4; lo = zero;
24625d43745Sragge 		t = ( y - x ) / ( x + y ); break;
24725d43745Sragge 
24825d43745Sragge 	    /* t is in [19/16,39/16] */
24925d43745Sragge 	    default:
25025d43745Sragge 		hi = at1fhi; lo = at1flo;
25125d43745Sragge 		z = y-x; y=y+y+y; t = x+x;
25225d43745Sragge 		t = ( (z+z)-x ) / ( t + y ); break;
25325d43745Sragge 	    }
25425d43745Sragge 	}
25525d43745Sragge 	/* end of if (t < 2.4375) */
25625d43745Sragge 
25725d43745Sragge 	else
25825d43745Sragge 	{
25925d43745Sragge 	    hi = PIo2; lo = zero;
26025d43745Sragge 
26125d43745Sragge 	    /* t is in [2.4375, big] */
26225d43745Sragge 	    if (t <= big)  t = - x / y;
26325d43745Sragge 
26425d43745Sragge 	    /* t is in [big, INF] */
26525d43745Sragge 	    else
26625d43745Sragge 	      { big+small;	/* raise inexact flag */
26725d43745Sragge 		t = zero; }
26825d43745Sragge 	}
26925d43745Sragge     /* end of argument reduction */
27025d43745Sragge 
27125d43745Sragge     /* compute atan(t) for t in [-.4375, .4375] */
27225d43745Sragge 	z = t*t;
2734380475dSragge #if defined(__vax__)||defined(tahoe)
27425d43745Sragge 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
27525d43745Sragge 			z*(a9+z*(a10+z*(a11+z*a12))))))))))));
2764380475dSragge #else	/* defined(__vax__)||defined(tahoe) */
27725d43745Sragge 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
27825d43745Sragge 			z*(a9+z*(a10+z*a11)))))))))));
2794380475dSragge #endif	/* defined(__vax__)||defined(tahoe) */
28025d43745Sragge 	z = lo - z; z += t; z += hi;
28125d43745Sragge 
28225d43745Sragge 	return(copysign((signx>zero)?z:PI-z,signy));
28325d43745Sragge }
284