]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/wrapping.rs
Number of filtered out tests in tests summary
[rust.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 = "op_assign_traits", since = "1.8.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 = "op_assign_traits", since = "1.8.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 = "op_assign_traits", since = "1.8.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 = "op_assign_traits", since = "1.8.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         forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
135                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
136
137         #[stable(feature = "op_assign_traits", since = "1.8.0")]
138         impl AddAssign for Wrapping<$t> {
139             #[inline(always)]
140             fn add_assign(&mut self, other: Wrapping<$t>) {
141                 *self = *self + other;
142             }
143         }
144
145         #[stable(feature = "rust1", since = "1.0.0")]
146         impl Sub for Wrapping<$t> {
147             type Output = Wrapping<$t>;
148
149             #[inline(always)]
150             fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
151                 Wrapping(self.0.wrapping_sub(other.0))
152             }
153         }
154         forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
155                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
156
157         #[stable(feature = "op_assign_traits", since = "1.8.0")]
158         impl SubAssign for Wrapping<$t> {
159             #[inline(always)]
160             fn sub_assign(&mut self, other: Wrapping<$t>) {
161                 *self = *self - other;
162             }
163         }
164
165         #[stable(feature = "rust1", since = "1.0.0")]
166         impl Mul for Wrapping<$t> {
167             type Output = Wrapping<$t>;
168
169             #[inline(always)]
170             fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
171                 Wrapping(self.0.wrapping_mul(other.0))
172             }
173         }
174         forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>,
175                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
176
177         #[stable(feature = "op_assign_traits", since = "1.8.0")]
178         impl MulAssign for Wrapping<$t> {
179             #[inline(always)]
180             fn mul_assign(&mut self, other: Wrapping<$t>) {
181                 *self = *self * other;
182             }
183         }
184
185         #[stable(feature = "wrapping_div", since = "1.3.0")]
186         impl Div for Wrapping<$t> {
187             type Output = Wrapping<$t>;
188
189             #[inline(always)]
190             fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
191                 Wrapping(self.0.wrapping_div(other.0))
192             }
193         }
194         forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
195                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
196
197         #[stable(feature = "op_assign_traits", since = "1.8.0")]
198         impl DivAssign for Wrapping<$t> {
199             #[inline(always)]
200             fn div_assign(&mut self, other: Wrapping<$t>) {
201                 *self = *self / other;
202             }
203         }
204
205         #[stable(feature = "wrapping_impls", since = "1.7.0")]
206         impl Rem for Wrapping<$t> {
207             type Output = Wrapping<$t>;
208
209             #[inline(always)]
210             fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
211                 Wrapping(self.0.wrapping_rem(other.0))
212             }
213         }
214         forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
215                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
216
217         #[stable(feature = "op_assign_traits", since = "1.8.0")]
218         impl RemAssign for Wrapping<$t> {
219             #[inline(always)]
220             fn rem_assign(&mut self, other: Wrapping<$t>) {
221                 *self = *self % other;
222             }
223         }
224
225         #[stable(feature = "rust1", since = "1.0.0")]
226         impl Not for Wrapping<$t> {
227             type Output = Wrapping<$t>;
228
229             #[inline(always)]
230             fn not(self) -> Wrapping<$t> {
231                 Wrapping(!self.0)
232             }
233         }
234         forward_ref_unop! { impl Not, not for Wrapping<$t>,
235                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
236
237         #[stable(feature = "rust1", since = "1.0.0")]
238         impl BitXor for Wrapping<$t> {
239             type Output = Wrapping<$t>;
240
241             #[inline(always)]
242             fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
243                 Wrapping(self.0 ^ other.0)
244             }
245         }
246         forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
247                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
248
249         #[stable(feature = "op_assign_traits", since = "1.8.0")]
250         impl BitXorAssign for Wrapping<$t> {
251             #[inline(always)]
252             fn bitxor_assign(&mut self, other: Wrapping<$t>) {
253                 *self = *self ^ other;
254             }
255         }
256
257         #[stable(feature = "rust1", since = "1.0.0")]
258         impl BitOr for Wrapping<$t> {
259             type Output = Wrapping<$t>;
260
261             #[inline(always)]
262             fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
263                 Wrapping(self.0 | other.0)
264             }
265         }
266         forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
267                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
268
269         #[stable(feature = "op_assign_traits", since = "1.8.0")]
270         impl BitOrAssign for Wrapping<$t> {
271             #[inline(always)]
272             fn bitor_assign(&mut self, other: Wrapping<$t>) {
273                 *self = *self | other;
274             }
275         }
276
277         #[stable(feature = "rust1", since = "1.0.0")]
278         impl BitAnd for Wrapping<$t> {
279             type Output = Wrapping<$t>;
280
281             #[inline(always)]
282             fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
283                 Wrapping(self.0 & other.0)
284             }
285         }
286         forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
287                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
288
289         #[stable(feature = "op_assign_traits", since = "1.8.0")]
290         impl BitAndAssign for Wrapping<$t> {
291             #[inline(always)]
292             fn bitand_assign(&mut self, other: Wrapping<$t>) {
293                 *self = *self & other;
294             }
295         }
296
297         #[stable(feature = "wrapping_neg", since = "1.10.0")]
298         impl Neg for Wrapping<$t> {
299             type Output = Self;
300             #[inline(always)]
301             fn neg(self) -> Self {
302                 Wrapping(0) - self
303             }
304         }
305         forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
306                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
307     )*)
308 }
309
310 wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
311
312 mod shift_max {
313     #![allow(non_upper_case_globals)]
314
315     #[cfg(target_pointer_width = "16")]
316     mod platform {
317         pub const usize: u32 = super::u16;
318         pub const isize: u32 = super::i16;
319     }
320
321     #[cfg(target_pointer_width = "32")]
322     mod platform {
323         pub const usize: u32 = super::u32;
324         pub const isize: u32 = super::i32;
325     }
326
327     #[cfg(target_pointer_width = "64")]
328     mod platform {
329         pub const usize: u32 = super::u64;
330         pub const isize: u32 = super::i64;
331     }
332
333     pub const i8: u32 = (1 << 3) - 1;
334     pub const i16: u32 = (1 << 4) - 1;
335     pub const i32: u32 = (1 << 5) - 1;
336     pub const i64: u32 = (1 << 6) - 1;
337     pub use self::platform::isize;
338
339     pub const u8: u32 = i8;
340     pub const u16: u32 = i16;
341     pub const u32: u32 = i32;
342     pub const u64: u32 = i64;
343     pub use self::platform::usize;
344 }