diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-04-01 18:36:21 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-04-01 18:36:21 -0700 |
| commit | 9edbf42a342ced7357fe5ec225975f214d872fbc (patch) | |
| tree | 3ac2106c48eb2997f0795c8bcef20f921407636b /src/libcore/num | |
| parent | 2e3b0c051dca9880bf66b5366dccd2e0bb424b99 (diff) | |
| parent | f86318d63c86568b312f39da20bea67e328c1fc5 (diff) | |
| download | rust-9edbf42a342ced7357fe5ec225975f214d872fbc.tar.gz rust-9edbf42a342ced7357fe5ec225975f214d872fbc.zip | |
rollup merge of #23945: pnkfelix/gate-u-negate
Feature-gate unsigned unary negate. Discussed in weekly meeting here: https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2015-03-31.md#feature-gate--expr and also in the internals thread here: http://internals.rust-lang.org/t/forbid-unsigned-integer/752
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/mod.rs | 2 | ||||
| -rw-r--r-- | src/libcore/num/wrapping.rs | 52 |
2 files changed, 50 insertions, 4 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 4e458e993a0..09bd2ab379a 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -516,7 +516,7 @@ macro_rules! uint_impl { fn min_value() -> $T { 0 } #[inline] - fn max_value() -> $T { -1 } + fn max_value() -> $T { !0 } #[inline] fn count_ones(self) -> u32 { diff --git a/src/libcore/num/wrapping.rs b/src/libcore/num/wrapping.rs index a78eed8ae5f..28276d0bf01 100644 --- a/src/libcore/num/wrapping.rs +++ b/src/libcore/num/wrapping.rs @@ -30,7 +30,7 @@ use intrinsics::{i16_mul_with_overflow, u16_mul_with_overflow}; use intrinsics::{i32_mul_with_overflow, u32_mul_with_overflow}; use intrinsics::{i64_mul_with_overflow, u64_mul_with_overflow}; -use ::{i8,i16,i32,i64,u8,u16,u32,u64}; +use ::{i8,i16,i32,i64}; #[unstable(feature = "core", reason = "may be removed, renamed, or relocated")] #[deprecated(since = "1.0.0", reason = "moved to inherent methods")] @@ -206,7 +206,7 @@ mod shift_max { pub const u64: u32 = i64; } -macro_rules! overflowing_impl { +macro_rules! signed_overflowing_impl { ($($t:ident)*) => ($( impl OverflowingOps for $t { #[inline(always)] @@ -259,7 +259,53 @@ macro_rules! overflowing_impl { )*) } -overflowing_impl! { u8 u16 u32 u64 i8 i16 i32 i64 } +macro_rules! unsigned_overflowing_impl { + ($($t:ident)*) => ($( + impl OverflowingOps for $t { + #[inline(always)] + fn overflowing_add(self, rhs: $t) -> ($t, bool) { + unsafe { + concat_idents!($t, _add_with_overflow)(self, rhs) + } + } + #[inline(always)] + fn overflowing_sub(self, rhs: $t) -> ($t, bool) { + unsafe { + concat_idents!($t, _sub_with_overflow)(self, rhs) + } + } + #[inline(always)] + fn overflowing_mul(self, rhs: $t) -> ($t, bool) { + unsafe { + concat_idents!($t, _mul_with_overflow)(self, rhs) + } + } + + #[inline(always)] + fn overflowing_div(self, rhs: $t) -> ($t, bool) { + (self/rhs, false) + } + #[inline(always)] + fn overflowing_rem(self, rhs: $t) -> ($t, bool) { + (self % rhs, false) + } + + #[inline(always)] + fn overflowing_shl(self, rhs: u32) -> ($t, bool) { + (self << (rhs & self::shift_max::$t), + (rhs > self::shift_max::$t)) + } + #[inline(always)] + fn overflowing_shr(self, rhs: u32) -> ($t, bool) { + (self >> (rhs & self::shift_max::$t), + (rhs > self::shift_max::$t)) + } + } + )*) +} + +signed_overflowing_impl! { i8 i16 i32 i64 } +unsigned_overflowing_impl! { u8 u16 u32 u64 } #[cfg(target_pointer_width = "64")] impl OverflowingOps for usize { |
