8139688: Port fdlibm exp to Java
Reviewed-by: bpb, nadezhin
This commit is contained in:
parent
2e27b2e68a
commit
be91309965
@ -150,7 +150,6 @@ SUNWprivate_1.1 {
|
||||
Java_java_lang_StrictMath_atan;
|
||||
Java_java_lang_StrictMath_atan2;
|
||||
Java_java_lang_StrictMath_cos;
|
||||
Java_java_lang_StrictMath_exp;
|
||||
Java_java_lang_StrictMath_log;
|
||||
Java_java_lang_StrictMath_log10;
|
||||
Java_java_lang_StrictMath_sin;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -79,7 +79,8 @@ class FdLibm {
|
||||
*/
|
||||
private static double __LO(double x, int low) {
|
||||
long transX = Double.doubleToRawLongBits(x);
|
||||
return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
|
||||
return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L) |
|
||||
(low & 0x0000_0000_FFFF_FFFFL));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,7 +97,8 @@ class FdLibm {
|
||||
*/
|
||||
private static double __HI(double x, int high) {
|
||||
long transX = Double.doubleToRawLongBits(x);
|
||||
return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
|
||||
return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) |
|
||||
( ((long)high)) << 32 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -580,4 +582,152 @@ class FdLibm {
|
||||
return s * z;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exponential of x.
|
||||
*
|
||||
* Method
|
||||
* 1. Argument reduction:
|
||||
* Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
|
||||
* Given x, find r and integer k such that
|
||||
*
|
||||
* x = k*ln2 + r, |r| <= 0.5*ln2.
|
||||
*
|
||||
* Here r will be represented as r = hi-lo for better
|
||||
* accuracy.
|
||||
*
|
||||
* 2. Approximation of exp(r) by a special rational function on
|
||||
* the interval [0,0.34658]:
|
||||
* Write
|
||||
* R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
|
||||
* We use a special Reme algorithm on [0,0.34658] to generate
|
||||
* a polynomial of degree 5 to approximate R. The maximum error
|
||||
* of this polynomial approximation is bounded by 2**-59. In
|
||||
* other words,
|
||||
* R(z) ~ 2.0 + 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 listed below)
|
||||
* and
|
||||
* | 5 | -59
|
||||
* | 2.0+P1*z+...+P5*z - R(z) | <= 2
|
||||
* | |
|
||||
* The computation of exp(r) thus becomes
|
||||
* 2*r
|
||||
* exp(r) = 1 + -------
|
||||
* R - r
|
||||
* r*R1(r)
|
||||
* = 1 + r + ----------- (for better accuracy)
|
||||
* 2 - R1(r)
|
||||
* where
|
||||
* 2 4 10
|
||||
* R1(r) = r - (P1*r + P2*r + ... + P5*r ).
|
||||
*
|
||||
* 3. Scale back to obtain exp(x):
|
||||
* From step 1, we have
|
||||
* exp(x) = 2^k * exp(r)
|
||||
*
|
||||
* Special cases:
|
||||
* exp(INF) is INF, exp(NaN) is NaN;
|
||||
* exp(-INF) is 0, and
|
||||
* for finite argument, only exp(0)=1 is exact.
|
||||
*
|
||||
* Accuracy:
|
||||
* according to an error analysis, the error is always less than
|
||||
* 1 ulp (unit in the last place).
|
||||
*
|
||||
* Misc. info.
|
||||
* For IEEE double
|
||||
* if x > 7.09782712893383973096e+02 then exp(x) overflow
|
||||
* if x < -7.45133219101941108420e+02 then exp(x) underflow
|
||||
*
|
||||
* Constants:
|
||||
* The hexadecimal values are the intended ones for the following
|
||||
* constants. The decimal values may be used, provided that the
|
||||
* compiler will convert from decimal to binary accurately enough
|
||||
* to produce the hexadecimal values shown.
|
||||
*/
|
||||
static class Exp {
|
||||
private static final double one = 1.0;
|
||||
private static final double[] half = {0.5, -0.5,};
|
||||
private static final double huge = 1.0e+300;
|
||||
private static final double twom1000= 0x1.0p-1000; // 9.33263618503218878990e-302 = 2^-1000
|
||||
private static final double o_threshold= 0x1.62e42fefa39efp9; // 7.09782712893383973096e+02
|
||||
private static final double u_threshold= -0x1.74910d52d3051p9; // -7.45133219101941108420e+02;
|
||||
private static final double[] ln2HI ={ 0x1.62e42feep-1, // 6.93147180369123816490e-01
|
||||
-0x1.62e42feep-1}; // -6.93147180369123816490e-01
|
||||
private static final double[] ln2LO ={ 0x1.a39ef35793c76p-33, // 1.90821492927058770002e-10
|
||||
-0x1.a39ef35793c76p-33}; // -1.90821492927058770002e-10
|
||||
private static final double invln2 = 0x1.71547652b82fep0; // 1.44269504088896338700e+00
|
||||
|
||||
private static final double P1 = 0x1.555555555553ep-3; // 1.66666666666666019037e-01
|
||||
private static final double P2 = -0x1.6c16c16bebd93p-9; // -2.77777777770155933842e-03
|
||||
private static final double P3 = 0x1.1566aaf25de2cp-14; // 6.61375632143793436117e-05
|
||||
private static final double P4 = -0x1.bbd41c5d26bf1p-20; // -1.65339022054652515390e-06
|
||||
private static final double P5 = 0x1.6376972bea4d0p-25; // 4.13813679705723846039e-08
|
||||
|
||||
// should be able to forgo strictfp due to controlled over/underflow
|
||||
public static strictfp double compute(double x) {
|
||||
double y;
|
||||
double hi = 0.0;
|
||||
double lo = 0.0;
|
||||
double c;
|
||||
double t;
|
||||
int k = 0;
|
||||
int xsb;
|
||||
/*unsigned*/ int hx;
|
||||
|
||||
hx = __HI(x); /* high word of x */
|
||||
xsb = (hx >> 31) & 1; /* sign bit of x */
|
||||
hx &= 0x7fffffff; /* high word of |x| */
|
||||
|
||||
/* filter out non-finite argument */
|
||||
if (hx >= 0x40862E42) { /* if |x| >= 709.78... */
|
||||
if (hx >= 0x7ff00000) {
|
||||
if (((hx & 0xfffff) | __LO(x)) != 0)
|
||||
return x + x; /* NaN */
|
||||
else
|
||||
return (xsb == 0) ? x : 0.0; /* exp(+-inf) = {inf, 0} */
|
||||
}
|
||||
if (x > o_threshold)
|
||||
return huge * huge; /* overflow */
|
||||
if (x < u_threshold) // unsigned compare needed here?
|
||||
return twom1000 * twom1000; /* underflow */
|
||||
}
|
||||
|
||||
/* argument reduction */
|
||||
if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
|
||||
if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
|
||||
hi = x - ln2HI[xsb];
|
||||
lo=ln2LO[xsb];
|
||||
k = 1 - xsb - xsb;
|
||||
} else {
|
||||
k = (int)(invln2 * x + half[xsb]);
|
||||
t = k;
|
||||
hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
|
||||
lo = t*ln2LO[0];
|
||||
}
|
||||
x = hi - lo;
|
||||
} else if (hx < 0x3e300000) { /* when |x|<2**-28 */
|
||||
if (huge + x > one)
|
||||
return one + x; /* trigger inexact */
|
||||
} else {
|
||||
k = 0;
|
||||
}
|
||||
|
||||
/* x is now in primary range */
|
||||
t = x * x;
|
||||
c = x - t*(P1 + t*(P2 + t*(P3 + t*(P4 + t*P5))));
|
||||
if (k == 0)
|
||||
return one - ((x*c)/(c - 2.0) - x);
|
||||
else
|
||||
y = one - ((lo - (x*c)/(2.0 - c)) - hi);
|
||||
|
||||
if(k >= -1021) {
|
||||
y = __HI(y, __HI(y) + (k << 20)); /* add k to y's exponent */
|
||||
return y;
|
||||
} else {
|
||||
y = __HI(y, __HI(y) + ((k + 1000) << 20)); /* add k to y's exponent */
|
||||
return y * twom1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -227,7 +227,9 @@ public final class StrictMath {
|
||||
* @return the value <i>e</i><sup>{@code a}</sup>,
|
||||
* where <i>e</i> is the base of the natural logarithms.
|
||||
*/
|
||||
public static native double exp(double a);
|
||||
public static double exp(double a) {
|
||||
return FdLibm.Exp.compute(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the natural logarithm (base <i>e</i>) of a {@code double}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -64,12 +64,6 @@ Java_java_lang_StrictMath_atan(JNIEnv *env, jclass unused, jdouble d)
|
||||
return (jdouble) jatan((double)d);
|
||||
}
|
||||
|
||||
JNIEXPORT jdouble JNICALL
|
||||
Java_java_lang_StrictMath_exp(JNIEnv *env, jclass unused, jdouble d)
|
||||
{
|
||||
return (jdouble) jexp((double)d);
|
||||
}
|
||||
|
||||
JNIEXPORT jdouble JNICALL
|
||||
Java_java_lang_StrictMath_log(JNIEnv *env, jclass unused, jdouble d)
|
||||
{
|
||||
|
147
jdk/test/java/lang/StrictMath/ExpTests.java
Normal file
147
jdk/test/java/lang/StrictMath/ExpTests.java
Normal file
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8139688
|
||||
* @key randomness
|
||||
* @library /lib/testlibrary/
|
||||
* @build jdk.testlibrary.RandomFactory
|
||||
* @build Tests
|
||||
* @build FdlibmTranslit
|
||||
* @build ExpTests
|
||||
* @run main ExpTests
|
||||
* @summary Tests specifically for StrictMath.exp
|
||||
*/
|
||||
|
||||
import jdk.testlibrary.RandomFactory;
|
||||
|
||||
/**
|
||||
* The role of this test is to verify that the FDLIBM exp algorithm is
|
||||
* being used by running golden file style tests on values that may
|
||||
* vary from one conforming exponential implementation to another.
|
||||
*/
|
||||
|
||||
public class ExpTests {
|
||||
private ExpTests(){}
|
||||
|
||||
public static void main(String [] argv) {
|
||||
int failures = 0;
|
||||
|
||||
failures += testExp();
|
||||
failures += testAgainstTranslit();
|
||||
|
||||
if (failures > 0) {
|
||||
System.err.println("Testing the exponential incurred "
|
||||
+ failures + " failures.");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
// From the fdlibm source, the overflow threshold in hex is:
|
||||
// 0x4086_2E42_FEFA_39EF.
|
||||
static final double EXP_OVERFLOW_THRESH = Double.longBitsToDouble(0x4086_2E42_FEFA_39EFL);
|
||||
|
||||
// From the fdlibm source, the underflow threshold in hex is:
|
||||
// 0xc087_4910_D52D_3051L.
|
||||
static final double EXP_UNDERFLOW_THRESH = Double.longBitsToDouble(0xc087_4910_D52D_3051L);
|
||||
|
||||
static int testExp() {
|
||||
int failures = 0;
|
||||
|
||||
double [][] testCases = {
|
||||
// Some of these could be moved to common Math/StrictMath exp testing.
|
||||
{Double.NaN, Double.NaN},
|
||||
{Double.MAX_VALUE, Double.POSITIVE_INFINITY},
|
||||
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
|
||||
{Double.NEGATIVE_INFINITY, +0.0},
|
||||
{EXP_OVERFLOW_THRESH, 0x1.ffff_ffff_fff2ap1023},
|
||||
{Math.nextUp(EXP_OVERFLOW_THRESH), Double.POSITIVE_INFINITY},
|
||||
{Math.nextDown(EXP_UNDERFLOW_THRESH), +0.0},
|
||||
{EXP_UNDERFLOW_THRESH, +Double.MIN_VALUE},
|
||||
};
|
||||
|
||||
for(double[] testCase: testCases)
|
||||
failures+=testExpCase(testCase[0], testCase[1]);
|
||||
|
||||
return failures;
|
||||
}
|
||||
|
||||
static int testExpCase(double input, double expected) {
|
||||
int failures = 0;
|
||||
|
||||
failures+=Tests.test("StrictMath.exp(double)", input,
|
||||
StrictMath.exp(input), expected);
|
||||
return failures;
|
||||
}
|
||||
|
||||
// Initialize shared random number generator
|
||||
private static java.util.Random random = RandomFactory.getRandom();
|
||||
|
||||
/**
|
||||
* Test StrictMath.exp against transliteration port of exp.
|
||||
*/
|
||||
private static int testAgainstTranslit() {
|
||||
int failures = 0;
|
||||
|
||||
double[] decisionPoints = {
|
||||
// Near overflow threshold
|
||||
EXP_OVERFLOW_THRESH - 512*Math.ulp(EXP_OVERFLOW_THRESH),
|
||||
|
||||
// Near underflow threshold
|
||||
EXP_UNDERFLOW_THRESH - 512*Math.ulp(EXP_UNDERFLOW_THRESH),
|
||||
|
||||
// Straddle algorithm conditional checks
|
||||
Double.longBitsToDouble(0x4086_2E42_0000_0000L - 512L),
|
||||
Double.longBitsToDouble(0x3fd6_2e42_0000_0000L - 512L),
|
||||
Double.longBitsToDouble(0x3FF0_A2B2_0000_0000L - 512L),
|
||||
Double.longBitsToDouble(0x3e30_0000_0000_0000L - 512L),
|
||||
|
||||
// Other notable points
|
||||
Double.MIN_NORMAL - Math.ulp(Double.MIN_NORMAL)*512,
|
||||
-Double.MIN_VALUE*512,
|
||||
};
|
||||
|
||||
for (double decisionPoint : decisionPoints) {
|
||||
double ulp = Math.ulp(decisionPoint);
|
||||
failures += testRange(decisionPoint - 1024*ulp, ulp, 1_024);
|
||||
}
|
||||
|
||||
// Try out some random values
|
||||
for (int i = 0; i < 100; i++) {
|
||||
double x = Tests.createRandomDouble(random);
|
||||
failures += testRange(x, Math.ulp(x), 100);
|
||||
}
|
||||
|
||||
return failures;
|
||||
}
|
||||
|
||||
private static int testRange(double start, double increment, int count) {
|
||||
int failures = 0;
|
||||
double x = start;
|
||||
for (int i = 0; i < count; i++, x += increment) {
|
||||
failures += testExpCase(x, FdlibmTranslit.Exp.compute(x));
|
||||
}
|
||||
return failures;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -48,7 +48,8 @@ public class FdlibmTranslit {
|
||||
*/
|
||||
private static double __LO(double x, int low) {
|
||||
long transX = Double.doubleToRawLongBits(x);
|
||||
return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
|
||||
return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L) |
|
||||
(low & 0x0000_0000_FFFF_FFFFL));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +66,8 @@ public class FdlibmTranslit {
|
||||
*/
|
||||
private static double __HI(double x, int high) {
|
||||
long transX = Double.doubleToRawLongBits(x);
|
||||
return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
|
||||
return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) |
|
||||
( ((long)high)) << 32 );
|
||||
}
|
||||
|
||||
public static double hypot(double x, double y) {
|
||||
@ -250,4 +252,136 @@ public class FdlibmTranslit {
|
||||
return w;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exponential of x.
|
||||
*
|
||||
* Method
|
||||
* 1. Argument reduction:
|
||||
* Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
|
||||
* Given x, find r and integer k such that
|
||||
*
|
||||
* x = k*ln2 + r, |r| <= 0.5*ln2.
|
||||
*
|
||||
* Here r will be represented as r = hi-lo for better
|
||||
* accuracy.
|
||||
*
|
||||
* 2. Approximation of exp(r) by a special rational function on
|
||||
* the interval [0,0.34658]:
|
||||
* Write
|
||||
* R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
|
||||
* We use a special Reme algorithm on [0,0.34658] to generate
|
||||
* a polynomial of degree 5 to approximate R. The maximum error
|
||||
* of this polynomial approximation is bounded by 2**-59. In
|
||||
* other words,
|
||||
* R(z) ~ 2.0 + 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 listed below)
|
||||
* and
|
||||
* | 5 | -59
|
||||
* | 2.0+P1*z+...+P5*z - R(z) | <= 2
|
||||
* | |
|
||||
* The computation of exp(r) thus becomes
|
||||
* 2*r
|
||||
* exp(r) = 1 + -------
|
||||
* R - r
|
||||
* r*R1(r)
|
||||
* = 1 + r + ----------- (for better accuracy)
|
||||
* 2 - R1(r)
|
||||
* where
|
||||
* 2 4 10
|
||||
* R1(r) = r - (P1*r + P2*r + ... + P5*r ).
|
||||
*
|
||||
* 3. Scale back to obtain exp(x):
|
||||
* From step 1, we have
|
||||
* exp(x) = 2^k * exp(r)
|
||||
*
|
||||
* Special cases:
|
||||
* exp(INF) is INF, exp(NaN) is NaN;
|
||||
* exp(-INF) is 0, and
|
||||
* for finite argument, only exp(0)=1 is exact.
|
||||
*
|
||||
* Accuracy:
|
||||
* according to an error analysis, the error is always less than
|
||||
* 1 ulp (unit in the last place).
|
||||
*
|
||||
* Misc. info.
|
||||
* For IEEE double
|
||||
* if x > 7.09782712893383973096e+02 then exp(x) overflow
|
||||
* if x < -7.45133219101941108420e+02 then exp(x) underflow
|
||||
*
|
||||
* Constants:
|
||||
* The hexadecimal values are the intended ones for the following
|
||||
* constants. The decimal values may be used, provided that the
|
||||
* compiler will convert from decimal to binary accurately enough
|
||||
* to produce the hexadecimal values shown.
|
||||
*/
|
||||
static class Exp {
|
||||
private static final double one = 1.0;
|
||||
private static final double[] halF = {0.5,-0.5,};
|
||||
private static final double huge = 1.0e+300;
|
||||
private static final double twom1000= 9.33263618503218878990e-302; /* 2**-1000=0x01700000,0*/
|
||||
private static final double o_threshold= 7.09782712893383973096e+02; /* 0x40862E42, 0xFEFA39EF */
|
||||
private static final double u_threshold= -7.45133219101941108420e+02; /* 0xc0874910, 0xD52D3051 */
|
||||
private static final double[] ln2HI ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
|
||||
-6.93147180369123816490e-01}; /* 0xbfe62e42, 0xfee00000 */
|
||||
private static final double[] ln2LO ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
|
||||
-1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */
|
||||
private static final double invln2 = 1.44269504088896338700e+00; /* 0x3ff71547, 0x652b82fe */
|
||||
private static final double P1 = 1.66666666666666019037e-01; /* 0x3FC55555, 0x5555553E */
|
||||
private static final double P2 = -2.77777777770155933842e-03; /* 0xBF66C16C, 0x16BEBD93 */
|
||||
private static final double P3 = 6.61375632143793436117e-05; /* 0x3F11566A, 0xAF25DE2C */
|
||||
private static final double P4 = -1.65339022054652515390e-06; /* 0xBEBBBD41, 0xC5D26BF1 */
|
||||
private static final double P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
|
||||
|
||||
public static strictfp double compute(double x) {
|
||||
double y,hi=0,lo=0,c,t;
|
||||
int k=0,xsb;
|
||||
/*unsigned*/ int hx;
|
||||
|
||||
hx = __HI(x); /* high word of x */
|
||||
xsb = (hx>>31)&1; /* sign bit of x */
|
||||
hx &= 0x7fffffff; /* high word of |x| */
|
||||
|
||||
/* filter out non-finite argument */
|
||||
if(hx >= 0x40862E42) { /* if |x|>=709.78... */
|
||||
if(hx>=0x7ff00000) {
|
||||
if(((hx&0xfffff)|__LO(x))!=0)
|
||||
return x+x; /* NaN */
|
||||
else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
|
||||
}
|
||||
if(x > o_threshold) return huge*huge; /* overflow */
|
||||
if(x < u_threshold) return twom1000*twom1000; /* underflow */
|
||||
}
|
||||
|
||||
/* argument reduction */
|
||||
if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
|
||||
if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
|
||||
hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
|
||||
} else {
|
||||
k = (int)(invln2*x+halF[xsb]);
|
||||
t = k;
|
||||
hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
|
||||
lo = t*ln2LO[0];
|
||||
}
|
||||
x = hi - lo;
|
||||
}
|
||||
else if(hx < 0x3e300000) { /* when |x|<2**-28 */
|
||||
if(huge+x>one) return one+x;/* trigger inexact */
|
||||
}
|
||||
else k = 0;
|
||||
|
||||
/* x is now in primary range */
|
||||
t = x*x;
|
||||
c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
|
||||
if(k==0) return one-((x*c)/(c-2.0)-x);
|
||||
else y = one-((lo-(x*c)/(2.0-c))-hi);
|
||||
if(k >= -1021) {
|
||||
y = __HI(y, __HI(y) + (k<<20)); /* add k to y's exponent */
|
||||
return y;
|
||||
} else {
|
||||
y = __HI(y, __HI(y) + ((k+1000)<<20));/* add k to y's exponent */
|
||||
return y*twom1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user