/*Converting from double precision to Multi-precision and calculating e^x */ double SECTION __slowexp(double x) { double w,z,res,eps=3.0e-26; #if 0 double y; #endif int p; #if 0 int orig,i; #endif mp_no mpx, mpy, mpz,mpw,mpeps,mpcor; p=6; __dbl_mp(x,&mpx,p); /* Convert a double precision number x */ /* into a multiple precision number mpx with prec. p. */ __mpexp(&mpx, &mpy, p); /* Multi-Precision exponential function */ __dbl_mp(eps,&mpeps,p); __mul(&mpeps,&mpy,&mpcor,p); __add(&mpy,&mpcor,&mpw,p); __sub(&mpy,&mpcor,&mpz,p); __mp_dbl(&mpw, &w, p); __mp_dbl(&mpz, &z, p); if (w == z) return w; else { /* if calculating is not exactly */ p = 32; __dbl_mp(x,&mpx,p); __mpexp(&mpx, &mpy, p); __mp_dbl(&mpy, &res, p); return res; } }
/* Final stages. Compute atan(x) by multiple precision arithmetic */ static double atanMp (double x, const int pr[]) { mp_no mpx, mpy, mpy2, mperr, mpt1, mpy1; double y1, y2; int i, p; for (i = 0; i < M; i++) { p = pr[i]; __dbl_mp (x, &mpx, p); __mpatan (&mpx, &mpy, p); __dbl_mp (u9[i].d, &mpt1, p); __mul (&mpy, &mpt1, &mperr, p); __add (&mpy, &mperr, &mpy1, p); __sub (&mpy, &mperr, &mpy2, p); __mp_dbl (&mpy1, &y1, p); __mp_dbl (&mpy2, &y2, p); if (y1 == y2) { LIBC_PROBE (slowatan, 3, &p, &x, &y1); return y1; } } LIBC_PROBE (slowatan_inexact, 3, &p, &x, &y1); return y1; /*if impossible to do exact computing */ }
double SECTION __cos32(double x, double res, double res1) { int p; mp_no a,b,c; p=32; __dbl_mp(res,&a,p); __dbl_mp(0.5*(res1-res),&b,p); __add(&a,&b,&c,p); if (x>2.4) { __sub(&pi,&c,&a,p); __c32(&a,&b,&c,p); b.d[0]=-b.d[0]; } else if (x>0.8) { __sub(&hp,&c,&a,p); __c32(&a,&c,&b,p); } else __c32(&c,&b,&a,p); /* b=cos(0.5*(res+res1)) */ __dbl_mp(x,&c,p); /* c = x */ __sub(&b,&c,&a,p); /* if a>0 return max(res,res1), otherwise return min(res,res1) */ if (a.d[0]>0) return (res>res1)?res:res1; else return (res<res1)?res:res1; }
/*Converting from double precision to Multi-precision and calculating e^x */ double SECTION __slowexp (double x) { #ifndef USE_LONG_DOUBLE_FOR_MP double w, z, res, eps = 3.0e-26; int p; mp_no mpx, mpy, mpz, mpw, mpeps, mpcor; /* Use the multiple precision __MPEXP function to compute the exponential First at 144 bits and if it is not accurate enough, at 768 bits. */ p = 6; __dbl_mp (x, &mpx, p); __mpexp (&mpx, &mpy, p); __dbl_mp (eps, &mpeps, p); __mul (&mpeps, &mpy, &mpcor, p); __add (&mpy, &mpcor, &mpw, p); __sub (&mpy, &mpcor, &mpz, p); __mp_dbl (&mpw, &w, p); __mp_dbl (&mpz, &z, p); if (w == z) return w; else { p = 32; __dbl_mp (x, &mpx, p); __mpexp (&mpx, &mpy, p); __mp_dbl (&mpy, &res, p); return res; } #else return (double) __ieee754_expl((long double)x); #endif }
/* Stage 3: Perform a multi-Precision computation */ static double SECTION atan2Mp (double x, double y, const int pr[]) { double z1, z2; int i, p; mp_no mpx, mpy, mpz, mpz1, mpz2, mperr, mpt1; for (i = 0; i < MM; i++) { p = pr[i]; __dbl_mp (x, &mpx, p); __dbl_mp (y, &mpy, p); __mpatan2 (&mpy, &mpx, &mpz, p); __dbl_mp (ud[i].d, &mpt1, p); __mul (&mpz, &mpt1, &mperr, p); __add (&mpz, &mperr, &mpz1, p); __sub (&mpz, &mperr, &mpz2, p); __mp_dbl (&mpz1, &z1, p); __mp_dbl (&mpz2, &z2, p); if (z1 == z2) { LIBC_PROBE (slowatan2, 4, &p, &x, &y, &z1); return z1; } } LIBC_PROBE (slowatan2_inexact, 4, &p, &x, &y, &z1); return z1; /*if impossible to do exact computing */ }
/* Receive double x and two double results of cos(x) and return result which is more accurate, computing cos(x) with multi precision routine c32. */ double SECTION __cos32 (double x, double res, double res1) { int p; mp_no a, b, c; p = 32; __dbl_mp (res, &a, p); __dbl_mp (0.5 * (res1 - res), &b, p); __add (&a, &b, &c, p); if (x > 2.4) { __sub (&pi, &c, &a, p); __c32 (&a, &b, &c, p); b.d[0] = -b.d[0]; } else if (x > 0.8) { __sub (&hp, &c, &a, p); __c32 (&a, &c, &b, p); } else __c32 (&c, &b, &a, p); /* b=cos(0.5*(res+res1)) */ __dbl_mp (x, &c, p); /* c = x */ __sub (&b, &c, &a, p); /* if a > 0 return max (res, res1), otherwise return min (res, res1). */ if ((a.d[0] > 0 && res <= res1) || (a.d[0] <= 0 && res >= res1)) res = res1; LIBC_PROBE (slowacos, 2, &res, &x); return res; }
/*Converting from double precision to Multi-precision and calculating e^x */ double __slowexp(double x) { #ifdef NO_LONG_DOUBLE double w,z,res,eps=3.0e-26; int p; mp_no mpx, mpy, mpz,mpw,mpeps,mpcor; p=6; __dbl_mp(x,&mpx,p); /* Convert a double precision number x */ /* into a multiple precision number mpx with prec. p. */ __mpexp(&mpx, &mpy, p); /* Multi-Precision exponential function */ __dbl_mp(eps,&mpeps,p); __mul(&mpeps,&mpy,&mpcor,p); __add(&mpy,&mpcor,&mpw,p); __sub(&mpy,&mpcor,&mpz,p); __mp_dbl(&mpw, &w, p); __mp_dbl(&mpz, &z, p); if (w == z) return w; else { /* if calculating is not exactly */ p = 32; __dbl_mp(x,&mpx,p); __mpexp(&mpx, &mpy, p); __mp_dbl(&mpy, &res, p); return res; } #else return (double) __ieee754_expl((long double)x); #endif }
/* Perform range reduction of a double number x into multi precision number y, such that y = x - n * pi / 2, abs (y) < pi / 4, n = 0, +-1, +-2, ... Return int which indicates in which quarter of circle x is. */ int SECTION __mpranred (double x, mp_no *y, int p) { number v; double t, xn; int i, k, n; mp_no a, b, c; if (ABS (x) < 2.8e14) { t = (x * hpinv.d + toint.d); xn = t - toint.d; v.d = t; n = v.i[LOW_HALF] & 3; __dbl_mp (xn, &a, p); __mul (&a, &hp, &b, p); __dbl_mp (x, &c, p); __sub (&c, &b, y, p); return n; } else { /* If x is very big more precision required. */ __dbl_mp (x, &a, p); a.d[0] = 1.0; k = a.e - 5; if (k < 0) k = 0; b.e = -k; b.d[0] = 1.0; for (i = 0; i < p; i++) b.d[i + 1] = toverp[i + k]; __mul (&a, &b, &c, p); t = c.d[c.e]; for (i = 1; i <= p - c.e; i++) c.d[i] = c.d[i + c.e]; for (i = p + 1 - c.e; i <= p; i++) c.d[i] = 0; c.e = 0; if (c.d[1] >= HALFRAD) { t += 1.0; __sub (&c, &__mpone, &b, p); __mul (&b, &hp, y, p); } else __mul (&c, &hp, y, p); n = (int) t; if (x < 0) { y->d[0] = -y->d[0]; n = -n; } return (n & 3); } }
/* Compute cos() of double-length number (X + DX) as Multi Precision number and return result as double. If REDUCE_RANGE is true, X is assumed to be the original input and DX is ignored. */ double SECTION __mpcos (double x, double dx, bool reduce_range) { double y; mp_no a, b, c, s; int n; int p = 32; if (reduce_range) { n = __mpranred (x, &a, p); /* n is 0, 1, 2 or 3. */ __c32 (&a, &c, &s, p); } else { n = -1; __dbl_mp (x, &b, p); __dbl_mp (dx, &c, p); __add (&b, &c, &a, p); if (x > 0.8) { __sub (&hp, &a, &b, p); __c32 (&b, &s, &c, p); } else __c32 (&a, &c, &s, p); /* a = cos(x+dx) */ } /* Convert result based on which quarter of unit circle y is in. */ switch (n) { case 1: __mp_dbl (&s, &y, p); y = -y; break; case 3: __mp_dbl (&s, &y, p); break; case 2: __mp_dbl (&c, &y, p); y = -y; break; /* Quadrant not set, so the result must be cos (X + DX), which is also stored in C. */ case 0: default: __mp_dbl (&c, &y, p); } LIBC_PROBE (slowcos, 3, &x, &dx, &y); return y; }
double __mpsin(double x, double dx) { int p; double y; mp_no a,b,c; p=32; __dbl_mp(x,&a,p); __dbl_mp(dx,&b,p); __add(&a,&b,&c,p); if (x>0.8) { __sub(&hp,&c,&a,p); __c32(&a,&b,&c,p); } else __c32(&c,&a,&b,p); /* b = sin(x+dx) */ __mp_dbl(&b,&y,p); return y; }
/* Treat the Denormalized case */ static double SECTION normalized (double ax, double ay, double y, double z) { int p; mp_no mpx, mpy, mpz, mperr, mpz2, mpt1; p = 6; __dbl_mp (ax, &mpx, p); __dbl_mp (ay, &mpy, p); __dvd (&mpy, &mpx, &mpz, p); __dbl_mp (ue.d, &mpt1, p); __mul (&mpz, &mpt1, &mperr, p); __sub (&mpz, &mperr, &mpz2, p); __mp_dbl (&mpz2, &z, p); return signArctan2 (y, z); }
void __inv(const mp_no *x, mp_no *y, int p) { int i; #if 0 int l; #endif double t; mp_no z,w; static const int np1[] = {0,0,0,0,1,2,2,2,2,3,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}; const mp_no mptwo = {1,{1.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}}; __cpy(x,&z,p); z.e=0; __mp_dbl(&z,&t,p); t=ONE/t; __dbl_mp(t,y,p); EY -= EX; for (i=0; i<np1[p]; i++) { __cpy(y,&w,p); __mul(x,&w,y,p); __sub(&mptwo,y,&z,p); __mul(&w,&z,y,p); } return; }
void SECTION __mpsqrt (mp_no *x, mp_no *y, int p) { int i, m, ey; double dx, dy; static const mp_no mphalf = {0, {1.0, HALFRAD}}; static const mp_no mp3halfs = {1, {1.0, 1.0, HALFRAD}}; mp_no mpxn, mpz, mpu, mpt1, mpt2; ey = EX / 2; __cpy (x, &mpxn, p); mpxn.e -= (ey + ey); __mp_dbl (&mpxn, &dx, p); dy = fastiroot (dx); __dbl_mp (dy, &mpu, p); __mul (&mpxn, &mphalf, &mpz, p); m = __mpsqrt_mp[p]; for (i = 0; i < m; i++) { __sqr (&mpu, &mpt1, p); __mul (&mpt1, &mpz, &mpt2, p); __sub (&mp3halfs, &mpt2, &mpt1, p); __mul (&mpu, &mpt1, &mpt2, p); __cpy (&mpt2, &mpu, p); } __mul (&mpxn, &mpu, y, p); EY += ey; }
/* Invert *X and store in *Y. Relative error bound: - For P = 2: 1.001 * R ^ (1 - P) - For P = 3: 1.063 * R ^ (1 - P) - For P > 3: 2.001 * R ^ (1 - P) *X = 0 is not permissible. */ static void SECTION __inv (const mp_no *x, mp_no *y, int p) { long i; double t; mp_no z, w; static const int np1[] = { 0, 0, 0, 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; __cpy (x, &z, p); z.e = 0; __mp_dbl (&z, &t, p); t = 1 / t; __dbl_mp (t, y, p); EY -= EX; for (i = 0; i < np1[p]; i++) { __cpy (y, &w, p); __mul (x, &w, y, p); __sub (&__mptwo, y, &z, p); __mul (&w, &z, y, p); } }
/*Converting from double precision to Multi-precision and calculating e^x */ double SECTION __slowexp (double x) { #ifndef USE_LONG_DOUBLE_FOR_MP double w, z, res, eps = 3.0e-26; int p; mp_no mpx, mpy, mpz, mpw, mpeps, mpcor; /* Use the multiple precision __MPEXP function to compute the exponential First at 144 bits and if it is not accurate enough, at 768 bits. */ p = 6; __dbl_mp (x, &mpx, p); __mpexp (&mpx, &mpy, p); __dbl_mp (eps, &mpeps, p); __mul (&mpeps, &mpy, &mpcor, p); __add (&mpy, &mpcor, &mpw, p); __sub (&mpy, &mpcor, &mpz, p); __mp_dbl (&mpw, &w, p); __mp_dbl (&mpz, &z, p); if (w == z) { /* Track how often we get to the slow exp code plus its input/output values. */ LIBC_PROBE (slowexp_p6, 2, &x, &w); return w; } else { p = 32; __dbl_mp (x, &mpx, p); __mpexp (&mpx, &mpy, p); __mp_dbl (&mpy, &res, p); /* Track how often we get to the uber-slow exp code plus its input/output values. */ LIBC_PROBE (slowexp_p32, 2, &x, &res); return res; } #else return (double) __ieee754_expl((long double)x); #endif }
double __slowpow(double x, double y, double z) { double res,res1; mp_no mpx, mpy, mpz,mpw,mpp,mpr,mpr1; static const mp_no eps = {-3,{1.0,4.0}}; int p; res = __halfulp(x,y); /* halfulp() returns -10 or x^y */ if (res >= 0) return res; /* if result was really computed by halfulp */ /* else, if result was not really computed by halfulp */ p = 10; /* p=precision */ __dbl_mp(x,&mpx,p); __dbl_mp(y,&mpy,p); __dbl_mp(z,&mpz,p); __mplog(&mpx, &mpz, p); /* log(x) = z */ __mul(&mpy,&mpz,&mpw,p); /* y * z =w */ __mpexp(&mpw, &mpp, p); /* e^w =pp */ __add(&mpp,&eps,&mpr,p); /* pp+eps =r */ __mp_dbl(&mpr, &res, p); __sub(&mpp,&eps,&mpr1,p); /* pp -eps =r1 */ __mp_dbl(&mpr1, &res1, p); /* converting into double precision */ if (res == res1) return res; p = 32; /* if we get here result wasn't calculated exactly, continue */ __dbl_mp(x,&mpx,p); /* for more exact calculation */ __dbl_mp(y,&mpy,p); __dbl_mp(z,&mpz,p); __mplog(&mpx, &mpz, p); /* log(c)=z */ __mul(&mpy,&mpz,&mpw,p); /* y*z =w */ __mpexp(&mpw, &mpp, p); /* e^w=pp */ __mp_dbl(&mpp, &res, p); /* converting into double precision */ return res; }
/*Converting from double precision to Multi-precision and calculating e^x */ double __slowexp(double x) { double w,z,res,eps=3.0e-26; #if 0 double y; #endif int p; #if 0 int orig,i; #endif mp_no mpx, mpy, mpz,mpw,mpeps,mpcor; p=6; __dbl_mp(x,&mpx,p); /* Convert a double precision number x */ /* into a multiple precision number mpx with prec. p. */ __mpexp(&mpx, &mpy, p); /* Multi-Precision exponential function */ __dbl_mp(eps,&mpeps,p); __mul(&mpeps,&mpy,&mpcor,p); __add(&mpy,&mpcor,&mpw,p); __sub(&mpy,&mpcor,&mpz,p); __mp_dbl(&mpw, &w, p); __mp_dbl(&mpz, &z, p); if (w == z) { /* Track how often we get to the slow exp code plus its input/output values. */ LIBC_PROBE (slowexp_p6, 2, &x, &w); return w; } else { /* if calculating is not exactly */ p = 32; __dbl_mp(x,&mpx,p); __mpexp(&mpx, &mpy, p); __mp_dbl(&mpy, &res, p); /* Track how often we get to the uber-slow exp code plus its input/output values. */ LIBC_PROBE (slowexp_p32, 2, &x, &res); return res; } }
/* Multi-Precision exponential function subroutine (for p >= 4, 2**(-55) <= abs(x) <= 1024). */ void SECTION __mpexp (mp_no *x, mp_no *y, int p) { int i, j, k, m, m1, m2, n; mantissa_t b; static const int np[33] = { 0, 0, 0, 0, 3, 3, 4, 4, 5, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8 }; static const int m1p[33] = { 0, 0, 0, 0, 17, 23, 23, 28, 27, 38, 42, 39, 43, 47, 43, 47, 50, 54, 57, 60, 64, 67, 71, 74, 68, 71, 74, 77, 70, 73, 76, 78, 81 }; static const int m1np[7][18] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 36, 48, 60, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 24, 32, 40, 48, 56, 64, 72, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 17, 23, 29, 35, 41, 47, 53, 59, 65, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 23, 28, 33, 38, 42, 47, 52, 57, 62, 66, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 39, 43, 47, 51, 55, 59, 63}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 47, 50, 54} }; mp_no mps, mpk, mpt1, mpt2; /* Choose m,n and compute a=2**(-m). */ n = np[p]; m1 = m1p[p]; b = X[1]; m2 = 24 * EX; for (; b < HALFRAD; m2--) b *= 2; if (b == HALFRAD) { for (i = 2; i <= p; i++) { if (X[i] != 0) break; } if (i == p + 1) m2--; } m = m1 + m2; if (__glibc_unlikely (m <= 0)) { /* The m1np array which is used to determine if we can reduce the polynomial expansion iterations, has only 18 elements. Besides, numbers smaller than those required by p >= 18 should not come here at all since the fast phase of exp returns 1.0 for anything less than 2^-55. */ assert (p < 18); m = 0; for (i = n - 1; i > 0; i--, n--) if (m1np[i][p] + m2 > 0) break; } /* Compute s=x*2**(-m). Put result in mps. This is the range-reduced input that we will use to compute e^s. For the final result, simply raise it to 2^m. */ __pow_mp (-m, &mpt1, p); __mul (x, &mpt1, &mps, p); /* Compute the Taylor series for e^s: 1 + x/1! + x^2/2! + x^3/3! ... for N iterations. We compute this as: e^x = 1 + (x * n!/1! + x^2 * n!/2! + x^3 * n!/3!) / n! = 1 + (x * (n!/1! + x * (n!/2! + x * (n!/3! + x ...)))) / n! k! is computed on the fly as KF and at the end of the polynomial loop, KF is n!, which can be used directly. */ __cpy (&mps, &mpt2, p); double kf = 1.0; /* Evaluate the rest. The result will be in mpt2. */ for (k = n - 1; k > 0; k--) { /* n! / k! = n * (n - 1) ... * (n - k + 1) */ kf *= k + 1; __dbl_mp (kf, &mpk, p); __add (&mpt2, &mpk, &mpt1, p); __mul (&mps, &mpt1, &mpt2, p); } __dbl_mp (kf, &mpk, p); __dvd (&mpt2, &mpk, &mpt1, p); __add (&__mpone, &mpt1, &mpt2, p); /* Raise polynomial value to the power of 2**m. Put result in y. */ for (k = 0, j = 0; k < m;) { __sqr (&mpt2, &mpt1, p); k++; if (k == m) { j = 1; break; } __sqr (&mpt1, &mpt2, p); k++; } if (j) __cpy (&mpt1, y, p); else __cpy (&mpt2, y, p); return; }
void __mpexp(mp_no *x, mp_no *y, int p) { int i,j,k,m,m1,m2,n; Double a,b; static const int np[33] = {0,0,0,0,3,3,4,4,5,4,4,5,5,5,6,6,6,6,6,6, 6,6,6,6,7,7,7,7,8,8,8,8,8}; static const int m1p[33]= {0,0,0,0,17,23,23,28,27,38,42,39,43,47,43,47,50,54, 57,60,64,67,71,74,68,71,74,77,70,73,76,78,81}; static const int m1np[7][18] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0,36,48,60,72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0,24,32,40,48,56,64,72, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0,17,23,29,35,41,47,53,59,65, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0,23,28,33,38,42,47,52,57,62,66, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0,27, 0, 0,39,43,47,51,55,59,63}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,43,47,50,54}}; mp_no mpone = {0,{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}}; mp_no mpk = {0,{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}}; mp_no mps,mpak,mpt1,mpt2; /* Choose m,n and compute a=2**(-m) */ n = np[p]; m1 = m1p[p]; a = twomm1[p].d(); for (i=0; i<EX; i++) a *= RADIXI; for ( ; i>EX; i--) a *= RADIX; b = X[1]*RADIXI; m2 = 24*EX; for (; b<HALF; m2--) { a *= TWO; b *= TWO; } if (b == HALF) { for (i=2; i<=p; i++) { if (X[i]!=ZERO) break; } if (i==p+1) { m2--; a *= TWO; } } if ((m=m1+m2) <= 0) { m=0; a=ONE; for (i=n-1; i>0; i--,n--) { if (m1np[i][p]+m2>0) break; } } /* Compute s=x*2**(-m). Put result in mps */ __dbl_mp(a,&mpt1,p); __mul(x,&mpt1,&mps,p); /* Evaluate the polynomial. Put result in mpt2 */ mpone.e=1; mpone.d(0)=ONE; mpone.d(1)=ONE; mpk.e = 1; mpk.d(0) = ONE; mpk.d(1)=nn[n].d(); __dvd(&mps,&mpk,&mpt1,p); __add(&mpone,&mpt1,&mpak,p); for (k=n-1; k>1; k--) { __mul(&mps,&mpak,&mpt1,p); mpk.d(1)=nn[k].d(); __dvd(&mpt1,&mpk,&mpt2,p); __add(&mpone,&mpt2,&mpak,p); } __mul(&mps,&mpak,&mpt1,p); __add(&mpone,&mpt1,&mpt2,p); /* Raise polynomial value to the power of 2**m. Put result in y */ for (k=0,j=0; k<m; ) { __mul(&mpt2,&mpt2,&mpt1,p); k++; if (k==m) { j=1; break; } __mul(&mpt1,&mpt1,&mpt2,p); k++; } if (j) __cpy(&mpt1,y,p); else __cpy(&mpt2,y,p); return; }
double SECTION __ieee754_log(double x) { #define M 4 static const int pr[M]={8,10,18,32}; int i,j,n,ux,dx,p; #if 0 int k; #endif double dbl_n,u,p0,q,r0,w,nln2a,luai,lubi,lvaj,lvbj, sij,ssij,ttij,A,B,B0,y,y1,y2,polI,polII,sa,sb, t1,t2,t7,t8,t,ra,rb,ww, a0,aa0,s1,s2,ss2,s3,ss3,a1,aa1,a,aa,b,bb,c; #ifndef DLA_FMS double t3,t4,t5,t6; #endif number num; mp_no mpx,mpy,mpy1,mpy2,mperr; #include "ulog.tbl" #include "ulog.h" /* Treating special values of x ( x<=0, x=INF, x=NaN etc.). */ num.d = x; ux = num.i[HIGH_HALF]; dx = num.i[LOW_HALF]; n=0; if (__builtin_expect(ux < 0x00100000, 0)) { if (__builtin_expect(((ux & 0x7fffffff) | dx) == 0, 0)) return MHALF/ZERO; /* return -INF */ if (__builtin_expect(ux < 0, 0)) return (x-x)/ZERO; /* return NaN */ n -= 54; x *= two54.d; /* scale x */ num.d = x; } if (__builtin_expect(ux >= 0x7ff00000, 0)) return x+x; /* INF or NaN */ /* Regular values of x */ w = x-ONE; if (__builtin_expect(ABS(w) > U03, 1)) { goto case_03; } /*--- Stage I, the case abs(x-1) < 0.03 */ t8 = MHALF*w; EMULV(t8,w,a,aa,t1,t2,t3,t4,t5) EADD(w,a,b,bb) /* Evaluate polynomial II */ polII = (b0.d+w*(b1.d+w*(b2.d+w*(b3.d+w*(b4.d+ w*(b5.d+w*(b6.d+w*(b7.d+w*b8.d))))))))*w*w*w; c = (aa+bb)+polII; /* End stage I, case abs(x-1) < 0.03 */ if ((y=b+(c+b*E2)) == b+(c-b*E2)) return y; /*--- Stage II, the case abs(x-1) < 0.03 */ a = d11.d+w*(d12.d+w*(d13.d+w*(d14.d+w*(d15.d+w*(d16.d+ w*(d17.d+w*(d18.d+w*(d19.d+w*d20.d)))))))); EMULV(w,a,s2,ss2,t1,t2,t3,t4,t5) ADD2(d10.d,dd10.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d9.d,dd9.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d8.d,dd8.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d7.d,dd7.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d6.d,dd6.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d5.d,dd5.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d4.d,dd4.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d3.d,dd3.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(d2.d,dd2.d,s2,ss2,s3,ss3,t1,t2) MUL2(w,ZERO,s3,ss3,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) MUL2(w,ZERO,s2,ss2,s3,ss3,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(w,ZERO, s3,ss3, b, bb,t1,t2) /* End stage II, case abs(x-1) < 0.03 */ if ((y=b+(bb+b*E4)) == b+(bb-b*E4)) return y; goto stage_n; /*--- Stage I, the case abs(x-1) > 0.03 */ case_03: /* Find n,u such that x = u*2**n, 1/sqrt(2) < u < sqrt(2) */ n += (num.i[HIGH_HALF] >> 20) - 1023; num.i[HIGH_HALF] = (num.i[HIGH_HALF] & 0x000fffff) | 0x3ff00000; if (num.d > SQRT_2) { num.d *= HALF; n++; } u = num.d; dbl_n = (double) n; /* Find i such that ui=1+(i-75)/2**8 is closest to u (i= 0,1,2,...,181) */ num.d += h1.d; i = (num.i[HIGH_HALF] & 0x000fffff) >> 12; /* Find j such that vj=1+(j-180)/2**16 is closest to v=u/ui (j= 0,...,361) */ num.d = u*Iu[i].d + h2.d; j = (num.i[HIGH_HALF] & 0x000fffff) >> 4; /* Compute w=(u-ui*vj)/(ui*vj) */ p0=(ONE+(i-75)*DEL_U)*(ONE+(j-180)*DEL_V); q=u-p0; r0=Iu[i].d*Iv[j].d; w=q*r0; /* Evaluate polynomial I */ polI = w+(a2.d+a3.d*w)*w*w; /* Add up everything */ nln2a = dbl_n*LN2A; luai = Lu[i][0].d; lubi = Lu[i][1].d; lvaj = Lv[j][0].d; lvbj = Lv[j][1].d; EADD(luai,lvaj,sij,ssij) EADD(nln2a,sij,A ,ttij) B0 = (((lubi+lvbj)+ssij)+ttij)+dbl_n*LN2B; B = polI+B0; /* End stage I, case abs(x-1) >= 0.03 */ if ((y=A+(B+E1)) == A+(B-E1)) return y; /*--- Stage II, the case abs(x-1) > 0.03 */ /* Improve the accuracy of r0 */ EMULV(p0,r0,sa,sb,t1,t2,t3,t4,t5) t=r0*((ONE-sa)-sb); EADD(r0,t,ra,rb) /* Compute w */ MUL2(q,ZERO,ra,rb,w,ww,t1,t2,t3,t4,t5,t6,t7,t8) EADD(A,B0,a0,aa0) /* Evaluate polynomial III */ s1 = (c3.d+(c4.d+c5.d*w)*w)*w; EADD(c2.d,s1,s2,ss2) MUL2(s2,ss2,w,ww,s3,ss3,t1,t2,t3,t4,t5,t6,t7,t8) MUL2(s3,ss3,w,ww,s2,ss2,t1,t2,t3,t4,t5,t6,t7,t8) ADD2(s2,ss2,w,ww,s3,ss3,t1,t2) ADD2(s3,ss3,a0,aa0,a1,aa1,t1,t2) /* End stage II, case abs(x-1) >= 0.03 */ if ((y=a1+(aa1+E3)) == a1+(aa1-E3)) return y; /* Final stages. Use multi-precision arithmetic. */ stage_n: for (i=0; i<M; i++) { p = pr[i]; __dbl_mp(x,&mpx,p); __dbl_mp(y,&mpy,p); __mplog(&mpx,&mpy,p); __dbl_mp(e[i].d,&mperr,p); __add(&mpy,&mperr,&mpy1,p); __sub(&mpy,&mperr,&mpy2,p); __mp_dbl(&mpy1,&y1,p); __mp_dbl(&mpy2,&y2,p); if (y1==y2) return y1; } return y1; }