00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 static char *rcs_id = "$Source: /home/bos/bk/kpro64-pending/libm/mips/SCCS/s.atan2.c $ $Revision: 1.5 $";
00060
00061 #ifdef _CALL_MATHERR
00062 #include <stdio.h>
00063 #include <math.h>
00064 #include <errno.h>
00065 #endif
00066
00067 #include "libm.h"
00068
00069 #if defined(mips) && !defined(__GNUC__)
00070 extern double atan2(double, double);
00071
00072 #pragma weak atan2 = __atan2
00073 #endif
00074
00075 #if defined(BUILD_OS_DARWIN)
00076 extern double __atan2(double, double);
00077 #pragma weak atan2
00078 double atan2(double y, double x) {
00079 return __atan2( y, x );
00080 }
00081 #elif defined(__GNUC__)
00082 extern double __atan2(double, double);
00083
00084 double atan2() __attribute__ ((weak, alias ("__atan2")));
00085
00086 #endif
00087
00088 extern const du _atan2res0[2][2];
00089 extern const du _atan2res1[2][2];
00090 extern const du _atan2res2[2][2];
00091 extern const du _atan2res4[4][4];
00092
00093 static const du Qnan =
00094 {D(QNANHI, QNANLO)};
00095
00096 static const du twop60 =
00097 {D(0x43b00000, 0x00000000)};
00098
00099 static const du twopm28 =
00100 {D(0x3e300000, 0x00000000)};
00101
00102 static const du limit1 =
00103 {D(0x3fc445f0, 0xfbb1cf92)};
00104
00105 static const du limit2 =
00106 {D(0x3fe04e08, 0x50c1dd5c)};
00107
00108 static const du rlimit4 =
00109 {D(0x3fe04e08, 0x50c1dd5c)};
00110
00111 static const du rlimit5 =
00112 {D(0x3fc445f0, 0xfbb1cf92)};
00113
00114 static const du piby2 =
00115 {D(0x3ff921fb, 0x54442d18)};
00116
00117 static const du m_piby2 =
00118 {D(0xbff921fb, 0x54442d18)};
00119
00120
00121
00122
00123
00124 static const du angletbl[] =
00125 {
00126 {D(0x00000000, 0x00000000)},
00127 {D(0x80000000, 0x00000000)},
00128 {D(0x400921fb, 0x54442d18)},
00129 {D(0xc00921fb, 0x54442d18)},
00130
00131 {D(0x3fd41b2f, 0x769ddfb2)},
00132 {D(0xbfd41b2f, 0x769ddfb2)},
00133 {D(0x40069e95, 0x65707122)},
00134 {D(0xc0069e95, 0x65707122)},
00135
00136 {D(0x3fe41b2f, 0x769cf2b1)},
00137 {D(0xbfe41b2f, 0x769cf2b1)},
00138 {D(0x40041b2f, 0x769cf06c)},
00139 {D(0xc0041b2f, 0x769cf06c)},
00140
00141 {D(0x3fee28c7, 0x31ec183d)},
00142 {D(0xbfee28c7, 0x31ec183d)},
00143 {D(0x400197c9, 0x87c92709)},
00144 {D(0xc00197c9, 0x87c92709)},
00145
00146 {D(0x3ff41b2f, 0x769cfe1f)},
00147 {D(0xbff41b2f, 0x769cfe1f)},
00148 {D(0x3ffe28c7, 0x31eb5c12)},
00149 {D(0xbffe28c7, 0x31eb5c12)},
00150
00151 {D(0x3ff921fb, 0x54442d18)},
00152 {D(0xbff921fb, 0x54442d18)},
00153 {D(0x3ff921fb, 0x54442d18)},
00154 {D(0xbff921fb, 0x54442d18)},
00155 };
00156
00157
00158
00159 static const du tantbl[] =
00160 {
00161 {D(0x00000000, 0x00000000)},
00162 {D(0x80000000, 0x00000000)},
00163 {D(0x80000000, 0x00000000)},
00164 {D(0x00000000, 0x00000000)},
00165
00166 {D(0x3fd4cb7b, 0xfb4a69b7)},
00167 {D(0xbfd4cb7b, 0xfb4a69b7)},
00168 {D(0xbfd4cb7b, 0xfb4a69b7)},
00169 {D(0x3fd4cb7b, 0xfb4a69b7)},
00170
00171 {D(0x3fe73fd6, 0x1d9df809)},
00172 {D(0xbfe73fd6, 0x1d9df809)},
00173 {D(0xbfe73fd6, 0x1d9df809)},
00174 {D(0x3fe73fd6, 0x1d9df809)},
00175
00176 {D(0x3ff605a9, 0x0c74a8a0)},
00177 {D(0xbff605a9, 0x0c74a8a0)},
00178 {D(0xbff605a9, 0x0c74a8a0)},
00179 {D(0x3ff605a9, 0x0c74a8a0)},
00180
00181 {D(0x40089f18, 0x8bdd1d09)},
00182 {D(0xc0089f18, 0x8bdd1d09)},
00183 {D(0xc0089f18, 0x8bdd1d09)},
00184 {D(0x40089f18, 0x8bdd1d09)},
00185 };
00186
00187
00188
00189
00190
00191 static const du P[] =
00192 {
00193 {D(0x3ff00000, 0x00000000)},
00194 {D(0xbfd55555, 0x55555547)},
00195 {D(0x3fc99999, 0x999924e3)},
00196 {D(0xbfc24924, 0x91a937fe)},
00197 {D(0x3fbc71c6, 0x4c76a27a)},
00198 {D(0xbfb74589, 0x00fd881c)},
00199 {D(0x3fb3a350, 0x167cd5be)},
00200 {D(0xbfaf5682, 0x2746dfc3)},
00201 };
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 double
00214 __atan2( y, x )
00215 double y, x;
00216 {
00217 #ifdef _32BIT_MACHINE
00218
00219 int ix, iy;
00220 int xptx, xpty, xpts;
00221 int signx, signy;
00222 int l;
00223
00224 #else
00225
00226 long long ix, iy;
00227 long long xptx, xpty, xpts;
00228 long long signx, signy;
00229 long long l;
00230
00231 #endif
00232
00233 int i, j, k;
00234 double absx, absy;
00235 double tk, zk;
00236 double poly;
00237 double u, v, s, ss;
00238 double result;
00239 #ifdef _CALL_MATHERR
00240 struct exception exstruct;
00241 #endif
00242
00243
00244
00245 #ifdef _32BIT_MACHINE
00246
00247 DBLHI2INT(y, iy);
00248 DBLHI2INT(x, ix);
00249 #else
00250 DBL2LL(y, iy);
00251 DBL2LL(x, ix);
00252 #endif
00253 xpty = (iy >> DMANTWIDTH);
00254 xpty &= 0x7ff;
00255
00256 xptx = (ix >> DMANTWIDTH);
00257 xptx &= 0x7ff;
00258
00259 signy = (iy >> (DMANTWIDTH + DEXPWIDTH));
00260 signy = (signy & 1);
00261
00262 signx = (ix >> (DMANTWIDTH + DEXPWIDTH));
00263 signx = (signx & 1);
00264
00265
00266
00267 if ( (xpty == 0x7ff) || (xptx == 0x7ff) )
00268 {
00269 if ( (y != y) || (x != x) )
00270 {
00271
00272
00273 #ifdef _CALL_MATHERR
00274 exstruct.type = DOMAIN;
00275 exstruct.name = "atan2";
00276 exstruct.arg1 = y;
00277 exstruct.arg2 = x;
00278 exstruct.retval = Qnan.d;
00279
00280 if ( matherr( &exstruct ) == 0 )
00281 {
00282 fprintf(stderr, "domain error in atan2\n");
00283 SETERRNO(EDOM);
00284 }
00285
00286 return ( exstruct.retval );
00287 #else
00288 NAN_SETERRNO(EDOM);
00289
00290 return ( Qnan.d );
00291 #endif
00292 }
00293 }
00294
00295
00296
00297 if ( x == 0.0 )
00298 {
00299 if ( y == 0.0 )
00300 {
00301 #ifdef _CALL_MATHERR
00302
00303 exstruct.type = DOMAIN;
00304 exstruct.name = "atan2";
00305 exstruct.arg1 = y;
00306 exstruct.arg2 = x;
00307 exstruct.retval = _atan2res0[signx][signy].d;
00308
00309 if ( matherr( &exstruct ) == 0 )
00310 {
00311 fprintf(stderr, "domain error in atan2\n");
00312 SETERRNO(EDOM);
00313 }
00314
00315 return ( exstruct.retval );
00316 #else
00317 SETERRNO(EDOM);
00318
00319 return ( _atan2res0[signx][signy].d );
00320 #endif
00321 }
00322
00323 return ( _atan2res1[signx][signy].d );
00324 }
00325 else if ( (xpty == 0) && (y == 0.0) )
00326 {
00327 result = _atan2res2[signx][signy].d;
00328 return ( result );
00329 }
00330
00331
00332
00333 if ( xpty > xptx + 54 )
00334 return ( (signy == 0) ? piby2.d : m_piby2.d );
00335
00336
00337
00338 if ( xpty < xptx - 1075 )
00339 return ( _atan2res2[signx][signy].d );
00340
00341
00342
00343
00344
00345
00346 if ( (xpty == 0) || (xptx == 0) )
00347 {
00348 y = twop60.d*y;
00349 x = twop60.d*x;
00350 }
00351
00352
00353
00354 i = (xptx == 0x7ff);
00355
00356 j = (xpty == 0x7ff);
00357
00358
00359
00360
00361
00362 if ( (xpty >= 0x7fc) || (xptx >= 0x7fc) )
00363 {
00364 y = 0.25*y;
00365 x = 0.25*x;
00366 }
00367
00368 if ( (i + j) == 0 )
00369 {
00370 absx = fabs(x);
00371
00372 absy = fabs(y);
00373
00374
00375
00376
00377
00378
00379 if ( absy < absx )
00380 {
00381 j = (absy >= absx*limit1.d);
00382 k = (absy >= absx*limit2.d);
00383 }
00384 else
00385 {
00386 j = (absx <= absy*rlimit4.d);
00387 k = (absx <= absy*rlimit5.d);
00388 j = j + 3;
00389 }
00390
00391 k = j + k;
00392
00393 k = 4*k + 2*signx + signy;
00394
00395
00396
00397 if ( k < 4 )
00398 {
00399 if ( xpty < (xptx - 1074) )
00400 {
00401
00402
00403 v = x;
00404
00405 #ifdef _32BIT_MACHINE
00406
00407 DBLHI2INT(v, l);
00408 l &= DEXPMASK;
00409 l |= (0x3ff << DMANTWIDTH);
00410 #else
00411 DBL2LL(v, l);
00412 l &= DEXPMASK;
00413 l |= (0x3ffll << DMANTWIDTH);
00414 #endif
00415 s = y/v;
00416
00417 #ifdef _32BIT_MACHINE
00418
00419 DBLHI2INT(s, xpts);
00420 #else
00421 DBL2LL(s, xpts);
00422 #endif
00423 xpts >>= DMANTWIDTH;
00424 xpts &= 0x7ff;
00425
00426 if ( (xpts + xptx - DEXPBIAS) < -1075 )
00427 {
00428
00429
00430 s = 0.0*s;
00431 }
00432 else
00433 {
00434 s = y/x;
00435 }
00436 }
00437 else
00438 {
00439 s = y/x;
00440 }
00441 }
00442 else if ( k > 19 )
00443 {
00444 s = -x/y;
00445 }
00446 else
00447 {
00448 tk = tantbl[k].d;
00449
00450 u = y - tk*x;
00451
00452 v = tk*y + x;
00453
00454 s = u/v;
00455 }
00456
00457 zk = angletbl[k].d;
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 if ( fabs(s) < twopm28.d )
00470 return ( s + zk );
00471
00472 ss = s*s;
00473
00474
00475 poly = (((((P[7].d*ss + P[6].d)*ss + P[5].d)*ss + P[4].d)*ss +
00476 P[3].d)*ss + P[2].d)*ss + P[1].d;
00477
00478 result = poly*(ss*s) + s + zk;
00479
00480 return ( result );
00481
00482 }
00483 else
00484 {
00485 i = i + i;
00486 i = i + signx;
00487
00488 j = j + j;
00489 j = j + signy;
00490
00491 return ( _atan2res4[i][j].d );
00492 }
00493 }
00494
00495 #if defined(BUILD_OS_DARWIN)
00496 #pragma weak atan2l
00497 long double atan2l( long double y, long double x ) {
00498 return ( (long double)__atan2((double)y, (double)x) );
00499 }
00500 #elif defined(__GNUC__)
00501 extern long double __atan2l(long double, long double);
00502
00503 long double atan2l() __attribute__ ((weak, alias ("__atan2l")));
00504
00505 #endif
00506
00507 long double
00508 __atan2l( y, x )
00509 long double y, x;
00510 {
00511 return ( (long double)__atan2((double)y, (double)x) );
00512 }
00513