Skip to content

Pre-RFC: Unchecked arithmetic #2508

Closed
Closed
@Ekleog

Description

@Ekleog

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 be unsafe functions. (addition of an Unchecked<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 as checked_*, overflowing_* and wrapping_* forms:

  • unchecked_add
  • unchecked_sub
  • unchecked_mul
  • unchecked_div
  • unchecked_div_euc
  • unchecked_rem
  • unchecked_mod_euc
  • unchecked_neg
  • unchecked_shl
  • unchecked_shr
  • unchecked_pow

Is 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 unchecked variants instead of overflowing (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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arithmeticArithmetic related proposals & ideasA-unsafeUnsafe related proposals & ideasT-langRelevant 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions