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.log.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
00070
00071
00072
00073
00074
00075
00076 #if defined(mips) && !defined(__GNUC__)
00077 extern double log(double);
00078
00079 #pragma weak log = __log
00080 #endif
00081
00082 #if defined(BUILD_OS_DARWIN)
00083 extern double __log(double);
00084 #pragma weak log
00085 double log( double x ) {
00086 return __log( x );
00087 }
00088 #elif defined(__GNUC__)
00089 extern double __log(double);
00090
00091 double log() __attribute__ ((weak, alias ("__log")));
00092
00093 #endif
00094
00095 extern const du _logtabhi[];
00096 extern const du _logtablo[];
00097 extern const du _log_ru[];
00098
00099 #ifdef _32BIT_MACHINE
00100
00101
00102
00103 static const int lim1 =
00104 {0x3fee0fab};
00105
00106
00107
00108 static const int lim2 =
00109 {0x3ff1082b};
00110
00111 static const int twop7 =
00112 {0x40600000};
00113
00114 #else
00115
00116
00117
00118 static const long long lim1 =
00119 {0x3fee0fabfbc702a3ll};
00120
00121
00122
00123 static const long long lim2 =
00124 {0x3ff1082b577d34eell};
00125
00126 static const long long twop7 =
00127 {0x4060000000000000ll};
00128
00129 #endif
00130
00131 static const du Qnan =
00132 {D(QNANHI, QNANLO)};
00133
00134 static const du Neginf =
00135 {D(0xfff00000, 0x00000000)};
00136
00137 static const du Inf =
00138 {D(0x7ff00000, 0x00000000)};
00139
00140 static const du twopm7 =
00141 {D(0x3f800000, 0x00000000)};
00142
00143 static const du log2_lead =
00144 {D(0x3fe62e42, 0xfefa4000)};
00145
00146 static const du log2_trail =
00147 {D(0xbd48432a, 0x1b0e2634)};
00148
00149 static const du Scaleup =
00150 {D(0x43300000, 0x00000000)};
00151
00152
00153
00154 static const du P[] =
00155 {
00156 {D(0x3ff00000, 0x00000000)},
00157 {D(0xbfe00000, 0x00000001)},
00158 {D(0x3fd55555, 0x55509ba5)},
00159 {D(0xbfcfffff, 0xffeb6526)},
00160 {D(0x3fc999b4, 0xdfed6fe4)},
00161 {D(0xbfc55576, 0x66472e04)},
00162 };
00163
00164
00165
00166
00167
00168 static const du Q[] =
00169 {
00170 {D(0x3ff00000, 0x00000000)},
00171 {D(0x3fb55555, 0x555554ed)},
00172 {D(0x3f899999, 0x99b929bd)},
00173 {D(0x3f624923, 0x14d70150)},
00174 {D(0x3f3c7ff7, 0xdaa9e72e)},
00175 };
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 double
00188 __log( double x )
00189 {
00190 #ifdef _32BIT_MACHINE
00191
00192 int ix, m;
00193
00194 #else
00195
00196 long long ix, m;
00197
00198 #endif
00199
00200 int k;
00201 double g, u, v, x1, x2;
00202 double u1, u2;
00203 double t;
00204 double xmu;
00205 double q;
00206 double result;
00207 double l_lead, l_trail;
00208 #ifdef _CALL_MATHERR
00209 struct exception exstruct;
00210 #endif
00211
00212
00213
00214
00215 #ifdef _32BIT_MACHINE
00216
00217 DBLHI2INT(x, ix);
00218 #else
00219 DBL2LL(x, ix);
00220 #endif
00221 m = (ix >> DMANTWIDTH);
00222
00223 if ( (0 < m) && (m < 0x7ff) )
00224 {
00225
00226
00227 m -= DEXPBIAS;
00228 L:
00229 if ( (ix >= lim2) || (ix <= lim1) )
00230 {
00231
00232
00233 ix &= DEXPMASK;
00234 ix |= twop7;
00235
00236 #ifdef _32BIT_MACHINE
00237
00238 INT2DBLHI(ix, x);
00239 #else
00240 LL2DBL(ix, x);
00241 #endif
00242 k = ROUND(x);
00243
00244 u = k;
00245
00246 k -= 128;
00247
00248 xmu = twopm7.d*(x - u);
00249
00250 t = _log_ru[k].d*xmu;
00251
00252
00253
00254
00255
00256
00257 if ( k > 64 )
00258 m++;
00259
00260 q = ((((P[5].d*t + P[4].d)*t + P[3].d)*t +
00261 P[2].d)*t + P[1].d)*(t*t);
00262
00263 l_lead = _logtabhi[k].d;
00264 l_trail = _logtablo[k].d;
00265
00266 l_lead += m*log2_lead.d;
00267 l_trail += m*log2_trail.d;
00268
00269 result = l_lead + (t + (q + l_trail));
00270
00271 return ( result );
00272 }
00273
00274 if ( x == 1.0 )
00275 return ( 0.0 );
00276
00277 x = x - 1.0;
00278
00279 g = 1.0/(2.0 + x);
00280
00281 u = x*g;
00282 u = u + u;
00283 v = u*u;
00284
00285 q = (((Q[4].d*v + Q[3].d)*v + Q[2].d)*v +
00286 Q[1].d)*(v*u);
00287
00288 u1 = (float)u;
00289 x1 = (float)x;
00290
00291 x2 = x - x1;
00292 u2 = x - u1;
00293 u2 = u2 + u2;
00294 u2 = ((u2 - u1*x1) - u1*x2)*g;
00295
00296 result = u1 + (u2 + q);
00297
00298 return ( result );
00299 }
00300
00301 if ( x != x )
00302 {
00303
00304
00305 #ifdef _CALL_MATHERR
00306
00307 exstruct.type = DOMAIN;
00308 exstruct.name = "log";
00309 exstruct.arg1 = x;
00310 exstruct.retval = Qnan.d;
00311
00312 if ( matherr( &exstruct ) == 0 )
00313 {
00314 fprintf(stderr, "domain error in log\n");
00315 SETERRNO(EDOM);
00316 }
00317
00318 return ( exstruct.retval );
00319 #else
00320 NAN_SETERRNO(EDOM);
00321
00322 return ( Qnan.d );
00323 #endif
00324 }
00325
00326 if ( x == Neginf.d )
00327 {
00328 #ifdef _CALL_MATHERR
00329
00330 exstruct.type = DOMAIN;
00331 exstruct.name = "log";
00332 exstruct.arg1 = x;
00333 exstruct.retval = Qnan.d;
00334
00335 if ( matherr( &exstruct ) == 0 )
00336 {
00337 fprintf(stderr, "domain error in log\n");
00338 SETERRNO(EDOM);
00339 }
00340
00341 return ( exstruct.retval );
00342 #else
00343 SETERRNO(EDOM);
00344
00345 return ( Qnan.d );
00346 #endif
00347 }
00348
00349 if ( x == 0.0 )
00350 {
00351 zeroarg:
00352
00353 #ifdef _CALL_MATHERR
00354
00355 exstruct.type = OVERFLOW;
00356 exstruct.name = "log";
00357 exstruct.arg1 = x;
00358 exstruct.retval = Neginf.d;
00359
00360 if ( matherr( &exstruct ) == 0 )
00361 {
00362 fprintf(stderr, "overflow range error in log\n");
00363 SETERRNO(ERANGE);
00364 }
00365
00366 return ( exstruct.retval );
00367 #else
00368 SETERRNO(ERANGE);
00369
00370 return ( Neginf.d );
00371 #endif
00372 }
00373
00374 if ( x == Inf.d )
00375 {
00376 #ifdef _CALL_MATHERR
00377
00378 exstruct.type = DOMAIN;
00379 exstruct.name = "log";
00380 exstruct.arg1 = x;
00381 exstruct.retval = Inf.d;
00382
00383 if ( matherr( &exstruct ) == 0 )
00384 {
00385 fprintf(stderr, "domain error in log\n");
00386 SETERRNO(EDOM);
00387 }
00388
00389 return ( exstruct.retval );
00390 #else
00391 SETERRNO(EDOM);
00392
00393 return ( Inf.d );
00394 #endif
00395 }
00396
00397 if ( x < 0.0 )
00398 {
00399 #ifdef _CALL_MATHERR
00400
00401 exstruct.type = DOMAIN;
00402 exstruct.name = "log";
00403 exstruct.arg1 = x;
00404 exstruct.retval = Qnan.d;
00405
00406 if ( matherr( &exstruct ) == 0 )
00407 {
00408 fprintf(stderr, "domain error in log\n");
00409 SETERRNO(EDOM);
00410 }
00411
00412 return ( exstruct.retval );
00413 #else
00414 SETERRNO(EDOM);
00415
00416 return ( Qnan.d );
00417 #endif
00418 }
00419
00420
00421
00422
00423
00424 x *= Scaleup.d;
00425
00426 if ( x == 0.0 )
00427 goto zeroarg;
00428
00429 #ifdef _32BIT_MACHINE
00430
00431 DBLHI2INT(x, ix);
00432 #else
00433 DBL2LL(x, ix);
00434 #endif
00435 m = (ix >> DMANTWIDTH);
00436 m -= DEXPBIAS;
00437 m -= 52;
00438 goto L;
00439 }
00440
00441 #ifdef NO_LONG_DOUBLE
00442
00443 #if defined(BUILD_OS_DARWIN)
00444 extern long double __logl(long double);
00445 long double logl( long double x ) {
00446 return ( (long double)__log((double)x) );
00447 }
00448 #elif defined(__GNUC__)
00449 extern long double __logl(long double);
00450
00451 long double logl() __attribute__ ((weak, alias ("__logl")));
00452
00453 #endif
00454
00455 long double
00456 __logl( long double x )
00457 {
00458 return ( (long double)__log((double)x) );
00459 }
00460
00461 #endif
00462