Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v3]

2025-05-12 Thread Joe Darcy
On Thu, 8 May 2025 15:13:19 GMT, Raffaello Giulietti wrote: >> Add `powExact()` and `unsignedPowExact()` methods that operate on integer >> values arguments. >> Further, add `unsignedMultiplyExact` methods as well. > > Raffaello Giulietti has updated the pull request incrementally with one > a

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v3]

2025-05-08 Thread Raffaello Giulietti
> Add `powExact()` and `unsignedPowExact()` methods that operate on integer > values arguments. > Further, add `unsignedMultiplyExact` methods as well. Raffaello Giulietti has updated the pull request incrementally with one additional commit since the last revision: Added tests.

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-05 Thread Joe Darcy
On Fri, 2 May 2025 17:49:54 GMT, Raffaello Giulietti wrote: > Yes, I'm familiar with both this Java code and the intrinsic code. > > Compare this with the much simpler proposed code. The checked multiplication > `unsignedMultiplyExact` apparently performs two 64x64->64 multiplications, > but

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 17:53:12 GMT, Raffaello Giulietti wrote: >>> Anyway, since the pre-checks are not precise, that would lead to an >>> implementation with a loop with checked, and another one with unchecked >>> multiplications. I don't think this buys you anything. >> >> It serves to skip th

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 17:26:21 GMT, fabioromano1 wrote: >> @fabioromano1 Well, there are two checks. In one the product can overflow, >> you'd need to convert one of the operands to `long`. >> >> Anyway, since the pre-checks are not precise, that would lead to an >> implementation with a loop wit

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 16:57:43 GMT, Shaojin Wen wrote: >> src/java.base/share/classes/java/lang/Math.java line 3500: >> >>> 3498: return (n & 0b1) == 0 ? 1 : -1; >>> 3499: } >>> 3500: >> >> Suggestion: >> >> >> if (x == 2) { >> if (n >= Integer.SIZE - 1)

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 17:30:55 GMT, fabioromano1 wrote: >> Again, I don't think that `n == 1` is a frequent case which would make any >> practical difference. >> >> As for the `bitLength` check, the product might overflow. >> Further, `bitLength` might not be that cheap. >> Finally, the test would

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 16:53:39 GMT, Raffaello Giulietti wrote: > As for the `bitLength` check, the product might overflow. Further, > `bitLength` might not be that cheap. The current implementations of `bitLength()` calls `numberOfLeadingZeros`, which are: public static int numberOfLeadingZeros

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 17:18:36 GMT, Raffaello Giulietti wrote: > Anyway, since the pre-checks are not precise, that would lead to an > implementation with a loop with checked, and another one with unchecked > multiplications. I don't think this buys you anything. It serves to skip the checks in

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 17:03:30 GMT, fabioromano1 wrote: >> I don't think there's a quick, _precise_ pre-check that would ensure that >> the loop can just use simple, unchecked `*` multiplications. >> >> Consider `unsignedPowExact(3L, 40)`, which does not overflow, versus >> `unsignedPowExact(3L,

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 16:45:28 GMT, Raffaello Giulietti wrote: > I don't think there's a quick, _precise_ pre-check that would ensure that the > loop can just use simple, unchecked `*` multiplications. > > Consider `unsignedPowExact(3L, 40)`, which does not overflow, versus > `unsignedPowExact(3

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Shaojin Wen
On Fri, 2 May 2025 16:04:18 GMT, fabioromano1 wrote: >> Raffaello Giulietti has updated the pull request incrementally with one >> additional commit since the last revision: >> >> Clearer and more complete spec of the *pow* methods. > > src/java.base/share/classes/java/lang/Math.java line 350

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 16:26:48 GMT, fabioromano1 wrote: >> @fabioromano1 Unless there's evidence that these cases are _very very_ >> common, there's no point in adding fast paths. >> See this comment in `unsignedPowExact(long,int)` >> >> >> /* >> * To keep the code as simple as p

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 16:06:42 GMT, fabioromano1 wrote: >> Raffaello Giulietti has updated the pull request incrementally with one >> additional commit since the last revision: >> >> Clearer and more complete spec of the *pow* methods. > > src/java.base/share/classes/java/lang/Math.java line 361

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 16:23:24 GMT, Raffaello Giulietti wrote: >> src/java.base/share/classes/java/lang/Math.java line 3567: >> >>> 3565: return 1; >>> 3566: } >>> 3567: if (x == 0 || x == 1) { >> >> Suggestion: >> >> if (x == 0 || x == 1 || n == 1) { > > @fab

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 16:07:31 GMT, fabioromano1 wrote: >> Raffaello Giulietti has updated the pull request incrementally with one >> additional commit since the last revision: >> >> Clearer and more complete spec of the *pow* methods. > > src/java.base/share/classes/java/lang/Math.java line 356

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 15:12:01 GMT, Raffaello Giulietti wrote: >> Add `powExact()` and `unsignedPowExact()` methods that operate on integer >> values arguments. >> Further, add `unsignedMultiplyExact` methods as well. > > Raffaello Giulietti has updated the pull request incrementally with one > a

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread fabioromano1
On Fri, 2 May 2025 15:12:01 GMT, Raffaello Giulietti wrote: >> Add `powExact()` and `unsignedPowExact()` methods that operate on integer >> values arguments. >> Further, add `unsignedMultiplyExact` methods as well. > > Raffaello Giulietti has updated the pull request incrementally with one > a

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath [v2]

2025-05-02 Thread Raffaello Giulietti
> Add `powExact()` and `unsignedPowExact()` methods that operate on integer > values arguments. > Further, add `unsignedMultiplyExact` methods as well. Raffaello Giulietti has updated the pull request incrementally with one additional commit since the last revision: Clearer and more complete

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath

2025-05-02 Thread Raffaello Giulietti
On Fri, 2 May 2025 14:06:23 GMT, Per Minborg wrote: >> Add `powExact()` and `unsignedPowExact()` methods that operate on integer >> values arguments. >> Further, add `unsignedMultiplyExact` methods as well. > > Maybe it is too late, but shouldn't there be a better way to structure all > these m

Re: RFR: 8355992: Add unsignedMultiplyExact and *powExact methods to Math and StrictMath

2025-05-02 Thread Per Minborg
On Fri, 2 May 2025 11:47:32 GMT, Raffaello Giulietti wrote: > Add `powExact()` and `unsignedPowExact()` methods that operate on integer > values arguments. > Further, add `unsignedMultiplyExact` methods as well. Maybe it is too late, but shouldn't there be a better way to structure all these