Skip to content

JDK-8301392: Port fdlibm log1p to Java #12301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from

Conversation

jddarcy
Copy link
Member

@jddarcy jddarcy commented Jan 31, 2023

Another day, another PR to port FDLBIM to Java, this time for the log1p method.

Other than using the two-argument form of the __HI method in Java transliteration version rather than C macro, there are no appreciable differences between the original C source in

src/java.base/share/native/libfdlibm/s_log1p.c

and the transliteration for testing purposes in

test/jdk/java/lang/StrictMath/FdlibmTranslit.java

The more idiomatic port in

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

has had a series of transformation applied layering on the transliteration. The intermediate commits show the progress.

The regression tests include probing around input values the implementation uses to decided which branch to take.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/12301/head:pull/12301
$ git checkout pull/12301

Update a local copy of the PR:
$ git checkout pull/12301
$ git pull https://git.openjdk.org/jdk pull/12301/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12301

View PR using the GUI difftool:
$ git pr show -t 12301

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/12301.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 31, 2023

👋 Welcome back darcy! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jan 31, 2023

@jddarcy The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Jan 31, 2023
@jddarcy jddarcy changed the title Jdk 8301392: Port fdlibm log1p to Java JDK-8301392: Port fdlibm log1p to Java Jan 31, 2023
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 31, 2023
@mlbridge
Copy link

mlbridge bot commented Jan 31, 2023

Webrevs

