Skip to content

Commit 3449324

Browse files
committed
8301396: Port fdlibm expm1 to Java
Reviewed-by: bpb
1 parent 3be5317 commit 3449324

File tree

4 files changed

+504
-19
lines changed

4 files changed

+504
-19
lines changed

src/java.base/share/classes/java/lang/FdLibm.java

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,4 +985,213 @@ public static double compute(double x) {
985985
}
986986
}
987987
}
988+
989+
/* expm1(x)
990+
* Returns exp(x)-1, the exponential of x minus 1.
991+
*
992+
* Method
993+
* 1. Argument reduction:
994+
* Given x, find r and integer k such that
995+
*
996+
* x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
997+
*
998+
* Here a correction term c will be computed to compensate
999+
* the error in r when rounded to a floating-point number.
1000+
*
1001+
* 2. Approximating expm1(r) by a special rational function on
1002+
* the interval [0,0.34658]:
1003+
* Since
1004+
* r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
1005+
* we define R1(r*r) by
1006+
* r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
1007+
* That is,
1008+
* R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
1009+
* = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
1010+
* = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
1011+
* We use a special Reme algorithm on [0,0.347] to generate
1012+
* a polynomial of degree 5 in r*r to approximate R1. The
1013+
* maximum error of this polynomial approximation is bounded
1014+
* by 2**-61. In other words,
1015+
* R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
1016+
* where Q1 = -1.6666666666666567384E-2,
1017+
* Q2 = 3.9682539681370365873E-4,
1018+
* Q3 = -9.9206344733435987357E-6,
1019+
* Q4 = 2.5051361420808517002E-7,
1020+
* Q5 = -6.2843505682382617102E-9;
1021+
* (where z=r*r, and the values of Q1 to Q5 are listed below)
1022+
* with error bounded by
1023+
* | 5 | -61
1024+
* | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
1025+
* | |
1026+
*
1027+
* expm1(r) = exp(r)-1 is then computed by the following
1028+
* specific way which minimize the accumulation rounding error:
1029+
* 2 3
1030+
* r r [ 3 - (R1 + R1*r/2) ]
1031+
* expm1(r) = r + --- + --- * [--------------------]
1032+
* 2 2 [ 6 - r*(3 - R1*r/2) ]
1033+
*
1034+
* To compensate the error in the argument reduction, we use
1035+
* expm1(r+c) = expm1(r) + c + expm1(r)*c
1036+
* ~ expm1(r) + c + r*c
1037+
* Thus c+r*c will be added in as the correction terms for
1038+
* expm1(r+c). Now rearrange the term to avoid optimization
1039+
* screw up:
1040+
* ( 2 2 )
1041+
* ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
1042+
* expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
1043+
* ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
1044+
* ( )
1045+
*
1046+
* = r - E
1047+
* 3. Scale back to obtain expm1(x):
1048+
* From step 1, we have
1049+
* expm1(x) = either 2^k*[expm1(r)+1] - 1
1050+
* = or 2^k*[expm1(r) + (1-2^-k)]
1051+
* 4. Implementation notes:
1052+
* (A). To save one multiplication, we scale the coefficient Qi
1053+
* to Qi*2^i, and replace z by (x^2)/2.
1054+
* (B). To achieve maximum accuracy, we compute expm1(x) by
1055+
* (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
1056+
* (ii) if k=0, return r-E
1057+
* (iii) if k=-1, return 0.5*(r-E)-0.5
1058+
* (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
1059+
* else return 1.0+2.0*(r-E);
1060+
* (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
1061+
* (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
1062+
* (vii) return 2^k(1-((E+2^-k)-r))
1063+
*
1064+
* Special cases:
1065+
* expm1(INF) is INF, expm1(NaN) is NaN;
1066+
* expm1(-INF) is -1, and
1067+
* for finite argument, only expm1(0)=0 is exact.
1068+
*
1069+
* Accuracy:
1070+
* according to an error analysis, the error is always less than
1071+
* 1 ulp (unit in the last place).
1072+
*
1073+
* Misc. info.
1074+
* For IEEE double
1075+
* if x > 7.09782712893383973096e+02 then expm1(x) overflow
1076+
*
1077+
* Constants:
1078+
* The hexadecimal values are the intended ones for the following
1079+
* constants. The decimal values may be used, provided that the
1080+
* compiler will convert from decimal to binary accurately enough
1081+
* to produce the hexadecimal values shown.
1082+
*/
1083+
static class Expm1 {
1084+
private static final double one = 1.0;
1085+
private static final double huge = 1.0e+300;
1086+
private static final double tiny = 1.0e-300;
1087+
private static final double o_threshold = 0x1.62e42fefa39efp9; // 7.09782712893383973096e+02
1088+
private static final double ln2_hi = 0x1.62e42feep-1; // 6.93147180369123816490e-01
1089+
private static final double ln2_lo = 0x1.a39ef35793c76p-33; // 1.90821492927058770002e-10
1090+
private static final double invln2 = 0x1.71547652b82fep0; // 1.44269504088896338700e+00
1091+
// scaled coefficients related to expm1
1092+
private static final double Q1 = -0x1.11111111110f4p-5; // -3.33333333333331316428e-02
1093+
private static final double Q2 = 0x1.a01a019fe5585p-10; // 1.58730158725481460165e-03
1094+
private static final double Q3 = -0x1.4ce199eaadbb7p-14; // -7.93650757867487942473e-05
1095+
private static final double Q4 = 0x1.0cfca86e65239p-18; // 4.00821782732936239552e-06
1096+
private static final double Q5 = -0x1.afdb76e09c32dp-23; // -2.01099218183624371326e-07
1097+
1098+
static double compute(double x) {
1099+
double y, hi, lo, c=0, t, e, hxs, hfx, r1;
1100+
int k, xsb;
1101+
/*unsigned*/ int hx;
1102+
1103+
hx = __HI(x); // high word of x
1104+
xsb = hx & 0x8000_0000; // sign bit of x
1105+
y = Math.abs(x);
1106+
hx &= 0x7fff_ffff; // high word of |x|
1107+
1108+
// filter out huge and non-finite argument
1109+
if (hx >= 0x4043_687A) { // if |x| >= 56*ln2
1110+
if (hx >= 0x4086_2E42) { // if |x| >= 709.78...
1111+
if (hx >= 0x7ff_00000) {
1112+
if (((hx & 0xf_ffff) | __LO(x)) != 0) {
1113+
return x + x; // NaN
1114+
} else {
1115+
return (xsb == 0)? x : -1.0; // exp(+-inf)={inf,-1}
1116+
}
1117+
}
1118+
if (x > o_threshold) {
1119+
return huge*huge; // overflow
1120+
}
1121+
}
1122+
if (xsb != 0) { // x < -56*ln2, return -1.0 with inexact
1123+
if (x + tiny < 0.0) { // raise inexact
1124+
return tiny - one; // return -1
1125+
}
1126+
}
1127+
}
1128+
1129+
// argument reduction
1130+
if (hx > 0x3fd6_2e42) { // if |x| > 0.5 ln2
1131+
if (hx < 0x3FF0_A2B2) { // and |x| < 1.5 ln2
1132+
if (xsb == 0) {
1133+
hi = x - ln2_hi;
1134+
lo = ln2_lo;
1135+
k = 1;
1136+
} else {
1137+
hi = x + ln2_hi;
1138+
lo = -ln2_lo;
1139+
k = -1;
1140+
}
1141+
} else {
1142+
k = (int)(invln2*x + ((xsb == 0) ? 0.5 : -0.5));
1143+
t = k;
1144+
hi = x - t*ln2_hi; // t*ln2_hi is exact here
1145+
lo = t*ln2_lo;
1146+
}
1147+
x = hi - lo;
1148+
c = (hi - x) - lo;
1149+
} else if (hx < 0x3c90_0000) { // when |x| < 2**-54, return x
1150+
t = huge + x; // return x with inexact flags when x != 0
1151+
return x - (t - (huge + x));
1152+
} else {
1153+
k = 0;
1154+
}
1155+
1156+
// x is now in primary range
1157+
hfx = 0.5*x;
1158+
hxs = x*hfx;
1159+
r1 = one + hxs*(Q1 + hxs*(Q2 + hxs*(Q3 + hxs*(Q4 + hxs*Q5))));
1160+
t = 3.0 - r1*hfx;
1161+
e = hxs *((r1 - t)/(6.0 - x*t));
1162+
if (k == 0) {
1163+
return x - (x*e - hxs); // c is 0
1164+
} else {
1165+
e = (x*(e - c) - c);
1166+
e -= hxs;
1167+
if (k == -1) {
1168+
return 0.5*(x - e) - 0.5;
1169+
}
1170+
if (k == 1) {
1171+
if (x < -0.25) {
1172+
return -2.0*(e - (x + 0.5));
1173+
} else {
1174+
return one + 2.0*(x - e);
1175+
}
1176+
}
1177+
if (k <= -2 || k > 56) { // suffice to return exp(x) - 1
1178+
y = one - (e - x);
1179+
y = __HI(y, __HI(y) + (k << 20)); // add k to y's exponent
1180+
return y - one;
1181+
}
1182+
t = one;
1183+
if (k < 20) {
1184+
t = __HI(t, 0x3ff0_0000 - (0x2_00000 >> k)); // t = 1-2^-k
1185+
y = t - ( e - x);
1186+
y = __HI(y, __HI(y) + (k << 20)); // add k to y's exponent
1187+
} else {
1188+
t = __HI(t, ((0x3ff - k) << 20)); // 2^-k
1189+
y = x - (e + t);
1190+
y += one;
1191+
y = __HI(y, __HI(y) + (k << 20)); // add k to y's exponent
1192+
}
1193+
}
1194+
return y;
1195+
}
1196+
}
9881197
}

