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.atan2f.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 float fatan2(float, float);
00071 extern float atan2f(float, float);
00072
00073 #pragma weak fatan2 = __atan2f
00074 #pragma weak atan2f = __atan2f
00075 #endif
00076
00077 #if defined(BUILD_OS_DARWIN)
00078 extern float __atan2f(float, float);
00079 #pragma weak atan2f
00080 float atan2f( float y, float x ) {
00081 return __atan2f( y, x );
00082 }
00083 #elif defined(__GNUC__)
00084 extern float __atan2f(float, float);
00085 float atan2f(float, float) __attribute__ ((weak, alias ("__atan2f")));
00086 #endif
00087
00088 extern const fu _atan2fres0[2][2];
00089 extern const fu _atan2fres1[2][2];
00090 extern const fu _atan2fres2[2][2];
00091 extern const fu _atan2fres4[4][4];
00092
00093 static const du twopm30 =
00094 {D(0x3e100000, 0x00000000)};
00095
00096 static const du twopm150 =
00097 {D(0x36900000, 0x00000000)};
00098
00099 static const du limit1 =
00100 {D(0x3fc445f0, 0xfbb1cf92)};
00101
00102 static const du limit2 =
00103 {D(0x3fe04e08, 0x50c1dd5c)};
00104
00105 static const du rlimit4 =
00106 {D(0x3fe04e08, 0x50c1dd5c)};
00107
00108 static const du rlimit5 =
00109 {D(0x3fc445f0, 0xfbb1cf92)};
00110
00111
00112
00113
00114
00115 static const du angletbl[] =
00116 {
00117 {D(0x00000000, 0x00000000)},
00118 {D(0x80000000, 0x00000000)},
00119 {D(0x400921fb, 0x54442d18)},
00120 {D(0xc00921fb, 0x54442d18)},
00121
00122 {D(0x3fd41b2f, 0x769ddfb2)},
00123 {D(0xbfd41b2f, 0x769ddfb2)},
00124 {D(0x40069e95, 0x65707122)},
00125 {D(0xc0069e95, 0x65707122)},
00126
00127 {D(0x3fe41b2f, 0x769cf2b1)},
00128 {D(0xbfe41b2f, 0x769cf2b1)},
00129 {D(0x40041b2f, 0x769cf06c)},
00130 {D(0xc0041b2f, 0x769cf06c)},
00131
00132 {D(0x3fee28c7, 0x31ec183d)},
00133 {D(0xbfee28c7, 0x31ec183d)},
00134 {D(0x400197c9, 0x87c92709)},
00135 {D(0xc00197c9, 0x87c92709)},
00136
00137 {D(0x3ff41b2f, 0x769cfe1f)},
00138 {D(0xbff41b2f, 0x769cfe1f)},
00139 {D(0x3ffe28c7, 0x31eb5c12)},
00140 {D(0xbffe28c7, 0x31eb5c12)},
00141
00142 {D(0x3ff921fb, 0x54442d18)},
00143 {D(0xbff921fb, 0x54442d18)},
00144 {D(0x3ff921fb, 0x54442d18)},
00145 {D(0xbff921fb, 0x54442d18)},
00146 };
00147
00148
00149
00150 static const du tantbl[] =
00151 {
00152 {D(0x00000000, 0x00000000)},
00153 {D(0x80000000, 0x00000000)},
00154 {D(0x80000000, 0x00000000)},
00155 {D(0x00000000, 0x00000000)},
00156
00157 {D(0x3fd4cb7b, 0xfb4a69b7)},
00158 {D(0xbfd4cb7b, 0xfb4a69b7)},
00159 {D(0xbfd4cb7b, 0xfb4a69b7)},
00160 {D(0x3fd4cb7b, 0xfb4a69b7)},
00161
00162 {D(0x3fe73fd6, 0x1d9df809)},
00163 {D(0xbfe73fd6, 0x1d9df809)},
00164 {D(0xbfe73fd6, 0x1d9df809)},
00165 {D(0x3fe73fd6, 0x1d9df809)},
00166
00167 {D(0x3ff605a9, 0x0c74a8a0)},
00168 {D(0xbff605a9, 0x0c74a8a0)},
00169 {D(0xbff605a9, 0x0c74a8a0)},
00170 {D(0x3ff605a9, 0x0c74a8a0)},
00171
00172 {D(0x40089f18, 0x8bdd1d09)},
00173 {D(0xc0089f18, 0x8bdd1d09)},
00174 {D(0xc0089f18, 0x8bdd1d09)},
00175 {D(0x40089f18, 0x8bdd1d09)},
00176 };
00177
00178
00179
00180
00181
00182 static const du P[] =
00183 {
00184 {D(0x3ff00000, 0x00000000)},
00185 {D(0xbfd55554, 0x19e773fa)},
00186 {D(0x3fc99738, 0x97341225)},
00187 {D(0xbfc1a142, 0x184deb25)},
00188 };
00189
00190 static const fu Qnan = {QNANF};
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 float
00203 __atan2f( float y, float x )
00204 {
00205 double dx, dy;
00206 int ix, iy;
00207 int xptx, xpty;
00208 int i, j, k;
00209 double dabsx, dabsy;
00210 int signx, signy;
00211 double tk, zk;
00212 double poly;
00213 double u, v, s, ss;
00214 double result;
00215 #ifdef _CALL_MATHERR
00216 struct exception exstruct;
00217 #endif
00218
00219 FLT2INT(y, iy);
00220 xpty = (iy >> MANTWIDTH);
00221 xpty &= 0xff;
00222
00223 FLT2INT(x, ix);
00224 xptx = (ix >> MANTWIDTH);
00225 xptx &= 0xff;
00226
00227 signy = (iy >> 31);
00228 signy = (signy & 1);
00229
00230 signx = (ix >> 31);
00231 signx = (signx & 1);
00232
00233
00234
00235 if ( (y != y) || (x != x) )
00236 {
00237
00238
00239 #ifdef _CALL_MATHERR
00240
00241 exstruct.type = DOMAIN;
00242 exstruct.name = "atan2f";
00243 exstruct.arg1 = y;
00244 exstruct.arg2 = x;
00245 exstruct.retval = Qnan.f;
00246
00247 if ( matherr( &exstruct ) == 0 )
00248 {
00249 fprintf(stderr, "domain error in atan2f\n");
00250 SETERRNO(EDOM);
00251 }
00252
00253 return ( exstruct.retval );
00254 #else
00255 NAN_SETERRNO(EDOM);
00256
00257 return ( Qnan.f );
00258 #endif
00259 }
00260
00261
00262
00263 if ( x == 0.0f )
00264 {
00265 if ( y == 0.0f )
00266 {
00267 #ifdef _CALL_MATHERR
00268 exstruct.type = DOMAIN;
00269 exstruct.name = "atan2f";
00270 exstruct.arg1 = y;
00271 exstruct.arg2 = x;
00272 exstruct.retval = _atan2fres0[signx][signy].f;
00273
00274 if ( matherr( &exstruct ) == 0 )
00275 {
00276 fprintf(stderr, "domain error in atan2f\n");
00277 SETERRNO(EDOM);
00278 }
00279
00280 return ( exstruct.retval );
00281 #else
00282 SETERRNO(EDOM);
00283
00284 return ( _atan2fres0[signx][signy].f );
00285 #endif
00286 }
00287
00288 return ( _atan2fres1[signx][signy].f );
00289 }
00290 else if ( y == 0.0f )
00291 {
00292 result = _atan2fres2[signx][signy].f;
00293 return ( result );
00294 }
00295
00296
00297
00298 i = (xptx == 0xff);
00299
00300 j = (xpty == 0xff);
00301
00302 if ( (i + j) == 0 )
00303 {
00304 dx = x;
00305 dy = y;
00306
00307 dabsx = fabs(dx);
00308
00309 dabsy = fabs(dy);
00310
00311 if ( dabsy < dabsx )
00312 {
00313 j = (dabsy >= dabsx*limit1.d);
00314 k = (dabsy >= dabsx*limit2.d);
00315 }
00316 else
00317 {
00318 j = (dabsx <= dabsy*rlimit4.d);
00319 k = (dabsx <= dabsy*rlimit5.d);
00320 j = j + 3;
00321 }
00322
00323 k = j + k;
00324
00325 k = 4*k + 2*signx + signy;
00326
00327 if ( k < 4 )
00328 {
00329 s = dy/dx;
00330 }
00331 else if ( k > 19 )
00332 {
00333 s = -dx/dy;
00334 }
00335 else
00336 {
00337 tk = tantbl[k].d;
00338
00339 u = dy - tk*dx;
00340
00341 v = tk*dy + dx;
00342
00343 s = u/v;
00344 }
00345
00346 zk = angletbl[k].d;
00347
00348 if ( fabs(s) < twopm30.d )
00349 {
00350 poly = s;
00351 }
00352 else
00353 {
00354 ss = s*s;
00355
00356 poly = ((P[3].d*ss + P[2].d)*ss + P[1].d)*(ss*s) + s;
00357 }
00358
00359 result = poly + zk;
00360
00361
00362
00363 if ( fabs(result) <= twopm150.d )
00364 return ( (signx == signy) ? 0.0f : -0.0f );
00365
00366 return ( (float)result );
00367
00368 }
00369 else
00370 {
00371 i = i + i;
00372 i = i + signx;
00373
00374 j = j + j;
00375 j = j + signy;
00376
00377 return ( _atan2fres4[i][j].f );
00378 }
00379 }
00380