@@ -1112,13 +1112,7 @@ $EndFeature, "
1112
1112
without modifying the original"]
1113
1113
#[ inline]
1114
1114
pub const fn wrapping_add( self , rhs: Self ) -> Self {
1115
- #[ cfg( bootstrap) ] {
1116
- intrinsics:: overflowing_add( self , rhs)
1117
- }
1118
-
1119
- #[ cfg( not( bootstrap) ) ] {
1120
- intrinsics:: wrapping_add( self , rhs)
1121
- }
1115
+ intrinsics:: wrapping_add( self , rhs)
1122
1116
}
1123
1117
}
1124
1118
@@ -1141,13 +1135,7 @@ $EndFeature, "
1141
1135
without modifying the original"]
1142
1136
#[ inline]
1143
1137
pub const fn wrapping_sub( self , rhs: Self ) -> Self {
1144
- #[ cfg( bootstrap) ] {
1145
- intrinsics:: overflowing_sub( self , rhs)
1146
- }
1147
-
1148
- #[ cfg( not( bootstrap) ) ] {
1149
- intrinsics:: wrapping_sub( self , rhs)
1150
- }
1138
+ intrinsics:: wrapping_sub( self , rhs)
1151
1139
}
1152
1140
}
1153
1141
@@ -1169,13 +1157,7 @@ $EndFeature, "
1169
1157
without modifying the original"]
1170
1158
#[ inline]
1171
1159
pub const fn wrapping_mul( self , rhs: Self ) -> Self {
1172
- #[ cfg( bootstrap) ] {
1173
- intrinsics:: overflowing_mul( self , rhs)
1174
- }
1175
-
1176
- #[ cfg( not( bootstrap) ) ] {
1177
- intrinsics:: wrapping_mul( self , rhs)
1178
- }
1160
+ intrinsics:: wrapping_mul( self , rhs)
1179
1161
}
1180
1162
}
1181
1163
@@ -1402,7 +1384,16 @@ $EndFeature, "
1402
1384
#[ stable( feature = "no_panic_abs" , since = "1.13.0" ) ]
1403
1385
#[ inline]
1404
1386
pub const fn wrapping_abs( self ) -> Self {
1405
- ( self ^ ( self >> ( $BITS - 1 ) ) ) . wrapping_sub( self >> ( $BITS - 1 ) )
1387
+ // sign is -1 (all ones) for negative numbers, 0 otherwise.
1388
+ let sign = self >> ( $BITS - 1 ) ;
1389
+ // For positive self, sign == 0 so the expression is simply
1390
+ // (self ^ 0).wrapping_sub(0) == self == abs(self).
1391
+ //
1392
+ // For negative self, self ^ sign == self ^ all_ones.
1393
+ // But all_ones ^ self == all_ones - self == -1 - self.
1394
+ // So for negative numbers, (self ^ sign).wrapping_sub(sign) is
1395
+ // (-1 - self).wrapping_sub(-1) == -self == abs(self).
1396
+ ( self ^ sign) . wrapping_sub( sign)
1406
1397
}
1407
1398
}
1408
1399
@@ -1761,7 +1752,7 @@ $EndFeature, "
1761
1752
#[ stable( feature = "no_panic_abs" , since = "1.13.0" ) ]
1762
1753
#[ inline]
1763
1754
pub const fn overflowing_abs( self ) -> ( Self , bool ) {
1764
- ( self ^ ( self >> ( $BITS - 1 ) ) ) . overflowing_sub ( self >> ( $BITS - 1 ) )
1755
+ ( self . wrapping_abs ( ) , self == Self :: min_value ( ) )
1765
1756
}
1766
1757
}
1767
1758
@@ -1969,7 +1960,21 @@ $EndFeature, "
1969
1960
// Note that the #[inline] above means that the overflow
1970
1961
// semantics of the subtraction depend on the crate we're being
1971
1962
// inlined into.
1972
- ( self ^ ( self >> ( $BITS - 1 ) ) ) - ( self >> ( $BITS - 1 ) )
1963
+
1964
+ // sign is -1 (all ones) for negative numbers, 0 otherwise.
1965
+ let sign = self >> ( $BITS - 1 ) ;
1966
+ // For positive self, sign == 0 so the expression is simply
1967
+ // (self ^ 0) - 0 == self == abs(self).
1968
+ //
1969
+ // For negative self, self ^ sign == self ^ all_ones.
1970
+ // But all_ones ^ self == all_ones - self == -1 - self.
1971
+ // So for negative numbers, (self ^ sign) - sign is
1972
+ // (-1 - self) - -1 == -self == abs(self).
1973
+ //
1974
+ // The subtraction overflows when self is min_value(), because
1975
+ // (-1 - min_value()) - -1 is max_value() - -1 which overflows.
1976
+ // This is exactly when we want self.abs() to overflow.
1977
+ ( self ^ sign) - sign
1973
1978
}
1974
1979
}
1975
1980
@@ -3040,13 +3045,7 @@ $EndFeature, "
3040
3045
without modifying the original"]
3041
3046
#[ inline]
3042
3047
pub const fn wrapping_add( self , rhs: Self ) -> Self {
3043
- #[ cfg( bootstrap) ] {
3044
- intrinsics:: overflowing_add( self , rhs)
3045
- }
3046
-
3047
- #[ cfg( not( bootstrap) ) ] {
3048
- intrinsics:: wrapping_add( self , rhs)
3049
- }
3048
+ intrinsics:: wrapping_add( self , rhs)
3050
3049
}
3051
3050
}
3052
3051
@@ -3068,13 +3067,7 @@ $EndFeature, "
3068
3067
without modifying the original"]
3069
3068
#[ inline]
3070
3069
pub const fn wrapping_sub( self , rhs: Self ) -> Self {
3071
- #[ cfg( bootstrap) ] {
3072
- intrinsics:: overflowing_sub( self , rhs)
3073
- }
3074
-
3075
- #[ cfg( not( bootstrap) ) ] {
3076
- intrinsics:: wrapping_sub( self , rhs)
3077
- }
3070
+ intrinsics:: wrapping_sub( self , rhs)
3078
3071
}
3079
3072
}
3080
3073
@@ -3097,13 +3090,7 @@ $EndFeature, "
3097
3090
without modifying the original"]
3098
3091
#[ inline]
3099
3092
pub const fn wrapping_mul( self , rhs: Self ) -> Self {
3100
- #[ cfg( bootstrap) ] {
3101
- intrinsics:: overflowing_mul( self , rhs)
3102
- }
3103
-
3104
- #[ cfg( not( bootstrap) ) ] {
3105
- intrinsics:: wrapping_mul( self , rhs)
3106
- }
3093
+ intrinsics:: wrapping_mul( self , rhs)
3107
3094
}
3108
3095
3109
3096
doc_comment! {
0 commit comments