src/java.base/share/classes/java/lang/StrictMath.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,9 @@ public static double hypot(double x, double y) {
20932093
* @return the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
20942094
* @since 1.5
20952095
*/
2096-
public static native double expm1(double x);
2096+
public static double expm1(double x) {
2097+
return FdLibm.Expm1.compute(x);
2098+
}
20972099

20982100
/**
20992101
* Returns the natural logarithm of the sum of the argument and 1.

test/jdk/java/lang/StrictMath/Expm1Tests.java

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,11 +23,17 @@
2323

2424
/*
2525
* @test
26-
* @bug 4851638
27-
* @summary Tests for StrictMath.expm1
28-
* @compile -Xdiags:verbose Expm1Tests.java
26+
* @bug 4851638 8301396
27+
* @key randomness
28+
* @library /test/lib
29+
* @build jdk.test.lib.RandomFactory
30+
* @build Tests
31+
* @build FdlibmTranslit
32+
* @build Expm1Tests
2933
* @run main Expm1Tests
34+
* @summary Tests for StrictMath.expm1
3035
*/
36+
import jdk.test.lib.RandomFactory;
3137

3238
/**
3339
* The tests in ../Math/Expm1Tests.java test properties that should
@@ -43,12 +49,95 @@
4349
public class Expm1Tests {
4450
private Expm1Tests(){}
4551

46-
static int testExpm1Case(double input, double expected) {
52+
public static void main(String... args) {
53+
int failures = 0;
54+
55+
failures += testAgainstTranslit();
56+
failures += testExpm1();
57+
58+
if (failures > 0) {
59+
System.err.println("Testing expm1 incurred "
60+
+ failures + " failures.");
61+
throw new RuntimeException();
62+
}
63+
}
64+
65+
// Initialize shared random number generator
66+
private static java.util.Random random = RandomFactory.getRandom();
67+
68+
/**
69+
* Test StrictMath.expm1 against transliteration port of expm1.
70+
*/
71+
private static int testAgainstTranslit() {
72+
int failures = 0;
73+
double x;
74+
75+
// Test just above subnormal threshold...
76+
x = Double.MIN_NORMAL;
77+
failures += testRange(x, Math.ulp(x), 1000);
78+
79+
// ... and just below subnormal threshold ...
80+
x = Math.nextDown(Double.MIN_NORMAL);
81+
failures += testRange(x, -Math.ulp(x), 1000);
82+
83+
// ... and near 1.0 ...
84+
failures += testRangeMidpoint(1.0, Math.ulp(x), 2000);
85+
// (Note: probes every-other value less than 1.0 due to
86+
// change in the size of an ulp at 1.0.
87+
88+
// Probe near decision points in the FDLIBM algorithm.
89+
double LN2 = StrictMath.log(2.0);
90+
double[] decisionPoints = {
91+
7.09782712893383973096e+02, // overflow threshold
92+
56.0 * LN2,
93+
-56.0 * LN2,
94+
0.5 * LN2,
95+
-0.5 * LN2,
96+
1.5 * LN2,
97+
-1.5 * LN2,
98+
0x1.0p-54,
99+
-0x1.0p-54,
100+
};
101+
102+
for (double testPoint : decisionPoints) {
103+
failures += testRangeMidpoint(testPoint, Math.ulp(testPoint), 1000);
104+
}
105+
106+
x = Tests.createRandomDouble(random);
107+
108+
// Make the increment twice the ulp value in case the random
109+
// value is near an exponent threshold. Don't worry about test
110+
// elements overflowing to infinity if the starting value is
111+
// near Double.MAX_VALUE.
112+
failures += testRange(x, 2.0 * Math.ulp(x), 1000);
113+
114+
return failures;
115+
}
116+
117+
private static int testRange(double start, double increment, int count) {
118+
int failures = 0;
119+
double x = start;
120+
for (int i = 0; i < count; i++, x += increment) {
121+
failures += testExpm1Case(x, FdlibmTranslit.expm1(x));
122+
}
123+
return failures;
124+
}
125+
126+
private static int testRangeMidpoint(double midpoint, double increment, int count) {
127+
int failures = 0;
128+
double x = midpoint - increment*(count / 2) ;
129+
for (int i = 0; i < count; i++, x += increment) {
130+
failures += testExpm1Case(x, FdlibmTranslit.expm1(x));
131+
}
132+
return failures;
133+
}
134+
135+
private static int testExpm1Case(double input, double expected) {
47136
return Tests.test("StrictMath.expm1(double)", input,
48137
StrictMath::expm1, expected);
49138
}
50139

51-
static int testExpm1() {
140+
private static int testExpm1() {
52141
int failures = 0;
53142

54143
// Test cases in the range [-36.75, 710]
@@ -781,16 +870,4 @@ static int testExpm1() {
781870

782871
return failures;
783872
}
784-
785-
public static void main(String [] argv) {
786-
int failures = 0;
787-
788-
failures += testExpm1();
789-
790-
if (failures > 0) {
791-
System.err.println("Testing expm1 incurred "
792-
+ failures + " failures.");
793-
throw new RuntimeException();
794-
}
795-
}
796873
}

0 commit comments

Comments
 (0)