sharedRuntimeTrans.cpp revision 1601
1472N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A#
include "incls/_precompiled.incl" 0N/A// This file contains copies of the fdlibm routines used by 0N/A// StrictMath. It turns out that it is almost always required to use 0N/A// these runtime routines; the Intel CPU doesn't meet the Java 0N/A// specification for sin/cos outside a certain limited argument range, 0N/A// and the SPARC CPU doesn't appear to have sin/cos instructions. It 0N/A// also turns out that avoiding the indirect call through function 0N/A// pointer out to libjava.so in SharedRuntime speeds these routines up 0N/A// Enabling optimizations in this file causes incorrect code to be 0N/A// generated; can not figure out how to turn down optimization for one 0N/A// file in the IDE on Windows 0N/A// VM_LITTLE_ENDIAN is #defined appropriately in the Makefiles 0N/A// [jk] this is not 100% correct because the float word order may different 0N/A// from the byte order (e.g. on ARM) 0N/A * ==================================================== 1472N/A * Copyright (c) 1998 Oracle and/or its affiliates. All rights reserved. 0N/A * Developed at SunSoft, a Sun Microsystems, Inc. business. 0N/A * Permission to use, copy, modify, and distribute this 0N/A * software is freely granted, provided that this notice 0N/A * ==================================================== 0N/A * scalbn (double x, int n) 0N/A * scalbn(x,n) returns x* 2**n computed by exponent 0N/A * manipulation rather than by actually performing an 0N/A * exponentiation or a multiplication. 0N/Atwo54 =
1.80143985094819840000e+16,
/* 0x43500000, 0x00000000 */ 0N/A twom54 =
5.55111512312578270212e-17,
/* 0x3C900000, 0x00000000 */ 0N/A k = (
hx&
0x7ff00000)>>
20;
/* extract exponent */ 0N/A if (k==0) {
/* 0 or subnormal x */ 0N/A if ((
lx|(
hx&
0x7fffffff))==0)
return x;
/* +-0 */ 0N/A k = ((
hx&
0x7ff00000)>>
20) -
54;
0N/A if (n< -
50000)
return tiny*x;
/*underflow*/ 0N/A if (k==
0x7ff)
return x+x;
/* NaN or Inf */ 0N/A if (k > 0)
/* normal result */ 0N/A {
__HI(x) = (
hx&
0x800fffff)|(k<<
20);
return x;}
0N/A if (n >
50000)
/* in case integer overflow in n+k */ 0N/A k +=
54;
/* subnormal result */ 0N/A * Return the logrithm of x 0N/A * 1. Argument Reduction: find k and f such that 0N/A * where sqrt(2)/2 < 1+f < sqrt(2) . 0N/A * 2. Approximation of log(1+f). 0N/A * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 0N/A * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 0N/A * We use a special Reme algorithm on [0,0.1716] to generate 0N/A * a polynomial of degree 14 to approximate R The maximum error 0N/A * of this polynomial approximation is bounded by 2**-58.45. In 0N/A * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s 0N/A * (the values of Lg1 to Lg7 are listed in the program) 0N/A * | Lg1*s +...+Lg7*s - R(z) | <= 2 0N/A * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 0N/A * In order to guarantee error in log below 1ulp, we compute log 0N/A * log(1+f) = f - s*(f - R) (if f is not too large) 0N/A * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) 0N/A * 3. Finally, log(x) = k*ln2 + log(1+f). 0N/A * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 0N/A * Here ln2 is split into two floating point number: 0N/A * where n*ln2_hi is always exact for |n| < 2000. 0N/A * log(x) is NaN with signal if x < 0 (including -INF) ; 0N/A * log(+INF) is +INF; log(0) is -INF with signal; 0N/A * log(NaN) is that NaN with no signal. 0N/A * according to an error analysis, the error is always less than 0N/A * 1 ulp (unit in the last place). 0N/A * The hexadecimal values are the intended ones for the following 0N/A * constants. The decimal values may be used, provided that the 0N/A * compiler will convert from decimal to binary accurately enough 0N/A * to produce the hexadecimal values shown. 0N/Aln2_hi =
6.93147180369123816490e-01,
/* 3fe62e42 fee00000 */ 0N/A ln2_lo =
1.90821492927058770002e-10,
/* 3dea39ef 35793c76 */ 0N/A Lg1 =
6.666666666666735130e-01,
/* 3FE55555 55555593 */ 0N/A Lg2 =
3.999999999940941908e-01,
/* 3FD99999 9997FA04 */ 0N/A Lg3 =
2.857142874366239149e-01,
/* 3FD24924 94229359 */ 0N/A Lg4 =
2.222219843214978396e-01,
/* 3FCC71C5 1D8E78AF */ 0N/A Lg5 =
1.818357216161805012e-01,
/* 3FC74664 96CB03DE */ 0N/A Lg6 =
1.531383769920937332e-01,
/* 3FC39A09 D078C69F */ 0N/A Lg7 =
1.479819860511658591e-01;
/* 3FC2F112 DF3E5244 */ 0N/A if (
hx <
0x00100000) {
/* x < 2**-1022 */ 0N/A if (
hx<0)
return (x-x)/
zero;
/* log(-#) = NaN */ 0N/A k -=
54; x *=
two54;
/* subnormal number, scale up x */ 0N/A if (
hx >=
0x7ff00000)
return x+x;
0N/A i = (
hx+
0x95f64)&
0x100000;
0N/A __HI(x) =
hx|(i^
0x3ff00000);
/* normalize x or x/2 */ 0N/A if((
0x000fffff&(
2+
hx))<
3) {
/* |f| < 2**-20 */ 0N/A R = f*f*(
0.5-
0.33333333333333333*f);
0N/A if(k==0)
return f-R;
else {
dk=(
double)k;
0N/A if(k==0)
return f-s*(f-R);
else 0N/A/* __ieee754_log10(x) 0N/A * Return the base 10 logarithm of x 0N/A * Let log10_2hi = leading 40 bits of log10(2) and 0N/A * log10_2lo = log10(2) - log10_2hi, 0N/A * ivln10 = 1/log(10) rounded. 0N/A * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x)) 0N/A * To guarantee log10(10**n)=n, where 10**n is normal, the rounding 0N/A * mode must set to Round-to-Nearest. 0N/A * [1/log(10)] rounded to 53 bits has error .198 ulps; 0N/A * log10 is monotonic at all binary break points. 0N/A * log10(x) is NaN with signal if x < 0; 0N/A * log10(+INF) is +INF with no signal; log10(0) is -INF with signal; 0N/A * log10(NaN) is that NaN with no signal; 0N/A * log10(10**N) = N for N=0,1,...,22. 0N/A * The hexadecimal values are the intended ones for the following constants. 0N/A * The decimal values may be used, provided that the compiler will convert 0N/A * from decimal to binary accurately enough to produce the hexadecimal values 0N/Aivln10 =
4.34294481903251816668e-01,
/* 0x3FDBCB7B, 0x1526E50E */ 0N/A log10_2hi =
3.01029995663611771306e-01,
/* 0x3FD34413, 0x509F6000 */ 0N/A log10_2lo =
3.69423907715893078616e-13;
/* 0x3D59FEF3, 0x11F12B36 */ 0N/A if (
hx <
0x00100000) {
/* x < 2**-1022 */ 0N/A if (
hx<0)
return (x-x)/
zero;
/* log(-#) = NaN */ 0N/A k -=
54; x *=
two54;
/* subnormal number, scale up x */ 0N/A if (
hx >=
0x7ff00000)
return x+x;
0N/A i = ((
unsigned)k&
0x80000000)>>
31;
0N/A hx = (
hx&
0x000fffff)|((
0x3ff-i)<<
20);
0N/A * Returns the exponential of x. 0N/A * 1. Argument reduction: 0N/A * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 0N/A * Given x, find r and integer k such that 0N/A * x = k*ln2 + r, |r| <= 0.5*ln2. 0N/A * Here r will be represented as r = hi-lo for better 0N/A * 2. Approximation of exp(r) by a special rational function on 0N/A * the interval [0,0.34658]: 0N/A * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 0N/A * We use a special Reme algorithm on [0,0.34658] to generate 0N/A * a polynomial of degree 5 to approximate R. The maximum error 0N/A * of this polynomial approximation is bounded by 2**-59. In 0N/A * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 0N/A * (where z=r*r, and the values of P1 to P5 are listed below) 0N/A * | 2.0+P1*z+...+P5*z - R(z) | <= 2 0N/A * The computation of exp(r) thus becomes 0N/A * exp(r) = 1 + ------- 0N/A * = 1 + r + ----------- (for better accuracy) 0N/A * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 0N/A * 3. Scale back to obtain exp(x): 0N/A * From step 1, we have 0N/A * exp(x) = 2^k * exp(r) 0N/A * exp(INF) is INF, exp(NaN) is NaN; 0N/A * exp(-INF) is 0, and 0N/A * for finite argument, only exp(0)=1 is exact. 0N/A * according to an error analysis, the error is always less than 0N/A * 1 ulp (unit in the last place). 0N/A * if x > 7.09782712893383973096e+02 then exp(x) overflow 0N/A * if x < -7.45133219101941108420e+02 then exp(x) underflow 0N/A * The hexadecimal values are the intended ones for the following 0N/A * constants. The decimal values may be used, provided that the 0N/A * compiler will convert from decimal to binary accurately enough 0N/A * to produce the hexadecimal values shown. 0N/A twom1000=
9.33263618503218878990e-302,
/* 2**-1000=0x01700000,0*/ 0N/A u_threshold= -
7.45133219101941108420e+02,
/* 0xc0874910, 0xD52D3051 */ 0N/A ln2HI[
2] ={
6.93147180369123816490e-01,
/* 0x3fe62e42, 0xfee00000 */ 0N/A -
6.93147180369123816490e-01,},
/* 0xbfe62e42, 0xfee00000 */ 0N/A ln2LO[
2] ={
1.90821492927058770002e-10,
/* 0x3dea39ef, 0x35793c76 */ 0N/A -
1.90821492927058770002e-10,},
/* 0xbdea39ef, 0x35793c76 */ 0N/A invln2 =
1.44269504088896338700e+00,
/* 0x3ff71547, 0x652b82fe */ 0N/A P1 =
1.66666666666666019037e-01,
/* 0x3FC55555, 0x5555553E */ 0N/A P2 = -
2.77777777770155933842e-03,
/* 0xBF66C16C, 0x16BEBD93 */ 0N/A P3 =
6.61375632143793436117e-05,
/* 0x3F11566A, 0xAF25DE2C */ 0N/A P4 = -
1.65339022054652515390e-06,
/* 0xBEBBBD41, 0xC5D26BF1 */ 0N/A P5 =
4.13813679705723846039e-08;
/* 0x3E663769, 0x72BEA4D0 */ 0N/A hx &=
0x7fffffff;
/* high word of |x| */ 0N/A /* filter out non-finite argument */ 0N/A if(
hx >=
0x40862E42) {
/* if |x|>=709.78... */ 0N/A return x+x;
/* NaN */ 0N/A else return (
xsb==0)? x:
0.0;
/* exp(+-inf)={inf,0} */ 0N/A /* argument reduction */ 0N/A if(
hx >
0x3fd62e42) {
/* if |x| > 0.5 ln2 */ 0N/A if(
hx <
0x3FF0A2B2) {
/* and |x| < 1.5 ln2 */ 0N/A else if(
hx <
0x3e300000) {
/* when |x|<2**-28 */ 0N/A /* x is now in primary range */ 0N/A if(k==0)
return one-((x*c)/(c-
2.0)-x);
0N/A __HI(y) += (k<<
20);
/* add k to y's exponent */ 0N/A __HI(y) += ((k+
1000)<<
20);
/* add k to y's exponent */ 0N/A/* __ieee754_pow(x,y) return x**y 0N/A * Method: Let x = 2 * (1+f) 0N/A * 1. Compute and return log2(x) in two pieces: 0N/A * log2(x) = w1 + w2, 0N/A * where w1 has 53-24 = 29 bit trailing zeros. 0N/A * 2. Perform y*log2(x) = n+y' by simulating muti-precision 0N/A * arithmetic, where |y'|<=0.5. 0N/A * 3. Return x**y = 2**n*exp(y'*log2) 0N/A * 1. (anything) ** 0 is 1 0N/A * 2. (anything) ** 1 is itself 0N/A * 3. (anything) ** NAN is NAN 0N/A * 4. NAN ** (anything except 0) is NAN 0N/A * 5. +-(|x| > 1) ** +INF is +INF 0N/A * 6. +-(|x| > 1) ** -INF is +0 0N/A * 7. +-(|x| < 1) ** +INF is +0 0N/A * 8. +-(|x| < 1) ** -INF is +INF 0N/A * 9. +-1 ** +-INF is NAN 0N/A * 10. +0 ** (+anything except 0, NAN) is +0 0N/A * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 0N/A * 12. +0 ** (-anything except 0, NAN) is +INF 0N/A * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF 0N/A * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) 0N/A * 15. +INF ** (+anything except 0,NAN) is +INF 0N/A * 16. +INF ** (-anything except 0,NAN) is +0 0N/A * 17. -INF ** (anything) = -0 ** (-anything) 0N/A * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) 0N/A * 19. (-anything except 0 and inf) ** (non-integer) is NAN 0N/A * pow(x,y) returns x**y nearly rounded. In particular 0N/A * pow(integer,integer) 0N/A * always returns the correct integer provided it is 0N/A * The hexadecimal values are the intended ones for the following 0N/A * constants. The decimal values may be used, provided that the 0N/A * compiler will convert from decimal to binary accurately enough 0N/A * to produce the hexadecimal values shown. 0N/A dp_h[] = {
0.0,
5.84962487220764160156e-01,},
/* 0x3FE2B803, 0x40000000 */ 0N/A dp_l[] = {
0.0,
1.35003920212974897128e-08,},
/* 0x3E4CFDEB, 0x43CFD006 */ 0N/A two53 =
9007199254740992.0,
/* 0x43400000, 0x00000000 */ 0N/A /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ 0N/A L1X =
5.99999999999994648725e-01,
/* 0x3FE33333, 0x33333303 */ 0N/A L2X =
4.28571428578550184252e-01,
/* 0x3FDB6DB6, 0xDB6FABFF */ 0N/A L3X =
3.33333329818377432918e-01,
/* 0x3FD55555, 0x518F264D */ 0N/A L4X =
2.72728123808534006489e-01,
/* 0x3FD17460, 0xA91D4101 */ 0N/A L5X =
2.30660745775561754067e-01,
/* 0x3FCD864A, 0x93C9DB65 */ 0N/A L6X =
2.06975017800338417784e-01,
/* 0x3FCA7E28, 0x4A454EEF */ 0N/A lg2 =
6.93147180559945286227e-01,
/* 0x3FE62E42, 0xFEFA39EF */ 0N/A lg2_h =
6.93147182464599609375e-01,
/* 0x3FE62E43, 0x00000000 */ 0N/A lg2_l = -
1.90465429995776804525e-09,
/* 0xBE205C61, 0x0CA86C39 */ 0N/A ovt =
8.0085662595372944372e-0017,
/* -(1024-log2(ovfl+.5ulp)) */ 0N/A cp =
9.61796693925975554329e-01,
/* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ 0N/A cp_h =
9.61796700954437255859e-01,
/* 0x3FEEC709, 0xE0000000 =(float)cp */ 0N/A cp_l = -
7.02846165095275826516e-09,
/* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ 0N/A ivln2 =
1.44269504088896338700e+00,
/* 0x3FF71547, 0x652B82FE =1/ln2 */ 0N/A ivln2_h =
1.44269502162933349609e+00,
/* 0x3FF71547, 0x60000000 =24b 1/ln2*/ 0N/A ivln2_l =
1.92596299112661746887e-08;
/* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ 0N/A /* y==zero: x**0 = 1 */ 0N/A /* +-NaN return x+y */ 0N/A if(
ix >
0x7ff00000 || ((
ix==
0x7ff00000)&&(
lx!=0)) ||
0N/A iy >
0x7ff00000 || ((
iy==
0x7ff00000)&&(
ly!=0)))
0N/A /* determine if y is an odd int when x < 0 0N/A * yisint = 0 ... y is not an integer 0N/A * yisint = 1 ... y is an odd int 0N/A * yisint = 2 ... y is an even int 0N/A else if(
iy>=
0x3ff00000) {
0N/A k = (
iy>>
20)-
0x3ff;
/* exponent */ 0N/A /* special value of y */ 0N/A if (
iy==
0x7ff00000) {
/* y is +-inf */ 0N/A return y - y;
/* inf**+-1 is NaN */ 0N/A else if (
ix >=
0x3ff00000)
/* (|x|>1)**+-inf = inf,0 */ 0N/A else /* (|x|<1)**-,+inf = inf,0 */ 0N/A if(
iy==
0x3ff00000) {
/* y is +-1 */ 0N/A if(
hy==
0x40000000)
return x*x;
/* y is 2 */ 0N/A if(
hy==
0x3fe00000) {
/* y is 0.5 */ 0N/A /* special value of x */ 0N/A z =
ax;
/*x is +-0,+-inf,+-1*/ 0N/A z = (z-z)/(z-z);
/* (-1)**non-int is NaN */ 0N/A z = -
1.0*z;
/* (x<0)**odd = -(|x|**odd) */ 0N/A /* (x<0)**(non-int) is NaN */ 0N/A s =
one;
/* s (sign of result -ve**odd) = -1 else = 1 */ 0N/A if(
iy>
0x41e00000) {
/* if |y| > 2**31 */ 0N/A /* now |1-x| is tiny <= 2**-20, suffice to compute 0N/A log(x) by x-x^2/2+x^3/3-x^4/4 */ 0N/A t =
ax-
one;
/* t has 20 trailing zeros */ 0N/A w = (t*t)*(
0.5-t*(
0.3333333333333333333333-t*
0.25));
0N/A /* take care subnormal number */ 0N/A /* determine interval */ 0N/A ix = j|
0x3ff00000;
/* normalize ix */ 0N/A if(j<=
0x3988E) k=0;
/* |x|<sqrt(3/2) */ 0N/A else if(j<
0xBB67A) k=
1;
/* |x|<sqrt(3) */ 0N/A else {k=0;n+=
1;
ix -=
0x00100000;}
0N/A /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ 0N/A u =
ax-
bp[k];
/* bp[0]=1.0, bp[1]=1.5 */ 0N/A /* t_h=ax+bp[k] High */ 0N/A /* compute log(ax) */ 0N/A /* u+v = ss*(1+...) */ 0N/A /* 2/(3log2)*(ss+...) */ 0N/A /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ 0N/A /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ 0N/A if (j>=
0x40900000) {
/* z >= 1024 */ 0N/A if(((j-
0x40900000)|i)!=0)
/* if z > 1024 */ 0N/A }
else if((j&
0x7fffffff)>=
0x4090cc00 ) {
/* z <= -1075 */ 0N/A if(((j-
0xc090cc00)|i)!=0)
/* z < -1075 */ 0N/A * compute 2**(p_h+p_l) 0N/A if(i>
0x3fe00000) {
/* if |z| > 0.5, set n = [z+0.5] */ 0N/A n = j+(
0x00100000>>(k+
1));
0N/A k = ((n&
0x7fffffff)>>
20)-
0x3ff;
/* new k for n */ 0N/A n = ((n&
0x000fffff)|
0x00100000)>>(
20-k);
0N/A if((j>>
20)<=0) z =
scalbn(z,n);
/* subnormal output */