RFR: 8375285: Port fdlibm asinh to Java [v6]

Raffaello Giulietti rgiulietti at openjdk.org
Tue Feb 3 14:45:38 UTC 2026


On Thu, 29 Jan 2026 12:01:46 GMT, Anton Artemov <aartemov at openjdk.org> wrote:

>> Hi, please consider the following changes:
>> 
>> This is a port of FDLIBM asinh method.
>> 
>> Original C vs transliteration port:
>> 
>> 
>> $ diff -w fdlib_asinh.c Asinh.translit.java
>> 1c1,3
>> < /* asinh(x)
>> ---
>>> /**
>>> * Return the Inverse Hyperbolic Sine of x
>>> *
>> 2a5
>>> *
>> 10a14,17
>>> private static final class Asinh {
>>>       private static final double one = 1.0;
>>>       private static final double ln2 = 6.93147180559945286227e-01;
>>>       private static final double huge = 1.0e300;
>> 12,29c19
>> < #include "fdlibm.h"
>> <
>> < #ifdef __STDC__
>> < static const double
>> < #else
>> < static double
>> < #endif
>> < one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
>> < ln2 =  6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
>> < huge=  1.00000000000000000000e+300;
>> <
>> < #ifdef __STDC__
>> <       double asinh(double x)
>> < #else
>> <       double asinh(x)
>> <       double x;
>> < #endif
>> < {
>> ---
>>>       static double compute(double x) {
>> 39c29
>> <           w = __ieee754_log(fabs(x))+ln2;
>> ---
>>>                       w = log(Math.abs(x))+ln2;
>> 41,42c31,32
>> <           t = fabs(x);
>> <           w = __ieee754_log(2.0*t+one/(sqrt(x*x+one)+t));
>> ---
>>>                       t = Math.abs(x);
>>>                       w = log(2.0*t+one/(sqrt(x*x+one)+t));
>> 45c35
>> <           w =log1p(fabs(x)+t/(one+sqrt(one+t)));
>> ---
>>>                       w =log1p(Math.abs(x)+t/(one+sqrt(one+t)));
>> 47a38
>>>       }
>> 
>> Transliteration port vs more idiomatic port:
>> 
>> 
>> $ diff -w Asinh.translit.java Asinh.fdlibm.java
>> 6,9c6,12
>> < *  Based on
>> < *    asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
>> < *   we have
>> < *   asinh(x) := x  if  1+x*x=1,
>> ---
>>>  *
>>>  *      asinh(x) is defined so that asinh(sinh(alpha)) = alpha -INF < alpha < < INF
>>>  *      and sinh(asinh(x)) = x, -INF < x  < INF.
>>>  *      It can be written as asinh(x) = ln(x + sqrt(x^2 + 1)), -INF < x  < INF.
>>>  *      1. Replace x by |x| as the function is odd.
>>>  *      2.
>>>  *          asinh(x) := x, if 1+x^2 = 1,
>> 12a16,22
>>>  *
>>>  *
>>>  *
>>>  * Special cases:
>>>  *      only asinh(0)=0 is exact for finite x.
>>>  *      asinh(NaN) is NaN
>>>  *      asinh(INF) is INF
>> 14,15c24
>> < private static final class Asinh {
>> <       private static final double one = 1.0;
>> ---
>>> static final class Asinh {
>> 24c33,35
>> <               if(ix>=0x7ff00000) return x+x;  /* x is inf or NaN */
>> ---
>>>               if(ix >= 0x7ff00000) {
>>>                       return x...
>
> Anton Artemov has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8375285: Added missed link in documentation.

Will review the test code in another round.

src/java.base/share/classes/java/lang/FdLibm.java line 3518:

> 3516:      *
> 3517:      *
> 3518:      *      asinh(x) is defined so that asinh(sinh(alpha)) = alpha, -INF < alpha < < INF

Suggestion:

     *      asinh(x) is defined so that asinh(sinh(alpha)) = alpha, -∞ < alpha < ∞

∞ looks better
< avoids confusing an HTML parser (not sure if it really needed, though)

src/java.base/share/classes/java/lang/FdLibm.java line 3519:

> 3517:      *
> 3518:      *      asinh(x) is defined so that asinh(sinh(alpha)) = alpha, -INF < alpha < < INF
> 3519:      *      and sinh(asinh(x)) = x, -INF < x  < INF.

Similarly

src/java.base/share/classes/java/lang/FdLibm.java line 3520:

> 3518:      *      asinh(x) is defined so that asinh(sinh(alpha)) = alpha, -INF < alpha < < INF
> 3519:      *      and sinh(asinh(x)) = x, -INF < x  < INF.
> 3520:      *      It can be written as asinh(x) = ln(x + sqrt(x^2 + 1)), -INF < x  < INF.

This is not the formulation in the original fdlibm code.

src/java.base/share/classes/java/lang/FdLibm.java line 3531:

> 3529:      *
> 3530:      * Special cases:
> 3531:      *      only asinh(0)=0 is exact for finite x.

Suggestion:

     *      only asinh(±0) = ±0 is exact for finite x.

src/java.base/share/classes/java/lang/FdLibm.java line 3533:

> 3531:      *      only asinh(0)=0 is exact for finite x.
> 3532:      *      asinh(NaN) is NaN
> 3533:      *      asinh(INF) is INF

Suggestion:

     *      asinh(±∞) = ±∞

src/java.base/share/classes/java/lang/FdLibm.java line 3543:

> 3541:             int hx, ix;
> 3542:             hx = __HI(x);
> 3543:             ix = hx & 0x7fffffff;

Suggestion:

            ix = hx & 0x7fff_ffff;

as well as other hexadecimal literals below. It's easier for counting whether the number of hex digits is correct.

src/java.base/share/classes/java/lang/Math.java line 2765:

> 2763:      * The inverse hyperbolic sine of <i>x</i> is defined to be a function such that
> 2764:      *  asinh({@linkplain Math#sinh sinh(<i>x</i>)}) = <i>x</i> for any <i>x</i>.
> 2765:      *  Note that range of the exact asinh is not limited.

Suggestion:

     *  Note that domain of the exact asinh is unrestricted.

src/java.base/share/classes/java/lang/Math.java line 2777:

> 2775:      * <li>If the argument is negative infinity, then the result is
> 2776:      * negative infinity.
> 2777:      *

It is possible to collapse both items into one, as is done for the zero case.

src/java.base/share/classes/java/lang/StrictMath.java line 2197:

> 2195:      * @return  The inverse hyperbolic sine of {@code x}.
> 2196:      * @since 27
> 2197:      */

See the comment for Math

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

PR Review: https://git.openjdk.org/jdk/pull/29273#pullrequestreview-3745650028
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759404605
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759404841
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759405044
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759405227
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759405470
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759405711
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759415326
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759415580
PR Review Comment: https://git.openjdk.org/jdk/pull/29273#discussion_r2759419099


More information about the core-libs-dev mailing list