/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */1/*2* ====================================================3* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.4*5* Developed at SunSoft, a Sun Microsystems, Inc. business.6* Permission to use, copy, modify, and distribute this7* software is freely granted, provided that this notice8* is preserved.9* ====================================================10*11*/12/* atan2(y,x)13* Method :14* 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).15* 2. Reduce x to positive by (if x and y are unexceptional):16* ARG (x+iy) = arctan(y/x) ... if x > 0,17* ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,18*19* Special cases:20*21* ATAN2((anything), NaN ) is NaN;22* ATAN2(NAN , (anything) ) is NaN;23* ATAN2(+-0, +(anything but NaN)) is +-0 ;24* ATAN2(+-0, -(anything but NaN)) is +-pi ;25* ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;26* ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;27* ATAN2(+-(anything but INF and NaN), -INF) is +-pi;28* ATAN2(+-INF,+INF ) is +-pi/4 ;29* ATAN2(+-INF,-INF ) is +-3pi/4;30* ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;31*32* Constants:33* The hexadecimal values are the intended ones for the following34* constants. The decimal values may be used, provided that the35* compiler will convert from decimal to binary accurately enough36* to produce the hexadecimal values shown.37*/3839#include "libm.h"4041static const double42pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */43pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */4445double __cdecl atan2(double y, double x)46{47double z;48uint32_t m,lx,ly,ix,iy;4950if (isnan(x) || isnan(y))51return x+y;52EXTRACT_WORDS(ix, lx, x);53EXTRACT_WORDS(iy, ly, y);54if ((ix-0x3ff00000 | lx) == 0) /* x = 1.0 */55return atan(y);56m = ((iy>>31)&1) | ((ix>>30)&2); /* 2*sign(x)+sign(y) */57ix = ix & 0x7fffffff;58iy = iy & 0x7fffffff;5960/* when y = 0 */61if ((iy|ly) == 0) {62switch(m) {63case 0:64case 1: return y; /* atan(+-0,+anything)=+-0 */65case 2: return pi; /* atan(+0,-anything) = pi */66case 3: return -pi; /* atan(-0,-anything) =-pi */67}68}69/* when x = 0 */70if ((ix|lx) == 0)71return m&1 ? -pi/2 : pi/2;72/* when x is INF */73if (ix == 0x7ff00000) {74if (iy == 0x7ff00000) {75switch(m) {76case 0: return pi/4; /* atan(+INF,+INF) */77case 1: return -pi/4; /* atan(-INF,+INF) */78case 2: return 3*pi/4; /* atan(+INF,-INF) */79case 3: return -3*pi/4; /* atan(-INF,-INF) */80}81} else {82switch(m) {83case 0: return 0.0; /* atan(+...,+INF) */84case 1: return -0.0; /* atan(-...,+INF) */85case 2: return pi; /* atan(+...,-INF) */86case 3: return -pi; /* atan(-...,-INF) */87}88}89}90/* |y/x| > 0x1p64 */91if (ix+(64<<20) < iy || iy == 0x7ff00000)92return m&1 ? -pi/2 : pi/2;9394/* z = atan(|y/x|) without spurious underflow */95if ((m&2) && iy+(64<<20) < ix) /* |y/x| < 0x1p-64, x<0 */96z = 0;97else98z = atan(fabs(y/x));99switch (m) {100case 0: return z; /* atan(+,+) */101case 1: return -z; /* atan(-,+) */102case 2: return pi - (z-pi_lo); /* atan(+,-) */103default: /* case 3 */104return (z-pi_lo) - pi; /* atan(-,-) */105}106}107108109