RFR: 8334755: Asymptotically faster implementation of square root algorithm [v32]
Raffaello Giulietti
rgiulietti at openjdk.org
Thu Jul 18 17:16:36 UTC 2024
On Thu, 18 Jul 2024 16:14:02 GMT, fabioromano1 <duke at openjdk.org> wrote:
>> I guess you are concerned about an overflow in `s2 + 2 * s`?
>>
>> If s = 2^32 - 1, then s2 = 2^64 - 2·2^32 + 1 and 2s = 2·2^32 - 2, without overflows.
>> Thus, s2 + 2s = 2^64 - 1, without overflows.
>
> @rgiulietti True, it's almost borderline code...
BTW, experimentally there are just 1024 cases for which `(long) Math.sqrt(x >= 0 ? x : x + 0x1p64)` is 2^32.
Thus, there are just 1024 cases for which `s2` is 0, and for these `Long.compareUnsigned(x, s2) < 0` is always false.
Therefore, I think it's statistically more convenient to interchange the two predicates of `||`, like so, to favor the case x < s2
if (Long.compareUnsigned(x, s2) < 0 || s == 1L << 32) {
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/19710#discussion_r1683220381
More information about the core-libs-dev
mailing list