< nwmcsween> nsz libm.h slow -> large
[libm] / 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 LD64
21 long double atan2l(long double y, long double x)
22 {
23         return atan2(y, x);
24 }
25 #elif LD80 || LD128
26 #include "__invtrigl.h"
27 static volatile long double
28 tiny = 1.0e-300;
29 static const long double
30 zero = 0.0;
31 /* XXX Work around the fact that gcc truncates long double constants on i386 */
32 static volatile double
33 pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1  */
34 pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
35 #define pi ((long double)pi1 + pi2)
36 #if 0
37 static const long double
38 pi = 3.14159265358979323846264338327950280e+00L;
39 #endif
40
41 long double atan2l(long double y, long double x)
42 {
43         union IEEEl2bits ux, uy;
44         long double z;
45         int32_t k,m;
46         int16_t exptx, expsignx, expty, expsigny;
47
48         uy.e = y;
49         expsigny = uy.xbits.expsign;
50         expty = expsigny & 0x7fff;
51         ux.e = x;
52         expsignx = ux.xbits.expsign;
53         exptx = expsignx & 0x7fff;
54         if ((exptx==BIAS+LDBL_MAX_EXP &&
55              ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
56             (expty==BIAS+LDBL_MAX_EXP &&
57              ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0))   /* y is NaN */
58                 return x+y;
59         if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
60                 return atanl(y);
61         m = ((expsigny>>15)&1) | ((expsignx>>14)&2);  /* 2*sign(x)+sign(y) */
62
63         /* when y = 0 */
64         if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
65                 switch(m) {
66                 case 0:
67                 case 1: return y;        /* atan(+-0,+anything)=+-0 */
68                 case 2: return  pi+tiny; /* atan(+0,-anything) = pi */
69                 case 3: return -pi-tiny; /* atan(-0,-anything) =-pi */
70                 }
71         }
72         /* when x = 0 */
73         if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
74                 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
75         /* when x is INF */
76         if (exptx == BIAS+LDBL_MAX_EXP) {
77                 if (expty == BIAS+LDBL_MAX_EXP) {
78                         switch(m) {
79                         case 0: return  pio2_hi*0.5+tiny; /* atan(+INF,+INF) */
80                         case 1: return -pio2_hi*0.5-tiny; /* atan(-INF,+INF) */
81                         case 2: return  1.5*pio2_hi+tiny; /*atan(+INF,-INF)*/
82                         case 3: return -1.5*pio2_hi-tiny; /*atan(-INF,-INF)*/
83                         }
84                 } else {
85                         switch(m) {
86                         case 0: return  zero;    /* atan(+...,+INF) */
87                         case 1: return -zero;    /* atan(-...,+INF) */
88                         case 2: return  pi+tiny; /* atan(+...,-INF) */
89                         case 3: return -pi-tiny; /* atan(-...,-INF) */
90                         }
91                 }
92         }
93         /* when y is INF */
94         if (expty == BIAS+LDBL_MAX_EXP)
95                 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
96
97         /* compute y/x */
98         k = expty-exptx;
99         if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
100                 z = pio2_hi+pio2_lo;
101                 m &= 1;
102         } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
103                 z = 0.0;
104         else                     /* safe to do y/x */
105                 z = atanl(fabsl(y/x));
106         switch (m) {
107         case 0: return z;              /* atan(+,+) */
108         case 1: return -z;             /* atan(-,+) */
109         case 2: return pi - (z-pi_lo); /* atan(+,-) */
110         default: /* case 3 */
111                 return (z-pi_lo) - pi; /* atan(-,-) */
112         }
113 }
114 #endif