@@ -779,7 +780,6 @@ public static double compute(double x) {
* shown.
*/
static class Log10 {
private static double two54 = 0x1.0p54; // 1.80143985094819840000e+16;
private static double ivln10 = 0x1.bcb7b1526e50ep-2; // 4.34294481903251816668e-01

private static double log10_2hi = 0x1.34413509f6p-2; // 3.01029995663611771306e-01;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are this constants intentionally not final ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just an oversight; I'll correct that. Thanks.

+ failures + " failures.");
throw new RuntimeException();
// ... and just below subnormal threshold ...
x = Math.nextDown(Double.MIN_NORMAL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x = Math.nextDown(Double.MIN_NORMAL);
x = Math.nextDown(Double.MIN_NORMAL);

@rgiulietti
Copy link
Contributor

For consistency with the code for log10, the hex int literals could be separated with _ for better readability, as in 0x3ff0_0000.

Otherwise LGTM

@openjdk openjdk bot removed the rfr Pull request is ready for review label Feb 1, 2023
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 1, 2023
@jddarcy
Copy link
Member Author

jddarcy commented Feb 1, 2023

PS To provide more context for the review, below are the "diff -w" output for the original C fdlibm with the transliteration port and the transliteration port with the more idiomatic port.

$ diff -w Log1p.c Log1p.translit.java 
1c1,2
< /* double log1p(double x)
---
>     /**
>      * Returns the natural logarithm of the sum of the argument and 1.
64a66,77
>     static class Log1p {
>         private static double ln2_hi  =  6.93147180369123816490e-01;  /* 3fe62e42 fee00000 */
>         private static double ln2_lo  =  1.90821492927058770002e-10;  /* 3dea39ef 35793c76 */
>         private static double two54   =  1.80143985094819840000e+16;  /* 43500000 00000000 */
>         private static double Lp1 = 6.666666666666735130e-01;  /* 3FE55555 55555593 */
>         private static double Lp2 = 3.999999999940941908e-01;  /* 3FD99999 9997FA04 */
>         private static double Lp3 = 2.857142874366239149e-01;  /* 3FD24924 94229359 */
>         private static double Lp4 = 2.222219843214978396e-01;  /* 3FCC71C5 1D8E78AF */
>         private static double Lp5 = 1.818357216161805012e-01;  /* 3FC74664 96CB03DE */
>         private static double Lp6 = 1.531383769920937332e-01;  /* 3FC39A09 D078C69F */
>         private static double Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
>         private static double zero = 0.0;
66,92c79
< #include "fdlibm.h"
< 
< #ifdef __STDC__
< static const double
< #else
< static double
< #endif
< ln2_hi  =  6.93147180369123816490e-01,  /* 3fe62e42 fee00000 */
< ln2_lo  =  1.90821492927058770002e-10,  /* 3dea39ef 35793c76 */
< two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
< Lp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
< Lp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
< Lp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
< Lp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
< Lp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
< Lp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
< Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
< 
< static double zero = 0.0;
< 
< #ifdef __STDC__
<         double log1p(double x)
< #else
<         double log1p(x)
<         double x;
< #endif
< {
---
>         public static double compute(double x) {
137c124
<                 __HI(u) = hu|0x3ff00000;        /* normalize u */
---
>                     u = __HI(u, hu|0x3ff00000);        /* normalize u */
140c127
<                 __HI(u) = hu|0x3fe00000;        /* normalize u/2 */
---
>                     u = __HI(u, hu|0x3fe00000);        /* normalize u/2 */
158a146
>     }

The constants are reformatted as expected and the __HI() C macro used on the left-hand side of an assignment is replaced by a Java method call.

And port-vs-port:

$ diff -w Log1p.translit.java Log1p.fdlibm.java 
67,77c67,75
<         private static double ln2_hi  =  6.93147180369123816490e-01;  /* 3fe62e42 fee00000 */
<         private static double ln2_lo  =  1.90821492927058770002e-10;  /* 3dea39ef 35793c76 */
<         private static double two54   =  1.80143985094819840000e+16;  /* 43500000 00000000 */
<         private static double Lp1 = 6.666666666666735130e-01;  /* 3FE55555 55555593 */
<         private static double Lp2 = 3.999999999940941908e-01;  /* 3FD99999 9997FA04 */
<         private static double Lp3 = 2.857142874366239149e-01;  /* 3FD24924 94229359 */
<         private static double Lp4 = 2.222219843214978396e-01;  /* 3FCC71C5 1D8E78AF */
<         private static double Lp5 = 1.818357216161805012e-01;  /* 3FC74664 96CB03DE */
<         private static double Lp6 = 1.531383769920937332e-01;  /* 3FC39A09 D078C69F */
<         private static double Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
<         private static double zero = 0.0;
---
>         private static final double ln2_hi = 0x1.62e42feep-1;       // 6.93147180369123816490e-01
>         private static final double ln2_lo = 0x1.a39ef35793c76p-33; // 1.90821492927058770002e-10
>         private static final double Lp1    = 0x1.5555555555593p-1;  // 6.666666666666735130e-01
>         private static final double Lp2    = 0x1.999999997fa04p-2;  // 3.999999999940941908e-01
>         private static final double Lp3    = 0x1.2492494229359p-2;  // 2.857142874366239149e-01
>         private static final double Lp4    = 0x1.c71c51d8e78afp-3;  // 2.222219843214978396e-01
>         private static final double Lp5    = 0x1.7466496cb03dep-3;  // 1.818357216161805012e-01
>         private static final double Lp6    = 0x1.39a09d078c69fp-3;  // 1.531383769920937332e-01
>         private static final double Lp7    = 0x1.2f112df3e5244p-3;  // 1.479819860511658591e-01
84c82
<             ax = hx&0x7fffffff;
---
>             ax = hx & 0x7fff_ffff;
87,94c85,88
<             if (hx < 0x3FDA827A) {                  /* x < 0.41422  */
<                 if(ax>=0x3ff00000) {                /* x <= -1.0 */
<                     /*
<                      * Added redundant test against hx to work around VC++
<                      * code generation problem.
<                      */
<                     if(x==-1.0 && (hx==0xbff00000)) /* log1p(-1)=-inf */
<                         return -two54/zero;
---
>             if (hx < 0x3FDA_827A) {                  /* x < 0.41422  */
>                 if (ax >= 0x3ff0_0000) {             /* x <= -1.0 */
>                     if (x == -1.0) /* log1p(-1)=-inf */
>                         return -INFINITY;
96c90
<                         return (x-x)/(x-x);           /* log1p(x<-1)=NaN */
---
>                         return Double.NaN;           /* log1p(x < -1) = NaN */
98,100c92,95
<                 if(ax<0x3e200000) {                 /* |x| < 2**-29 */
<                     if(two54+x>zero                 /* raise inexact */
<                        &&ax<0x3c900000)            /* |x| < 2**-54 */
---
> 
>                 if (ax < 0x3e20_0000) {                /* |x| < 2**-29 */
>                     if (TWO54 + x > 0.0                /* raise inexact */
>                        && ax < 0x3c90_0000)            /* |x| < 2**-54 */
105,106c100,105
<                 if(hx>0||hx<=((int)0xbfd2bec3)) {
<                     k=0;f=x;hu=1;}  /* -0.2929<x<0.41422 */
---
> 
>                 if (hx > 0 || hx <= 0xbfd2_bec3) { /* -0.2929 < x < 0.41422 */
>                     k=0;
>                     f=x;
>                     hu=1;
>                 }
108c107,111
<             if (hx >= 0x7ff00000) return x+x;
---
> 
>             if (hx >= 0x7ff0_0000) {
>                 return x+x;
>             }
> 
110c113
<                 if(hx<0x43400000) {
---
>                 if (hx < 0x4340_0000) {
122,124c125,127
<                 hu &= 0x000fffff;
<                 if(hu<0x6a09e) {
<                     u = __HI(u, hu|0x3ff00000);        /* normalize u */
---
>                 hu &= 0x000f_ffff;
>                 if (hu < 0x6_a09e) {
>                     u = __HI(u, hu | 0x3ff0_0000);       /* normalize u */
127,128c130,131
<                     u = __HI(u, hu|0x3fe00000);        /* normalize u/2 */
<                     hu = (0x00100000-hu)>>2;
---
>                     u = __HI(u, hu | 0x3fe0_0000);       /* normalize u/2 */
>                     hu = (0x0010_0000 - hu) >> 2;
131a135
> 
134,135c138,145
<                 if(f==zero) { if(k==0) return zero;
<                     else {c += k*ln2_lo; return k*ln2_hi+c;}}
---
>                 if (f == 0.0) {
>                     if (k == 0) {
>                         return 0.0;
>                     } else {
>                         c += k * ln2_lo;
>                         return k * ln2_hi + c;
>                     }
>                 }
137c147,149
<                 if(k==0) return f-R; else
---
>                 if (k == 0) {
>                     return f - R;
>                 } else {
139a152
>             }
143c156,158
<             if(k==0) return f-(hfsq-s*(hfsq+R)); else
---
>             if (k == 0) {
>                 return f-(hfsq-s*(hfsq+R));
>             } else {
144a160
>             }


-0x1.0p-54,
0x1.0p-54,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment like
/* approximations around sqrt(2)/2 - 1 and around sqrt(2) - 1 */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I mean, for the doubles starting at L.246)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment like /* approximations around sqrt(2)/2 - 1 and around sqrt(2) - 1 */

Good clarification.

Copy link
Member

@bplb bplb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problems apparent to me: Reviewed.

@openjdk
Copy link

openjdk bot commented Feb 2, 2023

@jddarcy This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8301392: Port fdlibm log1p to Java

Reviewed-by: bpb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 70 new commits pushed to the master branch:

  • f696785: 8300869: Make use of the Double.toString(double) algorithm in java.util.Formatter
  • cf6b9eb: 8301637: ThreadLocalRandom.current().doubles().parallel() contention
  • c647ae6: 8301149: Parallel: Refactor MutableNUMASpace::update_layout
  • de57733: 8301644: com/sun/jdi/JdbStopThreadTest.java fails after JDK-8300811
  • 930ec00: 8301636: Minor cleanup in CommentHelper and DocPretty
  • 725d57b: 8301659: Resolve initialization reordering issues on Windows for libawt and libsaproc
  • 2d50c7d: 8298979: Remove duplicated serviceability/jvmti/thread/GetAllThreads/allthr01/allthr01.java
  • 59b7fb1: 8300273: [IR framework] Handle message instead of bailing out
  • 5b1584b: 8298880: VectorLogicalOpIdentityTest.java IR test incorrectly use avx3 instead of avx512
  • 21c1afb: 8301612: OopLoadProxy constructor should be explicit
  • ... and 60 more: https://git.openjdk.org/jdk/compare/63bb2ce8debdeb7f34bda9a3845fe93cb4d741dd...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Feb 2, 2023
@jddarcy
Copy link
Member Author

jddarcy commented Feb 2, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Feb 2, 2023

Going to push as commit ee0f5b5.
Since your change was applied there have been 70 commits pushed to the master branch:

  • f696785: 8300869: Make use of the Double.toString(double) algorithm in java.util.Formatter
  • cf6b9eb: 8301637: ThreadLocalRandom.current().doubles().parallel() contention
  • c647ae6: 8301149: Parallel: Refactor MutableNUMASpace::update_layout
  • de57733: 8301644: com/sun/jdi/JdbStopThreadTest.java fails after JDK-8300811
  • 930ec00: 8301636: Minor cleanup in CommentHelper and DocPretty
  • 725d57b: 8301659: Resolve initialization reordering issues on Windows for libawt and libsaproc
  • 2d50c7d: 8298979: Remove duplicated serviceability/jvmti/thread/GetAllThreads/allthr01/allthr01.java
  • 59b7fb1: 8300273: [IR framework] Handle message instead of bailing out
  • 5b1584b: 8298880: VectorLogicalOpIdentityTest.java IR test incorrectly use avx3 instead of avx512
  • 21c1afb: 8301612: OopLoadProxy constructor should be explicit
  • ... and 60 more: https://git.openjdk.org/jdk/compare/63bb2ce8debdeb7f34bda9a3845fe93cb4d741dd...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Feb 2, 2023
@openjdk openjdk bot closed this Feb 2, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Feb 2, 2023
@openjdk
Copy link

openjdk bot commented Feb 2, 2023

@jddarcy Pushed as commit ee0f5b5.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@jddarcy jddarcy deleted the JDK-8301392 branch February 2, 2023 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants