1 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
14 * See comments in atan2.c.
15 * Converted to long double by David Schultz <das@FreeBSD.ORG>.
20 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
21 long double atan2l(long double y, long double x)
25 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
26 #include "__invtrigl.h"
27 static volatile long double
29 static const long double
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)
37 static const long double
38 pi = 3.14159265358979323846264338327950280e+00L;
41 long double atan2l(long double y, long double x)
43 union IEEEl2bits ux, uy;
46 int16_t exptx, expsignx, expty, expsigny;
49 expsigny = uy.xbits.expsign;
50 expty = expsigny & 0x7fff;
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 */
59 if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
61 m = ((expsigny>>15)&1) | ((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
64 if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==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 */
73 if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
74 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
76 if (exptx == BIAS+LDBL_MAX_EXP) {
77 if (expty == BIAS+LDBL_MAX_EXP) {
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)*/
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) */
94 if (expty == BIAS+LDBL_MAX_EXP)
95 return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
99 if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
102 } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
104 else /* safe to do y/x */
105 z = atanl(fabsl(y/x));
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(-,-) */