Skip to content

JDK-8302028: Port fdlibm atan2 to Java #12608

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 7 commits into from

Conversation

jddarcy
Copy link
Member

@jddarcy jddarcy commented Feb 16, 2023

Working down the porting list, next stop, atan2.


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/12608/head:pull/12608
$ git checkout pull/12608

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12608

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 16, 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 openjdk bot added the rfr Pull request is ready for review label Feb 16, 2023
@openjdk
Copy link

openjdk bot commented Feb 16, 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 Feb 16, 2023
@jddarcy
Copy link
Member Author

jddarcy commented Feb 16, 2023

Diffs of the various ports, starting with the original C vs the transliteration port:

$ diff -w Atan2.c Atan2.translit.java 
1c1,4
< /* __ieee754_atan2(y,x)
---
>     /**
>      * Returns the angle theta from the conversion of rectangular
>      * coordinates (x, y) to polar coordinates (r, theta).
>      *
27,34c30,31
< 
< #include "fdlibm.h"
< 
< #ifdef __STDC__
< static const double
< #else
< static double
< #endif
---
>     static class Atan2 {
>         private static final double
42,48c39
< #ifdef __STDC__
<         double __ieee754_atan2(double y, double x)
< #else
<         double __ieee754_atan2(y,x)
<         double  y,x;
< #endif
< {
---
>         static double compute(double y, double x) {
51c42
<         unsigned lx,ly;
---
>             /*unsigned*/ int lx,ly;
100c91
<         else z=atan(fabs(y/x));         /* safe to do y/x */
---
>             else z=atan(Math.abs(y/x));         /* safe to do y/x */
103c94,96
<             case 1: __HI(z) ^= 0x80000000;
---
>             case 1: 
>                 // original:__HI(z) ^= 0x80000000;
>                 z = __HI(z, __HI(x) ^ 0x80000000);
109a103
>     }

And the transliteration vs the more idiomatic port:

