RFR: 8346914: UB issue in scalbnA
Kim Barrett
kbarrett at openjdk.org
Tue Jun 10 06:32:28 UTC 2025
On Tue, 10 Jun 2025 05:51:39 GMT, Joe Darcy <darcy at openjdk.org> wrote:
>> src/hotspot/share/runtime/sharedRuntimeMath.hpp line 126:
>>
>>> 124: if (u_k <= (unsigned)-54) {
>>> 125: if (n > 50000) /* in case integer overflow in n+k */
>>> 126: return hugeX*copysignA(hugeX,x); /*overflow*/
>>
>> This case (`n > 50000`) isn't possible with my suggest change to limit the usage scope for `u_k`, and
>> can be deleted.
>
> FYI, as an alternative, there is a Java-only implementation of scalb (and supporting functionality) in java.lang.Math that could be ported to C as another way to avoid this issue.
`java.lang.Math.scalb()` doesn't seem useful here. It just transforms the
scale factor into a double power of 2 and then multiplies. It's not clear that
would result in exactly the same result for all arguments as this. And this is
(mostly) avoiding doing a double multiply for (perceived) performance reasons.
(For all I know, the complexity here could swamp the cost of a double
multiply.) Being certain of keeping the same results (on edge cases) is the
only reason to stay with this (but fixed to remove UB), rather than just
switching to using the C/C++ library `scalbn`. (And I don't know that
there *is* any potential difference between `scalbnA` and `scalbn` that would
actually matter. That would require more analysis than I've had time to do.)
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25656#discussion_r2137020078
More information about the hotspot-dev
mailing list