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 30 * C to Java. 31 * 32 * <p>The C version of fdlibm relied on the idiom of pointer aliasing 33 * a 64-bit double floating-point value as a two-element array of 34 * 32-bit integers and reading and writing the two halves of the 35 * double independently. This coding pattern was problematic to C 36 * optimizers and not directly expressible in Java. Therefore, rather 37 * than a memory level overlay, if portions of a double need to be 38 * operated on as integer values, the standard library methods for 39 * bitwise floating-point to integer conversion, 40 * Double.longBitsToDouble and Double.doubleToRawLongBits, are directly 41 * or indirectly used. 42 * 43 * <p>The C version of fdlibm also took some pains to signal the 44 * correct IEEE 754 exceptional conditions divide by zero, invalid, 45 * overflow and underflow. For example, overflow would be signaled by 46 * {@code huge * huge} where {@code huge} was a large constant that 47 * would overflow when squared. Since IEEE floating-point exceptional 48 * handling is not supported natively in the JVM, such coding patterns 49 * have been omitted from this port. For example, rather than {@code 50 * return huge * huge}, this port will use {@code return INFINITY}. 51 * 52 * <p>Various comparison and arithmetic operations in fdlibm could be 53 * done either based on the integer view of a value or directly on the 54 * floating-point representation. Which idiom is faster may depend on 55 * platform specific factors. However, for code clarity if no other 56 * reason, this port will favor expressing the semantics of those 57 * operations in terms of floating-point operations when convenient to 58 * do so. 59 */ 60 class FdLibm { 61 // Constants used by multiple algorithms 62 private static final double INFINITY = Double.POSITIVE_INFINITY; 63 64 private FdLibm() { 65 throw new UnsupportedOperationException("No FdLibm instances for you."); 66 } 67 68 /** 69 * Return the low-order 32 bits of the double argument as an int. 70 */ 71 private static int __LO(double x) { 72 long transducer = Double.doubleToRawLongBits(x); 73 return (int)transducer; 74 } 75 76 /** 77 * Return a double with its low-order bits of the second argument 78 * and the high-order bits of the first argument.. 79 */ 80 private static double __LO(double x, int low) { 81 long transX = Double.doubleToRawLongBits(x); 82 return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low ); 83 } 84 85 /** 86 * Return the high-order 32 bits of the double argument as an int. 87 */ 88 private static int __HI(double x) { 89 long transducer = Double.doubleToRawLongBits(x); 90 return (int)(transducer >> 32); 91 } 92 93 /** 94 * Return a double with its high-order bits of the second argument 95 * and the low-order bits of the first argument.. 96 */ 97 private static double __HI(double x, int high) { 98 long transX = Double.doubleToRawLongBits(x); 99 return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 ); 100 } 101 102 /** 103 * hypot(x,y) 104 * 105 * Method : 106 * If (assume round-to-nearest) z = x*x + y*y 107 * has error less than sqrt(2)/2 ulp, than 108 * sqrt(z) has error less than 1 ulp (exercise). 109 * 110 * So, compute sqrt(x*x + y*y) with some care as 111 * follows to get the error below 1 ulp: 112 * 113 * Assume x > y > 0; 114 * (if possible, set rounding to round-to-nearest) 115 * 1. if x > 2y use 116 * x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y 117 * where x1 = x with lower 32 bits cleared, x2 = x - x1; else 118 * 2. if x <= 2y use 119 * t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y)) 120 * where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1, 121 * y1= y with lower 32 bits chopped, y2 = y - y1. 122 * 123 * NOTE: scaling may be necessary if some argument is too 124 * large or too tiny 125 * 126 * Special cases: 127 * hypot(x,y) is INF if x or y is +INF or -INF; else 128 * hypot(x,y) is NAN if x or y is NAN. 129 * 130 * Accuracy: 131 * hypot(x,y) returns sqrt(x^2 + y^2) with error less 132 * than 1 ulp (unit in the last place) 133 */ 134 public static class Hypot { 135 public static final double TWO_MINUS_600 = 0x1.0p-600; 136 public static final double TWO_PLUS_600 = 0x1.0p+600; 137 138 public static strictfp double compute(double x, double y) { 139 double a = Math.abs(x); 140 double b = Math.abs(y); 141 142 if (!Double.isFinite(a) || !Double.isFinite(b)) { 143 if (a == INFINITY || b == INFINITY) 144 return INFINITY; 145 else 146 return a + b; // Propagate NaN significand bits 147 } 148 149 if (b > a) { 150 double tmp = a; 151 a = b; 152 b = tmp; 153 } 154 assert a >= b; 155 156 // Doing bitwise conversion after screening for NaN allows 157 // the code to not worry about the possibility of 158 // "negative" NaN values. 159 160 // Note: the ha and hb variables are the high-order 161 // 32-bits of a and b stored as integer values. The ha and 162 // hb values are used first for a rough magnitude 163 // comparison of a and b and second for simulating higher 164 // precision by allowing a and b, respectively, to be 165 // decomposed into non-overlapping portions. Both of these 166 // uses could be eliminated. The magnitude comparison 167 // could be eliminated by extracting and comparing the 168 // exponents of a and b or just be performing a 169 // floating-point divide. Splitting a floating-point 170 // number into non-overlapping portions can be 171 // accomplished by judicious use of multiplies and 172 // additions. For details see T. J. Dekker, A Floating 173 // Point Technique for Extending the Available Precision , 174 // Numerische Mathematik, vol. 18, 1971, pp.224-242 and 175 // subsequent work. 176 177 int ha = __HI(a); // high word of a 178 int hb = __HI(b); // high word of b 179 180 if ((ha - hb) > 0x3c00000) { 181 return a + b; // x / y > 2**60 182 } 183 184 int k = 0; 185 if (a > 0x1.00000_ffff_ffffp500) { // a > ~2**500 186 // scale a and b by 2**-600 187 ha -= 0x25800000; 188 hb -= 0x25800000; 189 a = a * TWO_MINUS_600; 190 b = b * TWO_MINUS_600; 191 k += 600; 192 } 193 double t1, t2; 194 if (b < 0x1.0p-500) { // b < 2**-500 195 if (b < Double.MIN_NORMAL) { // subnormal b or 0 */ 196 if (b == 0.0) 197 return a; 198 t1 = 0x1.0p1022; // t1 = 2^1022 199 b *= t1; 200 a *= t1; 201 k -= 1022; 202 } else { // scale a and b by 2^600 203 ha += 0x25800000; // a *= 2^600 204 hb += 0x25800000; // b *= 2^600 205 a = a * TWO_PLUS_600; 206 b = b * TWO_PLUS_600; 207 k -= 600; 208 } 209 } 210 // medium size a and b 211 double w = a - b; 212 if (w > b) { 213 t1 = 0; 214 t1 = __HI(t1, ha); 215 t2 = a - t1; 216 w = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1))); 217 } else { 218 double y1, y2; 219 a = a + a; 220 y1 = 0; 221 y1 = __HI(y1, hb); 222 y2 = b - y1; 223 t1 = 0; 224 t1 = __HI(t1, ha + 0x00100000); 225 t2 = a - t1; 226 w = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b))); 227 } 228 if (k != 0) { 229 return Math.powerOfTwoD(k) * w; 230 } else 231 return w; 232 } 233 } 234 235 /** 236 * Compute x**y 237 * n 238 * Method: Let x = 2 * (1+f) 239 * 1. Compute and return log2(x) in two pieces: 240 * log2(x) = w1 + w2, 241 * where w1 has 53 - 24 = 29 bit trailing zeros. 242 * 2. Perform y*log2(x) = n+y' by simulating multi-precision 243 * arithmetic, where |y'| <= 0.5. 244 * 3. Return x**y = 2**n*exp(y'*log2) 245 * 246 * Special cases: 247 * 1. (anything) ** 0 is 1 248 * 2. (anything) ** 1 is itself 249 * 3. (anything) ** NAN is NAN 250 * 4. NAN ** (anything except 0) is NAN 251 * 5. +-(|x| > 1) ** +INF is +INF 252 * 6. +-(|x| > 1) ** -INF is +0 253 * 7. +-(|x| < 1) ** +INF is +0 254 * 8. +-(|x| < 1) ** -INF is +INF 255 * 9. +-1 ** +-INF is NAN 256 * 10. +0 ** (+anything except 0, NAN) is +0 257 * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 258 * 12. +0 ** (-anything except 0, NAN) is +INF 259 * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF 260 * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) 261 * 15. +INF ** (+anything except 0,NAN) is +INF 262 * 16. +INF ** (-anything except 0,NAN) is +0 263 * 17. -INF ** (anything) = -0 ** (-anything) 264 * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) 265 * 19. (-anything except 0 and inf) ** (non-integer) is NAN 266 * 267 * Accuracy: 268 * pow(x,y) returns x**y nearly rounded. In particular 269 * pow(integer,integer) 270 * always returns the correct integer provided it is 271 * representable. 272 */ 273 public static class Pow { 274 public static strictfp double compute(final double x, final double y) { 275 double z; 276 double r, s, t, u, v, w; 277 int i, j, k, n; 278 279 // y == zero: x**0 = 1 280 if (y == 0.0) 281 return 1.0; 282 283 // +/-NaN return x + y to propagate NaN significands 284 if (Double.isNaN(x) || Double.isNaN(y)) 285 return x + y; 286 287 final double y_abs = Math.abs(y); 288 double x_abs = Math.abs(x); 289 // Special values of y 290 if (y == 2.0) { 291 return x * x; 292 } else if (y == 0.5) { 293 if (x >= -Double.MAX_VALUE) // Handle x == -infinity later 294 return Math.sqrt(x + 0.0); // Add 0.0 to properly handle x == -0.0 295 } else if (y_abs == 1.0) { // y is +/-1 296 return (y == 1.0) ? x : 1.0 / x; 297 } else if (y_abs == INFINITY) { // y is +/-infinity 298 if (x_abs == 1.0) 299 return y - y; // inf**+/-1 is NaN 300 else if (x_abs > 1.0) // (|x| > 1)**+/-inf = inf, 0 301 return (y >= 0) ? y : 0.0; 302 else // (|x| < 1)**-/+inf = inf, 0 303 return (y < 0) ? -y : 0.0; 304 } 305 306 final int hx = __HI(x); 307 int ix = hx & 0x7fffffff; 308 309 /* 310 * When x < 0, determine if y is an odd integer: 311 * y_is_int = 0 ... y is not an integer 312 * y_is_int = 1 ... y is an odd int 313 * y_is_int = 2 ... y is an even int 314 */ 315 int y_is_int = 0; 316 if (hx < 0) { 317 if (y_abs >= 0x1.0p53) // |y| >= 2^53 = 9.007199254740992E15 318 y_is_int = 2; // y is an even integer since ulp(2^53) = 2.0 319 else if (y_abs >= 1.0) { // |y| >= 1.0 320 long y_abs_as_long = (long) y_abs; 321 if ( ((double) y_abs_as_long) == y_abs) { 322 y_is_int = 2 - (int)(y_abs_as_long & 0x1L); 323 } 324 } 325 } 326 327 // Special value of x 328 if (x_abs == 0.0 || 329 x_abs == INFINITY || 330 x_abs == 1.0) { 331 z = x_abs; // x is +/-0, +/-inf, +/-1 332 if (y < 0.0) 333 z = 1.0/z; // z = (1/|x|) 334 if (hx < 0) { 335 if (((ix - 0x3ff00000) | y_is_int) == 0) { 336 z = (z-z)/(z-z); // (-1)**non-int is NaN 337 } else if (y_is_int == 1) 338 z = -1.0 * z; // (x < 0)**odd = -(|x|**odd) 339 } 340 return z; 341 } 342 343 n = (hx >> 31) + 1; 344 345 // (x < 0)**(non-int) is NaN 346 if ((n | y_is_int) == 0) 347 return (x-x)/(x-x); 348 349 s = 1.0; // s (sign of result -ve**odd) = -1 else = 1 350 if ( (n | (y_is_int - 1)) == 0) 351 s = -1.0; // (-ve)**(odd int) 352 353 double p_h, p_l, t1, t2; 354 // |y| is huge 355 if (y_abs > 0x1.00000_ffff_ffffp31) { // if |y| > ~2**31 356 final double INV_LN2 = 0x1.7154_7652_b82fep0; // 1.44269504088896338700e+00 = 1/ln2 357 final double INV_LN2_H = 0x1.715476p0; // 1.44269502162933349609e+00 = 24 bits of 1/ln2 358 final double INV_LN2_L = 0x1.4ae0_bf85_ddf44p-26; // 1.92596299112661746887e-08 = 1/ln2 tail 359 360 // Over/underflow if x is not close to one 361 if (x_abs < 0x1.fffff_0000_0000p-1) // |x| < ~0.9999995231628418 362 return (y < 0.0) ? s * INFINITY : s * 0.0; 363 if (x_abs > 0x1.00000_ffff_ffffp0) // |x| > ~1.0 364 return (y > 0.0) ? s * INFINITY : s * 0.0; 365 /* 366 * now |1-x| is tiny <= 2**-20, sufficient to compute 367 * log(x) by x - x^2/2 + x^3/3 - x^4/4 368 */ 369 t = x_abs - 1.0; // t has 20 trailing zeros 370 w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); 371 u = INV_LN2_H * t; // INV_LN2_H has 21 sig. bits 372 v = t * INV_LN2_L - w * INV_LN2; 373 t1 = u + v; 374 t1 =__LO(t1, 0); 375 t2 = v - (t1 - u); 376 } else { 377 final double CP = 0x1.ec70_9dc3_a03fdp-1; // 9.61796693925975554329e-01 = 2/(3ln2) 378 final double CP_H = 0x1.ec709ep-1; // 9.61796700954437255859e-01 = (float)cp 379 final double CP_L = -0x1.e2fe_0145_b01f5p-28; // -7.02846165095275826516e-09 = tail of CP_H 380 381 double z_h, z_l, ss, s2, s_h, s_l, t_h, t_l; 382 n = 0; 383 // Take care of subnormal numbers 384 if (ix < 0x00100000) { 385 x_abs *= 0x1.0p53; // 2^53 = 9007199254740992.0 386 n -= 53; 387 ix = __HI(x_abs); 388 } 389 n += ((ix) >> 20) - 0x3ff; 390 j = ix & 0x000fffff; 391 // Determine interval 392 ix = j | 0x3ff00000; // Normalize ix 393 if (j <= 0x3988E) 394 k = 0; // |x| <sqrt(3/2) 395 else if (j < 0xBB67A) 396 k = 1; // |x| <sqrt(3) 397 else { 398 k = 0; 399 n += 1; 400 ix -= 0x00100000; 401 } 402 x_abs = __HI(x_abs, ix); 403 404 // Compute ss = s_h + s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) 405 406 final double BP[] = {1.0, 407 1.5}; 408 final double DP_H[] = {0.0, 409 0x1.2b80_34p-1}; // 5.84962487220764160156e-01 410 final double DP_L[] = {0.0, 411 0x1.cfde_b43c_fd006p-27};// 1.35003920212974897128e-08 412 413 // Poly coefs for (3/2)*(log(x)-2s-2/3*s**3 414 final double L1 = 0x1.3333_3333_33303p-1; // 5.99999999999994648725e-01 415 final double L2 = 0x1.b6db_6db6_fabffp-2; // 4.28571428578550184252e-01 416 final double L3 = 0x1.5555_5518_f264dp-2; // 3.33333329818377432918e-01 417 final double L4 = 0x1.1746_0a91_d4101p-2; // 2.72728123808534006489e-01 418 final double L5 = 0x1.d864_a93c_9db65p-3; // 2.30660745775561754067e-01 419 final double L6 = 0x1.a7e2_84a4_54eefp-3; // 2.06975017800338417784e-01 420 u = x_abs - BP[k]; // BP[0]=1.0, BP[1]=1.5 421 v = 1.0 / (x_abs + BP[k]); 422 ss = u * v; 423 s_h = ss; 424 s_h = __LO(s_h, 0); 425 // t_h=x_abs + BP[k] High 426 t_h = 0.0; 427 t_h = __HI(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18) ); 428 t_l = x_abs - (t_h - BP[k]); 429 s_l = v * ((u - s_h * t_h) - s_h * t_l); 430 // Compute log(x_abs) 431 s2 = ss * ss; 432 r = s2 * s2* (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); 433 r += s_l * (s_h + ss); 434 s2 = s_h * s_h; 435 t_h = 3.0 + s2 + r; 436 t_h = __LO(t_h, 0); 437 t_l = r - ((t_h - 3.0) - s2); 438 // u+v = ss*(1+...) 439 u = s_h * t_h; 440 v = s_l * t_h + t_l * ss; 441 // 2/(3log2)*(ss + ...) 442 p_h = u + v; 443 p_h = __LO(p_h, 0); 444 p_l = v - (p_h - u); 445 z_h = CP_H * p_h; // CP_H + CP_L = 2/(3*log2) 446 z_l = CP_L * p_h + p_l * CP + DP_L[k]; 447 // log2(x_abs) = (ss + ..)*2/(3*log2) = n + DP_H + z_h + z_l 448 t = (double)n; 449 t1 = (((z_h + z_l) + DP_H[k]) + t); 450 t1 = __LO(t1, 0); 451 t2 = z_l - (((t1 - t) - DP_H[k]) - z_h); 452 } 453 454 // Split up y into (y1 + y2) and compute (y1 + y2) * (t1 + t2) 455 double y1 = y; 456 y1 = __LO(y1, 0); 457 p_l = (y - y1) * t1 + y * t2; 458 p_h = y1 * t1; 459 z = p_l + p_h; 460 j = __HI(z); 461 i = __LO(z); 462 if (j >= 0x40900000) { // z >= 1024 463 if (((j - 0x40900000) | i)!=0) // if z > 1024 464 return s * INFINITY; // Overflow 465 else { 466 final double OVT = 8.0085662595372944372e-0017; // -(1024-log2(ovfl+.5ulp)) 467 if (p_l + OVT > z - p_h) 468 return s * INFINITY; // Overflow 469 } 470 } else if ((j & 0x7fffffff) >= 0x4090cc00 ) { // z <= -1075 471 if (((j - 0xc090cc00) | i)!=0) // z < -1075 472 return s * 0.0; // Underflow 473 else { 474 if (p_l <= z - p_h) 475 return s * 0.0; // Underflow 476 } 477 } 478 /* 479 * Compute 2**(p_h+p_l) 480 */ 481 // Poly coefs for (3/2)*(log(x)-2s-2/3*s**3 482 final double P1 = 0x1.5555_5555_5553ep-3; // 1.66666666666666019037e-01 483 final double P2 = -0x1.6c16_c16b_ebd93p-9; // -2.77777777770155933842e-03 484 final double P3 = 0x1.1566_aaf2_5de2cp-14; // 6.61375632143793436117e-05 485 final double P4 = -0x1.bbd4_1c5d_26bf1p-20; // -1.65339022054652515390e-06 486 final double P5 = 0x1.6376_972b_ea4d0p-25; // 4.13813679705723846039e-08 487 final double LG2 = 0x1.62e4_2fef_a39efp-1; // 6.93147180559945286227e-01 488 final double LG2_H = 0x1.62e43p-1; // 6.93147182464599609375e-01 489 final double LG2_L = -0x1.05c6_10ca_86c39p-29; // -1.90465429995776804525e-09 490 i = j & 0x7fffffff; 491 k = (i >> 20) - 0x3ff; 492 n = 0; 493 if (i > 0x3fe00000) { // if |z| > 0.5, set n = [z + 0.5] 494 n = j + (0x00100000 >> (k + 1)); 495 k = ((n & 0x7fffffff) >> 20) - 0x3ff; // new k for n 496 t = 0.0; 497 t = __HI(t, (n & ~(0x000fffff >> k)) ); 498 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); 499 if (j < 0) 500 n = -n; 501 p_h -= t; 502 } 503 t = p_l + p_h; 504 t = __LO(t, 0); 505 u = t * LG2_H; 506 v = (p_l - (t - p_h)) * LG2 + t * LG2_L; 507 z = u + v; 508 w = v - (z - u); 509 t = z * z; 510 t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); 511 r = (z * t1)/(t1 - 2.0) - (w + z * w); 512 z = 1.0 - (r - z); 513 j = __HI(z); 514 j += (n << 20); 515 if ((j >> 20) <= 0) 516 z = Math.scalb(z, n); // subnormal output 517 else { 518 int z_hi = __HI(z); 519 z_hi += (n << 20); 520 z = __HI(z, z_hi); 521 } 522 return s * z; 523 } 524 } 525 }