Update.
[jlayton/glibc.git] / sysdeps / libm-ieee754 / e_atan2l.c
1 /* e_atan2l.c -- long double version of e_atan2.c.
2  * Conversion to long double by Ulrich Drepper,
3  * Cygnus Support, drepper@cygnus.com.
4  */
5
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16
17 #if defined(LIBM_SCCS) && !defined(lint)
18 static char rcsid[] = "$NetBSD: $";
19 #endif
20
21 /* __ieee754_atan2l(y,x)
22  * Method :
23  *      1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
24  *      2. Reduce x to positive by (if x and y are unexceptional):
25  *              ARG (x+iy) = arctan(y/x)           ... if x > 0,
26  *              ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
27  *
28  * Special cases:
29  *
30  *      ATAN2((anything), NaN ) is NaN;
31  *      ATAN2(NAN , (anything) ) is NaN;
32  *      ATAN2(+-0, +(anything but NaN)) is +-0  ;
33  *      ATAN2(+-0, -(anything but NaN)) is +-pi ;
34  *      ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
35  *      ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
36  *      ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
37  *      ATAN2(+-INF,+INF ) is +-pi/4 ;
38  *      ATAN2(+-INF,-INF ) is +-3pi/4;
39  *      ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
40  *
41  * Constants:
42  * The hexadecimal values are the intended ones for the following
43  * constants. The decimal values may be used, provided that the
44  * compiler will convert from decimal to binary accurately enough
45  * to produce the hexadecimal values shown.
46  */
47
48 #include "math.h"
49 #include "math_private.h"
50
51 #ifdef __STDC__
52 static const long double
53 #else
54 static long double
55 #endif
56 tiny  = 1.0e-4900L,
57 zero  = 0.0,
58 pi_o_4  = 7.85398163397448309628202E-01L, /* 0x3FFE, 0xC90FDAA2, 0x2168C235 */
59 pi_o_2  = 1.5707963267948966192564E+00L,  /* 0x3FFF, 0xC90FDAA2, 0x2168C235 */
60 pi      = 3.14159265358979323851281E+00L, /* 0x4000, 0xC90FDAA2, 0x2168C235 */
61 pi_lo   = -5.01655761266833202345176e-20L;/* 0xBFBE, 0xECE675D1, 0xFC8F8CBB */
62
63 #ifdef __STDC__
64         long double __ieee754_atan2l(long double y, long double x)
65 #else
66         long double __ieee754_atan2l(y,x)
67         long double  y,x;
68 #endif
69 {
70         long double z;
71         int32_t k,m,hx,hy,ix,iy;
72         u_int32_t sx,sy,lx,ly;
73
74         GET_LDOUBLE_WORDS(sx,hx,lx,x);
75         ix = sx&0x7fff;
76         lx |= hx & 0x7fffffff;
77         GET_LDOUBLE_WORDS(sy,hy,ly,y);
78         iy = sy&0x7fff;
79         ly |= hy & 0x7fffffff;
80         if(((2*ix|((lx|-lx)>>31))>0xfffe)||
81            ((2*iy|((ly|-ly)>>31))>0xfffe))      /* x or y is NaN */
82            return x+y;
83         if((sx-0x3fff|lx)==0) return __atanl(y);   /* x=1.0 */
84         m = ((sy>>15)&1)|((sx>>14)&2);  /* 2*sign(x)+sign(y) */
85
86     /* when y = 0 */
87         if((iy|ly)==0) {
88             switch(m) {
89                 case 0:
90                 case 1: return y;       /* atan(+-0,+anything)=+-0 */
91                 case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
92                 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
93             }
94         }
95     /* when x = 0 */
96         if((ix|lx)==0) return (sy>=0x8000)?  -pi_o_2-tiny: pi_o_2+tiny;
97
98     /* when x is INF */
99         if(ix==0x7fff) {
100             if(iy==0x7fff) {
101                 switch(m) {
102                     case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
103                     case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
104                     case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
105                     case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
106                 }
107             } else {
108                 switch(m) {
109                     case 0: return  zero  ;     /* atan(+...,+INF) */
110                     case 1: return -zero  ;     /* atan(-...,+INF) */
111                     case 2: return  pi+tiny  ;  /* atan(+...,-INF) */
112                     case 3: return -pi-tiny  ;  /* atan(-...,-INF) */
113                 }
114             }
115         }
116     /* when y is INF */
117         if(iy==0x7fff) return (sy>=0x8000)? -pi_o_2-tiny: pi_o_2+tiny;
118
119     /* compute y/x */
120         k = sy-sx;
121         if(k > 70) z=pi_o_2+0.5*pi_lo;  /* |y/x| >  2**70 */
122         else if(sx>=0x8000&&k<-70) z=0.0;       /* |y|/x < -2**70 */
123         else z=__atanl(fabsl(y/x));     /* safe to do y/x */
124         switch (m) {
125             case 0: return       z  ;   /* atan(+,+) */
126             case 1: {
127                       u_int32_t sz;
128                       GET_LDOUBLE_EXP(sz,z);
129                       SET_LDOUBLE_EXP(z,sz ^ 0x8000);
130                     }
131                     return       z  ;   /* atan(-,+) */
132             case 2: return  pi-(z-pi_lo);/* atan(+,-) */
133             default: /* case 3 */
134                     return  (z-pi_lo)-pi;/* atan(-,-) */
135         }
136 }