| 1 | /* |
| 2 | * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Oracle designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Oracle in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | * or visit www.oracle.com if you need additional information or have any |
| 23 | * questions. |
| 24 | */ |
| 25 | |
| 26 | /* __ieee754_atan2(y,x) |
| 27 | * Method : |
| 28 | * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). |
| 29 | * 2. Reduce x to positive by (if x and y are unexceptional): |
| 30 | * ARG (x+iy) = arctan(y/x) ... if x > 0, |
| 31 | * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, |
| 32 | * |
| 33 | * Special cases: |
| 34 | * |
| 35 | * ATAN2((anything), NaN ) is NaN; |
| 36 | * ATAN2(NAN , (anything) ) is NaN; |
| 37 | * ATAN2(+-0, +(anything but NaN)) is +-0 ; |
| 38 | * ATAN2(+-0, -(anything but NaN)) is +-pi ; |
| 39 | * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; |
| 40 | * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; |
| 41 | * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; |
| 42 | * ATAN2(+-INF,+INF ) is +-pi/4 ; |
| 43 | * ATAN2(+-INF,-INF ) is +-3pi/4; |
| 44 | * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; |
| 45 | * |
| 46 | * Constants: |
| 47 | * The hexadecimal values are the intended ones for the following |
| 48 | * constants. The decimal values may be used, provided that the |
| 49 | * compiler will convert from decimal to binary accurately enough |
| 50 | * to produce the hexadecimal values shown. |
| 51 | */ |
| 52 | |
| 53 | #include "fdlibm.h" |
| 54 | |
| 55 | #ifdef __STDC__ |
| 56 | static const double |
| 57 | #else |
| 58 | static double |
| 59 | #endif |
| 60 | tiny = 1.0e-300, |
| 61 | zero = 0.0, |
| 62 | pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ |
| 63 | pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ |
| 64 | pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ |
| 65 | pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ |
| 66 | |
| 67 | #ifdef __STDC__ |
| 68 | double __ieee754_atan2(double y, double x) |
| 69 | #else |
| 70 | double __ieee754_atan2(y,x) |
| 71 | double y,x; |
| 72 | #endif |
| 73 | { |
| 74 | double z; |
| 75 | int k,m,hx,hy,ix,iy; |
| 76 | unsigned lx,ly; |
| 77 | |
| 78 | hx = __HI(x); ix = hx&0x7fffffff; |
| 79 | lx = __LO(x); |
| 80 | hy = __HI(y); iy = hy&0x7fffffff; |
| 81 | ly = __LO(y); |
| 82 | if(((ix|((lx|-lx)>>31))>0x7ff00000)|| |
| 83 | ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ |
| 84 | return x+y; |
| 85 | if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */ |
| 86 | m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ |
| 87 | |
| 88 | /* when y = 0 */ |
| 89 | if((iy|ly)==0) { |
| 90 | switch(m) { |
| 91 | case 0: |
| 92 | case 1: return y; /* atan(+-0,+anything)=+-0 */ |
| 93 | case 2: return pi+tiny;/* atan(+0,-anything) = pi */ |
| 94 | case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ |
| 95 | } |
| 96 | } |
| 97 | /* when x = 0 */ |
| 98 | if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; |
| 99 | |
| 100 | /* when x is INF */ |
| 101 | if(ix==0x7ff00000) { |
| 102 | if(iy==0x7ff00000) { |
| 103 | switch(m) { |
| 104 | case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ |
| 105 | case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ |
| 106 | case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ |
| 107 | case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ |
| 108 | } |
| 109 | } else { |
| 110 | switch(m) { |
| 111 | case 0: return zero ; /* atan(+...,+INF) */ |
| 112 | case 1: return -1.0*zero ; /* atan(-...,+INF) */ |
| 113 | case 2: return pi+tiny ; /* atan(+...,-INF) */ |
| 114 | case 3: return -pi-tiny ; /* atan(-...,-INF) */ |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | /* when y is INF */ |
| 119 | if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; |
| 120 | |
| 121 | /* compute y/x */ |
| 122 | k = (iy-ix)>>20; |
| 123 | if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ |
| 124 | else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ |
| 125 | else z=atan(fabs(y/x)); /* safe to do y/x */ |
| 126 | switch (m) { |
| 127 | case 0: return z ; /* atan(+,+) */ |
| 128 | case 1: __HI(z) ^= 0x80000000; |
| 129 | return z ; /* atan(-,+) */ |
| 130 | case 2: return pi-(z-pi_lo);/* atan(+,-) */ |
| 131 | default: /* case 3 */ |
| 132 | return (z-pi_lo)-pi;/* atan(-,-) */ |
| 133 | } |
| 134 | } |
| 135 | |