You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Several helper methods have been added and/or stabilized to help implement addition and multiplication on big integer types (rust-lang/rust#85532). An extension of that is implementing division on big integers, which requires another building block: a wide division function (which can be seen as the reverse of widening_mul):
// Returns (quotient, remainer) of the division of `(high << 64 | low)` by `divisor`.// This assumes that the quotient fits into a u64 (the remainder always fits as the divisor is a `u64`).fnwide_div(high:u64,low:u64,divisor:u64) -> (u64,u64);
Contrary to the naive division on u128 (which is typically implemented by the __udivmodti4 compiler built-in, see also rust-lang/rust#44545):
Have a separate unsafe wide_div_unchecked() variant, when the caller asserts that the quotient will fit in a uN?
Also implement on signed integer types (e.g. to mirror the IDIV instruction on Intel)? Would that be useful? Should the low input be unsigned (as with iN::widening_mul)?
Naming? Order of the low & high parameters?
Alternatives
This could be implemented manually (as is indeed done in the num_bigint crate). However, this requires:
special-casing CPU architectures (currently x86 and x86_64) to obtain the best performance - which could be a maintenance concern,
unsafe code to emit the ideal div instruction on x86(_64),
dispatch code to other integer types for the case of usize,
Having such methods directly on integer types in the standard library could therefore be helpful and more maintainable for the wider ecosystem, similarly to other bigint helper methods (rust-lang/rust#85532).
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
Several helper methods have been added and/or stabilized to help implement addition and multiplication on big integer types (rust-lang/rust#85532). An extension of that is implementing division on big integers, which requires another building block: a wide division function (which can be seen as the reverse of
widening_mul):Contrary to the naive division on
u128(which is typically implemented by the__udivmodti4compiler built-in, see also rust-lang/rust#44545):...this
wide_divcan be more efficient:wide_div.The same
wide_divAPI could be offered on all unsigned integer types up tou64, as well asusize.Motivating examples or use cases
This API is a useful building block in several contexts:
Solution sketch
For all
u8,u16,u32,u64,usize:Default implementation:
Optimized implementation:
DIVinstruction on x86 variants,__udivmodti4built-in on other platforms, e.g. https://github.com/ridiculousfish/libdivide/blob/v5.3.0/libdivide.h#L554-L634.Open questions
unsafe wide_div_unchecked()variant, when the caller asserts that the quotient will fit in auN?lowinput be unsigned (as withiN::widening_mul)?Alternatives
This could be implemented manually (as is indeed done in the
num_bigintcrate). However, this requires:x86andx86_64) to obtain the best performance - which could be a maintenance concern,divinstruction onx86(_64),usize,__udivti3,__umodti3or__udivmodti4) which may not be ideal (128-bit integer division with remainder is not combined to a single operation rust#44545), e.g. https://github.com/ridiculousfish/libdivide/blob/v5.3.0/libdivide.h#L554-L634.Having such methods directly on integer types in the standard library could therefore be helpful and more maintainable for the wider ecosystem, similarly to other bigint helper methods (rust-lang/rust#85532).
Links and related work
num_bigintcrate: https://docs.rs/num-bigint/0.4.6/src/num_bigint/biguint/division.rs.htmllibdivide: https://github.com/ridiculousfish/libdivide/blob/v5.3.0/libdivide.h#L513-L636What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: