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

Sandhya Viswanathan sviswanathan at openjdk.org
Fri Mar 28 00:18:41 UTC 2025


On Fri, 28 Mar 2025 00:16:02 GMT, Mohamed Issa <duke at openjdk.org> wrote:

>> The changes described below are meant to resolve the performance regression introduced by the **x86_64 tanh** double precision floating point scalar intrinsic in #20657.
>> 
>> 1. Check and handle high magnitude input values before those in other ranges. If found, **+/- 1** is returned almost immediately without having to go through too many computations or branches.
>> 2. Reduce the lower bound of the input range that triggers a quick **+/- 1** return from **|x| >= 32** to **|x| >= 20**. This new endpoint is the closest value above the minimum (**55 * ln(2) / 2**) required for correctness that's possible when only retrieving the topmost word of the input register.
>> 
>> The results of all tests posted below were captured with an [Intel® Xeon 6761P](https://www.intel.com/content/www/us/en/products/sku/241842/intel-xeon-6761p-processor-336m-cache-2-50-ghz/specifications.html) using [OpenJDK v24-b33](https://github.com/openjdk/jdk/releases/tag/jdk-24%2B33) as the baseline version.
>> 
>> For performance data collected with the regression micro-benchmark referenced in the bug report, see the table below.  Each result is the mean of 3 individual runs. In the high value scenarios (100, 1000, 10000, 100000), the changes significantly improve execution times to the point where are almost at parity with the baseline. Also, there is almost no impact to the low value (1, 2) scenarios. 
>> 
>> | Input range (+/-) | Baseline (ms) | No fix (ms) | With fix (ms) | No fix vs baseline (%) | Fix vs baseline (%) |
>> | :------------------: | :-------------: | :-----------: | :-------------: | :----------------------: | :-------------------: |
>> | 1                          | 1842              | 1961           | 1969              | +6.46                          | +6.89                     |
>> | 2                          | 2102              | 2010           | 1998              | -4.38                           | -4.95                      |
>> | 100                      | 801                | 1018           | 716                | +27.09                        | -10.61                    |
>> | 1000                    | 498                | 803             | 519                | +61.24                        | +4.22                     |
>> | 10000                  | 474                | 755             | 491                | +59.28                        | +3.59                     |
>> | 100000                | 473                | 758             | 491                | +60.25        ...
>
> Mohamed Issa has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Change tanh intrinsic endpoint comparison value to match reference OpenJDK implementation

src/hotspot/cpu/x86/stubGenerator_x86_64_tanh.cpp line 332:

> 330:   __ andl(rcx, 32767);
> 331:   __ cmpl(rcx, 16436);
> 332:   __ jcc(Assembler::aboveEqual, L_2TAG_PACKET_2_0_1); // Branch only if |x| >= 20

It will be good to return +/- 1 when |x| >= 22 instead of 20 to match the Java code.

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

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


More information about the hotspot-compiler-dev mailing list