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