1 /*****************************************************************************/
2 /*****************************************************************************/
3 // atan2f from musl-0.9.15
4 /*****************************************************************************/
5 /*****************************************************************************/
6 
7 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */
8 /*
9  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
10  */
11 /*
12  * ====================================================
13  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
14  *
15  * Developed at SunPro, a Sun Microsystems, Inc. business.
16  * Permission to use, copy, modify, and distribute this
17  * software is freely granted, provided that this notice
18  * is preserved.
19  * ====================================================
20  */
21 
22 #include "libm.h"
23 
24 static const float
25 pi     = 3.1415927410e+00f, /* 0x40490fdb */
26 pi_lo  = -8.7422776573e-08f; /* 0xb3bbbd2e */
27 
atan2f(float y,float x)28 float atan2f(float y, float x)
29 {
30 	float z;
31 	uint32_t m,ix,iy;
32 
33 	if (isnan(x) || isnan(y))
34 		return x+y;
35 	GET_FLOAT_WORD(ix, x);
36 	GET_FLOAT_WORD(iy, y);
37 	if (ix == 0x3f800000)  /* x=1.0 */
38 		return atanf(y);
39 	m = ((iy>>31)&1) | ((ix>>30)&2);  /* 2*sign(x)+sign(y) */
40 	ix &= 0x7fffffff;
41 	iy &= 0x7fffffff;
42 
43 	/* when y = 0 */
44 	if (iy == 0) {
45 		switch (m) {
46 		case 0:
47 		case 1: return y;   /* atan(+-0,+anything)=+-0 */
48 		case 2: return  pi; /* atan(+0,-anything) = pi */
49 		case 3: return -pi; /* atan(-0,-anything) =-pi */
50 		}
51 	}
52 	/* when x = 0 */
53 	if (ix == 0)
54 		return m&1 ? -pi/2 : pi/2;
55 	/* when x is INF */
56 	if (ix == 0x7f800000) {
57 		if (iy == 0x7f800000) {
58 			switch (m) {
59 			case 0: return  pi/4; /* atan(+INF,+INF) */
60 			case 1: return -pi/4; /* atan(-INF,+INF) */
61 			case 2: return 3*pi/4;  /*atan(+INF,-INF)*/
62 			case 3: return -3*pi/4; /*atan(-INF,-INF)*/
63 			}
64 		} else {
65 			switch (m) {
66 			case 0: return  0.0f;    /* atan(+...,+INF) */
67 			case 1: return -0.0f;    /* atan(-...,+INF) */
68 			case 2: return  pi; /* atan(+...,-INF) */
69 			case 3: return -pi; /* atan(-...,-INF) */
70 			}
71 		}
72 	}
73 	/* |y/x| > 0x1p26 */
74 	if (ix+(26<<23) < iy || iy == 0x7f800000)
75 		return m&1 ? -pi/2 : pi/2;
76 
77 	/* z = atan(|y/x|) with correct underflow */
78 	if ((m&2) && iy+(26<<23) < ix)  /*|y/x| < 0x1p-26, x < 0 */
79 		z = 0.0;
80 	else
81 		z = atanf(fabsf(y/x));
82 	switch (m) {
83 	case 0: return z;              /* atan(+,+) */
84 	case 1: return -z;             /* atan(-,+) */
85 	case 2: return pi - (z-pi_lo); /* atan(+,-) */
86 	default: /* case 3 */
87 		return (z-pi_lo) - pi; /* atan(-,-) */
88 	}
89 }
90