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