1 /* 2 * Copyright (c) 1998, 2016, 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 package test.java.lang.StrictMath; 26 27 /** 28 * A transliteration of the "Freely Distributable Math Library" algorithms from C into Java. That 29 * is, this port of the algorithms is as close to the C originals as possible while still being 30 * readable legal Java. 31 */ 32 public class FdlibmTranslit { 33 FdlibmTranslit()34 private FdlibmTranslit() { 35 } 36 37 /** 38 * Return the low-order 32 bits of the double argument as an int. 39 */ __LO(double x)40 private static int __LO(double x) { 41 long transducer = Double.doubleToRawLongBits(x); 42 return (int) transducer; 43 } 44 45 /** 46 * Return a double with its low-order bits of the second argument and the high-order bits of the 47 * first argument.. 48 */ __LO(double x, int low)49 private static double __LO(double x, int low) { 50 long transX = Double.doubleToRawLongBits(x); 51 return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L) | 52 (low & 0x0000_0000_FFFF_FFFFL)); 53 } 54 55 /** 56 * Return the high-order 32 bits of the double argument as an int. 57 */ __HI(double x)58 private static int __HI(double x) { 59 long transducer = Double.doubleToRawLongBits(x); 60 return (int) (transducer >> 32); 61 } 62 63 /** 64 * Return a double with its high-order bits of the second argument and the low-order bits of the 65 * first argument.. 66 */ __HI(double x, int high)67 private static double __HI(double x, int high) { 68 long transX = Double.doubleToRawLongBits(x); 69 return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) | 70 (((long) high)) << 32); 71 } 72 hypot(double x, double y)73 public static double hypot(double x, double y) { 74 return Hypot.compute(x, y); 75 } 76 77 /** 78 * cbrt(x) Return cube root of x 79 */ 80 public static class Cbrt { 81 82 // unsigned 83 private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */ 84 private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ 85 86 private static final double C = 5.42857142857142815906e-01; /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 87 private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 88 private static final double E = 1.41428571428571436819e+00; /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 89 private static final double F = 1.60714285714285720630e+00; /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 90 private static final double G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 91 compute(double x)92 public static strictfp double compute(double x) { 93 int hx; 94 double r, s, t = 0.0, w; 95 int sign; // unsigned 96 97 hx = __HI(x); // high word of x 98 sign = hx & 0x80000000; // sign= sign(x) 99 hx ^= sign; 100 if (hx >= 0x7ff00000) { 101 return (x + x); // cbrt(NaN,INF) is itself 102 } 103 if ((hx | __LO(x)) == 0) { 104 return (x); // cbrt(0) is itself 105 } 106 107 x = __HI(x, hx); // x <- |x| 108 // rough cbrt to 5 bits 109 if (hx < 0x00100000) { // subnormal number 110 t = __HI(t, 0x43500000); // set t= 2**54 111 t *= x; 112 t = __HI(t, __HI(t) / 3 + B2); 113 } else { 114 t = __HI(t, hx / 3 + B1); 115 } 116 117 // new cbrt to 23 bits, may be implemented in single precision 118 r = t * t / x; 119 s = C + r * t; 120 t *= G + F / (s + E + D / s); 121 122 // chopped to 20 bits and make it larger than cbrt(x) 123 t = __LO(t, 0); 124 t = __HI(t, __HI(t) + 0x00000001); 125 126 // one step newton iteration to 53 bits with error less than 0.667 ulps 127 s = t * t; // t*t is exact 128 r = x / s; 129 w = t + t; 130 r = (r - t) / (w + r); // r-s is exact 131 t = t + t * r; 132 133 // restore the sign bit 134 t = __HI(t, __HI(t) | sign); 135 return (t); 136 } 137 } 138 139 /** 140 * hypot(x,y) 141 * 142 * Method : If (assume round-to-nearest) z = x*x + y*y has error less than sqrt(2)/2 ulp, than 143 * sqrt(z) has error less than 1 ulp (exercise). 144 * 145 * So, compute sqrt(x*x + y*y) with some care as follows to get the error below 1 ulp: 146 * 147 * Assume x > y > 0; (if possible, set rounding to round-to-nearest) 1. if x > 2y use x1*x1 + 148 * (y*y + (x2*(x + x1))) for x*x + y*y where x1 = x with lower 32 bits cleared, x2 = x - x1; 149 * else 2. if x <= 2y use t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y)) where t1 = 2x with lower 32 150 * bits cleared, t2 = 2x - t1, y1= y with lower 32 bits chopped, y2 = y - y1. 151 * 152 * NOTE: scaling may be necessary if some argument is too large or too tiny 153 * 154 * Special cases: hypot(x,y) is INF if x or y is +INF or -INF; else hypot(x,y) is NAN if x or y 155 * is NAN. 156 * 157 * Accuracy: hypot(x,y) returns sqrt(x^2 + y^2) with error less than 1 ulps (units in the last 158 * place) 159 */ 160 static class Hypot { 161 compute(double x, double y)162 public static double compute(double x, double y) { 163 double a = x; 164 double b = y; 165 double t1, t2, y1, y2, w; 166 int j, k, ha, hb; 167 168 ha = __HI(x) & 0x7fffffff; // high word of x 169 hb = __HI(y) & 0x7fffffff; // high word of y 170 if (hb > ha) { 171 a = y; 172 b = x; 173 j = ha; 174 ha = hb; 175 hb = j; 176 } else { 177 a = x; 178 b = y; 179 } 180 a = __HI(a, ha); // a <- |a| 181 b = __HI(b, hb); // b <- |b| 182 if ((ha - hb) > 0x3c00000) { 183 return a + b; // x / y > 2**60 184 } 185 k = 0; 186 if (ha > 0x5f300000) { // a>2**500 187 if (ha >= 0x7ff00000) { // Inf or NaN 188 w = a + b; // for sNaN 189 if (((ha & 0xfffff) | __LO(a)) == 0) { 190 w = a; 191 } 192 if (((hb ^ 0x7ff00000) | __LO(b)) == 0) { 193 w = b; 194 } 195 return w; 196 } 197 // scale a and b by 2**-600 198 ha -= 0x25800000; 199 hb -= 0x25800000; 200 k += 600; 201 a = __HI(a, ha); 202 b = __HI(b, hb); 203 } 204 if (hb < 0x20b00000) { // b < 2**-500 205 if (hb <= 0x000fffff) { // subnormal b or 0 */ 206 if ((hb | (__LO(b))) == 0) { 207 return a; 208 } 209 t1 = 0; 210 t1 = __HI(t1, 0x7fd00000); // t1=2^1022 211 b *= t1; 212 a *= t1; 213 k -= 1022; 214 } else { // scale a and b by 2^600 215 ha += 0x25800000; // a *= 2^600 216 hb += 0x25800000; // b *= 2^600 217 k -= 600; 218 a = __HI(a, ha); 219 b = __HI(b, hb); 220 } 221 } 222 // medium size a and b 223 w = a - b; 224 if (w > b) { 225 t1 = 0; 226 t1 = __HI(t1, ha); 227 t2 = a - t1; 228 w = Math.sqrt(t1 * t1 - (b * (-b) - t2 * (a + t1))); 229 } else { 230 a = a + a; 231 y1 = 0; 232 y1 = __HI(y1, hb); 233 y2 = b - y1; 234 t1 = 0; 235 t1 = __HI(t1, ha + 0x00100000); 236 t2 = a - t1; 237 w = Math.sqrt(t1 * y1 - (w * (-w) - (t1 * y2 + t2 * b))); 238 } 239 if (k != 0) { 240 t1 = 1.0; 241 int t1_hi = __HI(t1); 242 t1_hi += (k << 20); 243 t1 = __HI(t1, t1_hi); 244 return t1 * w; 245 } else { 246 return w; 247 } 248 } 249 } 250 251 /** 252 * Returns the exponential of x. 253 * 254 * Method 1. Argument reduction: Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. Given x, 255 * find r and integer k such that 256 * 257 * x = k*ln2 + r, |r| <= 0.5*ln2. 258 * 259 * Here r will be represented as r = hi-lo for better accuracy. 260 * 261 * 2. Approximation of exp(r) by a special rational function on the interval [0,0.34658]: Write 262 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... We use a special Reme 263 * algorithm on [0,0.34658] to generate a polynomial of degree 5 to approximate R. The maximum 264 * error of this polynomial approximation is bounded by 2**-59. In other words, R(z) ~ 2.0 + 265 * P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 (where z=r*r, and the values of P1 to P5 are 266 * listed below) and | 5 | -59 | 2.0+P1*z+...+P5*z - R(z) | <= 267 * 2 | | The computation of exp(r) thus becomes 2*r exp(r) = 1 + 268 * ------- R - r r*R1(r) = 1 + r + ----------- (for better accuracy) 2 - R1(r) where 2 4 269 * 10 R1(r) = r - (P1*r + P2*r + ... + P5*r ). 270 * 271 * 3. Scale back to obtain exp(x): From step 1, we have exp(x) = 2^k * exp(r) 272 * 273 * Special cases: exp(INF) is INF, exp(NaN) is NaN; exp(-INF) is 0, and for finite argument, 274 * only exp(0)=1 is exact. 275 * 276 * Accuracy: according to an error analysis, the error is always less than 1 ulp (unit in the 277 * last place). 278 * 279 * Misc. info. For IEEE double if x > 7.09782712893383973096e+02 then exp(x) overflow if x < 280 * -7.45133219101941108420e+02 then exp(x) underflow 281 * 282 * Constants: The hexadecimal values are the intended ones for the following constants. The 283 * decimal values may be used, provided that the compiler will convert from decimal to binary 284 * accurately enough to produce the hexadecimal values shown. 285 */ 286 static class Exp { 287 288 private static final double ONE = 1.0; 289 private static final double[] HAL_F = {0.5, -0.5,}; 290 private static final double HUGE = 1.0e+300; 291 private static final double TWOM_1000 = 9.33263618503218878990e-302; /* 2**-1000=0x01700000,0*/ 292 private static final double O_THRESHOLD = 7.09782712893383973096e+02; /* 0x40862E42, 0xFEFA39EF */ 293 private static final double U_THRESHOLD = -7.45133219101941108420e+02; /* 0xc0874910, 0xD52D3051 */ 294 private static final double[] LN_2_HI = {6.93147180369123816490e-01, 295 /* 0x3fe62e42, 0xfee00000 */ 296 -6.93147180369123816490e-01}; /* 0xbfe62e42, 0xfee00000 */ 297 private static final double[] LN_2_LO = {1.90821492927058770002e-10, 298 /* 0x3dea39ef, 0x35793c76 */ 299 -1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */ 300 private static final double INV_LN_2 = 1.44269504088896338700e+00; /* 0x3ff71547, 0x652b82fe */ 301 private static final double P1 = 1.66666666666666019037e-01; /* 0x3FC55555, 0x5555553E */ 302 private static final double P2 = -2.77777777770155933842e-03; /* 0xBF66C16C, 0x16BEBD93 */ 303 private static final double P3 = 6.61375632143793436117e-05; /* 0x3F11566A, 0xAF25DE2C */ 304 private static final double P4 = -1.65339022054652515390e-06; /* 0xBEBBBD41, 0xC5D26BF1 */ 305 private static final double P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 306 compute(double x)307 public static strictfp double compute(double x) { 308 double y, hi = 0, lo = 0, c, t; 309 int k = 0, xsb; 310 /*unsigned*/ 311 int hx; 312 313 hx = __HI(x); /* high word of x */ 314 xsb = (hx >> 31) & 1; /* sign bit of x */ 315 hx &= 0x7fffffff; /* high word of |x| */ 316 317 /* filter out non-finite argument */ 318 if (hx >= 0x40862E42) { /* if |x|>=709.78... */ 319 if (hx >= 0x7ff00000) { 320 if (((hx & 0xfffff) | __LO(x)) != 0) { 321 return x + x; /* NaN */ 322 } else { 323 return (xsb == 0) ? x : 0.0; /* exp(+-inf)={inf,0} */ 324 } 325 } 326 if (x > O_THRESHOLD) { 327 return HUGE * HUGE; /* overflow */ 328 } 329 if (x < U_THRESHOLD) { 330 return TWOM_1000 * TWOM_1000; /* underflow */ 331 } 332 } 333 334 /* argument reduction */ 335 if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 336 if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 337 hi = x - LN_2_HI[xsb]; 338 lo = LN_2_LO[xsb]; 339 k = 1 - xsb - xsb; 340 } else { 341 k = (int) (INV_LN_2 * x + HAL_F[xsb]); 342 t = k; 343 hi = x - t * LN_2_HI[0]; /* t*ln2HI is exact here */ 344 lo = t * LN_2_LO[0]; 345 } 346 x = hi - lo; 347 } else if (hx < 0x3e300000) { /* when |x|<2**-28 */ 348 if (HUGE + x > ONE) { 349 return ONE + x;/* trigger inexact */ 350 } 351 } else { 352 k = 0; 353 } 354 355 /* x is now in primary range */ 356 t = x * x; 357 c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); 358 if (k == 0) { 359 return ONE - ((x * c) / (c - 2.0) - x); 360 } else { 361 y = ONE - ((lo - (x * c) / (2.0 - c)) - hi); 362 } 363 if (k >= -1021) { 364 y = __HI(y, __HI(y) + (k << 20)); /* add k to y's exponent */ 365 return y; 366 } else { 367 y = __HI(y, __HI(y) + ((k + 1000) << 20));/* add k to y's exponent */ 368 return y * TWOM_1000; 369 } 370 } 371 } 372 } 373