]> git.proxmox.com Git - rustc.git/blame - src/libcore/num/wrapping.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / libcore / num / wrapping.rs
CommitLineData
c34b1796
AL
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
c34b1796
AL
11use super::Wrapping;
12
13use ops::*;
14
9cc50fc6
SL
15macro_rules! sh_impl_signed {
16 ($t:ident, $f:ident) => (
c34b1796
AL
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> {
9cc50fc6
SL
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
c30ab7b3 31 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
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
c30ab7b3 53 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
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
63macro_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
c30ab7b3 75 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
76 impl ShlAssign<$f> for Wrapping<$t> {
77 #[inline(always)]
78 fn shl_assign(&mut self, other: $f) {
79 *self = *self << other;
c34b1796
AL
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> {
9cc50fc6
SL
89 Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
90 }
91 }
92
c30ab7b3 93 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
94 impl ShrAssign<$f> for Wrapping<$t> {
95 #[inline(always)]
96 fn shr_assign(&mut self, other: $f) {
97 *self = *self >> other;
c34b1796
AL
98 }
99 }
100 )
101}
102
103// FIXME (#23545): uncomment the remaining impls
104macro_rules! sh_impl_all {
9cc50fc6
SL
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 }
c34b1796
AL
117 )*)
118}
119
120sh_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
121
9cc50fc6 122// FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
c34b1796
AL
123macro_rules! wrapping_impl {
124 ($($t:ty)*) => ($(
c34b1796
AL
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 }
c30ab7b3 134 forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t> }
c34b1796 135
7453a54e 136 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
137 impl AddAssign for Wrapping<$t> {
138 #[inline(always)]
139 fn add_assign(&mut self, other: Wrapping<$t>) {
140 *self = *self + other;
141 }
142 }
143
c34b1796
AL
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl Sub for Wrapping<$t> {
146 type Output = Wrapping<$t>;
147
148 #[inline(always)]
149 fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
150 Wrapping(self.0.wrapping_sub(other.0))
151 }
152 }
c30ab7b3 153 forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t> }
c34b1796 154
7453a54e 155 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
156 impl SubAssign for Wrapping<$t> {
157 #[inline(always)]
158 fn sub_assign(&mut self, other: Wrapping<$t>) {
159 *self = *self - other;
160 }
161 }
162
c34b1796
AL
163 #[stable(feature = "rust1", since = "1.0.0")]
164 impl Mul for Wrapping<$t> {
165 type Output = Wrapping<$t>;
166
167 #[inline(always)]
168 fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
169 Wrapping(self.0.wrapping_mul(other.0))
170 }
171 }
c30ab7b3 172 forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t> }
c34b1796 173
7453a54e 174 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
175 impl MulAssign for Wrapping<$t> {
176 #[inline(always)]
177 fn mul_assign(&mut self, other: Wrapping<$t>) {
178 *self = *self * other;
179 }
180 }
181
c1a9b12d
SL
182 #[stable(feature = "wrapping_div", since = "1.3.0")]
183 impl Div for Wrapping<$t> {
184 type Output = Wrapping<$t>;
185
186 #[inline(always)]
187 fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
188 Wrapping(self.0.wrapping_div(other.0))
189 }
190 }
c30ab7b3 191 forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t> }
c1a9b12d 192
7453a54e 193 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
194 impl DivAssign for Wrapping<$t> {
195 #[inline(always)]
196 fn div_assign(&mut self, other: Wrapping<$t>) {
197 *self = *self / other;
198 }
199 }
200
54a0048b 201 #[stable(feature = "wrapping_impls", since = "1.7.0")]
9cc50fc6
SL
202 impl Rem for Wrapping<$t> {
203 type Output = Wrapping<$t>;
204
205 #[inline(always)]
206 fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
207 Wrapping(self.0.wrapping_rem(other.0))
208 }
209 }
c30ab7b3 210 forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t> }
9cc50fc6 211
7453a54e 212 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
213 impl RemAssign for Wrapping<$t> {
214 #[inline(always)]
215 fn rem_assign(&mut self, other: Wrapping<$t>) {
216 *self = *self % other;
217 }
218 }
219
c34b1796
AL
220 #[stable(feature = "rust1", since = "1.0.0")]
221 impl Not for Wrapping<$t> {
222 type Output = Wrapping<$t>;
223
d9579d0f 224 #[inline(always)]
c34b1796
AL
225 fn not(self) -> Wrapping<$t> {
226 Wrapping(!self.0)
227 }
228 }
c30ab7b3 229 forward_ref_unop! { impl Not, not for Wrapping<$t> }
c34b1796
AL
230
231 #[stable(feature = "rust1", since = "1.0.0")]
232 impl BitXor for Wrapping<$t> {
233 type Output = Wrapping<$t>;
234
235 #[inline(always)]
236 fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
237 Wrapping(self.0 ^ other.0)
238 }
239 }
c30ab7b3 240 forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t> }
c34b1796 241
7453a54e 242 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
243 impl BitXorAssign for Wrapping<$t> {
244 #[inline(always)]
245 fn bitxor_assign(&mut self, other: Wrapping<$t>) {
246 *self = *self ^ other;
247 }
248 }
249
c34b1796
AL
250 #[stable(feature = "rust1", since = "1.0.0")]
251 impl BitOr for Wrapping<$t> {
252 type Output = Wrapping<$t>;
253
254 #[inline(always)]
255 fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
256 Wrapping(self.0 | other.0)
257 }
258 }
c30ab7b3 259 forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t> }
c34b1796 260
7453a54e 261 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
262 impl BitOrAssign for Wrapping<$t> {
263 #[inline(always)]
264 fn bitor_assign(&mut self, other: Wrapping<$t>) {
265 *self = *self | other;
266 }
267 }
268
c34b1796
AL
269 #[stable(feature = "rust1", since = "1.0.0")]
270 impl BitAnd for Wrapping<$t> {
271 type Output = Wrapping<$t>;
272
273 #[inline(always)]
274 fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
275 Wrapping(self.0 & other.0)
276 }
277 }
c30ab7b3 278 forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t> }
9cc50fc6 279
7453a54e 280 #[stable(feature = "op_assign_traits", since = "1.8.0")]
9cc50fc6
SL
281 impl BitAndAssign for Wrapping<$t> {
282 #[inline(always)]
283 fn bitand_assign(&mut self, other: Wrapping<$t>) {
284 *self = *self & other;
285 }
286 }
a7813a04
XL
287
288 #[stable(feature = "wrapping_neg", since = "1.10.0")]
289 impl Neg for Wrapping<$t> {
290 type Output = Self;
291 #[inline(always)]
292 fn neg(self) -> Self {
293 Wrapping(0) - self
294 }
295 }
c30ab7b3 296 forward_ref_unop! { impl Neg, neg for Wrapping<$t> }
c34b1796
AL
297 )*)
298}
299
300wrapping_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
301
302mod shift_max {
303 #![allow(non_upper_case_globals)]
304
3157f602
XL
305 #[cfg(target_pointer_width = "16")]
306 mod platform {
307 pub const usize: u32 = super::u16;
308 pub const isize: u32 = super::i16;
309 }
310
9cc50fc6
SL
311 #[cfg(target_pointer_width = "32")]
312 mod platform {
313 pub const usize: u32 = super::u32;
314 pub const isize: u32 = super::i32;
315 }
316
317 #[cfg(target_pointer_width = "64")]
318 mod platform {
319 pub const usize: u32 = super::u64;
320 pub const isize: u32 = super::i64;
321 }
322
c30ab7b3 323 pub const i8: u32 = (1 << 3) - 1;
c34b1796
AL
324 pub const i16: u32 = (1 << 4) - 1;
325 pub const i32: u32 = (1 << 5) - 1;
326 pub const i64: u32 = (1 << 6) - 1;
9cc50fc6 327 pub use self::platform::isize;
c34b1796 328
c30ab7b3 329 pub const u8: u32 = i8;
c34b1796
AL
330 pub const u16: u32 = i16;
331 pub const u32: u32 = i32;
332 pub const u64: u32 = i64;
9cc50fc6 333 pub use self::platform::usize;
c34b1796 334}