File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 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 {
20682068 pub const fn rem_euclid( self , rhs: Self ) -> Self {
20692069 let r = self % rhs;
20702070 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`).
20752079 r. wrapping_add( rhs. wrapping_abs( ) )
20762080 } else {
20772081 r
You can’t perform that action at this time.
0 commit comments