]> git.proxmox.com Git - rustc.git/blob - src/libcore/num/wrapping.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcore / num / wrapping.rs
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 use super::Wrapping;
12
13 use ops::*;
14
15 macro_rules! sh_impl_signed {
16 ($t:ident, $f:ident) => (
17 #[stable(feature = "rust1", since = "1.0.0")]
18 impl Shl<$f> for Wrapping<$t> {
19 type Output = Wrapping<$t>;
20
21 #[inline(always)]
22 fn shl(self, other: $f) -> Wrapping<$t> {
23 if other < 0 {
24 Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
25 } else {
26 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
27 }
28 }
29 }
30
31 #[stable(feature = "wrapping_impls", since = "1.7.0")]
32 impl ShlAssign<$f> for Wrapping<$t> {
33 #[inline(always)]
34 fn shl_assign(&mut self, other: $f) {
35 *self = *self << other;
36 }
37 }
38
39 #[stable(feature = "rust1", since = "1.0.0")]
40 impl Shr<$f> for Wrapping<$t> {
41 type Output = Wrapping<$t>;
42
43 #[inline(always)]
44 fn shr(self, other: $f) -> Wrapping<$t> {
45 if other < 0 {
46 Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
47 } else {
48 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
49 }
50 }
51 }
52
53 #[stable(feature = "wrapping_impls", since = "1.7.0")]
54 impl ShrAssign<$f> for Wrapping<$t> {
55 #[inline(always)]
56 fn shr_assign(&mut self, other: $f) {
57 *self = *self >> other;
58 }
59 }
60 )
61 }
62
63 macro_rules! sh_impl_unsigned {
64 ($t:ident, $f:ident) => (
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl Shl<$f> for Wrapping<$t> {
67 type Output = Wrapping<$t>;
68
69 #[inline(always)]
70 fn shl(self, other: $f) -> Wrapping<$t> {
71 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
72 }
73 }
74
75 #[stable(feature = "wrapping_impls", since = "1.7.0")]
76 impl ShlAssign<$f> for Wrapping<$t> {
77 #[inline(always)]
78 fn shl_assign(&mut self, other: $f) {
79 *self = *self << other;
80 }
81 }
82
83 #[stable(feature = "rust1", since = "1.0.0")]
84 impl Shr<$f> for Wrapping<$t> {
85 type Output = Wrapping<$t>;
86
87 #[inline(always)]
88 fn shr(self, other: $f) -> Wrapping<$t> {
89 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
90 }
91 }
92
93 #[stable(feature = "wrapping_impls", since = "1.7.0")]
94 impl ShrAssign<$f> for Wrapping<$t> {
95 #[inline(always)]
96 fn shr_assign(&mut self, other: $f) {
97 *self = *self >> other;
98 }
99 }
100 )
101 }
102
103 // FIXME (#23545): uncomment the remaining impls
104 macro_rules! sh_impl_all {
105 ($($t:ident)*) => ($(
106 //sh_impl_unsigned! { $t, u8 }
107 //sh_impl_unsigned! { $t, u16 }
108 //sh_impl_unsigned! { $t, u32 }
109 //sh_impl_unsigned! { $t, u64 }
110 sh_impl_unsigned! { $t, usize }
111
112 //sh_impl_signed! { $t, i8 }
113 //sh_impl_signed! { $t, i16 }
114 //sh_impl_signed! { $t, i32 }
115 //sh_impl_signed! { $t, i64 }
116 //sh_impl_signed! { $t, isize }
117 )*)
118 }
119
120 sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
121
122 // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
123 macro_rules! wrapping_impl {
124 ($($t:ty)*) => ($(
125 #[stable(feature = "rust1", since = "1.0.0")]
126 impl Add for Wrapping<$t> {
127 type Output = Wrapping<$t>;
128
129 #[inline(always)]
130 fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
131 Wrapping(self.0.wrapping_add(other.0))
132 }
133 }
134
135 #[stable(feature = "op_assign_traits", since = "1.8.0")]
136 impl AddAssign for Wrapping<$t> {
137 #[inline(always)]
138 fn add_assign(&mut self, other: Wrapping<$t>) {
139 *self = *self + other;
140 }
141 }
142
143 #[stable(feature = "rust1", since = "1.0.0")]
144 impl Sub for Wrapping<$t> {
145 type Output = Wrapping<$t>;
146
147 #[inline(always)]
148 fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
149 Wrapping(self.0.wrapping_sub(other.0))
150 }
151 }
152
153 #[stable(feature = "op_assign_traits", since = "1.8.0")]
154 impl SubAssign for Wrapping<$t> {
155 #[inline(always)]
156 fn sub_assign(&mut self, other: Wrapping<$t>) {
157 *self = *self - other;
158 }
159 }
160
161 #[stable(feature = "rust1", since = "1.0.0")]
162 impl Mul for Wrapping<$t> {
163 type Output = Wrapping<$t>;
164
165 #[inline(always)]
166 fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
167 Wrapping(self.0.wrapping_mul(other.0))
168 }
169 }
170
171 #[stable(feature = "op_assign_traits", since = "1.8.0")]
172 impl MulAssign for Wrapping<$t> {
173 #[inline(always)]
174 fn mul_assign(&mut self, other: Wrapping<$t>) {
175 *self = *self * other;
176 }
177 }
178
179 #[stable(feature = "wrapping_div", since = "1.3.0")]
180 impl Div for Wrapping<$t> {
181 type Output = Wrapping<$t>;
182
183 #[inline(always)]
184 fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
185 Wrapping(self.0.wrapping_div(other.0))
186 }
187 }
188
189 #[stable(feature = "op_assign_traits", since = "1.8.0")]
190 impl DivAssign for Wrapping<$t> {
191 #[inline(always)]
192 fn div_assign(&mut self, other: Wrapping<$t>) {
193 *self = *self / other;
194 }
195 }
196
197 #[stable(feature = "wrapping_impls", since = "1.7.0")]
198 impl Rem for Wrapping<$t> {
199 type Output = Wrapping<$t>;
200
201 #[inline(always)]
202 fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
203 Wrapping(self.0.wrapping_rem(other.0))
204 }
205 }
206
207 #[stable(feature = "op_assign_traits", since = "1.8.0")]
208 impl RemAssign for Wrapping<$t> {
209 #[inline(always)]
210 fn rem_assign(&mut self, other: Wrapping<$t>) {
211 *self = *self % other;
212 }
213 }
214
215 #[stable(feature = "rust1", since = "1.0.0")]
216 impl Not for Wrapping<$t> {
217 type Output = Wrapping<$t>;
218
219 #[inline(always)]
220 fn not(self) -> Wrapping<$t> {
221 Wrapping(!self.0)
222 }
223 }
224
225 #[stable(feature = "rust1", since = "1.0.0")]
226 impl BitXor for Wrapping<$t> {
227 type Output = Wrapping<$t>;
228
229 #[inline(always)]
230 fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
231 Wrapping(self.0 ^ other.0)
232 }
233 }
234
235 #[stable(feature = "op_assign_traits", since = "1.8.0")]
236 impl BitXorAssign for Wrapping<$t> {
237 #[inline(always)]
238 fn bitxor_assign(&mut self, other: Wrapping<$t>) {
239 *self = *self ^ other;
240 }
241 }
242
243 #[stable(feature = "rust1", since = "1.0.0")]
244 impl BitOr for Wrapping<$t> {
245 type Output = Wrapping<$t>;
246
247 #[inline(always)]
248 fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
249 Wrapping(self.0 | other.0)
250 }
251 }
252
253 #[stable(feature = "op_assign_traits", since = "1.8.0")]
254 impl BitOrAssign for Wrapping<$t> {
255 #[inline(always)]
256 fn bitor_assign(&mut self, other: Wrapping<$t>) {
257 *self = *self | other;
258 }
259 }
260
261 #[stable(feature = "rust1", since = "1.0.0")]
262 impl BitAnd for Wrapping<$t> {
263 type Output = Wrapping<$t>;
264
265 #[inline(always)]
266 fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
267 Wrapping(self.0 & other.0)
268 }
269 }
270
271 #[stable(feature = "op_assign_traits", since = "1.8.0")]
272 impl BitAndAssign for Wrapping<$t> {
273 #[inline(always)]
274 fn bitand_assign(&mut self, other: Wrapping<$t>) {
275 *self = *self & other;
276 }
277 }
278 )*)
279 }
280
281 wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
282
283 mod shift_max {
284 #![allow(non_upper_case_globals)]
285
286 #[cfg(target_pointer_width = "32")]
287 mod platform {
288 pub const usize: u32 = super::u32;
289 pub const isize: u32 = super::i32;
290 }
291
292 #[cfg(target_pointer_width = "64")]
293 mod platform {
294 pub const usize: u32 = super::u64;
295 pub const isize: u32 = super::i64;
296 }
297
298 pub const i8: u32 = (1 << 3) - 1;
299 pub const i16: u32 = (1 << 4) - 1;
300 pub const i32: u32 = (1 << 5) - 1;
301 pub const i64: u32 = (1 << 6) - 1;
302 pub use self::platform::isize;
303
304 pub const u8: u32 = i8;
305 pub const u16: u32 = i16;
306 pub const u32: u32 = i32;
307 pub const u64: u32 = i64;
308 pub use self::platform::usize;
309 }