-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking Issue for bigint helper methods #85532
Comments
I'd like a mul_mod, as shown in #85017, because I think you can't implement it efficiently without asm and it's a basic block for power_mod and other things. |
Another set of methods that could be useful that I'll probably offer implementations for at some point: /// `(self << rhs) | carry`
fn carrying_shl(self, rhs: u32, carry: Self) -> (Self, Self);
/// `(self >> rhs) | carry`
fn borrowing_shr(self, rhs: u32, carry: Self) -> (Self, Self);
/// `self << rhs`
fn widening_shl(self, rhs: u32) -> (Self, Self);
/// `self >> rhs`
fn widening_shr(self, rhs: u32) -> (Self, Self); Essentially, return the two halves of a rotation, i.e. |
From @scottmcm in the original PR:
|
Why don't we add |
Mostly effort implementing them efficiently. In the meantime, you can do it with four calls to the |
I was very confused by this function name at first, since borrowing in Rust usually refers to references. I am not a native speaker, but I do formal mathematical work in English professionally, and yet I never before heard the term "borrowing" in the context of subtraction. So I think this, at least, needs some explanation in the docs. (I would have expected something like The current docs for some of the other methods could probably also be improved: they talk about not having the "ability to overflow", which makes it sound like not overflowing is a bad thing. |
The word borrow here comes from the terminology for a full subtractor. I am thinking that maybe the borrowing_sub function could be removed altogether. The same effect that borrowing_sub has can be obtained from carrying_add by making the first carrying_add in the chain have a set carry bit, and then bitnot every rhs. This fact could be put in the documentation of carrying_add. |
Considering how the primary goal of these methods is to be as efficient as possible, usually optimising down to a single instruction, I don't think it'd be reasonable to just get rid of subtraction in favour of telling everyone to use addition instead. Definitely open to changing the name, though. |
These helper methods will not be very useful to me unless they are implemented for every kind of integer. Here is an implementation for a widening multiplication-addition for u128:
I have tested this with my crate edit: There is a version of this that uses the Karatsuba trick to use 3 multiplications instead of 4, but it incurs extra summations, branches, and is not as parallel. For typical desktop processors the above should be the fastest. |
I would make a PR for that. |
Some alternative signatures include |
I would also change up the documentation headers for the
I specifically note |
|
But unsigned overflow and signed overflow are different. For example, on x86_64, while unsigned and signed integers share addition and subtraction instructions, unsigned overflow is detected using the carry flag while signed overflow is detected using the overflow flag. As a concrete example: Edit: I think I had misread your comment and thought the middle part of your comment was the current doc, not your suggestion, so it looks like I completely misinterpreted your final comment. |
Yes signed and unsigned overflow are different, but the |
I think all of these are good suggestions, and like mentioned earlier, these changes definitely should go in a PR if you have the time. I think one important thing to note is that so far the APIs here seem good, but the documentation definitely could use some work. Although if there's a bigger case for changing the subtraction behaviour to be more in line with what's expected (the existing behaviour is mostly modelled after the x86 instructions adc and sbb), then I'm for that. That said, the main goal is to make it relatively painless to write correct code that compiles down to the right instructions in release mode, so, I would say we should make sure that happens regardless of what's done. I would have added an explicit test for that but I honestly don't know how. |
…riplett Add more text and examples to `carrying_{add|mul}` `feature(bigint_helper_methods)` tracking issue rust-lang#85532 cc `@clarfonthey`
…riplett Add more text and examples to `carrying_{add|mul}` `feature(bigint_helper_methods)` tracking issue rust-lang#85532 cc ``@clarfonthey``
…riplett Add more text and examples to `carrying_{add|mul}` `feature(bigint_helper_methods)` tracking issue rust-lang#85532 cc ```@clarfonthey```
…riplett Add more text and examples to `carrying_{add|mul}` `feature(bigint_helper_methods)` tracking issue rust-lang#85532 cc ````@clarfonthey````
…riplett Add more text and examples to `carrying_{add|mul}` `feature(bigint_helper_methods)` tracking issue rust-lang#85532 cc `````@clarfonthey`````
Multiplication, and carry-less multiplication, are inherently a widening operation. Unfortunately, at the time of writing, the types in Rust don't capture this well, being built around fixed-width wrapping multiplication. Rust's stdlib can rely on compiler-level optimizations to clean up performance issues from unnecessarily-wide multiplications, but this becomes a bit of an issue for our library, especially for u64 types, since we rely on intrinsics, which may be hard for compilers to optimize around. This commit adds widening_mul, based on a proposal to add widening_mul to Rust's primitive types: rust-lang/rust#85532 As well as several other tweaks to how xmul is provided, moving more arch-level details into xmul, but still limiting when it is emitted.
@typetetris @nickkuk Your messages made me realize something: the way to justify If you want to send a PR adding it to nightly, I'm willing to approve it, though it'll need naming feedback from libs-api at some before before it would have a chance at stabilizing. (I could see @clarfonthey We have intrinsics with fallback bodies now, so we can add a poor version in rust in a way that it can be overridden by LLVM (to emit |
So, I was looking into this again and wanted to summarise some of the stuff folks have discussed so far, to see what common ground has been established. Addition methodsSo far, we're totally okay with the signatures of the two methods, where
The main issue is naming these methods, and there doesn't seem to be a consensus so far. A few of the options for addition:
And for subtraction:
One option I particularly like is using the
Although we definitely need to come up with a name consensus before stabilisation. The shed awaits… Multiplication methodsThese ones have undergone a lot of different discussion. A few notes that folks have added:
So, just to fully cover the signatures of these, using not-Rust syntax. Going to use a period for concatenation, and give a temporary name to them to hopefully make sense.
Not a whole lot has been done for names. Generally, people think of the two-word versions of multiplication as a "wide mul" or "widening mul" and that's okay. Probably the names Right now, it's unclear whether Finally, there's the subject of the return value. It seems reasonable to create a struct for this, since otherwise it won't be clear what word is which in most cases. Right now, only the Bit-shift methodsPeople seemed pretty amenable to adding these, although again, the carry/borrow name is a bit weird. Particularly, These aren't on nightly yet. ConclusionsIt seems definitely reasonable to add intrinsics for some of these methods as a good next step, since it's unlikely we'll get the right result without help. |
I don't think it's unlikely to get the right result for all of these without intrinsics. The intrinsics are needed to get them right and fast (easy to optimizable). |
So, I attempted to start writing some intrinsics for these, but I have not done this before and may have run into the most cursed compiler bug I've ever encountered, and I've also never done this before. But uh, we'll see what this ends up as later. Figured I'd mention in case anyone else was thinking of trying. In case you're wondering, it's me finding out that LLVM doesn't document any of its methods and secretly makes them all have unknown preconditions that can trigger UB if not properly met. I was getting illegal instructions just randomly in the middle of building my code. |
Is there a link pointing to some Internals-Forum or Zulip thread for bike-shedding the names? I'm aware that tracking-issues aren't meant for discussion, that's why I'm asking 😅 |
No, but you're more than welcome to make one and link it here! |
After looking at the assembly, I say that
|
It turns out that rustc was able to optimize the current implementation of |
Opened #133663 to add an intrinsic for |
Replace (Classic problem that Fixed in #133674 |
…nieu Fix chaining `carrying_add`s Something about the MIR lowering for `||` ended up breaking this, but it's fixed by changing the code to use `|` instead. I also added an assembly test to ensure it *keeps* being [`adc`](https://www.felixcloutier.com/x86/adc). cc rust-lang#85532 (comment), which noticed this.
…nieu Fix chaining `carrying_add`s Something about the MIR lowering for `||` ended up breaking this, but it's fixed by changing the code to use `|` instead. I also added an assembly test to ensure it *keeps* being [`adc`](https://www.felixcloutier.com/x86/adc). cc rust-lang#85532 (comment), which noticed this.
Rollup merge of rust-lang#133674 - scottmcm:chain-carrying-add, r=Amanieu Fix chaining `carrying_add`s Something about the MIR lowering for `||` ended up breaking this, but it's fixed by changing the code to use `|` instead. I also added an assembly test to ensure it *keeps* being [`adc`](https://www.felixcloutier.com/x86/adc). cc rust-lang#85532 (comment), which noticed this.
Add a compiler intrinsic to back `bigint_helper_methods` cc rust-lang#85532 This adds a new `carrying_mul_add` intrinsic, to implement `wide_mul` and `carrying_mul`. It has fallback MIR for all types -- including `u128`, which isn't currently supported on nightly -- so that it'll continue to work on all backends, including CTFE. Then it's overridden in `cg_llvm` to use wider intermediate types, including `i256` for `u128::carrying_mul`.
Add a compiler intrinsic to back `bigint_helper_methods` cc rust-lang#85532 This adds a new `carrying_mul_add` intrinsic, to implement `wide_mul` and `carrying_mul`. It has fallback MIR for all types -- including `u128`, which isn't currently supported on nightly -- so that it'll continue to work on all backends, including CTFE. Then it's overridden in `cg_llvm` to use wider intermediate types, including `i256` for `u128::carrying_mul`.
Rollup merge of rust-lang#133663 - scottmcm:carrying_mul_add, r=Amanieu Add a compiler intrinsic to back `bigint_helper_methods` cc rust-lang#85532 This adds a new `carrying_mul_add` intrinsic, to implement `wide_mul` and `carrying_mul`. It has fallback MIR for all types -- including `u128`, which isn't currently supported on nightly -- so that it'll continue to work on all backends, including CTFE. Then it's overridden in `cg_llvm` to use wider intermediate types, including `i256` for `u128::carrying_mul`.
Add a compiler intrinsic to back `bigint_helper_methods` cc rust-lang#85532 This adds a new `carrying_mul_add` intrinsic, to implement `wide_mul` and `carrying_mul`. It has fallback MIR for all types -- including `u128`, which isn't currently supported on nightly -- so that it'll continue to work on all backends, including CTFE. Then it's overridden in `cg_llvm` to use wider intermediate types, including `i256` for `u128::carrying_mul`.
Feature gate:
#![feature(bigint_helper_methods)]
This is a tracking issue for the following methods on integers:
carrying_add
borrowing_sub
carrying_mul
widening_mul
These methods are intended to help centralise the effort required for creating efficient big integer implementations, by offering a few methods which would otherwise require special compiler intrinsics or custom assembly code in order to do efficiently. They do not alone constitute big integer implementations themselves, but are necessary building blocks for a larger implementation.
Public API
Steps / History
widening_mul
RFC: widening_mul rfcs#2417carrying_add
,borrowing_sub
,carrying_mul
, andwidening_mul
Add carrying_add, borrowing_sub, widening_mul, carrying_mul methods to integers #85017carrying_add
andborrowing_sub
on signed types: Reimplementcarrying_add
andborrowing_sub
for signed integers. #93873bigint_helper_methods
#133663("without the ability to overflow" can be confusing.)
Unresolved Questions
widening_mul
that simply returns the next-larger type? What would we do foru128
/i128
?The text was updated successfully, but these errors were encountered: