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
31 */
32 public class FdlibmTranslit {
33 private FdlibmTranslit() {
34 throw new UnsupportedOperationException("No FdLibmTranslit instances for you.");
35 }
36
37 /**
38 * Return the low-order 32 bits of the double argument as an int.
39 */
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
47 * and the high-order bits of the first argument..
48 */
49 private static double __LO(double x, int low) {
50 long transX = Double.doubleToRawLongBits(x);
51 return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
52 }
53
54 /**
55 * Return the high-order 32 bits of the double argument as an int.
56 */
57 private static int __HI(double x) {
58 long transducer = Double.doubleToRawLongBits(x);
59 return (int)(transducer >> 32);
60 }
61
62 /**
63 * Return a double with its high-order bits of the second argument
64 * and the low-order bits of the first argument..
65 */
66 private static double __HI(double x, int high) {
67 long transX = Double.doubleToRawLongBits(x);
68 return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
69 }
70
71 public static double hypot(double x, double y) {
72 return Hypot.compute(x, y);
73 }
74
75 /**
76 * cbrt(x)
77 * Return cube root of x
78 */
79 public static class Cbrt {
80 // unsigned
81 private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */
82 private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
83
84 private static final double C = 5.42857142857142815906e-01; /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
85 private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */
86 private static final double E = 1.41428571428571436819e+00; /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
87 private static final double F = 1.60714285714285720630e+00; /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
88 private static final double G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
231 t2 = a - t1;
232 w = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
233 } else {
234 a = a + a;
235 y1 = 0;
236 y1 = __HI(y1, hb);
237 y2 = b - y1;
238 t1 = 0;
239 t1 = __HI(t1, ha + 0x00100000);
240 t2 = a - t1;
241 w = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
242 }
243 if (k != 0) {
244 t1 = 1.0;
245 int t1_hi = __HI(t1);
246 t1_hi += (k << 20);
247 t1 = __HI(t1, t1_hi);
248 return t1 * w;
249 } else
250 return w;
251 }
252 }
253 }
|
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
31 */
32 public class FdlibmTranslit {
33 private FdlibmTranslit() {
34 throw new UnsupportedOperationException("No FdLibmTranslit instances for you.");
35 }
36
37 /**
38 * Return the low-order 32 bits of the double argument as an int.
39 */
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
47 * and the high-order bits of the first argument..
48 */
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 */
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
65 * and the low-order bits of the first argument..
66 */
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
73 public static double hypot(double x, double y) {
74 return Hypot.compute(x, y);
75 }
76
77 /**
78 * cbrt(x)
79 * Return cube root of x
80 */
81 public static class Cbrt {
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 */
233 t2 = a - t1;
234 w = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
235 } else {
236 a = a + a;
237 y1 = 0;
238 y1 = __HI(y1, hb);
239 y2 = b - y1;
240 t1 = 0;
241 t1 = __HI(t1, ha + 0x00100000);
242 t2 = a - t1;
243 w = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
244 }
245 if (k != 0) {
246 t1 = 1.0;
247 int t1_hi = __HI(t1);
248 t1_hi += (k << 20);
249 t1 = __HI(t1, t1_hi);
250 return t1 * w;
251 } else
252 return w;
253 }
254 }
255
256 /**
257 * Returns the exponential of x.
258 *
259 * Method
260 * 1. Argument reduction:
261 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
262 * Given x, find r and integer k such that
263 *
264 * x = k*ln2 + r, |r| <= 0.5*ln2.
265 *
266 * Here r will be represented as r = hi-lo for better
267 * accuracy.
268 *
269 * 2. Approximation of exp(r) by a special rational function on
270 * the interval [0,0.34658]:
271 * Write
272 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
273 * We use a special Reme algorithm on [0,0.34658] to generate
274 * a polynomial of degree 5 to approximate R. The maximum error
275 * of this polynomial approximation is bounded by 2**-59. In
276 * other words,
277 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
278 * (where z=r*r, and the values of P1 to P5 are listed below)
279 * and
280 * | 5 | -59
281 * | 2.0+P1*z+...+P5*z - R(z) | <= 2
282 * | |
283 * The computation of exp(r) thus becomes
284 * 2*r
285 * exp(r) = 1 + -------
286 * R - r
287 * r*R1(r)
288 * = 1 + r + ----------- (for better accuracy)
289 * 2 - R1(r)
290 * where
291 * 2 4 10
292 * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
293 *
294 * 3. Scale back to obtain exp(x):
295 * From step 1, we have
296 * exp(x) = 2^k * exp(r)
297 *
298 * Special cases:
299 * exp(INF) is INF, exp(NaN) is NaN;
300 * exp(-INF) is 0, and
301 * for finite argument, only exp(0)=1 is exact.
302 *
303 * Accuracy:
304 * according to an error analysis, the error is always less than
305 * 1 ulp (unit in the last place).
306 *
307 * Misc. info.
308 * For IEEE double
309 * if x > 7.09782712893383973096e+02 then exp(x) overflow
310 * if x < -7.45133219101941108420e+02 then exp(x) underflow
311 *
312 * Constants:
313 * The hexadecimal values are the intended ones for the following
314 * constants. The decimal values may be used, provided that the
315 * compiler will convert from decimal to binary accurately enough
316 * to produce the hexadecimal values shown.
317 */
318 static class Exp {
319 private static final double one = 1.0;
320 private static final double[] halF = {0.5,-0.5,};
321 private static final double huge = 1.0e+300;
322 private static final double twom1000= 9.33263618503218878990e-302; /* 2**-1000=0x01700000,0*/
323 private static final double o_threshold= 7.09782712893383973096e+02; /* 0x40862E42, 0xFEFA39EF */
324 private static final double u_threshold= -7.45133219101941108420e+02; /* 0xc0874910, 0xD52D3051 */
325 private static final double[] ln2HI ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
326 -6.93147180369123816490e-01}; /* 0xbfe62e42, 0xfee00000 */
327 private static final double[] ln2LO ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
328 -1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */
329 private static final double invln2 = 1.44269504088896338700e+00; /* 0x3ff71547, 0x652b82fe */
330 private static final double P1 = 1.66666666666666019037e-01; /* 0x3FC55555, 0x5555553E */
331 private static final double P2 = -2.77777777770155933842e-03; /* 0xBF66C16C, 0x16BEBD93 */
332 private static final double P3 = 6.61375632143793436117e-05; /* 0x3F11566A, 0xAF25DE2C */
333 private static final double P4 = -1.65339022054652515390e-06; /* 0xBEBBBD41, 0xC5D26BF1 */
334 private static final double P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
335
336 public static strictfp double compute(double x) {
337 double y,hi=0,lo=0,c,t;
338 int k=0,xsb;
339 /*unsigned*/ int hx;
340
341 hx = __HI(x); /* high word of x */
342 xsb = (hx>>31)&1; /* sign bit of x */
343 hx &= 0x7fffffff; /* high word of |x| */
344
345 /* filter out non-finite argument */
346 if(hx >= 0x40862E42) { /* if |x|>=709.78... */
347 if(hx>=0x7ff00000) {
348 if(((hx&0xfffff)|__LO(x))!=0)
349 return x+x; /* NaN */
350 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
351 }
352 if(x > o_threshold) return huge*huge; /* overflow */
353 if(x < u_threshold) return twom1000*twom1000; /* underflow */
354 }
355
356 /* argument reduction */
357 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
358 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
359 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
360 } else {
361 k = (int)(invln2*x+halF[xsb]);
362 t = k;
363 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
364 lo = t*ln2LO[0];
365 }
366 x = hi - lo;
367 }
368 else if(hx < 0x3e300000) { /* when |x|<2**-28 */
369 if(huge+x>one) return one+x;/* trigger inexact */
370 }
371 else k = 0;
372
373 /* x is now in primary range */
374 t = x*x;
375 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
376 if(k==0) return one-((x*c)/(c-2.0)-x);
377 else y = one-((lo-(x*c)/(2.0-c))-hi);
378 if(k >= -1021) {
379 y = __HI(y, __HI(y) + (k<<20)); /* add k to y's exponent */
380 return y;
381 } else {
382 y = __HI(y, __HI(y) + ((k+1000)<<20));/* add k to y's exponent */
383 return y*twom1000;
384 }
385 }
386 }
387 }
|