1 /*
   2  * Copyright (c) 1998, 2015, 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 package java.lang;
  27 
  28 /**
  29  * Port of the "Freely Distributable Math Library", version 5.3, from C to Java.
  30  *
  31  * <<Note on avoiding strictfp, worry (or not) about setting the sticky flags>>
  32  * <<prefer floating-point operations to integer ones>>
  33  * <<can't just return huge * huge since that could live on the stack as an inaccurate finite value>>
  34  *
  35  */
  36 class FdLibm {
  37     /**
  38      * A field whose purpose is to witness writes of double values
  39      * whose computation sets the IEEE 754 exception status,
  40      * principally underflow, overflow, and invalid. The actual value
  41      * of this field does not matter. Writes can be racy and the field
  42      * does *not* need to volatile or otherwise synchronized. The
  43      * field is made public (within a package-private class) to thwart
  44      * optimizations that a JVM might otherwise be justified in
  45      * making.
  46      */
  47     public static double exceptionWitness;
  48 
  49     /**
  50      * Return the low-order 32 bits of the double argument as an int.
  51      */
  52     private static int __LO(double x) {
  53         long transducer = Double.doubleToLongBits(x);
  54         return (int)transducer;
  55     }
  56 
  57     /**
  58      * Return the a double with its low-order bits reset.
  59      */
  60     private static double __LO(double x, int low) {
  61         long transX = Double.doubleToLongBits(x);
  62         return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
  63     }
  64 
  65     /**
  66      * Return the high-order 32 bits of the double argument as an int.
  67      */
  68     private static int __HI(double x) {
  69         long transducer = Double.doubleToLongBits(x);
  70         return (int)(transducer >> 32);
  71     }
  72     /**
  73      * Return the a double with its high-order bits reset.
  74      */
  75     private static double __HI(double x, int high) {
  76         long transX = Double.doubleToLongBits(x);
  77         return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
  78     }
  79 
  80     /**
  81      * Compute x**y
  82      *                    n
  83      * Method:  Let x =  2   * (1+f)
  84      *      1. Compute and return log2(x) in two pieces:
  85      *              log2(x) = w1 + w2,
  86      *         where w1 has 53 - 24 = 29 bit trailing zeros.
  87      *      2. Perform y*log2(x) = n+y' by simulating muti-precision
  88      *         arithmetic, where |y'| <= 0.5.
  89      *      3. Return x**y = 2**n*exp(y'*log2)
  90      *
  91      * Special cases:
  92      *      1.  (anything) ** 0  is 1
  93      *      2.  (anything) ** 1  is itself
  94      *      3.  (anything) ** NAN is NAN
  95      *      4.  NAN ** (anything except 0) is NAN
  96      *      5.  +-(|x| > 1) **  +INF is +INF
  97      *      6.  +-(|x| > 1) **  -INF is +0
  98      *      7.  +-(|x| < 1) **  +INF is +0
  99      *      8.  +-(|x| < 1) **  -INF is +INF
 100      *      9.  +-1         ** +-INF is NAN
 101      *      10. +0 ** (+anything except 0, NAN)               is +0
 102      *      11. -0 ** (+anything except 0, NAN, odd integer)  is +0
 103      *      12. +0 ** (-anything except 0, NAN)               is +INF
 104      *      13. -0 ** (-anything except 0, NAN, odd integer)  is +INF
 105      *      14. -0 ** (odd integer) = -( +0 ** (odd integer) )
 106      *      15. +INF ** (+anything except 0,NAN) is +INF
 107      *      16. +INF ** (-anything except 0,NAN) is +0
 108      *      17. -INF ** (anything)  = -0 ** (-anything)
 109      *      18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
 110      *      19. (-anything except 0 and inf) ** (non-integer) is NAN
 111      *
 112      * Accuracy:
 113      *      pow(x,y) returns x**y nearly rounded. In particular
 114      *                      pow(integer,integer)
 115      *      always returns the correct integer provided it is
 116      *      representable.
 117      */
 118     public static class Pow {
 119         static final double bp[]    = {1.0,
 120                                        1.5};
 121         static final double dp_h[]  = {0.0,
 122                                        0x1.2b8034p-1};        // 5.84962487220764160156e-01
 123         static final double dp_l[]  = {0.0,
 124                                        0x1.cfdeb43cfd006p-27};// 1.35003920212974897128e-08
 125         static final double zero    =  0.0;
 126         static final double one     =  1.0;
 127         static final double two     =  2.0;
 128         static final double two53   =  0x1.0p53;              // 9007199254740992.0
 129         static final double huge    =  1.0e300;
 130         static final double tiny    =  1.0e-300;
 131         // Poly coefs for (3/2)*(log(x)-2s-2/3*s**3
 132         static final double L1      =  0x1.3333333333303p-1;  //  5.99999999999994648725e-01
 133         static final double L2      =  0x1.b6db6db6fabffp-2;  //  4.28571428578550184252e-01
 134         static final double L3      =  0x1.55555518f264dp-2;  //  3.33333329818377432918e-01
 135         static final double L4      =  0x1.17460a91d4101p-2;  //  2.72728123808534006489e-01
 136         static final double L5      =  0x1.d864a93c9db65p-3;  //  2.30660745775561754067e-01
 137         static final double L6      =  0x1.a7e284a454eefp-3;  //  2.06975017800338417784e-01
 138         static final double P1      =  0x1.555555555553ep-3;  //  1.66666666666666019037e-01
 139         static final double P2      = -0x1.6c16c16bebd93p-9;  // -2.77777777770155933842e-03
 140         static final double P3      =  0x1.1566aaf25de2cp-14; //  6.61375632143793436117e-05
 141         static final double P4      = -0x1.bbd41c5d26bf1p-20; // -1.65339022054652515390e-06
 142         static final double P5      =  0x1.6376972bea4d0p-25; //  4.13813679705723846039e-08
 143         static final double lg2     =  0x1.62e42fefa39efp-1;  //  6.93147180559945286227e-01
 144         static final double lg2_h   =  0x1.62e43p-1;          //  6.93147182464599609375e-01
 145         static final double lg2_l   = -0x1.05c610ca86c39p-29; // -1.90465429995776804525e-09
 146         static final double ovt     =  8.0085662595372944372e-0017; // -(1024-log2(ovfl+.5ulp))
 147         static final double cp      =  0x1.ec709dc3a03fdp-1;  //  9.61796693925975554329e-01 = 2/(3ln2)
 148         static final double cp_h    =  0x1.ec709ep-1;         //  9.61796700954437255859e-01 = (float)cp
 149         static final double cp_l    = -0x1.e2fe0145b01f5p-28; // -7.02846165095275826516e-09 = tail of cp_h
 150         static final double ivln2   =  0x1.71547652b82fep0;   //  1.44269504088896338700e+00 = 1/ln2
 151         static final double ivln2_h =  0x1.715476p0;          //  1.44269502162933349609e+00 = 24 bits of 1/ln2
 152         static final double ivln2_l =  0x1.4ae0bf85ddf44p-26; //  1.92596299112661746887e-08 = 1/ln2 tail
 153 
 154         public static double compute(final double x, final double y) {
 155             double z;
 156             double t1, t2, r, s, t, u, v, w;
 157             int i, j, k, n;
 158 
 159             // y == zero: x**0 = 1
 160             if (y == 0.0)
 161                 return 1.0;
 162 
 163             // +/-NaN return x + y to propagate NaN significands
 164             if (Double.isNaN(x) || Double.isNaN(y))
 165                 return x + y;
 166 
 167             final double y_abs = Math.abs(y);
 168             double x_abs   = Math.abs(x);
 169             // Special values of y
 170             if (y == 2.0) {
 171                 return x * x;
 172             } else if (y == 0.5) {
 173                 if (x >= -Double.MAX_VALUE) // Handle x == -infinity later
 174                     return Math.sqrt(x + 0.0); // Add 0.0 to properly handle x == -0.0
 175             } else if (y_abs == 1.0) {        // y is  +/-1
 176                 return (y == 1.0) ? x : one / x;
 177             } else if (y_abs == Double.POSITIVE_INFINITY) {       // y is +/-infinity
 178                 if (x_abs == 1.0)
 179                     return  y - y;         // inf**+/-1 is NaN
 180                 else if (x_abs > 1.0) // (|x| > 1)**+/-inf = inf, 0
 181                     return (y >= 0) ? y : zero;
 182                 else                       // (|x| < 1)**-, +inf = inf, 0
 183                     return (y < 0) ? -y : zero;
 184             } 
 185 
 186             final int hx = __HI(x); // Try to replace with copysign usage
 187             // final int lx = __LO(x);
 188             // final int hy = __HI(y);
 189             final int ly = __LO(y);
 190             int ix = hx & 0x7fffffff;
 191             final int iy = __HI(y) & 0x7fffffff; // Try to replace with getExponent in yisint
 192 
 193             /*
 194              * When x < 0, determine if y is an odd integer:
 195              * yisint = 0       ... y is not an integer
 196              * yisint = 1       ... y is an odd int
 197              * yisint = 2       ... y is an even int
 198              */
 199             {
 200                 int yisint  = 0;
 201                 if (hx < 0) {
 202                     if (y_abs >= 0x1.0p53)   // |y| >= 2^53 = 9.007199254740992E15
 203                         yisint = 2; // even integer y
 204                     else if (y_abs >= 1.0) { // |y| >= 1.0
 205                         k = (iy >> 20) - 0x3ff;        // exponent
 206                         if (k > 20) {
 207                             j = ly >> (52 - k);
 208                             if ((j << (52 - k) ) == ly)
 209                                 yisint = 2 - (j & 1);
 210                         } else if (ly == 0) {
 211                             j = iy >> (20 - k);
 212                             if ((j << (20 - k)) == iy) {
 213                                 yisint = 2 - (j & 1);
 214                             }
 215                         }
 216                     }
 217                 }
 218 
 219                 // Special value of x
 220                 if (x_abs == 0.0 ||
 221                     x_abs == Double.POSITIVE_INFINITY ||
 222                     x_abs == 1.0) {
 223                     z = x_abs;                 // x is +/-0, +/-inf, +/-1
 224                     if (y < 0.0)
 225                         z = one/z;     // z = (1/|x|)
 226                     if (hx < 0) {
 227                         if (((ix - 0x3ff00000) | yisint) == 0) {
 228                             z = (z-z)/(z-z); // (-1)**non-int is NaN
 229                         } else if (yisint == 1)
 230                             z = -1.0 * z;             // (x < 0)**odd = -(|x|**odd)
 231                     }
 232                     return z;
 233                 }
 234                 
 235                 n = (hx >> 31) + 1;
 236 
 237                 // (x < 0)**(non-int) is NaN
 238                 if ((n | yisint) == 0)
 239                     return (x-x)/(x-x);
 240 
 241                 s = one; // s (sign of result -ve**odd) = -1 else = 1
 242                 if ( (n | (yisint - 1)) == 0)
 243                     s = -one; // (-ve)**(odd int)
 244             }
 245             double p_h, p_l; 
 246             // |y| is huge
 247             if (y_abs > 0x1.0p31) { // if |y| > 2**31
 248                 if (y_abs > 0x1.0p64){  // if |y| > 2**64, must over/underflow
 249                     if (x_abs <= 0x1.fffffp-1) // |x| <= 0.9999995231628418
 250                         return (y < 0.0) ? huge*huge : tiny*tiny;
 251                     if (x_abs >= 1.0)          // |x| >= 1.0
 252                         return (y > 0.0) ? huge*huge : tiny*tiny;
 253                 }
 254                 // Over/underflow if x is not close to one
 255                 if (x_abs < 0x1.fffffp-1) // |x| < 0.9999995231628418
 256                     return (y < 0.0) ? s*huge*huge : s*tiny*tiny;
 257                 if (x_abs > 1.0)         // |x| > 1.0
 258                     return (y > 0.0) ? s*huge*huge : s*tiny*tiny;
 259                 /*
 260                  * now |1-x| is tiny <= 2**-20, sufficient to compute
 261                  * log(x) by x - x^2/2 + x^3/3 - x^4/4
 262                  */
 263                 t = x_abs - one;        // t has 20 trailing zeros
 264                 w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
 265                 u = ivln2_h * t;      // ivln2_h has 21 sig. bits
 266                 v =  t * ivln2_l - w * ivln2;
 267                 t1 = u + v;
 268                 t1 =__LO(t1, 0);
 269                 t2 = v - (t1 - u);
 270             } else {
 271                 double z_h, z_l, ss, s2, s_h, s_l, t_h, t_l;
 272                 n = 0;
 273                 // Take care of subnormal numbers
 274                 if (ix < 0x00100000) {
 275                     x_abs *= two53;
 276                     n -= 53;
 277                     ix = __HI(x_abs);
 278                 }
 279                 n  += ((ix) >> 20) - 0x3ff;
 280                 j  = ix & 0x000fffff;
 281                 // Determine interval
 282                 ix = j | 0x3ff00000;          // Normalize ix
 283                 if (j <= 0x3988E)
 284                     k = 0;         // |x| <sqrt(3/2)
 285                 else if (j < 0xBB67A)
 286                     k = 1;         // |x| <sqrt(3)
 287                 else {
 288                     k = 0;
 289                     n += 1;
 290                     ix -= 0x00100000;
 291                 }
 292                 x_abs = __HI(x_abs, ix);
 293 
 294                 // Compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5)
 295                 u = x_abs - bp[k];               // bp[0]=1.0, bp[1]=1.5
 296                 v = one / (x_abs + bp[k]);
 297                 ss = u * v;
 298                 s_h = ss;
 299                 s_h = __LO(s_h, 0);
 300                 // t_h=x_abs+bp[k] High
 301                 t_h = zero;
 302                 t_h = __HI(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18) );
 303                 t_l = x_abs - (t_h - bp[k]);
 304                 s_l = v * ((u - s_h * t_h) - s_h * t_l);
 305                 // Compute log(x_abs)
 306                 s2 = ss * ss;
 307                 r = s2 * s2* (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
 308                 r += s_l * (s_h + ss);
 309                 s2  = s_h * s_h;
 310                 t_h = 3.0 + s2 + r;
 311                 t_h = __LO(t_h, 0);
 312                 t_l = r - ((t_h - 3.0) - s2);
 313                 // u+v = ss*(1+...)
 314                 u = s_h * t_h;
 315                 v = s_l * t_h + t_l * ss;
 316                 // 2/(3log2)*(ss+...)
 317                 p_h = u + v;
 318                 p_h = __LO(p_h, 0);
 319                 p_l = v - (p_h - u);
 320                 z_h = cp_h * p_h;             // cp_h+cp_l = 2/(3*log2)
 321                 z_l = cp_l * p_h + p_l * cp + dp_l[k];
 322                 // log2(x_abs) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l
 323                 t = (double)n;
 324                 t1 = (((z_h + z_l) + dp_h[k]) + t);
 325                 t1 = __LO(t1, 0);
 326                 t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
 327             }
 328 
 329             // Split up y into (y1 + y2) and compute (y1 + y2) * (t1 + t2)
 330             double y1  = y;
 331             y1 = __LO(y1, 0);
 332             p_l = (y - y1) * t1 + y * t2;
 333             p_h = y1 * t1;
 334             z = p_l + p_h;
 335             j = __HI(z);
 336             i = __LO(z);
 337             if (j >= 0x40900000) {                            // z >= 1024
 338                 if (((j - 0x40900000) | i)!=0)                   // if z > 1024
 339                     return s*huge*huge;                     // Overflow
 340                 else {
 341                     if (p_l + ovt > z - p_h)
 342                         return s*huge*huge;   // Overflow
 343                 }
 344             } else if ((j & 0x7fffffff) >= 0x4090cc00 ) {        // z <= -1075
 345                 if (((j - 0xc090cc00) | i)!=0)           // z < -1075
 346                     return s*tiny*tiny;             // Underflow
 347                 else {
 348                     if (p_l <= z - p_h)
 349                         return s*tiny*tiny;      // Underflow
 350                 }
 351             }
 352             /*
 353              * Compute 2**(p_h+p_l)
 354              */
 355             i = j & 0x7fffffff;
 356             k = (i >> 20) - 0x3ff;
 357             n = 0;
 358             if (i > 0x3fe00000) {              // if |z| > 0.5, set n = [z+0.5]
 359                 n = j + (0x00100000 >> (k + 1));
 360                 k = ((n & 0x7fffffff) >> 20) - 0x3ff;     // new k for n
 361                 t = zero;
 362                 t = __HI(t, (n & ~(0x000fffff >> k)) );
 363                 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
 364                 if (j < 0)
 365                     n = -n;
 366                 p_h -= t;
 367             }
 368             t = p_l + p_h;
 369             t = __LO(t, 0);
 370             u = t * lg2_h;
 371             v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
 372             z = u + v;
 373             w = v - (z - u);
 374             t  = z * z;
 375             t1  = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
 376             r  = (z * t1)/(t1 - two) - (w + z * w);
 377             z  = one - (r - z);
 378             j  = __HI(z);
 379             j += (n << 20);
 380             if ((j >> 20) <= 0)
 381                 z = Math.scalb(z, n); // subnormal output
 382             else {
 383                 int z_hi = __HI(z);
 384                 z_hi += (n << 20);
 385                 z = __HI(z, z_hi);
 386             }
 387             return s * z;
 388         }
 389     }
 390 }