blob: 408f3646f612a2bd4a38169445f355e9d9f0df72 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* e_atan2f.c -- float version of e_atan2.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
4
5/*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016#include "math.h"
17#include "math_private.h"
18
Elliott Hughesa0ee0782013-01-30 19:06:37 -080019static volatile float
20tiny = 1.0e-30;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080021static const float
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080022zero = 0.0,
23pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */
24pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */
Elliott Hughesa0ee0782013-01-30 19:06:37 -080025pi = 3.1415927410e+00; /* 0x40490fdb */
26static volatile float
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080027pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */
28
29float
Elliott Hughes4088e3a2023-08-03 13:33:56 -070030atan2f(float y, float x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031{
32 float z;
33 int32_t k,m,hx,hy,ix,iy;
34
35 GET_FLOAT_WORD(hx,x);
36 ix = hx&0x7fffffff;
37 GET_FLOAT_WORD(hy,y);
38 iy = hy&0x7fffffff;
39 if((ix>0x7f800000)||
40 (iy>0x7f800000)) /* x or y is NaN */
Elliott Hughesab528072018-07-24 00:01:52 +000041 return nan_mix(x, y);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042 if(hx==0x3f800000) return atanf(y); /* x=1.0 */
43 m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
44
45 /* when y = 0 */
46 if(iy==0) {
47 switch(m) {
48 case 0:
49 case 1: return y; /* atan(+-0,+anything)=+-0 */
50 case 2: return pi+tiny;/* atan(+0,-anything) = pi */
51 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
52 }
53 }
54 /* when x = 0 */
55 if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
56
57 /* when x is INF */
58 if(ix==0x7f800000) {
59 if(iy==0x7f800000) {
60 switch(m) {
61 case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
62 case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
63 case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
64 case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
65 }
66 } else {
67 switch(m) {
68 case 0: return zero ; /* atan(+...,+INF) */
69 case 1: return -zero ; /* atan(-...,+INF) */
70 case 2: return pi+tiny ; /* atan(+...,-INF) */
71 case 3: return -pi-tiny ; /* atan(-...,-INF) */
72 }
73 }
74 }
75 /* when y is INF */
76 if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
77
78 /* compute y/x */
79 k = (iy-ix)>>23;
Elliott Hughesa0ee0782013-01-30 19:06:37 -080080 if(k > 26) { /* |y/x| > 2**26 */
81 z=pi_o_2+(float)0.5*pi_lo;
82 m&=1;
83 }
84 else if(k<-26&&hx<0) z=0.0; /* 0 > |y|/x > -2**-26 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085 else z=atanf(fabsf(y/x)); /* safe to do y/x */
86 switch (m) {
87 case 0: return z ; /* atan(+,+) */
Elliott Hughesa0ee0782013-01-30 19:06:37 -080088 case 1: return -z ; /* atan(-,+) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089 case 2: return pi-(z-pi_lo);/* atan(+,-) */
90 default: /* case 3 */
91 return (z-pi_lo)-pi;/* atan(-,-) */
92 }
93}