-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Pre-RFC: Unchecked arithmetic #2508
Copy link
Copy link
Closed
Labels
A-arithmeticArithmetic related proposals & ideasArithmetic related proposals & ideasA-unsafeUnsafe related proposals & ideasUnsafe related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.Relevant to the library API team, which will review and decide on the RFC.
Metadata
Metadata
Assignees
Labels
A-arithmeticArithmetic related proposals & ideasArithmetic related proposals & ideasA-unsafeUnsafe related proposals & ideasUnsafe related proposals & ideasT-langRelevant to the language team, which will review and decide on the RFC.Relevant to the language team, which will review and decide on the RFC.T-libs-apiRelevant to the library API team, which will review and decide on the RFC.Relevant to the library API team, which will review and decide on the RFC.
Type
Fields
Give feedbackNo fields configured for issues without a type.
On IRC, we recently discussed the speed of addition. I think Rust currently has no way to say “I know this cannot overflow, so just optimize and UB if it actually can”.
Hence the idea of adding
unchecked_add, etc., that would beunsafefunctions. (addition of anUnchecked<u*>type is left to later assessment)Specific list of functions that'd be available as
unchecked_*is the same set of functions that are currently available aschecked_*,overflowing_*andwrapping_*forms:unchecked_addunchecked_subunchecked_mulunchecked_divunchecked_div_eucunchecked_remunchecked_mod_eucunchecked_negunchecked_shlunchecked_shrunchecked_powIs there any major roadblock/risks that you think I should be aware of before starting to write this RFC? Do you like this idea or not?
For the potential for improvement, even though it's hard to pinpoint the exact reason why, using signed arithmetic instead of unsigned arithmetic in C here made the code run 10% faster -- this is likely an equivalent gain we could have using
uncheckedvariants instead ofoverflowing(or equivalent) ones. :) (not even counting debug builds, where most of the huge gain could already be had by using eg.overflowing_*, even though it wouldn't really be semantically correct)Note: this is the same idea as rust-lang/rust#52205 , except it'd be available to user code