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.
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.
11 #![allow(missing_docs)]
12 #![unstable(feature = "old_wrapping", reason = "may be removed or relocated",
15 use intrinsics::{add_with_overflow, sub_with_overflow, mul_with_overflow};
21 use ::{i8, i16, i32, i64, isize};
23 #[unstable(feature = "old_wrapping", reason = "may be removed or relocated",
25 #[rustc_deprecated(since = "1.7.0", reason = "moved to inherent methods")]
26 pub trait OverflowingOps {
27 fn overflowing_add(self, rhs: Self) -> (Self, bool);
28 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
29 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
31 fn overflowing_div(self, rhs: Self) -> (Self, bool);
32 fn overflowing_rem(self, rhs: Self) -> (Self, bool);
33 fn overflowing_neg(self) -> (Self, bool);
35 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
36 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
39 macro_rules! sh_impl_signed {
40 ($t:ident, $f:ident) => (
41 #[stable(feature = "rust1", since = "1.0.0")]
42 impl Shl<$f> for Wrapping<$t> {
43 type Output = Wrapping<$t>;
46 fn shl(self, other: $f) -> Wrapping<$t> {
48 Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
50 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
55 #[stable(feature = "wrapping_impls", since = "1.7.0")]
56 impl ShlAssign<$f> for Wrapping<$t> {
58 fn shl_assign(&mut self, other: $f) {
59 *self = *self << other;
63 #[stable(feature = "rust1", since = "1.0.0")]
64 impl Shr<$f> for Wrapping<$t> {
65 type Output = Wrapping<$t>;
68 fn shr(self, other: $f) -> Wrapping<$t> {
70 Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
72 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
77 #[stable(feature = "wrapping_impls", since = "1.7.0")]
78 impl ShrAssign<$f> for Wrapping<$t> {
80 fn shr_assign(&mut self, other: $f) {
81 *self = *self >> other;
87 macro_rules! sh_impl_unsigned {
88 ($t:ident, $f:ident) => (
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl Shl<$f> for Wrapping<$t> {
91 type Output = Wrapping<$t>;
94 fn shl(self, other: $f) -> Wrapping<$t> {
95 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
99 #[stable(feature = "wrapping_impls", since = "1.7.0")]
100 impl ShlAssign<$f> for Wrapping<$t> {
102 fn shl_assign(&mut self, other: $f) {
103 *self = *self << other;
107 #[stable(feature = "rust1", since = "1.0.0")]
108 impl Shr<$f> for Wrapping<$t> {
109 type Output = Wrapping<$t>;
112 fn shr(self, other: $f) -> Wrapping<$t> {
113 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
117 #[stable(feature = "wrapping_impls", since = "1.7.0")]
118 impl ShrAssign<$f> for Wrapping<$t> {
120 fn shr_assign(&mut self, other: $f) {
121 *self = *self >> other;
127 // FIXME (#23545): uncomment the remaining impls
128 macro_rules! sh_impl_all {
129 ($($t:ident)*) => ($(
130 //sh_impl_unsigned! { $t, u8 }
131 //sh_impl_unsigned! { $t, u16 }
132 //sh_impl_unsigned! { $t, u32 }
133 //sh_impl_unsigned! { $t, u64 }
134 sh_impl_unsigned! { $t, usize }
136 //sh_impl_signed! { $t, i8 }
137 //sh_impl_signed! { $t, i16 }
138 //sh_impl_signed! { $t, i32 }
139 //sh_impl_signed! { $t, i64 }
140 //sh_impl_signed! { $t, isize }
144 sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
146 // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
147 macro_rules! wrapping_impl {
149 #[stable(feature = "rust1", since = "1.0.0")]
150 impl Add for Wrapping<$t> {
151 type Output = Wrapping<$t>;
154 fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
155 Wrapping(self.0.wrapping_add(other.0))
159 #[stable(feature = "op_assign_traits", since = "1.8.0")]
160 impl AddAssign for Wrapping<$t> {
162 fn add_assign(&mut self, other: Wrapping<$t>) {
163 *self = *self + other;
167 #[stable(feature = "rust1", since = "1.0.0")]
168 impl Sub for Wrapping<$t> {
169 type Output = Wrapping<$t>;
172 fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
173 Wrapping(self.0.wrapping_sub(other.0))
177 #[stable(feature = "op_assign_traits", since = "1.8.0")]
178 impl SubAssign for Wrapping<$t> {
180 fn sub_assign(&mut self, other: Wrapping<$t>) {
181 *self = *self - other;
185 #[stable(feature = "rust1", since = "1.0.0")]
186 impl Mul for Wrapping<$t> {
187 type Output = Wrapping<$t>;
190 fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
191 Wrapping(self.0.wrapping_mul(other.0))
195 #[stable(feature = "op_assign_traits", since = "1.8.0")]
196 impl MulAssign for Wrapping<$t> {
198 fn mul_assign(&mut self, other: Wrapping<$t>) {
199 *self = *self * other;
203 #[stable(feature = "wrapping_div", since = "1.3.0")]
204 impl Div for Wrapping<$t> {
205 type Output = Wrapping<$t>;
208 fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
209 Wrapping(self.0.wrapping_div(other.0))
213 #[stable(feature = "op_assign_traits", since = "1.8.0")]
214 impl DivAssign for Wrapping<$t> {
216 fn div_assign(&mut self, other: Wrapping<$t>) {
217 *self = *self / other;
221 #[stable(feature = "wrapping_impls", since = "1.7.0")]
222 impl Rem for Wrapping<$t> {
223 type Output = Wrapping<$t>;
226 fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
227 Wrapping(self.0.wrapping_rem(other.0))
231 #[stable(feature = "op_assign_traits", since = "1.8.0")]
232 impl RemAssign for Wrapping<$t> {
234 fn rem_assign(&mut self, other: Wrapping<$t>) {
235 *self = *self % other;
239 #[stable(feature = "rust1", since = "1.0.0")]
240 impl Not for Wrapping<$t> {
241 type Output = Wrapping<$t>;
244 fn not(self) -> Wrapping<$t> {
249 #[stable(feature = "rust1", since = "1.0.0")]
250 impl BitXor for Wrapping<$t> {
251 type Output = Wrapping<$t>;
254 fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
255 Wrapping(self.0 ^ other.0)
259 #[stable(feature = "op_assign_traits", since = "1.8.0")]
260 impl BitXorAssign for Wrapping<$t> {
262 fn bitxor_assign(&mut self, other: Wrapping<$t>) {
263 *self = *self ^ other;
267 #[stable(feature = "rust1", since = "1.0.0")]
268 impl BitOr for Wrapping<$t> {
269 type Output = Wrapping<$t>;
272 fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
273 Wrapping(self.0 | other.0)
277 #[stable(feature = "op_assign_traits", since = "1.8.0")]
278 impl BitOrAssign for Wrapping<$t> {
280 fn bitor_assign(&mut self, other: Wrapping<$t>) {
281 *self = *self | other;
285 #[stable(feature = "rust1", since = "1.0.0")]
286 impl BitAnd for Wrapping<$t> {
287 type Output = Wrapping<$t>;
290 fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
291 Wrapping(self.0 & other.0)
295 #[stable(feature = "op_assign_traits", since = "1.8.0")]
296 impl BitAndAssign for Wrapping<$t> {
298 fn bitand_assign(&mut self, other: Wrapping<$t>) {
299 *self = *self & other;
305 wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
308 #![allow(non_upper_case_globals)]
310 #[cfg(target_pointer_width = "32")]
312 pub const usize: u32 = super::u32;
313 pub const isize: u32 = super::i32;
316 #[cfg(target_pointer_width = "64")]
318 pub const usize: u32 = super::u64;
319 pub const isize: u32 = super::i64;
322 pub const i8: u32 = (1 << 3) - 1;
323 pub const i16: u32 = (1 << 4) - 1;
324 pub const i32: u32 = (1 << 5) - 1;
325 pub const i64: u32 = (1 << 6) - 1;
326 pub use self::platform::isize;
328 pub const u8: u32 = i8;
329 pub const u16: u32 = i16;
330 pub const u32: u32 = i32;
331 pub const u64: u32 = i64;
332 pub use self::platform::usize;
335 macro_rules! signed_overflowing_impl {
336 ($($t:ident)*) => ($(
338 impl OverflowingOps for $t {
340 fn overflowing_add(self, rhs: $t) -> ($t, bool) {
342 add_with_overflow(self, rhs)
346 fn overflowing_sub(self, rhs: $t) -> ($t, bool) {
348 sub_with_overflow(self, rhs)
352 fn overflowing_mul(self, rhs: $t) -> ($t, bool) {
354 mul_with_overflow(self, rhs)
359 fn overflowing_div(self, rhs: $t) -> ($t, bool) {
360 if self == $t::MIN && rhs == -1 {
367 fn overflowing_rem(self, rhs: $t) -> ($t, bool) {
368 if self == $t::MIN && rhs == -1 {
376 fn overflowing_shl(self, rhs: u32) -> ($t, bool) {
377 (self << (rhs & self::shift_max::$t),
378 (rhs > self::shift_max::$t))
381 fn overflowing_shr(self, rhs: u32) -> ($t, bool) {
382 (self >> (rhs & self::shift_max::$t),
383 (rhs > self::shift_max::$t))
387 fn overflowing_neg(self) -> ($t, bool) {
398 macro_rules! unsigned_overflowing_impl {
399 ($($t:ident)*) => ($(
401 impl OverflowingOps for $t {
403 fn overflowing_add(self, rhs: $t) -> ($t, bool) {
405 add_with_overflow(self, rhs)
409 fn overflowing_sub(self, rhs: $t) -> ($t, bool) {
411 sub_with_overflow(self, rhs)
415 fn overflowing_mul(self, rhs: $t) -> ($t, bool) {
417 mul_with_overflow(self, rhs)
422 fn overflowing_div(self, rhs: $t) -> ($t, bool) {
426 fn overflowing_rem(self, rhs: $t) -> ($t, bool) {
431 fn overflowing_shl(self, rhs: u32) -> ($t, bool) {
432 (self << (rhs & self::shift_max::$t),
433 (rhs > self::shift_max::$t))
436 fn overflowing_shr(self, rhs: u32) -> ($t, bool) {
437 (self >> (rhs & self::shift_max::$t),
438 (rhs > self::shift_max::$t))
442 fn overflowing_neg(self) -> ($t, bool) {
443 ((!self).wrapping_add(1), true)
449 signed_overflowing_impl! { i8 i16 i32 i64 isize }
450 unsigned_overflowing_impl! { u8 u16 u32 u64 usize }