Merge from vendor branch LIBPCAP:
[dragonfly.git] / lib / msun / src / e_atan2f.c
1 /* e_atan2f.c -- float version of e_atan2.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  *
4  * $FreeBSD: src/lib/msun/src/e_atan2f.c,v 1.5 1999/08/28 00:06:29 peter Exp $
5  * $DragonFly: src/lib/msun/src/Attic/e_atan2f.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
6  */
7
8 /*
9  * ====================================================
10  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
11  *
12  * Developed at SunPro, a Sun Microsystems, Inc. business.
13  * Permission to use, copy, modify, and distribute this
14  * software is freely granted, provided that this notice
15  * is preserved.
16  * ====================================================
17  */
18
19 #include "math.h"
20 #include "math_private.h"
21
22 #ifdef __STDC__
23 static const float
24 #else
25 static float
26 #endif
27 tiny  = 1.0e-30,
28 zero  = 0.0,
29 pi_o_4  = 7.8539818525e-01, /* 0x3f490fdb */
30 pi_o_2  = 1.5707963705e+00, /* 0x3fc90fdb */
31 pi      = 3.1415925026e+00, /* 0x40490fda */
32 pi_lo   = 1.5099578832e-07; /* 0x34222168 */
33
34 #ifdef __STDC__
35         float __ieee754_atan2f(float y, float x)
36 #else
37         float __ieee754_atan2f(y,x)
38         float  y,x;
39 #endif
40 {
41         float z;
42         int32_t k,m,hx,hy,ix,iy;
43
44         GET_FLOAT_WORD(hx,x);
45         ix = hx&0x7fffffff;
46         GET_FLOAT_WORD(hy,y);
47         iy = hy&0x7fffffff;
48         if((ix>0x7f800000)||
49            (iy>0x7f800000))     /* x or y is NaN */
50            return x+y;
51         if(hx==0x3f800000) return atanf(y);   /* x=1.0 */
52         m = ((hy>>31)&1)|((hx>>30)&2);  /* 2*sign(x)+sign(y) */
53
54     /* when y = 0 */
55         if(iy==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(ix==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
65
66     /* when x is INF */
67         if(ix==0x7f800000) {
68             if(iy==0x7f800000) {
69                 switch(m) {
70                     case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
71                     case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
72                     case 2: return  (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
73                     case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
74                 }
75             } else {
76                 switch(m) {
77                     case 0: return  zero  ;     /* atan(+...,+INF) */
78                     case 1: return -zero  ;     /* 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(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
86
87     /* compute y/x */
88         k = (iy-ix)>>23;
89         if(k > 60) z=pi_o_2+(float)0.5*pi_lo;   /* |y/x| >  2**60 */
90         else if(hx<0&&k<-60) z=0.0;     /* |y|/x < -2**60 */
91         else z=atanf(fabsf(y/x));       /* safe to do y/x */
92         switch (m) {
93             case 0: return       z  ;   /* atan(+,+) */
94             case 1: {
95                       u_int32_t zh;
96                       GET_FLOAT_WORD(zh,z);
97                       SET_FLOAT_WORD(z,zh ^ 0x80000000);
98                     }
99                     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 }