/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */1/*2* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].3*/4/*5* ====================================================6* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.7*8* Developed at SunPro, a Sun Microsystems, Inc. business.9* Permission to use, copy, modify, and distribute this10* software is freely granted, provided that this notice11* is preserved.12* ====================================================13*/1415#include "libm.h"1617static const float18pi = 3.1415927410e+00, /* 0x40490fdb */19pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */2021float __cdecl atan2f(float y, float x)22{23float z;24uint32_t m,ix,iy;2526if (isnan(x) || isnan(y))27return x+y;28GET_FLOAT_WORD(ix, x);29GET_FLOAT_WORD(iy, y);30if (ix == 0x3f800000) /* x=1.0 */31return atanf(y);32m = ((iy>>31)&1) | ((ix>>30)&2); /* 2*sign(x)+sign(y) */33ix &= 0x7fffffff;34iy &= 0x7fffffff;3536/* when y = 0 */37if (iy == 0) {38switch (m) {39case 0:40case 1: return y; /* atan(+-0,+anything)=+-0 */41case 2: return pi; /* atan(+0,-anything) = pi */42case 3: return -pi; /* atan(-0,-anything) =-pi */43}44}45/* when x = 0 */46if (ix == 0)47return m&1 ? -pi/2 : pi/2;48/* when x is INF */49if (ix == 0x7f800000) {50if (iy == 0x7f800000) {51switch (m) {52case 0: return pi/4; /* atan(+INF,+INF) */53case 1: return -pi/4; /* atan(-INF,+INF) */54case 2: return 3*pi/4; /*atan(+INF,-INF)*/55case 3: return -3*pi/4; /*atan(-INF,-INF)*/56}57} else {58switch (m) {59case 0: return 0.0f; /* atan(+...,+INF) */60case 1: return -0.0f; /* atan(-...,+INF) */61case 2: return pi; /* atan(+...,-INF) */62case 3: return -pi; /* atan(-...,-INF) */63}64}65}66/* |y/x| > 0x1p26 */67if (ix+(26<<23) < iy || iy == 0x7f800000)68return m&1 ? -pi/2 : pi/2;6970/* z = atan(|y/x|) with correct underflow */71if ((m&2) && iy+(26<<23) < ix) /*|y/x| < 0x1p-26, x < 0 */72z = 0.0;73else74z = atanf(fabsf(y/x));75switch (m) {76case 0: return z; /* atan(+,+) */77case 1: return -z; /* atan(-,+) */78case 2: return pi - (z-pi_lo); /* atan(+,-) */79default: /* case 3 */80return (z-pi_lo) - pi; /* atan(-,-) */81}82}838485