RFR: 8266028: C2 computes -0.0 for Math.pow(-0.0, 0.5)

Vladimir Kozlov kvn at openjdk.java.net
Tue Apr 27 16:48:38 UTC 2021


On Tue, 27 Apr 2021 08:29:04 GMT, Jie Fu <jiefu at openjdk.org> wrote:

> Hi all,
> 
> C2 computes -0.0 for Math.pow(-0.0, 0.5) which should be 0.0 according to the API specs.
> This bug was found while I was implementing the same optimization for x86_32 (JDK-8265940).
> 
> The reason is that CmpDNode [1] doesn't distinguish -0.0 from 0.0.
> Math.pow(-0.0, 0.5) was replaced with Math.sqrt(-0.0) incorrectly since CmpDNode says -0.0 < 0.0 if false.
> 
> The fix excludes C2's optimization for x=0.0/-0.0. 
> And the jtreg test has been improved to make sure the corner cases are covered by the compilers.
> 
> Thanks.
> Best regards,
> Jie
> 
> [1] https://github.com/openjdk/jdk/blob/master/src/hotspot/share/opto/library_call.cpp#L1656

I am fine with your fix - it is rare case so we can skip 0 case. But we need to verify that Interpreter and stubs (and Java) give the same result for -0.0 in 32- and 64-bit VMs.

C code uses `if(hx>=0)` so what result will it give for `-0.0`? :
 https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/sharedRuntimeTrans.cpp#L497
 
 64-bit stub code use Integer compare which should be fine I think:
 https://github.com/openjdk/jdk/blob/master/src/hotspot/cpu/x86/macroAssembler_x86_pow.cpp#L839
 
 Java code adds `+0.0` to get correct result (would be nice to see what code C2 generates when it see such bytecode):
 https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/FdLibm.java#L364

-------------

PR: https://git.openjdk.java.net/jdk/pull/3712


More information about the hotspot-compiler-dev mailing list