Commit 8c49486 1 parent f6faf0b commit 8c49486 Copy full SHA for 8c49486
File tree 1 file changed +6
-10
lines changed
1 file changed +6
-10
lines changed Original file line number Diff line number Diff line change @@ -1416,18 +1416,14 @@ $EndFeature, "
1416
1416
```" ) ,
1417
1417
#[ stable( feature = "no_panic_abs" , since = "1.13.0" ) ]
1418
1418
#[ rustc_const_stable( feature = "const_int_methods" , since = "1.32.0" ) ]
1419
+ #[ allow_internal_unstable( const_if_match) ]
1419
1420
#[ inline]
1420
1421
pub const fn wrapping_abs( self ) -> Self {
1421
- // sign is -1 (all ones) for negative numbers, 0 otherwise.
1422
- let sign = self >> ( $BITS - 1 ) ;
1423
- // For positive self, sign == 0 so the expression is simply
1424
- // (self ^ 0).wrapping_sub(0) == self == abs(self).
1425
- //
1426
- // For negative self, self ^ sign == self ^ all_ones.
1427
- // But all_ones ^ self == all_ones - self == -1 - self.
1428
- // So for negative numbers, (self ^ sign).wrapping_sub(sign) is
1429
- // (-1 - self).wrapping_sub(-1) == -self == abs(self).
1430
- ( self ^ sign) . wrapping_sub( sign)
1422
+ if self . is_negative( ) {
1423
+ self . wrapping_neg( )
1424
+ } else {
1425
+ self
1426
+ }
1431
1427
}
1432
1428
}
1433
1429
You can’t perform that action at this time.
0 commit comments