Path: blob/master/src/java.base/share/native/libfdlibm/e_atan2.c
41149 views
/*1* Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* __ieee754_atan2(y,x)26* Method :27* 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).28* 2. Reduce x to positive by (if x and y are unexceptional):29* ARG (x+iy) = arctan(y/x) ... if x > 0,30* ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,31*32* Special cases:33*34* ATAN2((anything), NaN ) is NaN;35* ATAN2(NAN , (anything) ) is NaN;36* ATAN2(+-0, +(anything but NaN)) is +-0 ;37* ATAN2(+-0, -(anything but NaN)) is +-pi ;38* ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;39* ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;40* ATAN2(+-(anything but INF and NaN), -INF) is +-pi;41* ATAN2(+-INF,+INF ) is +-pi/4 ;42* ATAN2(+-INF,-INF ) is +-3pi/4;43* ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;44*45* Constants:46* The hexadecimal values are the intended ones for the following47* constants. The decimal values may be used, provided that the48* compiler will convert from decimal to binary accurately enough49* to produce the hexadecimal values shown.50*/5152#include "fdlibm.h"5354#ifdef __STDC__55static const double56#else57static double58#endif59tiny = 1.0e-300,60zero = 0.0,61pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */62pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */63pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */64pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */6566#ifdef __STDC__67double __ieee754_atan2(double y, double x)68#else69double __ieee754_atan2(y,x)70double y,x;71#endif72{73double z;74int k,m,hx,hy,ix,iy;75unsigned lx,ly;7677hx = __HI(x); ix = hx&0x7fffffff;78lx = __LO(x);79hy = __HI(y); iy = hy&0x7fffffff;80ly = __LO(y);81if(((ix|((lx|-lx)>>31))>0x7ff00000)||82((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */83return x+y;84if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */85m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */8687/* when y = 0 */88if((iy|ly)==0) {89switch(m) {90case 0:91case 1: return y; /* atan(+-0,+anything)=+-0 */92case 2: return pi+tiny;/* atan(+0,-anything) = pi */93case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */94}95}96/* when x = 0 */97if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;9899/* when x is INF */100if(ix==0x7ff00000) {101if(iy==0x7ff00000) {102switch(m) {103case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */104case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */105case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/106case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/107}108} else {109switch(m) {110case 0: return zero ; /* atan(+...,+INF) */111case 1: return -1.0*zero ; /* atan(-...,+INF) */112case 2: return pi+tiny ; /* atan(+...,-INF) */113case 3: return -pi-tiny ; /* atan(-...,-INF) */114}115}116}117/* when y is INF */118if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;119120/* compute y/x */121k = (iy-ix)>>20;122if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */123else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */124else z=atan(fabs(y/x)); /* safe to do y/x */125switch (m) {126case 0: return z ; /* atan(+,+) */127case 1: __HI(z) ^= 0x80000000;128return z ; /* atan(-,+) */129case 2: return pi-(z-pi_lo);/* atan(+,-) */130default: /* case 3 */131return (z-pi_lo)-pi;/* atan(-,-) */132}133}134135136