POK
e_atan2.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* @(#)e_atan2.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 /* __ieee754_atan2(y,x)
30  * Method :
31  * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
32  * 2. Reduce x to positive by (if x and y are unexceptional):
33  * ARG (x+iy) = arctan(y/x) ... if x > 0,
34  * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
35  *
36  * Special cases:
37  *
38  * ATAN2((anything), NaN ) is NaN;
39  * ATAN2(NAN , (anything) ) is NaN;
40  * ATAN2(+-0, +(anything but NaN)) is +-0 ;
41  * ATAN2(+-0, -(anything but NaN)) is +-pi ;
42  * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
43  * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
44  * ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
45  * ATAN2(+-INF,+INF ) is +-pi/4 ;
46  * ATAN2(+-INF,-INF ) is +-3pi/4;
47  * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
48  *
49  * Constants:
50  * The hexadecimal values are the intended ones for the following
51  * constants. The decimal values may be used, provided that the
52  * compiler will convert from decimal to binary accurately enough
53  * to produce the hexadecimal values shown.
54  */
55 
56 #ifdef POK_NEEDS_LIBMATH
57 #include <libm.h>
58 #include "math_private.h"
59 
60 static const double
61 tiny = 1.0e-300,
62 zero = 0.0,
63 pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
64 pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
65 pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
66 pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
67 
68 double
69 __ieee754_atan2(double y, double x)
70 {
71  double z;
72  int32_t k,m,hx,hy,ix,iy;
73  uint32_t lx,ly;
74 
75  EXTRACT_WORDS(hx,lx,x);
76  ix = hx&0x7fffffff;
77  EXTRACT_WORDS(hy,ly,y);
78  iy = hy&0x7fffffff;
79  if(((ix|((lx|-lx)>>31))>0x7ff00000)||
80  ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */
81  return x+y;
82  if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */
83  m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
84 
85  /* when y = 0 */
86  if((iy|ly)==0) {
87  switch(m) {
88  case 0:
89  case 1: return y; /* atan(+-0,+anything)=+-0 */
90  case 2: return pi+tiny;/* atan(+0,-anything) = pi */
91  case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
92  }
93  }
94  /* when x = 0 */
95  if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
96 
97  /* when x is INF */
98  if(ix==0x7ff00000) {
99  if(iy==0x7ff00000) {
100  switch(m) {
101  case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
102  case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
103  case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
104  case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
105  }
106  } else {
107  switch(m) {
108  case 0: return zero ; /* atan(+...,+INF) */
109  case 1: return -zero ; /* atan(-...,+INF) */
110  case 2: return pi+tiny ; /* atan(+...,-INF) */
111  case 3: return -pi-tiny ; /* atan(-...,-INF) */
112  }
113  }
114  }
115  /* when y is INF */
116  if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
117 
118  /* compute y/x */
119  k = (iy-ix)>>20;
120  if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */
121  else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */
122  else z=atan(fabs(y/x)); /* safe to do y/x */
123  switch (m) {
124  case 0: return z ; /* atan(+,+) */
125  case 1: {
126  uint32_t zh;
127  GET_HIGH_WORD(zh,z);
128  SET_HIGH_WORD(z,zh ^ 0x80000000);
129  }
130  return z ; /* atan(-,+) */
131  case 2: return pi-(z-pi_lo);/* atan(+,-) */
132  default: /* case 3 */
133  return (z-pi_lo)-pi;/* atan(-,-) */
134  }
135 }
136 #endif