-
Notifications
You must be signed in to change notification settings - Fork 150
Add scalar multiplication to BigUint, BigInt #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
BigUint and BigInt can now be multiplied by a BigDigit, re-using the same buffer for the output, thereby reducing allocations and copying.
As an example of the benefit, I compared the speed of calculating
The results were:
|
That speedup is great, especially in eliminating sys time!
Yes please, we should have this both ways. I don't think users should have to worry about order when it's so easy to make both work.
Right, you don't get the buffer reuse, but the actual multiplication might still be faster.
Hmm. This opens a can of worms, that we might actually want For that matter, we probably should have scalars for all ops too. There's a stronger performance reason to have That's a lot of scope creep, I realize. If you're game, I'd love to see the full monty. If you're not, we should at least plan for how much work it is to get there. It might just be that we should fill out naive implementations everywhere first (convert to bigint and do a normal op), then optimize when we can. |
I agree with you there, in terms of implementing the arithmetic operations for all integer types. I think the work breaks down into pieces quite nicely:
The first and last bullets are where the real work is, the others can almost certainly be done with some useful macros, in the style of the rest of Does that general approach sound like the right thing to do? I think I'm game - even though, as you say, it's a lot more than I originally set out to do with this PR. |
Yes, I think your breakdown sounds great, and we can merge those incrementally over several PRs. One note on the 64-bit part - even if we find it hard to do a fully-optimized scalar version, we can probably still avoid a full |
Refactor json_internal macro to need no helper macros
Closing in favor of #313 |
313: Scalar operations across all integer types r=cuviper With my apologies for opening a new PR, and also for the 8 month delay, this continues the work started in #237 - the discussion there outlines the goals I was aiming for. I suppose this supersedes that PR and the other one can now be closed. This PR adds support for Add, Sub, Mul, Div and Rem operations involving one BigInt/BigUint and one primitive integer, with operands in either order, and any combination of owned/borrowed arguments.
BigUint
andBigInt
can now be multiplied by aBigDigit
, re-using the same buffer for the output, thereby reducing allocations and copying.Points for comment:
Mul<BigDigit> for BigUint
, and notMul<BigUint> for BigDigit
. It won't be any trouble to add the other direction if it's felt that it should exist.impl<'a> Mul<BigDigit> for &'a BigUint
). That's because you would not be able to reuse the same buffer for output, and that misses the point somewhat. Again, not a lot of trouble to add it in if it would be useful, but I'm less convinced of that.impl Mul<i32> for BigInt
, or can users cope with multiplying byu32
and negating the result?