File tree Expand file tree Collapse file tree 1 file changed +6
-18
lines changed Expand file tree Collapse file tree 1 file changed +6
-18
lines changed Original file line number Diff line number Diff line change @@ -1997,27 +1997,15 @@ $EndFeature, "
19971997```" ) ,
19981998 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
19991999 #[ rustc_const_stable( feature = "const_int_methods" , since = "1.32.0" ) ]
2000+ #[ allow_internal_unstable( const_if_match) ]
20002001 #[ inline]
20012002 #[ rustc_inherit_overflow_checks]
20022003 pub const fn abs( self ) -> Self {
2003- // Note that the #[inline] above means that the overflow
2004- // semantics of the subtraction depend on the crate we're being
2005- // inlined into.
2006-
2007- // sign is -1 (all ones) for negative numbers, 0 otherwise.
2008- let sign = self >> ( $BITS - 1 ) ;
2009- // For positive self, sign == 0 so the expression is simply
2010- // (self ^ 0) - 0 == self == abs(self).
2011- //
2012- // For negative self, self ^ sign == self ^ all_ones.
2013- // But all_ones ^ self == all_ones - self == -1 - self.
2014- // So for negative numbers, (self ^ sign) - sign is
2015- // (-1 - self) - -1 == -self == abs(self).
2016- //
2017- // The subtraction overflows when self is min_value(), because
2018- // (-1 - min_value()) - -1 is max_value() - -1 which overflows.
2019- // This is exactly when we want self.abs() to overflow.
2020- ( self ^ sign) - sign
2004+ if self . is_negative( ) {
2005+ -self
2006+ } else {
2007+ self
2008+ }
20212009 }
20222010 }
20232011
You can’t perform that action at this time.
0 commit comments