|
| 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 | +#![allow(missing_docs)] |
| 11 | + |
| 12 | +use ops::*; |
| 13 | + |
| 14 | +#[cfg(not(stage0))] |
| 15 | +use intrinsics::{overflowing_add, overflowing_sub, overflowing_mul}; |
| 16 | + |
| 17 | +pub trait WrappingOps { |
| 18 | + fn wrapping_add(self, rhs: Self) -> Self; |
| 19 | + fn wrapping_sub(self, rhs: Self) -> Self; |
| 20 | + fn wrapping_mul(self, rhs: Self) -> Self; |
| 21 | +} |
| 22 | + |
| 23 | +#[cfg(not(stage0))] |
| 24 | +macro_rules! wrapping_impl { |
| 25 | + ($($t:ty)*) => ($( |
| 26 | + impl WrappingOps for $t { |
| 27 | + #[inline(always)] |
| 28 | + fn wrapping_add(self, rhs: $t) -> $t { |
| 29 | + unsafe { |
| 30 | + overflowing_add(self, rhs) |
| 31 | + } |
| 32 | + } |
| 33 | + #[inline(always)] |
| 34 | + fn wrapping_sub(self, rhs: $t) -> $t { |
| 35 | + unsafe { |
| 36 | + overflowing_sub(self, rhs) |
| 37 | + } |
| 38 | + } |
| 39 | + #[inline(always)] |
| 40 | + fn wrapping_mul(self, rhs: $t) -> $t { |
| 41 | + unsafe { |
| 42 | + overflowing_mul(self, rhs) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + )*) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(stage0)] |
| 50 | +macro_rules! wrapping_impl { |
| 51 | + ($($t:ty)*) => ($( |
| 52 | + impl WrappingOps for $t { |
| 53 | + #[inline(always)] |
| 54 | + fn wrapping_add(self, rhs: $t) -> $t { |
| 55 | + self + rhs |
| 56 | + } |
| 57 | + #[inline(always)] |
| 58 | + fn wrapping_sub(self, rhs: $t) -> $t { |
| 59 | + self - rhs |
| 60 | + } |
| 61 | + #[inline(always)] |
| 62 | + fn wrapping_mul(self, rhs: $t) -> $t { |
| 63 | + self * rhs |
| 64 | + } |
| 65 | + } |
| 66 | + )*) |
| 67 | +} |
| 68 | + |
| 69 | +wrapping_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } |
| 70 | + |
| 71 | +#[unstable(feature = "core", reason = "may be removed, renamed, or relocated")] |
| 72 | +#[derive(PartialEq,Eq,PartialOrd,Ord,Clone,Copy)] |
| 73 | +pub struct Wrapping<T>(pub T); |
| 74 | + |
| 75 | +impl<T:WrappingOps> Add for Wrapping<T> { |
| 76 | + type Output = Wrapping<T>; |
| 77 | + |
| 78 | + #[inline(always)] |
| 79 | + fn add(self, other: Wrapping<T>) -> Wrapping<T> { |
| 80 | + Wrapping(self.0.wrapping_add(other.0)) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<T:WrappingOps> Sub for Wrapping<T> { |
| 85 | + type Output = Wrapping<T>; |
| 86 | + |
| 87 | + #[inline(always)] |
| 88 | + fn sub(self, other: Wrapping<T>) -> Wrapping<T> { |
| 89 | + Wrapping(self.0.wrapping_sub(other.0)) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<T:WrappingOps> Mul for Wrapping<T> { |
| 94 | + type Output = Wrapping<T>; |
| 95 | + |
| 96 | + #[inline(always)] |
| 97 | + fn mul(self, other: Wrapping<T>) -> Wrapping<T> { |
| 98 | + Wrapping(self.0.wrapping_mul(other.0)) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<T:WrappingOps+Not<Output=T>> Not for Wrapping<T> { |
| 103 | + type Output = Wrapping<T>; |
| 104 | + |
| 105 | + fn not(self) -> Wrapping<T> { |
| 106 | + Wrapping(!self.0) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<T:WrappingOps+BitXor<Output=T>> BitXor for Wrapping<T> { |
| 111 | + type Output = Wrapping<T>; |
| 112 | + |
| 113 | + #[inline(always)] |
| 114 | + fn bitxor(self, other: Wrapping<T>) -> Wrapping<T> { |
| 115 | + Wrapping(self.0 ^ other.0) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<T:WrappingOps+BitOr<Output=T>> BitOr for Wrapping<T> { |
| 120 | + type Output = Wrapping<T>; |
| 121 | + |
| 122 | + #[inline(always)] |
| 123 | + fn bitor(self, other: Wrapping<T>) -> Wrapping<T> { |
| 124 | + Wrapping(self.0 | other.0) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl<T:WrappingOps+BitAnd<Output=T>> BitAnd for Wrapping<T> { |
| 129 | + type Output = Wrapping<T>; |
| 130 | + |
| 131 | + #[inline(always)] |
| 132 | + fn bitand(self, other: Wrapping<T>) -> Wrapping<T> { |
| 133 | + Wrapping(self.0 & other.0) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl<T:WrappingOps+Shl<uint,Output=T>> Shl<uint> for Wrapping<T> { |
| 138 | + type Output = Wrapping<T>; |
| 139 | + |
| 140 | + #[inline(always)] |
| 141 | + fn shl(self, other: uint) -> Wrapping<T> { |
| 142 | + Wrapping(self.0 << other) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<T:WrappingOps+Shr<uint,Output=T>> Shr<uint> for Wrapping<T> { |
| 147 | + type Output = Wrapping<T>; |
| 148 | + |
| 149 | + #[inline(always)] |
| 150 | + fn shr(self, other: uint) -> Wrapping<T> { |
| 151 | + Wrapping(self.0 >> other) |
| 152 | + } |
| 153 | +} |
0 commit comments