Skip to content

Commit 9593730

Browse files
committed
8362376: Use @stable annotation in Java FDLIBM implementation
Reviewed-by: liach, rgiulietti
1 parent 8cd7975 commit 9593730

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, 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
@@ -25,6 +25,8 @@
2525

2626
package java.lang;
2727

28+
import jdk.internal.vm.annotation.Stable;
29+
2830
/**
2931
* Port of the "Freely Distributable Math Library", version 5.3, from
3032
* C to Java.
@@ -451,8 +453,10 @@ static double compute(double x) {
451453
*/
452454
private static final double
453455
pio4 = 0x1.921fb54442d18p-1, // 7.85398163397448278999e-01
454-
pio4lo= 0x1.1a62633145c07p-55, // 3.06161699786838301793e-17
455-
T[] = {
456+
pio4lo= 0x1.1a62633145c07p-55; // 3.06161699786838301793e-17
457+
@Stable
458+
private static final double[]
459+
T = {
456460
0x1.5555555555563p-2, // 3.33333333333334091986e-01
457461
0x1.111111110fe7ap-3, // 1.33333333333201242699e-01
458462
0x1.ba1ba1bb341fep-5, // 5.39682539762260521377e-02
@@ -546,6 +550,7 @@ static final class RemPio2 {
546550
/*
547551
* Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
548552
*/
553+
@Stable
549554
private static final int[] two_over_pi = {
550555
0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
551556
0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A,
@@ -560,6 +565,7 @@ static final class RemPio2 {
560565
0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B,
561566
};
562567

568+
@Stable
563569
private static final int[] npio2_hw = {
564570
0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C,
565571
0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C,
@@ -807,8 +813,10 @@ static final class KernelRemPio2 {
807813
* to produce the hexadecimal values shown.
808814
*/
809815

816+
@Stable
810817
private static final int init_jk[] = {2, 3, 4, 6}; // initial value for jk
811818

819+
@Stable
812820
private static final double PIo2[] = {
813821
0x1.921fb4p0, // 1.57079625129699707031e+00
814822
0x1.4442dp-24, // 7.54978941586159635335e-08
@@ -1232,20 +1240,23 @@ static double compute(double x) {
12321240
static final class Atan {
12331241
private Atan() {throw new UnsupportedOperationException();}
12341242

1243+
@Stable
12351244
private static final double atanhi[] = {
12361245
0x1.dac670561bb4fp-2, // atan(0.5)hi 4.63647609000806093515e-01
12371246
0x1.921fb54442d18p-1, // atan(1.0)hi 7.85398163397448278999e-01
12381247
0x1.f730bd281f69bp-1, // atan(1.5)hi 9.82793723247329054082e-01
12391248
0x1.921fb54442d18p0, // atan(inf)hi 1.57079632679489655800e+00
12401249
};
12411250

1251+
@Stable
12421252
private static final double atanlo[] = {
12431253
0x1.a2b7f222f65e2p-56, // atan(0.5)lo 2.26987774529616870924e-17
12441254
0x1.1a62633145c07p-55, // atan(1.0)lo 3.06161699786838301793e-17
12451255
0x1.007887af0cbbdp-56, // atan(1.5)lo 1.39033110312309984516e-17
12461256
0x1.1a62633145c07p-54, // atan(inf)lo 6.12323399573676603587e-17
12471257
};
12481258

1259+
@Stable
12491260
private static final double aT[] = {
12501261
0x1.555555555550dp-2, // 3.33333333333329318027e-01
12511262
-0x1.999999998ebc4p-3, // -1.99999999998764832476e-01
@@ -2245,12 +2256,8 @@ else if (j < 0xBB67A)
22452256

22462257
// Compute ss = s_h + s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5)
22472258

2248-
final double BP[] = {1.0,
2249-
1.5};
2250-
final double DP_H[] = {0.0,
2251-
0x1.2b80_34p-1}; // 5.84962487220764160156e-01
2252-
final double DP_L[] = {0.0,
2253-
0x1.cfde_b43c_fd006p-27};// 1.35003920212974897128e-08
2259+
final double DP_H = 0x1.2b80_34p-1; // 5.84962487220764160156e-01
2260+
final double DP_L = 0x1.cfde_b43c_fd006p-27; // 1.35003920212974897128e-08
22542261

22552262
// Poly coefs for (3/2)*(log(x)-2s-2/3*s**3
22562263
final double L1 = 0x1.3333_3333_33303p-1; // 5.99999999999994648725e-01
@@ -2259,15 +2266,17 @@ else if (j < 0xBB67A)
22592266
final double L4 = 0x1.1746_0a91_d4101p-2; // 2.72728123808534006489e-01
22602267
final double L5 = 0x1.d864_a93c_9db65p-3; // 2.30660745775561754067e-01
22612268
final double L6 = 0x1.a7e2_84a4_54eefp-3; // 2.06975017800338417784e-01
2262-
u = x_abs - BP[k]; // BP[0]=1.0, BP[1]=1.5
2263-
v = 1.0 / (x_abs + BP[k]);
2269+
2270+
double BP_k = 1.0 + 0.5*k; // BP[0]=1.0, BP[1]=1.5
2271+
u = x_abs - BP_k;
2272+
v = 1.0 / (x_abs + BP_k);
22642273
ss = u * v;
22652274
s_h = ss;
22662275
s_h = __LO(s_h, 0);
22672276
// t_h=x_abs + BP[k] High
22682277
t_h = 0.0;
22692278
t_h = __HI(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18) );
2270-
t_l = x_abs - (t_h - BP[k]);
2279+
t_l = x_abs - (t_h - BP_k);
22712280
s_l = v * ((u - s_h * t_h) - s_h * t_l);
22722281
// Compute log(x_abs)
22732282
s2 = ss * ss;
@@ -2285,12 +2294,12 @@ else if (j < 0xBB67A)
22852294
p_h = __LO(p_h, 0);
22862295
p_l = v - (p_h - u);
22872296
z_h = CP_H * p_h; // CP_H + CP_L = 2/(3*log2)
2288-
z_l = CP_L * p_h + p_l * CP + DP_L[k];
2297+
z_l = CP_L * p_h + p_l * CP + DP_L*k;
22892298
// log2(x_abs) = (ss + ..)*2/(3*log2) = n + DP_H + z_h + z_l
22902299
t = (double)n;
2291-
t1 = (((z_h + z_l) + DP_H[k]) + t);
2300+
t1 = (((z_h + z_l) + DP_H*k) + t);
22922301
t1 = __LO(t1, 0);
2293-
t2 = z_l - (((t1 - t) - DP_H[k]) - z_h);
2302+
t2 = z_l - (((t1 - t) - DP_H*k) - z_h);
22942303
}
22952304

22962305
// Split up y into (y1 + y2) and compute (y1 + y2) * (t1 + t2)
@@ -2430,13 +2439,13 @@ else if (j < 0xBB67A)
24302439
static final class Exp {
24312440
private Exp() {throw new UnsupportedOperationException();}
24322441

2433-
private static final double[] half = {0.5, -0.5,};
24342442
private static final double huge = 1.0e+300;
24352443
private static final double twom1000= 0x1.0p-1000; // 9.33263618503218878990e-302 = 2^-1000
24362444
private static final double o_threshold= 0x1.62e42fefa39efp9; // 7.09782712893383973096e+02
24372445
private static final double u_threshold= -0x1.74910d52d3051p9; // -7.45133219101941108420e+02;
2438-
private static final double[] ln2HI ={ 0x1.62e42feep-1, // 6.93147180369123816490e-01
2439-
-0x1.62e42feep-1}; // -6.93147180369123816490e-01
2446+
private static final double ln2HI = 0x1.62e42feep-1; // 6.93147180369123816490e-01
2447+
2448+
@Stable
24402449
private static final double[] ln2LO ={ 0x1.a39ef35793c76p-33, // 1.90821492927058770002e-10
24412450
-0x1.a39ef35793c76p-33}; // -1.90821492927058770002e-10
24422451
private static final double invln2 = 0x1.71547652b82fep0; // 1.44269504088896338700e+00
@@ -2478,13 +2487,13 @@ public static double compute(double x) {
24782487
/* argument reduction */
24792488
if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
24802489
if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
2481-
hi = x - ln2HI[xsb];
2490+
hi = x - ln2HI*(1 - 2*xsb); /* +/- ln2HI */
24822491
lo=ln2LO[xsb];
24832492
k = 1 - xsb - xsb;
24842493
} else {
2485-
k = (int)(invln2 * x + half[xsb]);
2494+
k = (int)(invln2 * x + 0.5 * (1 - 2*xsb) /* +/- 0.5 */ );
24862495
t = k;
2487-
hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
2496+
hi = x - t*ln2HI; /* t*ln2HI is exact here */
24882497
lo = t*ln2LO[0];
24892498
}
24902499
x = hi - lo;

test/jdk/java/lang/StrictMath/ExpTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, 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,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8139688
26+
* @bug 8139688 8362376
2727
* @key randomness
2828
* @library /test/lib
2929
* @build jdk.test.lib.RandomFactory
@@ -87,6 +87,10 @@ static int testExp() {
8787
{-0x1.49f33ad2c1c58p+9, 0x1.f3ccc815431b6p-953},
8888
{+0x1.fce66609f7428p+5, 0x1.b59724cb0bc4cp91},
8989
{-0x1.49f33ad2c1c58p+9, 0x1.f3ccc815431b6p-953},
90+
91+
// Test values near 0.5*ln*(2) and 1.5*ln(2) to check refactoring
92+
{0.34657359027997275, 1.4142135623730951},
93+
{1.0397207708399183, 2.828427124746191},
9094
};
9195

9296
for(double[] testCase: testCases)

test/jdk/java/lang/StrictMath/PowTests.java

Lines changed: 17 additions & 2 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, 2025, 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,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8136874
26+
* @bug 8136874 8362376
2727
* @summary Tests for StrictMath.pow
2828
*/
2929

@@ -283,6 +283,21 @@ private static int testPow() {
283283
0x1.ffffffffffd9fp1023, // 1.7976931348621944E308
284284
},
285285

286+
// Check refactoring, abs(y) < 2^31, x < sqrt(3/2), x < sqrt(3)
287+
{1.2, // x < sqrt(3/2)
288+
5.0,
289+
2.4883199999999994
290+
},
291+
292+
{1.4142135623730951, // sqrt(3/2) < x < sqrt(3)
293+
5.0,
294+
5.656854249492382
295+
},
296+
297+
{2.23606797749979, // x > sqrt(3)
298+
5.0,
299+
55.901699437494756
300+
},
286301
};
287302

288303
for (double[] testCase: testCases)

0 commit comments

Comments
 (0)