math: ld80 invtrig cleanups
[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 // FIXME:
28 static const volatile long double tiny = 1.0e-300;
29
30 long double atan2l(long double y, long double x)
31 {
32         union IEEEl2bits ux, uy;
33         long double z;
34         int32_t k,m;
35         int16_t exptx, expsignx, expty, expsigny;
36
37         uy.e = y;
38         expsigny = uy.xbits.expsign;
39         expty = expsigny & 0x7fff;
40         ux.e = x;
41         expsignx = ux.xbits.expsign;
42         exptx = expsignx & 0x7fff;
43         if ((exptx==BIAS+LDBL_MAX_EXP &&
44              ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
45             (expty==BIAS+LDBL_MAX_EXP &&
46              ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0))   /* y is NaN */
47                 return x+y;
48         if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
49                 return atanl(y);
50         m = ((expsigny>>15)&1) | ((expsignx>>14)&2);  /* 2*sign(x)+sign(y) */
51
52         /* when y = 0 */
53         if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
54                 switch(m) {
55                 case 0:
56                 case 1: return y;           /* atan(+-0,+anything)=+-0 */
57                 case 2: return  pi_hi+tiny; /* atan(+0,-anything) = pi */
58                 case 3: return -pi_hi-tiny; /* atan(-0,-anything) =-pi */
59                 }
60         }
61         /* when x = 0 */
62         if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
63                 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
64         /* when x is INF */
65         if (exptx == BIAS+LDBL_MAX_EXP) {
66                 if (expty == BIAS+LDBL_MAX_EXP) {
67                         switch(m) {
68                         case 0: return  pio2_hi*0.5+tiny; /* atan(+INF,+INF) */
69                         case 1: return -pio2_hi*0.5-tiny; /* atan(-INF,+INF) */
70                         case 2: return  1.5*pio2_hi+tiny; /* atan(+INF,-INF) */
71                         case 3: return -1.5*pio2_hi-tiny; /* atan(-INF,-INF) */
72                         }
73                 } else {
74                         switch(m) {
75                         case 0: return  0.0;        /* atan(+...,+INF) */
76                         case 1: return -0.0;        /* atan(-...,+INF) */
77                         case 2: return  pi_hi+tiny; /* atan(+...,-INF) */
78                         case 3: return -pi_hi-tiny; /* atan(-...,-INF) */
79                         }
80                 }
81         }
82         /* when y is INF */
83         if (expty == BIAS+LDBL_MAX_EXP)
84                 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
85
86         /* compute y/x */
87         k = expty-exptx;
88         if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
89                 z = pio2_hi+pio2_lo;
90                 m &= 1;
91         } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
92                 z = 0.0;
93         else                     /* safe to do y/x */
94                 z = atanl(fabsl(y/x));
95         switch (m) {
96         case 0: return z;               /* atan(+,+) */
97         case 1: return -z;              /* atan(-,+) */
98         case 2: return pi_hi-(z-pi_lo); /* atan(+,-) */
99         default: /* case 3 */
100                 return (z-pi_lo)-pi_hi; /* atan(-,-) */
101         }
102 }
103 #endif