]>
Commit | Line | Data |
---|---|---|
c34b1796 AL |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | #![allow(missing_docs)] | |
e9174d1e SL |
12 | #![unstable(feature = "wrapping", reason = "may be removed or relocated", |
13 | issue = "27755")] | |
c34b1796 | 14 | |
92a42be0 SL |
15 | #[cfg(stage0)] |
16 | pub use intrinsics::{ | |
17 | u8_add_with_overflow, i8_add_with_overflow, | |
18 | u16_add_with_overflow, i16_add_with_overflow, | |
19 | u32_add_with_overflow, i32_add_with_overflow, | |
20 | u64_add_with_overflow, i64_add_with_overflow, | |
21 | ||
22 | u8_sub_with_overflow, i8_sub_with_overflow, | |
23 | u16_sub_with_overflow, i16_sub_with_overflow, | |
24 | u32_sub_with_overflow, i32_sub_with_overflow, | |
25 | u64_sub_with_overflow, i64_sub_with_overflow, | |
26 | ||
27 | u8_mul_with_overflow, i8_mul_with_overflow, | |
28 | u16_mul_with_overflow, i16_mul_with_overflow, | |
29 | u32_mul_with_overflow, i32_mul_with_overflow, | |
30 | u64_mul_with_overflow, i64_mul_with_overflow, | |
31 | }; | |
32 | ||
33 | #[cfg(not(stage0))] | |
34 | pub use intrinsics::{ | |
35 | add_with_overflow, | |
36 | sub_with_overflow, | |
37 | mul_with_overflow, | |
38 | }; | |
39 | ||
c34b1796 AL |
40 | use super::Wrapping; |
41 | ||
42 | use ops::*; | |
43 | ||
c34b1796 AL |
44 | use ::{i8,i16,i32,i64}; |
45 | ||
c34b1796 AL |
46 | pub trait OverflowingOps { |
47 | fn overflowing_add(self, rhs: Self) -> (Self, bool); | |
48 | fn overflowing_sub(self, rhs: Self) -> (Self, bool); | |
49 | fn overflowing_mul(self, rhs: Self) -> (Self, bool); | |
50 | ||
51 | fn overflowing_div(self, rhs: Self) -> (Self, bool); | |
52 | fn overflowing_rem(self, rhs: Self) -> (Self, bool); | |
d9579d0f | 53 | fn overflowing_neg(self) -> (Self, bool); |
c34b1796 AL |
54 | |
55 | fn overflowing_shl(self, rhs: u32) -> (Self, bool); | |
56 | fn overflowing_shr(self, rhs: u32) -> (Self, bool); | |
57 | } | |
58 | ||
59 | macro_rules! sh_impl { | |
60 | ($t:ty, $f:ty) => ( | |
61 | #[stable(feature = "rust1", since = "1.0.0")] | |
62 | impl Shl<$f> for Wrapping<$t> { | |
63 | type Output = Wrapping<$t>; | |
64 | ||
65 | #[inline(always)] | |
66 | fn shl(self, other: $f) -> Wrapping<$t> { | |
67 | Wrapping(self.0 << other) | |
68 | } | |
69 | } | |
70 | ||
71 | #[stable(feature = "rust1", since = "1.0.0")] | |
72 | impl Shr<$f> for Wrapping<$t> { | |
73 | type Output = Wrapping<$t>; | |
74 | ||
75 | #[inline(always)] | |
76 | fn shr(self, other: $f) -> Wrapping<$t> { | |
77 | Wrapping(self.0 >> other) | |
78 | } | |
79 | } | |
80 | ) | |
81 | } | |
82 | ||
83 | // FIXME (#23545): uncomment the remaining impls | |
84 | macro_rules! sh_impl_all { | |
85 | ($($t:ty)*) => ($( | |
86 | // sh_impl! { $t, u8 } | |
87 | // sh_impl! { $t, u16 } | |
88 | // sh_impl! { $t, u32 } | |
89 | // sh_impl! { $t, u64 } | |
90 | sh_impl! { $t, usize } | |
91 | ||
92 | // sh_impl! { $t, i8 } | |
93 | // sh_impl! { $t, i16 } | |
94 | // sh_impl! { $t, i32 } | |
95 | // sh_impl! { $t, i64 } | |
96 | // sh_impl! { $t, isize } | |
97 | )*) | |
98 | } | |
99 | ||
100 | sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } | |
101 | ||
102 | macro_rules! wrapping_impl { | |
103 | ($($t:ty)*) => ($( | |
c34b1796 AL |
104 | #[stable(feature = "rust1", since = "1.0.0")] |
105 | impl Add for Wrapping<$t> { | |
106 | type Output = Wrapping<$t>; | |
107 | ||
108 | #[inline(always)] | |
109 | fn add(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
110 | Wrapping(self.0.wrapping_add(other.0)) | |
111 | } | |
112 | } | |
113 | ||
114 | #[stable(feature = "rust1", since = "1.0.0")] | |
115 | impl Sub for Wrapping<$t> { | |
116 | type Output = Wrapping<$t>; | |
117 | ||
118 | #[inline(always)] | |
119 | fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
120 | Wrapping(self.0.wrapping_sub(other.0)) | |
121 | } | |
122 | } | |
123 | ||
124 | #[stable(feature = "rust1", since = "1.0.0")] | |
125 | impl Mul for Wrapping<$t> { | |
126 | type Output = Wrapping<$t>; | |
127 | ||
128 | #[inline(always)] | |
129 | fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
130 | Wrapping(self.0.wrapping_mul(other.0)) | |
131 | } | |
132 | } | |
133 | ||
c1a9b12d SL |
134 | #[stable(feature = "wrapping_div", since = "1.3.0")] |
135 | impl Div for Wrapping<$t> { | |
136 | type Output = Wrapping<$t>; | |
137 | ||
138 | #[inline(always)] | |
139 | fn div(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
140 | Wrapping(self.0.wrapping_div(other.0)) | |
141 | } | |
142 | } | |
143 | ||
c34b1796 AL |
144 | #[stable(feature = "rust1", since = "1.0.0")] |
145 | impl Not for Wrapping<$t> { | |
146 | type Output = Wrapping<$t>; | |
147 | ||
d9579d0f | 148 | #[inline(always)] |
c34b1796 AL |
149 | fn not(self) -> Wrapping<$t> { |
150 | Wrapping(!self.0) | |
151 | } | |
152 | } | |
153 | ||
154 | #[stable(feature = "rust1", since = "1.0.0")] | |
155 | impl BitXor for Wrapping<$t> { | |
156 | type Output = Wrapping<$t>; | |
157 | ||
158 | #[inline(always)] | |
159 | fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
160 | Wrapping(self.0 ^ other.0) | |
161 | } | |
162 | } | |
163 | ||
164 | #[stable(feature = "rust1", since = "1.0.0")] | |
165 | impl BitOr for Wrapping<$t> { | |
166 | type Output = Wrapping<$t>; | |
167 | ||
168 | #[inline(always)] | |
169 | fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
170 | Wrapping(self.0 | other.0) | |
171 | } | |
172 | } | |
173 | ||
174 | #[stable(feature = "rust1", since = "1.0.0")] | |
175 | impl BitAnd for Wrapping<$t> { | |
176 | type Output = Wrapping<$t>; | |
177 | ||
178 | #[inline(always)] | |
179 | fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> { | |
180 | Wrapping(self.0 & other.0) | |
181 | } | |
182 | } | |
183 | )*) | |
184 | } | |
185 | ||
186 | wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 } | |
187 | ||
188 | mod shift_max { | |
189 | #![allow(non_upper_case_globals)] | |
190 | ||
191 | pub const i8: u32 = (1 << 3) - 1; | |
192 | pub const i16: u32 = (1 << 4) - 1; | |
193 | pub const i32: u32 = (1 << 5) - 1; | |
194 | pub const i64: u32 = (1 << 6) - 1; | |
195 | ||
196 | pub const u8: u32 = i8; | |
197 | pub const u16: u32 = i16; | |
198 | pub const u32: u32 = i32; | |
199 | pub const u64: u32 = i64; | |
200 | } | |
201 | ||
202 | macro_rules! signed_overflowing_impl { | |
203 | ($($t:ident)*) => ($( | |
204 | impl OverflowingOps for $t { | |
205 | #[inline(always)] | |
92a42be0 | 206 | #[cfg(stage0)] |
c34b1796 AL |
207 | fn overflowing_add(self, rhs: $t) -> ($t, bool) { |
208 | unsafe { | |
209 | concat_idents!($t, _add_with_overflow)(self, rhs) | |
210 | } | |
211 | } | |
212 | #[inline(always)] | |
92a42be0 SL |
213 | #[cfg(not(stage0))] |
214 | fn overflowing_add(self, rhs: $t) -> ($t, bool) { | |
215 | unsafe { | |
216 | add_with_overflow(self, rhs) | |
217 | } | |
218 | } | |
219 | #[inline(always)] | |
220 | #[cfg(stage0)] | |
c34b1796 AL |
221 | fn overflowing_sub(self, rhs: $t) -> ($t, bool) { |
222 | unsafe { | |
223 | concat_idents!($t, _sub_with_overflow)(self, rhs) | |
224 | } | |
225 | } | |
226 | #[inline(always)] | |
92a42be0 SL |
227 | #[cfg(not(stage0))] |
228 | fn overflowing_sub(self, rhs: $t) -> ($t, bool) { | |
229 | unsafe { | |
230 | sub_with_overflow(self, rhs) | |
231 | } | |
232 | } | |
233 | #[inline(always)] | |
234 | #[cfg(stage0)] | |
c34b1796 AL |
235 | fn overflowing_mul(self, rhs: $t) -> ($t, bool) { |
236 | unsafe { | |
237 | concat_idents!($t, _mul_with_overflow)(self, rhs) | |
238 | } | |
239 | } | |
92a42be0 SL |
240 | #[inline(always)] |
241 | #[cfg(not(stage0))] | |
242 | fn overflowing_mul(self, rhs: $t) -> ($t, bool) { | |
243 | unsafe { | |
244 | mul_with_overflow(self, rhs) | |
245 | } | |
246 | } | |
c34b1796 AL |
247 | |
248 | #[inline(always)] | |
249 | fn overflowing_div(self, rhs: $t) -> ($t, bool) { | |
250 | if self == $t::MIN && rhs == -1 { | |
d9579d0f | 251 | (self, true) |
c34b1796 AL |
252 | } else { |
253 | (self/rhs, false) | |
254 | } | |
255 | } | |
256 | #[inline(always)] | |
257 | fn overflowing_rem(self, rhs: $t) -> ($t, bool) { | |
258 | if self == $t::MIN && rhs == -1 { | |
259 | (0, true) | |
260 | } else { | |
261 | (self % rhs, false) | |
262 | } | |
263 | } | |
264 | ||
265 | #[inline(always)] | |
266 | fn overflowing_shl(self, rhs: u32) -> ($t, bool) { | |
267 | (self << (rhs & self::shift_max::$t), | |
268 | (rhs > self::shift_max::$t)) | |
269 | } | |
270 | #[inline(always)] | |
271 | fn overflowing_shr(self, rhs: u32) -> ($t, bool) { | |
272 | (self >> (rhs & self::shift_max::$t), | |
273 | (rhs > self::shift_max::$t)) | |
274 | } | |
d9579d0f AL |
275 | |
276 | #[inline(always)] | |
277 | fn overflowing_neg(self) -> ($t, bool) { | |
278 | if self == $t::MIN { | |
279 | ($t::MIN, true) | |
280 | } else { | |
281 | (-self, false) | |
282 | } | |
283 | } | |
c34b1796 AL |
284 | } |
285 | )*) | |
286 | } | |
287 | ||
288 | macro_rules! unsigned_overflowing_impl { | |
289 | ($($t:ident)*) => ($( | |
290 | impl OverflowingOps for $t { | |
291 | #[inline(always)] | |
92a42be0 | 292 | #[cfg(stage0)] |
c34b1796 AL |
293 | fn overflowing_add(self, rhs: $t) -> ($t, bool) { |
294 | unsafe { | |
295 | concat_idents!($t, _add_with_overflow)(self, rhs) | |
296 | } | |
297 | } | |
298 | #[inline(always)] | |
92a42be0 SL |
299 | #[cfg(not(stage0))] |
300 | fn overflowing_add(self, rhs: $t) -> ($t, bool) { | |
301 | unsafe { | |
302 | add_with_overflow(self, rhs) | |
303 | } | |
304 | } | |
305 | #[inline(always)] | |
306 | #[cfg(stage0)] | |
c34b1796 AL |
307 | fn overflowing_sub(self, rhs: $t) -> ($t, bool) { |
308 | unsafe { | |
309 | concat_idents!($t, _sub_with_overflow)(self, rhs) | |
310 | } | |
311 | } | |
312 | #[inline(always)] | |
92a42be0 SL |
313 | #[cfg(not(stage0))] |
314 | fn overflowing_sub(self, rhs: $t) -> ($t, bool) { | |
315 | unsafe { | |
316 | sub_with_overflow(self, rhs) | |
317 | } | |
318 | } | |
319 | #[inline(always)] | |
320 | #[cfg(stage0)] | |
c34b1796 AL |
321 | fn overflowing_mul(self, rhs: $t) -> ($t, bool) { |
322 | unsafe { | |
323 | concat_idents!($t, _mul_with_overflow)(self, rhs) | |
324 | } | |
325 | } | |
92a42be0 SL |
326 | #[inline(always)] |
327 | #[cfg(not(stage0))] | |
328 | fn overflowing_mul(self, rhs: $t) -> ($t, bool) { | |
329 | unsafe { | |
330 | mul_with_overflow(self, rhs) | |
331 | } | |
332 | } | |
c34b1796 AL |
333 | |
334 | #[inline(always)] | |
335 | fn overflowing_div(self, rhs: $t) -> ($t, bool) { | |
336 | (self/rhs, false) | |
337 | } | |
338 | #[inline(always)] | |
339 | fn overflowing_rem(self, rhs: $t) -> ($t, bool) { | |
340 | (self % rhs, false) | |
341 | } | |
342 | ||
343 | #[inline(always)] | |
344 | fn overflowing_shl(self, rhs: u32) -> ($t, bool) { | |
345 | (self << (rhs & self::shift_max::$t), | |
346 | (rhs > self::shift_max::$t)) | |
347 | } | |
348 | #[inline(always)] | |
349 | fn overflowing_shr(self, rhs: u32) -> ($t, bool) { | |
350 | (self >> (rhs & self::shift_max::$t), | |
351 | (rhs > self::shift_max::$t)) | |
352 | } | |
d9579d0f AL |
353 | |
354 | #[inline(always)] | |
355 | fn overflowing_neg(self) -> ($t, bool) { | |
356 | ((!self).wrapping_add(1), true) | |
357 | } | |
c34b1796 AL |
358 | } |
359 | )*) | |
360 | } | |
361 | ||
362 | signed_overflowing_impl! { i8 i16 i32 i64 } | |
363 | unsigned_overflowing_impl! { u8 u16 u32 u64 } | |
364 | ||
365 | #[cfg(target_pointer_width = "64")] | |
366 | impl OverflowingOps for usize { | |
367 | #[inline(always)] | |
92a42be0 | 368 | #[cfg(stage0)] |
c34b1796 AL |
369 | fn overflowing_add(self, rhs: usize) -> (usize, bool) { |
370 | unsafe { | |
371 | let res = u64_add_with_overflow(self as u64, rhs as u64); | |
372 | (res.0 as usize, res.1) | |
373 | } | |
374 | } | |
375 | #[inline(always)] | |
92a42be0 SL |
376 | #[cfg(not(stage0))] |
377 | fn overflowing_add(self, rhs: usize) -> (usize, bool) { | |
378 | unsafe { | |
379 | add_with_overflow(self, rhs) | |
380 | } | |
381 | } | |
382 | #[inline(always)] | |
383 | #[cfg(stage0)] | |
c34b1796 AL |
384 | fn overflowing_sub(self, rhs: usize) -> (usize, bool) { |
385 | unsafe { | |
386 | let res = u64_sub_with_overflow(self as u64, rhs as u64); | |
387 | (res.0 as usize, res.1) | |
388 | } | |
389 | } | |
390 | #[inline(always)] | |
92a42be0 SL |
391 | #[cfg(not(stage0))] |
392 | fn overflowing_sub(self, rhs: usize) -> (usize, bool) { | |
393 | unsafe { | |
394 | sub_with_overflow(self, rhs) | |
395 | } | |
396 | } | |
397 | #[inline(always)] | |
398 | #[cfg(stage0)] | |
c34b1796 AL |
399 | fn overflowing_mul(self, rhs: usize) -> (usize, bool) { |
400 | unsafe { | |
401 | let res = u64_mul_with_overflow(self as u64, rhs as u64); | |
402 | (res.0 as usize, res.1) | |
403 | } | |
404 | } | |
405 | #[inline(always)] | |
92a42be0 SL |
406 | #[cfg(not(stage0))] |
407 | fn overflowing_mul(self, rhs: usize) -> (usize, bool) { | |
408 | unsafe { | |
409 | mul_with_overflow(self, rhs) | |
410 | } | |
411 | } | |
412 | #[inline(always)] | |
c34b1796 AL |
413 | fn overflowing_div(self, rhs: usize) -> (usize, bool) { |
414 | let (r, f) = (self as u64).overflowing_div(rhs as u64); | |
415 | (r as usize, f) | |
416 | } | |
417 | #[inline(always)] | |
418 | fn overflowing_rem(self, rhs: usize) -> (usize, bool) { | |
419 | let (r, f) = (self as u64).overflowing_rem(rhs as u64); | |
420 | (r as usize, f) | |
421 | } | |
422 | #[inline(always)] | |
d9579d0f AL |
423 | fn overflowing_neg(self) -> (usize, bool) { |
424 | let (r, f) = (self as u64).overflowing_neg(); | |
425 | (r as usize, f) | |
426 | } | |
427 | #[inline(always)] | |
c34b1796 AL |
428 | fn overflowing_shl(self, rhs: u32) -> (usize, bool) { |
429 | let (r, f) = (self as u64).overflowing_shl(rhs); | |
430 | (r as usize, f) | |
431 | } | |
432 | #[inline(always)] | |
433 | fn overflowing_shr(self, rhs: u32) -> (usize, bool) { | |
434 | let (r, f) = (self as u64).overflowing_shr(rhs); | |
435 | (r as usize, f) | |
436 | } | |
437 | } | |
438 | ||
439 | #[cfg(target_pointer_width = "32")] | |
440 | impl OverflowingOps for usize { | |
441 | #[inline(always)] | |
92a42be0 | 442 | #[cfg(stage0)] |
c34b1796 AL |
443 | fn overflowing_add(self, rhs: usize) -> (usize, bool) { |
444 | unsafe { | |
445 | let res = u32_add_with_overflow(self as u32, rhs as u32); | |
446 | (res.0 as usize, res.1) | |
447 | } | |
448 | } | |
449 | #[inline(always)] | |
92a42be0 SL |
450 | #[cfg(not(stage0))] |
451 | fn overflowing_add(self, rhs: usize) -> (usize, bool) { | |
452 | unsafe { | |
453 | add_with_overflow(self, rhs) | |
454 | } | |
455 | } | |
456 | #[inline(always)] | |
457 | #[cfg(stage0)] | |
c34b1796 AL |
458 | fn overflowing_sub(self, rhs: usize) -> (usize, bool) { |
459 | unsafe { | |
460 | let res = u32_sub_with_overflow(self as u32, rhs as u32); | |
461 | (res.0 as usize, res.1) | |
462 | } | |
463 | } | |
464 | #[inline(always)] | |
92a42be0 SL |
465 | #[cfg(not(stage0))] |
466 | fn overflowing_sub(self, rhs: usize) -> (usize, bool) { | |
467 | unsafe { | |
468 | sub_with_overflow(self, rhs) | |
469 | } | |
470 | } | |
471 | #[inline(always)] | |
472 | #[cfg(stage0)] | |
c34b1796 AL |
473 | fn overflowing_mul(self, rhs: usize) -> (usize, bool) { |
474 | unsafe { | |
475 | let res = u32_mul_with_overflow(self as u32, rhs as u32); | |
476 | (res.0 as usize, res.1) | |
477 | } | |
478 | } | |
479 | #[inline(always)] | |
92a42be0 SL |
480 | #[cfg(not(stage0))] |
481 | fn overflowing_mul(self, rhs: usize) -> (usize, bool) { | |
482 | unsafe { | |
483 | mul_with_overflow(self, rhs) | |
484 | } | |
485 | } | |
486 | #[inline(always)] | |
c34b1796 AL |
487 | fn overflowing_div(self, rhs: usize) -> (usize, bool) { |
488 | let (r, f) = (self as u32).overflowing_div(rhs as u32); | |
489 | (r as usize, f) | |
490 | } | |
491 | #[inline(always)] | |
492 | fn overflowing_rem(self, rhs: usize) -> (usize, bool) { | |
493 | let (r, f) = (self as u32).overflowing_rem(rhs as u32); | |
494 | (r as usize, f) | |
495 | } | |
496 | #[inline(always)] | |
d9579d0f AL |
497 | fn overflowing_neg(self) -> (usize, bool) { |
498 | let (r, f) = (self as u32).overflowing_neg(); | |
499 | (r as usize, f) | |
500 | } | |
501 | #[inline(always)] | |
c34b1796 AL |
502 | fn overflowing_shl(self, rhs: u32) -> (usize, bool) { |
503 | let (r, f) = (self as u32).overflowing_shl(rhs); | |
504 | (r as usize, f) | |
505 | } | |
506 | #[inline(always)] | |
507 | fn overflowing_shr(self, rhs: u32) -> (usize, bool) { | |
508 | let (r, f) = (self as u32).overflowing_shr(rhs); | |
509 | (r as usize, f) | |
510 | } | |
511 | } | |
512 | ||
513 | #[cfg(target_pointer_width = "64")] | |
514 | impl OverflowingOps for isize { | |
515 | #[inline(always)] | |
92a42be0 | 516 | #[cfg(stage0)] |
c34b1796 AL |
517 | fn overflowing_add(self, rhs: isize) -> (isize, bool) { |
518 | unsafe { | |
519 | let res = i64_add_with_overflow(self as i64, rhs as i64); | |
520 | (res.0 as isize, res.1) | |
521 | } | |
522 | } | |
523 | #[inline(always)] | |
92a42be0 SL |
524 | #[cfg(not(stage0))] |
525 | fn overflowing_add(self, rhs: isize) -> (isize, bool) { | |
526 | unsafe { | |
527 | add_with_overflow(self, rhs) | |
528 | } | |
529 | } | |
530 | #[inline(always)] | |
531 | #[cfg(stage0)] | |
c34b1796 AL |
532 | fn overflowing_sub(self, rhs: isize) -> (isize, bool) { |
533 | unsafe { | |
534 | let res = i64_sub_with_overflow(self as i64, rhs as i64); | |
535 | (res.0 as isize, res.1) | |
536 | } | |
537 | } | |
538 | #[inline(always)] | |
92a42be0 SL |
539 | #[cfg(not(stage0))] |
540 | fn overflowing_sub(self, rhs: isize) -> (isize, bool) { | |
541 | unsafe { | |
542 | sub_with_overflow(self, rhs) | |
543 | } | |
544 | } | |
545 | #[inline(always)] | |
546 | #[cfg(stage0)] | |
c34b1796 AL |
547 | fn overflowing_mul(self, rhs: isize) -> (isize, bool) { |
548 | unsafe { | |
549 | let res = i64_mul_with_overflow(self as i64, rhs as i64); | |
550 | (res.0 as isize, res.1) | |
551 | } | |
552 | } | |
553 | #[inline(always)] | |
92a42be0 SL |
554 | #[cfg(not(stage0))] |
555 | fn overflowing_mul(self, rhs: isize) -> (isize, bool) { | |
556 | unsafe { | |
557 | mul_with_overflow(self, rhs) | |
558 | } | |
559 | } | |
560 | #[inline(always)] | |
c34b1796 AL |
561 | fn overflowing_div(self, rhs: isize) -> (isize, bool) { |
562 | let (r, f) = (self as i64).overflowing_div(rhs as i64); | |
563 | (r as isize, f) | |
564 | } | |
565 | #[inline(always)] | |
566 | fn overflowing_rem(self, rhs: isize) -> (isize, bool) { | |
567 | let (r, f) = (self as i64).overflowing_rem(rhs as i64); | |
568 | (r as isize, f) | |
569 | } | |
570 | #[inline(always)] | |
d9579d0f AL |
571 | fn overflowing_neg(self) -> (isize, bool) { |
572 | let (r, f) = (self as i64).overflowing_neg(); | |
573 | (r as isize, f) | |
574 | } | |
575 | #[inline(always)] | |
c34b1796 AL |
576 | fn overflowing_shl(self, rhs: u32) -> (isize, bool) { |
577 | let (r, f) = (self as i64).overflowing_shl(rhs); | |
578 | (r as isize, f) | |
579 | } | |
580 | #[inline(always)] | |
581 | fn overflowing_shr(self, rhs: u32) -> (isize, bool) { | |
582 | let (r, f) = (self as i64).overflowing_shr(rhs); | |
583 | (r as isize, f) | |
584 | } | |
585 | } | |
586 | ||
587 | #[cfg(target_pointer_width = "32")] | |
588 | impl OverflowingOps for isize { | |
589 | #[inline(always)] | |
92a42be0 | 590 | #[cfg(stage0)] |
c34b1796 AL |
591 | fn overflowing_add(self, rhs: isize) -> (isize, bool) { |
592 | unsafe { | |
593 | let res = i32_add_with_overflow(self as i32, rhs as i32); | |
594 | (res.0 as isize, res.1) | |
595 | } | |
596 | } | |
597 | #[inline(always)] | |
92a42be0 SL |
598 | #[cfg(not(stage0))] |
599 | fn overflowing_add(self, rhs: isize) -> (isize, bool) { | |
600 | unsafe { | |
601 | add_with_overflow(self, rhs) | |
602 | } | |
603 | } | |
604 | #[inline(always)] | |
605 | #[cfg(stage0)] | |
c34b1796 AL |
606 | fn overflowing_sub(self, rhs: isize) -> (isize, bool) { |
607 | unsafe { | |
608 | let res = i32_sub_with_overflow(self as i32, rhs as i32); | |
609 | (res.0 as isize, res.1) | |
610 | } | |
611 | } | |
612 | #[inline(always)] | |
92a42be0 SL |
613 | #[cfg(not(stage0))] |
614 | fn overflowing_sub(self, rhs: isize) -> (isize, bool) { | |
615 | unsafe { | |
616 | sub_with_overflow(self, rhs) | |
617 | } | |
618 | } | |
619 | #[inline(always)] | |
620 | #[cfg(stage0)] | |
c34b1796 AL |
621 | fn overflowing_mul(self, rhs: isize) -> (isize, bool) { |
622 | unsafe { | |
623 | let res = i32_mul_with_overflow(self as i32, rhs as i32); | |
624 | (res.0 as isize, res.1) | |
625 | } | |
626 | } | |
627 | #[inline(always)] | |
92a42be0 SL |
628 | #[cfg(not(stage0))] |
629 | fn overflowing_mul(self, rhs: isize) -> (isize, bool) { | |
630 | unsafe { | |
631 | mul_with_overflow(self, rhs) | |
632 | } | |
633 | } | |
634 | #[inline(always)] | |
c34b1796 AL |
635 | fn overflowing_div(self, rhs: isize) -> (isize, bool) { |
636 | let (r, f) = (self as i32).overflowing_div(rhs as i32); | |
637 | (r as isize, f) | |
638 | } | |
639 | #[inline(always)] | |
640 | fn overflowing_rem(self, rhs: isize) -> (isize, bool) { | |
641 | let (r, f) = (self as i32).overflowing_rem(rhs as i32); | |
642 | (r as isize, f) | |
643 | } | |
644 | #[inline(always)] | |
d9579d0f AL |
645 | fn overflowing_neg(self) -> (isize, bool) { |
646 | let (r, f) = (self as i32).overflowing_neg(); | |
647 | (r as isize, f) | |
648 | } | |
649 | #[inline(always)] | |
c34b1796 AL |
650 | fn overflowing_shl(self, rhs: u32) -> (isize, bool) { |
651 | let (r, f) = (self as i32).overflowing_shl(rhs); | |
652 | (r as isize, f) | |
653 | } | |
654 | #[inline(always)] | |
655 | fn overflowing_shr(self, rhs: u32) -> (isize, bool) { | |
656 | let (r, f) = (self as i32).overflowing_shr(rhs); | |
657 | (r as isize, f) | |
658 | } | |
659 | } |