-
Notifications
You must be signed in to change notification settings - Fork 150
Scalar operations across all integer types #313
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.
A BigDigit can be added to a BigUint - this is one of several operations being implemented to allow scalar operations on BigInt and BigUint across the board.
A BigDigit can be subtracted from a BigUint - this is one of several operations being implemented to allow scalar operations on BigInt and BigUint across the board.
A BigUint can be divided by a BigDigit - this is one of several operations being implemented to allow scalar operations on BigInt and BigUint across the board.
Allow the addition to occur with either operand order, and with any combination of owned and borrowed arguments.
Allow the multiplication to occur with either operand order and with any combination of owned and borrowed arguments.
Allow the subtraction to occur with either operand order and with any combination of owned and borrowed arguments.
Allow the division to occur with either operand order and with any combination of owned and borrowed arguments.
- Don't apply attributes to statements (1.12.0) - Don't use checked_abs (1.13.0)
Great! I've been meaning to ping you about this. :) Before I dig into a full review, I'm having second thoughts about doing this for all integer types, because of this one usability wart: type inference fails for unspecified literals.
(that example file isn't committed here, just something I wrote to try it out.) When rustc can't infer the exact type for an integer literal, it just chooses
What do you think? |
What a funny little error! Of your three options, I prefer the first. The compiler error is very clear about what's going on, and it doesn't seem unreasonable to be expected to add the type suffix in this case.
I don't like this option. Either
I think it's more irritating / confusing for a user to think "Why can't I do this operation on a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an impressive PR, thanks!
The few things I found are very minor. Unless you happen to be standing by right now, ready to make changes, I'll just go ahead myself.
bigint/src/bigint.rs
Outdated
@@ -299,6 +302,24 @@ impl Signed for BigInt { | |||
} | |||
} | |||
|
|||
// A convenience method for getting the absolute value of an i32 in a u32. | |||
fn i32_abs_as_u32(a: i32) -> u32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably should be #[inline]
.
bigint/src/bigint.rs
Outdated
} | ||
|
||
// A convenience method for getting the absolute value of an i64 in a u64. | ||
fn i64_abs_as_u64(a: i64) -> u64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also #[inline]
.
bigint/src/biguint.rs
Outdated
self.data.push(0); | ||
} | ||
|
||
let carry = __add2(&mut self.data, &[other]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For len() == 0 && other == 0
, won't this fail the __add2
length assertion?
(could just return self
right away for other == 0
, of course)
bigint/src/biguint.rs
Outdated
} | ||
|
||
let (lo, hi) = big_digit::from_doublebigdigit(other); | ||
let carry = __add2(&mut self.data, &[lo, hi]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly here, the __add2
length assertion doesn't care if they're zeros.
bors r+ |
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.
Build succeeded |
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.