File tree 1 file changed +8
-4
lines changed
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -2068,10 +2068,14 @@ macro_rules! int_impl {
2068
2068
pub const fn rem_euclid( self , rhs: Self ) -> Self {
2069
2069
let r = self % rhs;
2070
2070
if r < 0 {
2071
- // if rhs is `integer::MIN`, rhs.wrapping_abs() == rhs.wrapping_abs,
2072
- // thus r.wrapping_add(rhs.wrapping_abs()) == r.wrapping_add(rhs) == r - rhs,
2073
- // which suits our need.
2074
- // otherwise, rhs.wrapping_abs() == -rhs, which won't overflow since r is negative.
2071
+ // Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`.
2072
+ // If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow
2073
+ // and is clearly equivalent, because `r` is negative.
2074
+ // Otherwise, `rhs` is `Self::MIN`, then we have
2075
+ // `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates
2076
+ // to `r.wrapping_add(Self::MIN)`, which is equivalent to
2077
+ // `r - Self::MIN`, which is what we wanted (and will not overflow
2078
+ // for negative `r`).
2075
2079
r. wrapping_add( rhs. wrapping_abs( ) )
2076
2080
} else {
2077
2081
r
You can’t perform that action at this time.
0 commit comments