32928
|
1 |
/*
|
42751
|
2 |
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
|
32928
|
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 |
/**
|
|
27 |
* A transliteration of the "Freely Distributable Math Library"
|
|
28 |
* algorithms from C into Java. That is, this port of the algorithms
|
|
29 |
* is as close to the C originals as possible while still being
|
|
30 |
* readable legal Java.
|
|
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);
|
42751
|
51 |
return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L) |
|
|
52 |
(low & 0x0000_0000_FFFF_FFFFL));
|
32928
|
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);
|
42751
|
69 |
return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) |
|
|
70 |
( ((long)high)) << 32 );
|
32928
|
71 |
}
|
|
72 |
|
|
73 |
public static double hypot(double x, double y) {
|
|
74 |
return Hypot.compute(x, y);
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
32992
|
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 */
|
|
91 |
|
|
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 |
if ((hx | __LO(x)) == 0)
|
|
103 |
return(x); // cbrt(0) is itself
|
|
104 |
|
|
105 |
x = __HI(x, hx); // x <- |x|
|
|
106 |
// rough cbrt to 5 bits
|
|
107 |
if (hx < 0x00100000) { // subnormal number
|
|
108 |
t = __HI(t, 0x43500000); // set t= 2**54
|
|
109 |
t *= x;
|
|
110 |
t = __HI(t, __HI(t)/3+B2);
|
|
111 |
} else {
|
|
112 |
t = __HI(t, hx/3+B1);
|
|
113 |
}
|
|
114 |
|
|
115 |
// new cbrt to 23 bits, may be implemented in single precision
|
|
116 |
r = t * t/x;
|
|
117 |
s = C + r*t;
|
|
118 |
t *= G + F/(s + E + D/s);
|
|
119 |
|
|
120 |
// chopped to 20 bits and make it larger than cbrt(x)
|
|
121 |
t = __LO(t, 0);
|
|
122 |
t = __HI(t, __HI(t)+0x00000001);
|
|
123 |
|
|
124 |
|
|
125 |
// one step newton iteration to 53 bits with error less than 0.667 ulps
|
|
126 |
s = t * t; // t*t is exact
|
|
127 |
r = x / s;
|
|
128 |
w = t + t;
|
|
129 |
r= (r - t)/(w + r); // r-s is exact
|
|
130 |
t= t + t*r;
|
|
131 |
|
|
132 |
// retore the sign bit
|
|
133 |
t = __HI(t, __HI(t) | sign);
|
|
134 |
return(t);
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
32928
|
139 |
* hypot(x,y)
|
|
140 |
*
|
|
141 |
* Method :
|
|
142 |
* If (assume round-to-nearest) z = x*x + y*y
|
|
143 |
* has error less than sqrt(2)/2 ulp, than
|
|
144 |
* sqrt(z) has error less than 1 ulp (exercise).
|
|
145 |
*
|
|
146 |
* So, compute sqrt(x*x + y*y) with some care as
|
|
147 |
* follows to get the error below 1 ulp:
|
|
148 |
*
|
|
149 |
* Assume x > y > 0;
|
|
150 |
* (if possible, set rounding to round-to-nearest)
|
|
151 |
* 1. if x > 2y use
|
|
152 |
* x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y
|
|
153 |
* where x1 = x with lower 32 bits cleared, x2 = x - x1; else
|
|
154 |
* 2. if x <= 2y use
|
|
155 |
* t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y))
|
|
156 |
* where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1,
|
|
157 |
* y1= y with lower 32 bits chopped, y2 = y - y1.
|
|
158 |
*
|
|
159 |
* NOTE: scaling may be necessary if some argument is too
|
|
160 |
* large or too tiny
|
|
161 |
*
|
|
162 |
* Special cases:
|
|
163 |
* hypot(x,y) is INF if x or y is +INF or -INF; else
|
|
164 |
* hypot(x,y) is NAN if x or y is NAN.
|
|
165 |
*
|
|
166 |
* Accuracy:
|
|
167 |
* hypot(x,y) returns sqrt(x^2 + y^2) with error less
|
|
168 |
* than 1 ulps (units in the last place)
|
|
169 |
*/
|
|
170 |
static class Hypot {
|
|
171 |
public static double compute(double x, double y) {
|
|
172 |
double a = x;
|
|
173 |
double b = y;
|
|
174 |
double t1, t2, y1, y2, w;
|
|
175 |
int j, k, ha, hb;
|
|
176 |
|
|
177 |
ha = __HI(x) & 0x7fffffff; // high word of x
|
|
178 |
hb = __HI(y) & 0x7fffffff; // high word of y
|
|
179 |
if(hb > ha) {
|
|
180 |
a = y;
|
|
181 |
b = x;
|
|
182 |
j = ha;
|
|
183 |
ha = hb;
|
|
184 |
hb = j;
|
|
185 |
} else {
|
|
186 |
a = x;
|
|
187 |
b = y;
|
|
188 |
}
|
|
189 |
a = __HI(a, ha); // a <- |a|
|
|
190 |
b = __HI(b, hb); // b <- |b|
|
|
191 |
if ((ha - hb) > 0x3c00000) {
|
|
192 |
return a + b; // x / y > 2**60
|
|
193 |
}
|
|
194 |
k=0;
|
|
195 |
if (ha > 0x5f300000) { // a>2**500
|
|
196 |
if (ha >= 0x7ff00000) { // Inf or NaN
|
|
197 |
w = a + b; // for sNaN
|
|
198 |
if (((ha & 0xfffff) | __LO(a)) == 0)
|
|
199 |
w = a;
|
|
200 |
if (((hb ^ 0x7ff00000) | __LO(b)) == 0)
|
|
201 |
w = b;
|
|
202 |
return w;
|
|
203 |
}
|
|
204 |
// scale a and b by 2**-600
|
|
205 |
ha -= 0x25800000;
|
|
206 |
hb -= 0x25800000;
|
|
207 |
k += 600;
|
|
208 |
a = __HI(a, ha);
|
|
209 |
b = __HI(b, hb);
|
|
210 |
}
|
|
211 |
if (hb < 0x20b00000) { // b < 2**-500
|
|
212 |
if (hb <= 0x000fffff) { // subnormal b or 0 */
|
|
213 |
if ((hb | (__LO(b))) == 0)
|
|
214 |
return a;
|
|
215 |
t1 = 0;
|
|
216 |
t1 = __HI(t1, 0x7fd00000); // t1=2^1022
|
|
217 |
b *= t1;
|
|
218 |
a *= t1;
|
|
219 |
k -= 1022;
|
|
220 |
} else { // scale a and b by 2^600
|
|
221 |
ha += 0x25800000; // a *= 2^600
|
|
222 |
hb += 0x25800000; // b *= 2^600
|
|
223 |
k -= 600;
|
|
224 |
a = __HI(a, ha);
|
|
225 |
b = __HI(b, hb);
|
|
226 |
}
|
|
227 |
}
|
|
228 |
// medium size a and b
|
|
229 |
w = a - b;
|
|
230 |
if (w > b) {
|
|
231 |
t1 = 0;
|
|
232 |
t1 = __HI(t1, ha);
|
|
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 |
}
|
42751
|
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 |
}
|
32928
|
387 |
}
|