POK
e_atan2f.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_atan2f.c -- float version of e_atan2.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 #include <libm.h>
34 #include "math_private.h"
35 
36 static const float
37 tiny = 1.0e-30,
38 zero = 0.0,
39 pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */
40 pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */
41 pi = 3.1415925026e+00, /* 0x40490fda */
42 pi_lo = 1.5099578832e-07; /* 0x34222168 */
43 
44 float
45 __ieee754_atan2f(float y, float x)
46 {
47  float z;
48  int32_t k,m,hx,hy,ix,iy;
49 
50  GET_FLOAT_WORD(hx,x);
51  ix = hx&0x7fffffff;
52  GET_FLOAT_WORD(hy,y);
53  iy = hy&0x7fffffff;
54  if((ix>0x7f800000)||
55  (iy>0x7f800000)) /* x or y is NaN */
56  return x+y;
57  if(hx==0x3f800000) return atanf(y); /* x=1.0 */
58  m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
59 
60  /* when y = 0 */
61  if(iy==0) {
62  switch(m) {
63  case 0:
64  case 1: return y; /* atan(+-0,+anything)=+-0 */
65  case 2: return pi+tiny;/* atan(+0,-anything) = pi */
66  case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
67  }
68  }
69  /* when x = 0 */
70  if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
71 
72  /* when x is INF */
73  if(ix==0x7f800000) {
74  if(iy==0x7f800000) {
75  switch(m) {
76  case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
77  case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
78  case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
79  case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
80  }
81  } else {
82  switch(m) {
83  case 0: return zero ; /* atan(+...,+INF) */
84  case 1: return -zero ; /* atan(-...,+INF) */
85  case 2: return pi+tiny ; /* atan(+...,-INF) */
86  case 3: return -pi-tiny ; /* atan(-...,-INF) */
87  }
88  }
89  }
90  /* when y is INF */
91  if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
92 
93  /* compute y/x */
94  k = (iy-ix)>>23;
95  if(k > 60) z=pi_o_2+(float)0.5*pi_lo; /* |y/x| > 2**60 */
96  else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */
97  else z=atanf(fabsf(y/x)); /* safe to do y/x */
98  switch (m) {
99  case 0: return z ; /* atan(+,+) */
100  case 1: {
101  uint32_t zh;
102  GET_FLOAT_WORD(zh,z);
103  SET_FLOAT_WORD(z,zh ^ 0x80000000);
104  }
105  return z ; /* atan(-,+) */
106  case 2: return pi-(z-pi_lo);/* atan(+,-) */
107  default: /* case 3 */
108  return (z-pi_lo)-pi;/* atan(-,-) */
109  }
110 }
111 #endif