$ diff -w Atan2.translit.java Atan2.fdlibm.java 
30a31,32
>         private Atan2() {throw new UnsupportedOperationException();}
> 
33,37c35,37
<             zero  = 0.0,
<             pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
<             pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
<             pi      = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
<             pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
---
>             pi_o_4  = 0x1.921fb54442d18p-1,  // 7.8539816339744827900E-01
>             pi_o_2  = 0x1.921fb54442d18p0,   // 1.5707963267948965580E+00
>             pi_lo   = 0x1.1a62633145c07p-53; // 1.2246467991473531772E-16
44c44,45
<             hx = __HI(x); ix = hx&0x7fffffff;
---
>             hx = __HI(x);
>             ix = hx & 0x7fff_ffff;
46c47,48
<             hy = __HI(y); iy = hy&0x7fffffff;
---
>             hy = __HI(y);
>             iy = hy&0x7fff_ffff;
48,49c50,51
<             if(((ix|((lx|-lx)>>31))>0x7ff00000)||
<                ((iy|((ly|-ly)>>31))>0x7ff00000))    /* x or y is NaN */
---
>             if (((ix | ((lx | -lx) >> 31)) > 0x7ff0_0000)||
>                 ((iy |((ly | - ly) >> 31)) > 0x7ff0_0000))    // x or y is NaN
51,52c53,55
<             if(((hx-0x3ff00000)|lx)==0) return atan(y);   /* x=1.0 */
<             m = ((hy>>31)&1)|((hx>>30)&2);  /* 2*sign(x)+sign(y) */
---
>             if (((hx - 0x3ff0_0000) | lx) == 0) // x = 1.0
>                 return StrictMath.atan(y);
>             m = ((hy >> 31) & 1)|((hx >> 30) & 2);  // 2*sign(x) + sign(y)
54c57
<             /* when y = 0 */
---
>             // when y = 0
56,73c59,67
<                 switch(m) {
<                 case 0:
<                 case 1: return y;       /* atan(+-0,+anything)=+-0 */
<                 case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
<                 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
<                 }
<             }
<             /* when x = 0 */
<             if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
< 
<             /* when x is INF */
<             if(ix==0x7ff00000) {
<                 if(iy==0x7ff00000) {
<                     switch(m) {
<                     case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
<                     case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
<                     case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
<                     case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
---
>                 return switch(m) {
>                 case 0, 1 ->  y;              // atan(+/-0, +anything)  = +/-0
>                 case 2    ->  Math.PI + tiny; // atan(+0,   -anything)  =  pi
>                 default   -> -Math.PI - tiny; // atan(-0,   -anything)  = -pi
>                 };
>             }
>             // when x = 0
>             if ((ix | lx) == 0) {
>                 return (hy < 0)?  -pi_o_2 - tiny : pi_o_2 + tiny;
74a69,78
> 
>             // when x is INF
>             if (ix == 0x7ff0_0000) {
>                 if (iy == 0x7ff0_0000) {
>                     return switch(m) {
>                     case 0  ->  pi_o_4 + tiny;      // atan(+INF, +INF)
>                     case 1  -> -pi_o_4 - tiny;      // atan(-INF, +INF)
>                     case 2  ->  3.0*pi_o_4 + tiny;  // atan(+INF, -INF)
>                     default -> -3.0*pi_o_4 - tiny;  // atan(-INF, -INF)
>                     };
76,80c80,85
<                     switch(m) {
<                     case 0: return  zero  ;     /* atan(+...,+INF) */
<                     case 1: return -1.0*zero  ; /* atan(-...,+INF) */
<                     case 2: return  pi+tiny  ;  /* atan(+...,-INF) */
<                     case 3: return -pi-tiny  ;  /* atan(-...,-INF) */
---
>                     return switch(m) {
>                     case 0  ->  0.0;                // atan(+..., +INF)
>                     case 1  -> -0.0;                // atan(-..., +INF)
>                     case 2  ->  Math.PI + tiny;     // atan(+..., -INF)
>                     default -> -Math.PI - tiny;     // atan(-..., -INF)
>                     };
82a88,90
>             // when y is INF
>             if (iy == 0x7ff0_0000) {
>                 return (hy < 0)? -pi_o_2 - tiny : pi_o_2 + tiny;
84,85d91
<             /* when y is INF */
<             if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
87c93
<             /* compute y/x */
---
>             // compute y/x
89,101c95,107
<             if(k > 60) z=pi_o_2+0.5*pi_lo;  /* |y/x| >  2**60 */
<             else if(hx<0&&k<-60) z=0.0;     /* |y|/x < -2**60 */
<             else z=atan(Math.abs(y/x));         /* safe to do y/x */
<             switch (m) {
<             case 0: return       z  ;   /* atan(+,+) */
<             case 1: 
<                 // original:__HI(z) ^= 0x80000000;
<                 z = __HI(z, __HI(x) ^ 0x80000000);
<                 return       z  ;   /* atan(-,+) */
<             case 2: return  pi-(z-pi_lo);/* atan(+,-) */
<             default: /* case 3 */
<                 return  (z-pi_lo)-pi;/* atan(-,-) */
<             }
---
>             if (k > 60) {   // |y/x| >  2**60
>                 z = pi_o_2+0.5*pi_lo;
>             } else if (hx < 0 && k < -60) { // |y|/x < -2**60
>                 z = 0.0;
>             } else { // safe to do y/x
>                 z = StrictMath.atan(Math.abs(y/x));
>             }
>             return switch (m) {
>             case 0  ->  z;                    // atan(+, +)
>             case 1  -> -z;                    // atan(-, +)
>             case 2  -> Math.PI - (z - pi_lo); // atan(+, -)
>             default -> (z - pi_lo) - Math.PI; // atan(-, -), case 3
>             };

A few notes: I decided to use expression switches, but that is not necessary. Also, in the final switch I used "-z" as a more direct idiom to negate z than the form in the original fdlibm.

@mlbridge
Copy link

mlbridge bot commented Feb 16, 2023

Webrevs

@bplb
Copy link
Member

bplb commented Feb 16, 2023

The port looks reasonable to me.

hy = __HI(y); iy = hy&0x7fffffff;
ly = __LO(y);
if(((ix|((lx|-lx)>>31))>0x7ff00000)||
((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that >>31 must be replaced by >>>31.

iy = hy&0x7fff_ffff;
ly = __LO(y);
if (((ix | ((lx | -lx) >> 31)) > 0x7ff0_0000)||
((iy |((ly | - ly) >> 31)) > 0x7ff0_0000)) // x or y is NaN
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that >> 31 must be replaced by >>> 31.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well spotted; corrected the code and added additional tests to catch that case. Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well spotted; corrected the code and added additional tests to catch that case. Thanks.

PS From some quick manual inspections, the other recent ports of FDLIBM methods don't look vulnerable to this kind of problem. However, as follow-up work later in 21 I'll implement: JDK-8302800: Augment NaN handling tests of FDLIBM methods.

return switch(m) {
case 0, 1 -> y; // atan(+/-0, +anything) = +/-0
case 2 -> Math.PI + tiny; // atan(+0, -anything) = pi
default -> -Math.PI - tiny; // atan(-0, -anything) = -pi
Copy link
Contributor

Choose a reason for hiding this comment

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

The original switch statement and this switch expression are semantically equivalent only because of our knowledge that m can only assume values 0, 1, 2, or 3. This requires more reasoning than a more verbatim copy of the original statement. Not sure if it is worth.

The same holds for the switch expressions below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the use of switch expressions here is certainly not strictly necessary.

Something that helped convince me it was okay was that the final switch on m in atan2 uses the set of case labels {0, 1, 2, default}., further implying that m can only take on the values {0, 1, 2, 3}.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted to switch statements rather than switch expressions.

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.

Looks good.

@openjdk
Copy link

openjdk bot commented Feb 22, 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:

8302028: Port fdlibm atan2 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 33 new commits pushed to the master branch:

  • 07e976a: 8303081: Serial: Remove unused VM_MarkSweep
  • 8de841d: 8302667: Improve message format when failing to load symbols or libraries
  • f893d23: 8303024: (fs) WindowsFileSystem.supportedFileAttributeViews can use Set.of
  • d7ada66: 8303084: G1 Heap region liveness verification has inverted return value
  • 5d7e7e2: 8302760: Improve liveness/remembered set verification for G1
  • b0e0f37: 8303067: G1: Remove unimplemented G1FullGCScope::heap_transition
  • 1a62a12: 8302880: Fix includes in g1ConcurrentMarkObjArrayProcessor files
  • ee37af4: 8302975: Remove redundant mark verification during G1 Full GC
  • 83bea26: 8300575: JVMTI support when using alternative virtual thread implementation
  • 25bfed3: 8302979: (fs) Files usage of SUPPORTED_CHARSETS could be simplified
  • ... and 23 more: https://git.openjdk.org/jdk/compare/dfce4e1943f2f95b74b5a9cdde9d738dcffd0b43...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 22, 2023
@jddarcy
Copy link
Member Author

jddarcy commented Feb 22, 2023

/integrate

1 similar comment
@jddarcy
Copy link
Member Author

jddarcy commented Feb 22, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Feb 22, 2023

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

  • 07e976a: 8303081: Serial: Remove unused VM_MarkSweep
  • 8de841d: 8302667: Improve message format when failing to load symbols or libraries
  • f893d23: 8303024: (fs) WindowsFileSystem.supportedFileAttributeViews can use Set.of
  • d7ada66: 8303084: G1 Heap region liveness verification has inverted return value
  • 5d7e7e2: 8302760: Improve liveness/remembered set verification for G1
  • b0e0f37: 8303067: G1: Remove unimplemented G1FullGCScope::heap_transition
  • 1a62a12: 8302880: Fix includes in g1ConcurrentMarkObjArrayProcessor files
  • ee37af4: 8302975: Remove redundant mark verification during G1 Full GC
  • 83bea26: 8300575: JVMTI support when using alternative virtual thread implementation
  • 25bfed3: 8302979: (fs) Files usage of SUPPORTED_CHARSETS could be simplified
  • ... and 23 more: https://git.openjdk.org/jdk/compare/dfce4e1943f2f95b74b5a9cdde9d738dcffd0b43...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Feb 22, 2023

@jddarcy Pushed as commit fcaf871.

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

@openjdk
Copy link

openjdk bot commented Feb 22, 2023

@jddarcy The command integrate can only be used in open pull requests.

@jddarcy jddarcy deleted the JDK-8302028 branch October 26, 2024 19:26
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.

3 participants