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