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
pubfnoverflowing_signed_difference(a:u64,b:u64) -> (i64,bool){// Translate the unsigned range 0..=u64::MAX// to the signed range i64::MIN..=i64::MAXlet a = (a asi64).wrapping_add(i64::MIN);let b = (b asi64).wrapping_add(i64::MIN);// The difference is preserved
a.overflowing_sub(b)}
For consistency with existing methods like i64::overflowing_sub_signed(i64, u64) -> (i64, bool), I believe that name is clearer (the difference is signed, as opposed to subtracting a signed number). Even then it could be confusing.
This is useful in the case of
This is better than casting u64s to u128s and doing
try_into
i64 as u128s cannot be upgraded to u256sGCC and Clang have this feature via
__builtin_sub_overflow(a, b, &output)
which generates the following code: https://godbolt.org/z/E64GnaEz6
This can be implemented in rust with
sub_check2
: https://godbolt.org/z/dxrP4d9vTwhich has zero branches for optimal performance
sub_large: 1.029584449 ns / rep
sub_check: 1.436846155 ns / rep
sub_check2: 1.02297698 ns / rep
CPU: AMD R7 5800X
New code:
The text was updated successfully, but these errors were encountered: