e86dbffb79860c0737ec5029d66822ef3017a0c6
[musl] / src / math / atan2l.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  *
12  */
13 /*
14  * See comments in atan2.c.
15  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
16  */
17
18 #include "libm.h"
19
20 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
21 long double atan2l(long double y, long double x)
22 {
23         return atan2(y, x);
24 }
25 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
26 #include "__invtrigl.h"
27
28 long double atan2l(long double y, long double x)
29 {
30         union IEEEl2bits ux, uy;
31         long double z;
32         int32_t k,m;
33         int16_t exptx, expsignx, expty, expsigny;
34
35         uy.e = y;
36         expsigny = uy.xbits.expsign;
37         expty = expsigny & 0x7fff;
38         ux.e = x;
39         expsignx = ux.xbits.expsign;
40         exptx = expsignx & 0x7fff;
41         if ((exptx==0x7fff &&
42              ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
43             (expty==0x7fff &&
44              ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0))   /* y is NaN */
45                 return x+y;
46         if (expsignx==0x3fff && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
47                 return atanl(y);
48         m = ((expsigny>>15)&1) | ((expsignx>>14)&2);  /* 2*sign(x)+sign(y) */
49
50         /* when y = 0 */
51         if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
52                 switch(m) {
53                 case 0:
54                 case 1: return y;           /* atan(+-0,+anything)=+-0 */
55                 case 2: return  2*pio2_hi+0x1p-1000; /* atan(+0,-anything) = pi */
56                 case 3: return -2*pio2_hi-0x1p-1000; /* atan(-0,-anything) =-pi */
57                 }
58         }
59         /* when x = 0 */
60         if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
61                 return expsigny < 0 ? -pio2_hi-0x1p-1000 : pio2_hi+0x1p-1000;
62         /* when x is INF */
63         if (exptx == 0x7fff) {
64                 if (expty == 0x7fff) {
65                         switch(m) {
66                         case 0: return  pio2_hi*0.5+0x1p-1000; /* atan(+INF,+INF) */
67                         case 1: return -pio2_hi*0.5-0x1p-1000; /* atan(-INF,+INF) */
68                         case 2: return  1.5*pio2_hi+0x1p-1000; /* atan(+INF,-INF) */
69                         case 3: return -1.5*pio2_hi-0x1p-1000; /* atan(-INF,-INF) */
70                         }
71                 } else {
72                         switch(m) {
73                         case 0: return  0.0;        /* atan(+...,+INF) */
74                         case 1: return -0.0;        /* atan(-...,+INF) */
75                         case 2: return  2*pio2_hi+0x1p-1000; /* atan(+...,-INF) */
76                         case 3: return -2*pio2_hi-0x1p-1000; /* atan(-...,-INF) */
77                         }
78                 }
79         }
80         /* when y is INF */
81         if (expty == 0x7fff)
82                 return expsigny < 0 ? -pio2_hi-0x1p-1000 : pio2_hi+0x1p-1000;
83
84         /* compute y/x */
85         k = expty-exptx;
86         if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
87                 z = pio2_hi+0x1p-1000;
88                 m &= 1;
89         } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
90                 z = 0.0;
91         else                     /* safe to do y/x */
92                 z = atanl(fabsl(y/x));
93         switch (m) {
94         case 0: return z;               /* atan(+,+) */
95         case 1: return -z;              /* atan(-,+) */
96         case 2: return 2*pio2_hi-(z-2*pio2_lo); /* atan(+,-) */
97         default: /* case 3 */
98                 return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */
99         }
100 }
101 #endif