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