RFR: 8346914: UB issue in scalbnA

Johan Sjölen jsjolen at openjdk.org
Thu Jun 5 14:05:50 UTC 2025


On Thu, 5 Jun 2025 13:00:48 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> I like this change. It's easier to follow, at least for me.
>
> I think that is equivalent, but I did not want to disrupt the general control flow of the algorithm so that it still closely resembles the original fdlibm code.
> 
> I also initially misread your suggestion and now I am wondering if I can actually simplify it to a simple two line change:
> 
> unsigned u_k = k + n; // avoid UB signed integer overflow
> k = (int) u_k;  // safely assign to k
> 
> does that bypass any UB?

Why would it be OK to do `(k+n)<<20` but UB to do `k = k+n`?  The addition may still overflow, which would be UB.

>> For me it's less clear and far easier to miss.
>> I'll vote for `if (u_k <= (unsigned)-54) {`.
>> Reading `if (u_k <= -54u) {` just cooks my brain.
>
> I'm suprised that is even valid TBH. It strikes me as a numerical oxymoron.

>"If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —end note ]

So this is OK.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25656#discussion_r2128940099
PR Review Comment: https://git.openjdk.org/jdk/pull/25656#discussion_r2128921557


More information about the hotspot-dev mailing list