@@ -1401,12 +1401,8 @@ $EndFeature, "
1401
1401
```" ) ,
1402
1402
#[ stable( feature = "no_panic_abs" , since = "1.13.0" ) ]
1403
1403
#[ inline]
1404
- pub fn wrapping_abs( self ) -> Self {
1405
- if self . is_negative( ) {
1406
- self . wrapping_neg( )
1407
- } else {
1408
- self
1409
- }
1404
+ pub const fn wrapping_abs( self ) -> Self {
1405
+ ( self ^ ( self >> ( $BITS - 1 ) ) ) . wrapping_sub( self >> ( $BITS - 1 ) )
1410
1406
}
1411
1407
}
1412
1408
@@ -1764,12 +1760,8 @@ $EndFeature, "
1764
1760
```" ) ,
1765
1761
#[ stable( feature = "no_panic_abs" , since = "1.13.0" ) ]
1766
1762
#[ inline]
1767
- pub fn overflowing_abs( self ) -> ( Self , bool ) {
1768
- if self . is_negative( ) {
1769
- self . overflowing_neg( )
1770
- } else {
1771
- ( self , false )
1772
- }
1763
+ pub const fn overflowing_abs( self ) -> ( Self , bool ) {
1764
+ ( self ^ ( self >> ( $BITS - 1 ) ) ) . overflowing_sub( self >> ( $BITS - 1 ) )
1773
1765
}
1774
1766
}
1775
1767
@@ -1973,15 +1965,11 @@ $EndFeature, "
1973
1965
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1974
1966
#[ inline]
1975
1967
#[ rustc_inherit_overflow_checks]
1976
- pub fn abs( self ) -> Self {
1977
- if self . is_negative( ) {
1978
- // Note that the #[inline] above means that the overflow
1979
- // semantics of this negation depend on the crate we're being
1980
- // inlined into.
1981
- -self
1982
- } else {
1983
- self
1984
- }
1968
+ pub const fn abs( self ) -> Self {
1969
+ // Note that the #[inline] above means that the overflow
1970
+ // semantics of the subtraction depend on the crate we're being
1971
+ // inlined into.
1972
+ ( self ^ ( self >> ( $BITS - 1 ) ) ) - ( self >> ( $BITS - 1 ) )
1985
1973
}
1986
1974
}
1987
1975
0 commit comments