@@ -14,12 +14,32 @@ use ops::*;
14
14
#[ cfg( not( stage0) ) ]
15
15
use intrinsics:: { overflowing_add, overflowing_sub, overflowing_mul} ;
16
16
17
+ use intrinsics:: { i8_add_with_overflow, u8_add_with_overflow} ;
18
+ use intrinsics:: { i16_add_with_overflow, u16_add_with_overflow} ;
19
+ use intrinsics:: { i32_add_with_overflow, u32_add_with_overflow} ;
20
+ use intrinsics:: { i64_add_with_overflow, u64_add_with_overflow} ;
21
+ use intrinsics:: { i8_sub_with_overflow, u8_sub_with_overflow} ;
22
+ use intrinsics:: { i16_sub_with_overflow, u16_sub_with_overflow} ;
23
+ use intrinsics:: { i32_sub_with_overflow, u32_sub_with_overflow} ;
24
+ use intrinsics:: { i64_sub_with_overflow, u64_sub_with_overflow} ;
25
+ use intrinsics:: { i8_mul_with_overflow, u8_mul_with_overflow} ;
26
+ use intrinsics:: { i16_mul_with_overflow, u16_mul_with_overflow} ;
27
+ use intrinsics:: { i32_mul_with_overflow, u32_mul_with_overflow} ;
28
+ use intrinsics:: { i64_mul_with_overflow, u64_mul_with_overflow} ;
29
+
17
30
pub trait WrappingOps {
18
31
fn wrapping_add ( self , rhs : Self ) -> Self ;
19
32
fn wrapping_sub ( self , rhs : Self ) -> Self ;
20
33
fn wrapping_mul ( self , rhs : Self ) -> Self ;
21
34
}
22
35
36
+ #[ unstable( feature = "core" , reason = "may be removed, renamed, or relocated" ) ]
37
+ pub trait OverflowingOps {
38
+ fn overflowing_add ( self , rhs : Self ) -> ( Self , bool ) ;
39
+ fn overflowing_sub ( self , rhs : Self ) -> ( Self , bool ) ;
40
+ fn overflowing_mul ( self , rhs : Self ) -> ( Self , bool ) ;
41
+ }
42
+
23
43
#[ cfg( not( stage0) ) ]
24
44
macro_rules! wrapping_impl {
25
45
( $( $t: ty) * ) => ( $(
@@ -151,3 +171,130 @@ impl<T:WrappingOps+Shr<uint,Output=T>> Shr<uint> for Wrapping<T> {
151
171
Wrapping ( self . 0 >> other)
152
172
}
153
173
}
174
+
175
+ macro_rules! overflowing_impl {
176
+ ( $( $t: ident) * ) => ( $(
177
+ impl OverflowingOps for $t {
178
+ #[ inline( always) ]
179
+ fn overflowing_add( self , rhs: $t) -> ( $t, bool ) {
180
+ unsafe {
181
+ concat_idents!( $t, _add_with_overflow) ( self , rhs)
182
+ }
183
+ }
184
+ #[ inline( always) ]
185
+ fn overflowing_sub( self , rhs: $t) -> ( $t, bool ) {
186
+ unsafe {
187
+ concat_idents!( $t, _sub_with_overflow) ( self , rhs)
188
+ }
189
+ }
190
+ #[ inline( always) ]
191
+ fn overflowing_mul( self , rhs: $t) -> ( $t, bool ) {
192
+ unsafe {
193
+ concat_idents!( $t, _mul_with_overflow) ( self , rhs)
194
+ }
195
+ }
196
+ }
197
+ ) * )
198
+ }
199
+
200
+ overflowing_impl ! { u8 u16 u32 u64 i8 i16 i32 i64 }
201
+
202
+ #[ cfg( target_pointer_width = "64" ) ]
203
+ impl OverflowingOps for usize {
204
+ #[ inline( always) ]
205
+ fn overflowing_add ( self , rhs : usize ) -> ( usize , bool ) {
206
+ unsafe {
207
+ let res = u64_add_with_overflow ( self as u64 , rhs as u64 ) ;
208
+ ( res. 0 as usize , res. 1 )
209
+ }
210
+ }
211
+ #[ inline( always) ]
212
+ fn overflowing_sub ( self , rhs : usize ) -> ( usize , bool ) {
213
+ unsafe {
214
+ let res = u64_sub_with_overflow ( self as u64 , rhs as u64 ) ;
215
+ ( res. 0 as usize , res. 1 )
216
+ }
217
+ }
218
+ #[ inline( always) ]
219
+ fn overflowing_mul ( self , rhs : usize ) -> ( usize , bool ) {
220
+ unsafe {
221
+ let res = u64_mul_with_overflow ( self as u64 , rhs as u64 ) ;
222
+ ( res. 0 as usize , res. 1 )
223
+ }
224
+ }
225
+ }
226
+
227
+ #[ cfg( target_pointer_width = "32" ) ]
228
+ impl OverflowingOps for usize {
229
+ #[ inline( always) ]
230
+ fn overflowing_add ( self , rhs : usize ) -> ( usize , bool ) {
231
+ unsafe {
232
+ let res = u32_add_with_overflow ( self as u32 , rhs as u32 ) ;
233
+ ( res. 0 as usize , res. 1 )
234
+ }
235
+ }
236
+ #[ inline( always) ]
237
+ fn overflowing_sub ( self , rhs : usize ) -> ( usize , bool ) {
238
+ unsafe {
239
+ let res = u32_sub_with_overflow ( self as u32 , rhs as u32 ) ;
240
+ ( res. 0 as usize , res. 1 )
241
+ }
242
+ }
243
+ #[ inline( always) ]
244
+ fn overflowing_mul ( self , rhs : usize ) -> ( usize , bool ) {
245
+ unsafe {
246
+ let res = u32_mul_with_overflow ( self as u32 , rhs as u32 ) ;
247
+ ( res. 0 as usize , res. 1 )
248
+ }
249
+ }
250
+ }
251
+
252
+ #[ cfg( target_pointer_width = "64" ) ]
253
+ impl OverflowingOps for isize {
254
+ #[ inline( always) ]
255
+ fn overflowing_add ( self , rhs : isize ) -> ( isize , bool ) {
256
+ unsafe {
257
+ let res = i64_add_with_overflow ( self as i64 , rhs as i64 ) ;
258
+ ( res. 0 as isize , res. 1 )
259
+ }
260
+ }
261
+ #[ inline( always) ]
262
+ fn overflowing_sub ( self , rhs : isize ) -> ( isize , bool ) {
263
+ unsafe {
264
+ let res = i64_sub_with_overflow ( self as i64 , rhs as i64 ) ;
265
+ ( res. 0 as isize , res. 1 )
266
+ }
267
+ }
268
+ #[ inline( always) ]
269
+ fn overflowing_mul ( self , rhs : isize ) -> ( isize , bool ) {
270
+ unsafe {
271
+ let res = i64_mul_with_overflow ( self as i64 , rhs as i64 ) ;
272
+ ( res. 0 as isize , res. 1 )
273
+ }
274
+ }
275
+ }
276
+
277
+ #[ cfg( target_pointer_width = "32" ) ]
278
+ impl OverflowingOps for isize {
279
+ #[ inline( always) ]
280
+ fn overflowing_add ( self , rhs : isize ) -> ( isize , bool ) {
281
+ unsafe {
282
+ let res = i32_add_with_overflow ( self as i32 , rhs as i32 ) ;
283
+ ( res. 0 as isize , res. 1 )
284
+ }
285
+ }
286
+ #[ inline( always) ]
287
+ fn overflowing_sub ( self , rhs : isize ) -> ( isize , bool ) {
288
+ unsafe {
289
+ let res = i32_sub_with_overflow ( self as i32 , rhs as i32 ) ;
290
+ ( res. 0 as isize , res. 1 )
291
+ }
292
+ }
293
+ #[ inline( always) ]
294
+ fn overflowing_mul ( self , rhs : isize ) -> ( isize , bool ) {
295
+ unsafe {
296
+ let res = i32_mul_with_overflow ( self as i32 , rhs as i32 ) ;
297
+ ( res. 0 as isize , res. 1 )
298
+ }
299
+ }
300
+ }
0 commit comments