xref: /netbsd-src/lib/libm/src/e_atan2l.c (revision 3f3b0ded092b378b433be0bc7541910db5a04ce4)
1*3f3b0dedSchristos /*	$NetBSD: e_atan2l.c,v 1.2 2024/01/23 15:45:07 christos Exp $	*/
2cfe182f3Schristos /* @(#)e_atan2.c 1.3 95/01/18 */
3cfe182f3Schristos /* FreeBSD: head/lib/msun/src/e_atan2.c 176451 2008-02-22 02:30:36Z das */
4cfe182f3Schristos /*
5cfe182f3Schristos  * ====================================================
6cfe182f3Schristos  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7cfe182f3Schristos  *
8cfe182f3Schristos  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9cfe182f3Schristos  * Permission to use, copy, modify, and distribute this
10cfe182f3Schristos  * software is freely granted, provided that this notice
11cfe182f3Schristos  * is preserved.
12cfe182f3Schristos  * ====================================================
13cfe182f3Schristos  *
14cfe182f3Schristos  */
15cfe182f3Schristos 
16cfe182f3Schristos #include <sys/cdefs.h>
17*3f3b0dedSchristos __RCSID("$NetBSD: e_atan2l.c,v 1.2 2024/01/23 15:45:07 christos Exp $");
18cfe182f3Schristos 
19cfe182f3Schristos /*
20cfe182f3Schristos  * See comments in e_atan2.c.
21cfe182f3Schristos  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
22cfe182f3Schristos  */
23cfe182f3Schristos 
24cfe182f3Schristos #include "namespace.h"
25cfe182f3Schristos 
26cfe182f3Schristos #ifdef __weak_alias
27cfe182f3Schristos __weak_alias(atan2l, _atan2l)
28cfe182f3Schristos #endif
29cfe182f3Schristos 
30cfe182f3Schristos #include <float.h>
31*3f3b0dedSchristos #include <machine/ieee.h>
32cfe182f3Schristos 
33cfe182f3Schristos #include "math.h"
34cfe182f3Schristos #include "math_private.h"
35cfe182f3Schristos 
36cfe182f3Schristos #ifdef __HAVE_LONG_DOUBLE
37cfe182f3Schristos 
38cfe182f3Schristos #if LDBL_MANT_DIG == 64
39cfe182f3Schristos #include "../ld80/invtrig.h"
40cfe182f3Schristos #elif LDBL_MANT_DIG == 113
41cfe182f3Schristos #include "../ld128/invtrig.h"
42cfe182f3Schristos #else
43cfe182f3Schristos #error "Unsupported long double format"
44cfe182f3Schristos #endif
45cfe182f3Schristos 
46cfe182f3Schristos #ifdef LDBL_IMPLICIT_NBIT
47cfe182f3Schristos #define	LDBL_NBIT	0
48cfe182f3Schristos #endif
49cfe182f3Schristos 
50cfe182f3Schristos static volatile long double
51cfe182f3Schristos tiny  = 1.0e-300;
52cfe182f3Schristos static const long double
53cfe182f3Schristos zero  = 0.0;
54cfe182f3Schristos 
55cfe182f3Schristos #ifdef __i386__
56cfe182f3Schristos /* XXX Work around the fact that gcc truncates long double constants on i386 */
57cfe182f3Schristos static volatile double
58cfe182f3Schristos pi1 =  3.14159265358979311600e+00,	/*  0x1.921fb54442d18p+1  */
59cfe182f3Schristos pi2 =  1.22514845490862001043e-16;	/*  0x1.1a80000000000p-53 */
60cfe182f3Schristos #define	pi	((long double)pi1 + pi2)
61cfe182f3Schristos #else
62cfe182f3Schristos static const long double
63cfe182f3Schristos pi =  3.14159265358979323846264338327950280e+00L;
64cfe182f3Schristos #endif
65cfe182f3Schristos 
66cfe182f3Schristos long double
atan2l(long double y,long double x)67cfe182f3Schristos atan2l(long double y, long double x)
68cfe182f3Schristos {
69cfe182f3Schristos 	union ieee_ext_u ux, uy;
70cfe182f3Schristos 	long double z;
71cfe182f3Schristos 	int32_t k,m;
72cfe182f3Schristos 	int16_t exptx, expsignx, expty, expsigny;
73cfe182f3Schristos 
74cfe182f3Schristos 	uy.extu_ld = y;
75cfe182f3Schristos 	expsigny = GET_EXPSIGN(&uy);
76cfe182f3Schristos 	expty = expsigny & 0x7fff;
77cfe182f3Schristos 	ux.extu_ld = x;
78cfe182f3Schristos 	expsignx = GET_EXPSIGN(&ux);
79cfe182f3Schristos 	exptx = expsignx & 0x7fff;
80cfe182f3Schristos 
81cfe182f3Schristos 	if ((exptx==BIAS+LDBL_MAX_EXP &&
82cfe182f3Schristos 	     ((ux.extu_frach&~LDBL_NBIT)|ux.extu_fracl)!=0) ||	/* x is NaN */
83cfe182f3Schristos 	    (expty==BIAS+LDBL_MAX_EXP &&
84cfe182f3Schristos 	     ((uy.extu_frach&~LDBL_NBIT)|uy.extu_fracl)!=0))	/* y is NaN */
85cfe182f3Schristos 	    return nan_mix(x, y);
86cfe182f3Schristos 	if (expsignx==BIAS && ((ux.extu_frach&~LDBL_NBIT)|ux.extu_fracl)==0)
87cfe182f3Schristos 	    return atanl(y);					/* x=1.0 */
88cfe182f3Schristos 	m = ((expsigny>>15)&1)|((expsignx>>14)&2);	/* 2*sign(x)+sign(y) */
89cfe182f3Schristos 
90cfe182f3Schristos     /* when y = 0 */
91cfe182f3Schristos 	if(expty==0 && ((uy.extu_frach&~LDBL_NBIT)|uy.extu_fracl)==0) {
92cfe182f3Schristos 	    switch(m) {
93cfe182f3Schristos 		case 0:
94cfe182f3Schristos 		case 1: return y; 	/* atan(+-0,+anything)=+-0 */
95cfe182f3Schristos 		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
96cfe182f3Schristos 		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
97cfe182f3Schristos 	    }
98cfe182f3Schristos 	}
99cfe182f3Schristos     /* when x = 0 */
100cfe182f3Schristos 	if(exptx==0 && ((ux.extu_frach&~LDBL_NBIT)|ux.extu_fracl)==0)
101cfe182f3Schristos 	    return (expsigny<0)?  -pio2_hi-tiny: pio2_hi+tiny;
102cfe182f3Schristos 
103cfe182f3Schristos     /* when x is INF */
104cfe182f3Schristos 	if(exptx==BIAS+LDBL_MAX_EXP) {
105cfe182f3Schristos 	    if(expty==BIAS+LDBL_MAX_EXP) {
106cfe182f3Schristos 		switch(m) {
107cfe182f3Schristos 		    case 0: return  pio2_hi*0.5+tiny;/* atan(+INF,+INF) */
108cfe182f3Schristos 		    case 1: return -pio2_hi*0.5-tiny;/* atan(-INF,+INF) */
109cfe182f3Schristos 		    case 2: return  1.5*pio2_hi+tiny;/*atan(+INF,-INF)*/
110cfe182f3Schristos 		    case 3: return -1.5*pio2_hi-tiny;/*atan(-INF,-INF)*/
111cfe182f3Schristos 		}
112cfe182f3Schristos 	    } else {
113cfe182f3Schristos 		switch(m) {
114cfe182f3Schristos 		    case 0: return  zero  ;	/* atan(+...,+INF) */
115cfe182f3Schristos 		    case 1: return -zero  ;	/* atan(-...,+INF) */
116cfe182f3Schristos 		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
117cfe182f3Schristos 		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
118cfe182f3Schristos 		}
119cfe182f3Schristos 	    }
120cfe182f3Schristos 	}
121cfe182f3Schristos     /* when y is INF */
122cfe182f3Schristos 	if(expty==BIAS+LDBL_MAX_EXP)
123cfe182f3Schristos 	    return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
124cfe182f3Schristos 
125cfe182f3Schristos     /* compute y/x */
126cfe182f3Schristos 	k = expty-exptx;
127cfe182f3Schristos 	if(k > LDBL_MANT_DIG+2) {			/* |y/x| huge */
128cfe182f3Schristos 	    z=pio2_hi+pio2_lo;
129cfe182f3Schristos 	    m&=1;
130cfe182f3Schristos 	}
131cfe182f3Schristos 	else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0; 	/* |y/x| tiny, x<0 */
132cfe182f3Schristos 	else z=atanl(fabsl(y/x));		/* safe to do y/x */
133cfe182f3Schristos 	switch (m) {
134cfe182f3Schristos 	    case 0: return       z  ;	/* atan(+,+) */
135cfe182f3Schristos 	    case 1: return      -z  ;	/* atan(-,+) */
136cfe182f3Schristos 	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
137cfe182f3Schristos 	    default: /* case 3 */
138cfe182f3Schristos 	    	    return  (z-pi_lo)-pi;/* atan(-,-) */
139cfe182f3Schristos 	}
140cfe182f3Schristos }
141cfe182f3Schristos 
142cfe182f3Schristos #else
143cfe182f3Schristos 
144cfe182f3Schristos long double
145cfe182f3Schristos atan2l(long double y, long double x)
146cfe182f3Schristos {
147cfe182f3Schristos 	return atan2(y, x);
148cfe182f3Schristos }
149cfe182f3Schristos 
150cfe182f3Schristos #endif
151