RFR: 8348638: Performance regression in Math.tanh [v5]

Jatin Bhateja jbhateja at openjdk.org
Thu Apr 17 15:51:48 UTC 2025


On Thu, 17 Apr 2025 13:48:38 GMT, Jatin Bhateja <jbhateja at openjdk.org> wrote:

>> test/micro/org/openjdk/bench/java/lang/MathBench.java line 73:
>> 
>>> 71: 
>>> 72:     @Param("2.7755575615628914E-17")
>>> 73:     public double tanhBound2;
>> 
>> We can declare tanBoundIndex as a Parameter and then select from [hard-coded value ranges](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/FdLibm.java#L3258), which will allow us to execute all the special ranges and NaN value.
>> double tanhRangeArray [][] = { 0.0 , 0x1.0P-56}, {0x1.0P-56, 1.0}, {1.0, 22.0}, {22.0,  Double.POSITIVE_INFINITY}}
>> double tanhRangeLowerBound = tanhRangeArray[tanhRangeIndex][0];
>> double tanhRangeLowerBound = tanhRangeArray[tanhRangeIndex][1];
>
> I see in a standalone micro that NaN / Inf case performs better without intrinsics
> 
> NaN:-
> GNR>java -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_dtanh -cp . test
> [time] 174 ms  [res]
> GNR>java -XX:+UnlockDiagnosticVMOptions -cp . test
> [time] 278 ms  [res]
> 
> Inf:-
> GNR>java -XX:+UnlockDiagnosticVMOptions  -cp . test
> [time] 410 ms  [res]
> GNR>java -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_dtanh -cp . test
> [time] 165 ms  [res]

It looks like after |x| > 22.0 we always check for NaN/Inf value and then take a different control path, if we flip this and check for NaN values upfront we can avoid going through newly added instruction sequence for |X| > 22.0

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

PR Review Comment: https://git.openjdk.org/jdk/pull/23889#discussion_r2049003055


More information about the hotspot-compiler-dev mailing list