]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/wrapping.rs
Rollup merge of #69587 - petrochenkov:reqname, r=Centril
[rust.git] / src / libcore / num / wrapping.rs
1 use super::Wrapping;
2
3 use crate::ops::*;
4
5 #[allow(unused_macros)]
6 macro_rules! sh_impl_signed {
7     ($t:ident, $f:ident) => {
8         #[stable(feature = "rust1", since = "1.0.0")]
9         impl Shl<$f> for Wrapping<$t> {
10             type Output = Wrapping<$t>;
11
12             #[inline]
13             fn shl(self, other: $f) -> Wrapping<$t> {
14                 if other < 0 {
15                     Wrapping(self.0.wrapping_shr((-other & self::shift_max::$t as $f) as u32))
16                 } else {
17                     Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
18                 }
19             }
20         }
21         forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
22         #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
23
24         #[stable(feature = "op_assign_traits", since = "1.8.0")]
25         impl ShlAssign<$f> for Wrapping<$t> {
26             #[inline]
27             fn shl_assign(&mut self, other: $f) {
28                 *self = *self << other;
29             }
30         }
31         forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
32
33         #[stable(feature = "rust1", since = "1.0.0")]
34         impl Shr<$f> for Wrapping<$t> {
35             type Output = Wrapping<$t>;
36
37             #[inline]
38             fn shr(self, other: $f) -> Wrapping<$t> {
39                 if other < 0 {
40                     Wrapping(self.0.wrapping_shl((-other & self::shift_max::$t as $f) as u32))
41                 } else {
42                     Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
43                 }
44             }
45         }
46         forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
47         #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
48
49         #[stable(feature = "op_assign_traits", since = "1.8.0")]
50         impl ShrAssign<$f> for Wrapping<$t> {
51             #[inline]
52             fn shr_assign(&mut self, other: $f) {
53                 *self = *self >> other;
54             }
55         }
56         forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
57     };
58 }
59
60 macro_rules! sh_impl_unsigned {
61     ($t:ident, $f:ident) => {
62         #[stable(feature = "rust1", since = "1.0.0")]
63         impl Shl<$f> for Wrapping<$t> {
64             type Output = Wrapping<$t>;
65
66             #[inline]
67             fn shl(self, other: $f) -> Wrapping<$t> {
68                 Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
69             }
70         }
71         forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
72         #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
73
74         #[stable(feature = "op_assign_traits", since = "1.8.0")]
75         impl ShlAssign<$f> for Wrapping<$t> {
76             #[inline]
77             fn shl_assign(&mut self, other: $f) {
78                 *self = *self << other;
79             }
80         }
81         forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
82
83         #[stable(feature = "rust1", since = "1.0.0")]
84         impl Shr<$f> for Wrapping<$t> {
85             type Output = Wrapping<$t>;
86
87             #[inline]
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         forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
93         #[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }
94
95         #[stable(feature = "op_assign_traits", since = "1.8.0")]
96         impl ShrAssign<$f> for Wrapping<$t> {
97             #[inline]
98             fn shr_assign(&mut self, other: $f) {
99                 *self = *self >> other;
100             }
101         }
102         forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
103     };
104 }
105
106 // FIXME (#23545): uncomment the remaining impls
107 macro_rules! sh_impl_all {
108     ($($t:ident)*) => ($(
109         //sh_impl_unsigned! { $t, u8 }
110         //sh_impl_unsigned! { $t, u16 }
111         //sh_impl_unsigned! { $t, u32 }
112         //sh_impl_unsigned! { $t, u64 }
113         //sh_impl_unsigned! { $t, u128 }
114         sh_impl_unsigned! { $t, usize }
115
116         //sh_impl_signed! { $t, i8 }
117         //sh_impl_signed! { $t, i16 }
118         //sh_impl_signed! { $t, i32 }
119         //sh_impl_signed! { $t, i64 }
120         //sh_impl_signed! { $t, i128 }
121         //sh_impl_signed! { $t, isize }
122     )*)
123 }
124
125 sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
126
127 // FIXME(30524): impl Op<T> for Wrapping<T>, impl OpAssign<T> for Wrapping<T>
128 macro_rules! wrapping_impl {
129     ($($t:ty)*) => ($(
130         #[stable(feature = "rust1", since = "1.0.0")]
131         impl Add for Wrapping<$t> {
132             type Output = Wrapping<$t>;
133
134             #[inline]
135             fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
136                 Wrapping(self.0.wrapping_add(other.0))
137             }
138         }
139         forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
140                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
141
142         #[stable(feature = "op_assign_traits", since = "1.8.0")]
143         impl AddAssign for Wrapping<$t> {
144             #[inline]
145             fn add_assign(&mut self, other: Wrapping<$t>) {
146                 *self = *self + other;
147             }
148         }
149         forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }
150
151         #[stable(feature = "rust1", since = "1.0.0")]
152         impl Sub for Wrapping<$t> {
153             type Output = Wrapping<$t>;
154
155             #[inline]
156             fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
157                 Wrapping(self.0.wrapping_sub(other.0))
158             }
159         }
160         forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
161                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
162
163         #[stable(feature = "op_assign_traits", since = "1.8.0")]
164         impl SubAssign for Wrapping<$t> {
165             #[inline]
166             fn sub_assign(&mut self, other: Wrapping<$t>) {
167                 *self = *self - other;
168             }
169         }
170         forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }
171
172         #[stable(feature = "rust1", since = "1.0.0")]
173         impl Mul for Wrapping<$t> {
174             type Output = Wrapping<$t>;
175
176             #[inline]
177             fn mul(self, other: Wrapping<$t>) -> Wrapping<$t> {
178                 Wrapping(self.0.wrapping_mul(other.0))
179             }
180         }
181         forward_ref_binop! { impl Mul, mul for Wrapping<$t>, Wrapping<$t>,
182                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
183
184         #[stable(feature = "op_assign_traits", since = "1.8.0")]
185         impl MulAssign for Wrapping<$t> {
186             #[inline]
187             fn mul_assign(&mut self, other: Wrapping<$t>) {
188                 *self = *self * other;
189             }
190         }
191         forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }
192
193         #[stable(feature = "wrapping_div", since = "1.3.0")]
194         impl Div for Wrapping<$t> {
195             type Output = Wrapping<$t>;
196
197             #[inline]
198             fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
199                 Wrapping(self.0.wrapping_div(other.0))
200             }
201         }
202         forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
203                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
204
205         #[stable(feature = "op_assign_traits", since = "1.8.0")]
206         impl DivAssign for Wrapping<$t> {
207             #[inline]
208             fn div_assign(&mut self, other: Wrapping<$t>) {
209                 *self = *self / other;
210             }
211         }
212         forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }
213
214         #[stable(feature = "wrapping_impls", since = "1.7.0")]
215         impl Rem for Wrapping<$t> {
216             type Output = Wrapping<$t>;
217
218             #[inline]
219             fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
220                 Wrapping(self.0.wrapping_rem(other.0))
221             }
222         }
223         forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
224                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
225
226         #[stable(feature = "op_assign_traits", since = "1.8.0")]
227         impl RemAssign for Wrapping<$t> {
228             #[inline]
229             fn rem_assign(&mut self, other: Wrapping<$t>) {
230                 *self = *self % other;
231             }
232         }
233         forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }
234
235         #[stable(feature = "rust1", since = "1.0.0")]
236         impl Not for Wrapping<$t> {
237             type Output = Wrapping<$t>;
238
239             #[inline]
240             fn not(self) -> Wrapping<$t> {
241                 Wrapping(!self.0)
242             }
243         }
244         forward_ref_unop! { impl Not, not for Wrapping<$t>,
245                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
246
247         #[stable(feature = "rust1", since = "1.0.0")]
248         impl BitXor for Wrapping<$t> {
249             type Output = Wrapping<$t>;
250
251             #[inline]
252             fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
253                 Wrapping(self.0 ^ other.0)
254             }
255         }
256         forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
257                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
258
259         #[stable(feature = "op_assign_traits", since = "1.8.0")]
260         impl BitXorAssign for Wrapping<$t> {
261             #[inline]
262             fn bitxor_assign(&mut self, other: Wrapping<$t>) {
263                 *self = *self ^ other;
264             }
265         }
266         forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }
267
268         #[stable(feature = "rust1", since = "1.0.0")]
269         impl BitOr for Wrapping<$t> {
270             type Output = Wrapping<$t>;
271
272             #[inline]
273             fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
274                 Wrapping(self.0 | other.0)
275             }
276         }
277         forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
278                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
279
280         #[stable(feature = "op_assign_traits", since = "1.8.0")]
281         impl BitOrAssign for Wrapping<$t> {
282             #[inline]
283             fn bitor_assign(&mut self, other: Wrapping<$t>) {
284                 *self = *self | other;
285             }
286         }
287         forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }
288
289         #[stable(feature = "rust1", since = "1.0.0")]
290         impl BitAnd for Wrapping<$t> {
291             type Output = Wrapping<$t>;
292
293             #[inline]
294             fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
295                 Wrapping(self.0 & other.0)
296             }
297         }
298         forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
299                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
300
301         #[stable(feature = "op_assign_traits", since = "1.8.0")]
302         impl BitAndAssign for Wrapping<$t> {
303             #[inline]
304             fn bitand_assign(&mut self, other: Wrapping<$t>) {
305                 *self = *self & other;
306             }
307         }
308         forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }
309
310         #[stable(feature = "wrapping_neg", since = "1.10.0")]
311         impl Neg for Wrapping<$t> {
312             type Output = Self;
313             #[inline]
314             fn neg(self) -> Self {
315                 Wrapping(0) - self
316             }
317         }
318         forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
319                 #[stable(feature = "wrapping_ref", since = "1.14.0")] }
320
321     )*)
322 }
323
324 wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
325
326 macro_rules! wrapping_int_impl {
327     ($($t:ty)*) => ($(
328         impl Wrapping<$t> {
329             doc_comment! {
330                 concat!("Returns the smallest value that can be represented by this integer type.
331
332 # Examples
333
334 Basic usage:
335
336 ```
337 #![feature(wrapping_int_impl)]
338 use std::num::Wrapping;
339
340 assert_eq!(<Wrapping<", stringify!($t), ">>::min_value(), ",
341 "Wrapping(", stringify!($t), "::min_value()));
342 ```"),
343                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
344                 #[inline]
345                 pub const fn min_value() -> Self {
346                     Wrapping(<$t>::min_value())
347                 }
348             }
349
350             doc_comment! {
351                 concat!("Returns the largest value that can be represented by this integer type.
352
353 # Examples
354
355 Basic usage:
356
357 ```
358 #![feature(wrapping_int_impl)]
359 use std::num::Wrapping;
360
361 assert_eq!(<Wrapping<", stringify!($t), ">>::max_value(), ",
362 "Wrapping(", stringify!($t), "::max_value()));
363 ```"),
364                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
365                 #[inline]
366                 pub const fn max_value() -> Self {
367                     Wrapping(<$t>::max_value())
368                 }
369             }
370
371             doc_comment! {
372                 concat!("Returns the number of ones in the binary representation of `self`.
373
374 # Examples
375
376 Basic usage:
377
378 ```
379 #![feature(wrapping_int_impl)]
380 use std::num::Wrapping;
381
382 let n = Wrapping(0b01001100", stringify!($t), ");
383
384 assert_eq!(n.count_ones(), 3);
385 ```"),
386                 #[inline]
387                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
388                 pub const fn count_ones(self) -> u32 {
389                     self.0.count_ones()
390                 }
391             }
392
393             doc_comment! {
394                 concat!("Returns the number of zeros in the binary representation of `self`.
395
396 # Examples
397
398 Basic usage:
399
400 ```
401 #![feature(wrapping_int_impl)]
402 use std::num::Wrapping;
403
404 assert_eq!(Wrapping(!0", stringify!($t), ").count_zeros(), 0);
405 ```"),
406                 #[inline]
407                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
408                 pub const fn count_zeros(self) -> u32 {
409                     self.0.count_zeros()
410                 }
411             }
412
413             doc_comment! {
414                 concat!("Returns the number of trailing zeros in the binary representation
415 of `self`.
416
417 # Examples
418
419 Basic usage:
420
421 ```
422 #![feature(wrapping_int_impl)]
423 use std::num::Wrapping;
424
425 let n = Wrapping(0b0101000", stringify!($t), ");
426
427 assert_eq!(n.trailing_zeros(), 3);
428 ```"),
429                 #[inline]
430                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
431                 pub const fn trailing_zeros(self) -> u32 {
432                     self.0.trailing_zeros()
433                 }
434             }
435
436             /// Shifts the bits to the left by a specified amount, `n`,
437             /// wrapping the truncated bits to the end of the resulting
438             /// integer.
439             ///
440             /// Please note this isn't the same operation as the `<<` shifting
441             /// operator!
442             ///
443             /// # Examples
444             ///
445             /// Basic usage:
446             ///
447             /// ```
448             /// #![feature(wrapping_int_impl)]
449             /// use std::num::Wrapping;
450             ///
451             /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
452             /// let m: Wrapping<i64> = Wrapping(-0x76543210FEDCBA99);
453             ///
454             /// assert_eq!(n.rotate_left(32), m);
455             /// ```
456             #[inline]
457             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
458             pub const fn rotate_left(self, n: u32) -> Self {
459                 Wrapping(self.0.rotate_left(n))
460             }
461
462             /// Shifts the bits to the right by a specified amount, `n`,
463             /// wrapping the truncated bits to the beginning of the resulting
464             /// integer.
465             ///
466             /// Please note this isn't the same operation as the `>>` shifting
467             /// operator!
468             ///
469             /// # Examples
470             ///
471             /// Basic usage:
472             ///
473             /// ```
474             /// #![feature(wrapping_int_impl)]
475             /// use std::num::Wrapping;
476             ///
477             /// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
478             /// let m: Wrapping<i64> = Wrapping(-0xFEDCBA987654322);
479             ///
480             /// assert_eq!(n.rotate_right(4), m);
481             /// ```
482             #[inline]
483             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
484             pub const fn rotate_right(self, n: u32) -> Self {
485                 Wrapping(self.0.rotate_right(n))
486             }
487
488             /// Reverses the byte order of the integer.
489             ///
490             /// # Examples
491             ///
492             /// Basic usage:
493             ///
494             /// ```
495             /// #![feature(wrapping_int_impl)]
496             /// use std::num::Wrapping;
497             ///
498             /// let n: Wrapping<i16> = Wrapping(0b0000000_01010101);
499             /// assert_eq!(n, Wrapping(85));
500             ///
501             /// let m = n.swap_bytes();
502             ///
503             /// assert_eq!(m, Wrapping(0b01010101_00000000));
504             /// assert_eq!(m, Wrapping(21760));
505             /// ```
506             #[inline]
507             #[unstable(feature = "wrapping_int_impl", issue = "32463")]
508             pub const fn swap_bytes(self) -> Self {
509                 Wrapping(self.0.swap_bytes())
510             }
511
512             /// Reverses the bit pattern of the integer.
513             ///
514             /// # Examples
515             ///
516             /// Please note that this example is shared between integer types.
517             /// Which explains why `i16` is used here.
518             ///
519             /// Basic usage:
520             ///
521             /// ```
522             /// use std::num::Wrapping;
523             ///
524             /// let n = Wrapping(0b0000000_01010101i16);
525             /// assert_eq!(n, Wrapping(85));
526             ///
527             /// let m = n.reverse_bits();
528             ///
529             /// assert_eq!(m.0 as u16, 0b10101010_00000000);
530             /// assert_eq!(m, Wrapping(-22016));
531             /// ```
532             #[stable(feature = "reverse_bits", since = "1.37.0")]
533             #[rustc_const_stable(feature = "const_reverse_bits", since = "1.37.0")]
534             #[inline]
535             #[must_use]
536             pub const fn reverse_bits(self) -> Self {
537                 Wrapping(self.0.reverse_bits())
538             }
539
540             doc_comment! {
541                 concat!("Converts an integer from big endian to the target's endianness.
542
543 On big endian this is a no-op. On little endian the bytes are
544 swapped.
545
546 # Examples
547
548 Basic usage:
549
550 ```
551 #![feature(wrapping_int_impl)]
552 use std::num::Wrapping;
553
554 let n = Wrapping(0x1A", stringify!($t), ");
555
556 if cfg!(target_endian = \"big\") {
557     assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n)
558 } else {
559     assert_eq!(<Wrapping<", stringify!($t), ">>::from_be(n), n.swap_bytes())
560 }
561 ```"),
562                 #[inline]
563                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
564                 pub const fn from_be(x: Self) -> Self {
565                     Wrapping(<$t>::from_be(x.0))
566                 }
567             }
568
569             doc_comment! {
570                 concat!("Converts an integer from little endian to the target's endianness.
571
572 On little endian this is a no-op. On big endian the bytes are
573 swapped.
574
575 # Examples
576
577 Basic usage:
578
579 ```
580 #![feature(wrapping_int_impl)]
581 use std::num::Wrapping;
582
583 let n = Wrapping(0x1A", stringify!($t), ");
584
585 if cfg!(target_endian = \"little\") {
586     assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n)
587 } else {
588     assert_eq!(<Wrapping<", stringify!($t), ">>::from_le(n), n.swap_bytes())
589 }
590 ```"),
591                 #[inline]
592                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
593                 pub const fn from_le(x: Self) -> Self {
594                     Wrapping(<$t>::from_le(x.0))
595                 }
596             }
597
598             doc_comment! {
599                 concat!("Converts `self` to big endian from the target's endianness.
600
601 On big endian this is a no-op. On little endian the bytes are
602 swapped.
603
604 # Examples
605
606 Basic usage:
607
608 ```
609 #![feature(wrapping_int_impl)]
610 use std::num::Wrapping;
611
612 let n = Wrapping(0x1A", stringify!($t), ");
613
614 if cfg!(target_endian = \"big\") {
615     assert_eq!(n.to_be(), n)
616 } else {
617     assert_eq!(n.to_be(), n.swap_bytes())
618 }
619 ```"),
620                 #[inline]
621                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
622                 pub const fn to_be(self) -> Self {
623                     Wrapping(self.0.to_be())
624                 }
625             }
626
627             doc_comment! {
628                 concat!("Converts `self` to little endian from the target's endianness.
629
630 On little endian this is a no-op. On big endian the bytes are
631 swapped.
632
633 # Examples
634
635 Basic usage:
636
637 ```
638 #![feature(wrapping_int_impl)]
639 use std::num::Wrapping;
640
641 let n = Wrapping(0x1A", stringify!($t), ");
642
643 if cfg!(target_endian = \"little\") {
644     assert_eq!(n.to_le(), n)
645 } else {
646     assert_eq!(n.to_le(), n.swap_bytes())
647 }
648 ```"),
649                 #[inline]
650                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
651                 pub const fn to_le(self) -> Self {
652                     Wrapping(self.0.to_le())
653                 }
654             }
655
656         doc_comment! {
657             concat!("Raises self to the power of `exp`, using exponentiation by squaring.
658
659 # Examples
660
661 Basic usage:
662
663 ```
664 #![feature(wrapping_int_impl)]
665 use std::num::Wrapping;
666
667 assert_eq!(Wrapping(3", stringify!($t), ").pow(4), Wrapping(81));
668 ```
669
670 Results that are too large are wrapped:
671
672 ```
673 #![feature(wrapping_int_impl)]
674 use std::num::Wrapping;
675
676 assert_eq!(Wrapping(3i8).pow(5), Wrapping(-13));
677 assert_eq!(Wrapping(3i8).pow(6), Wrapping(-39));
678 ```"),
679                 #[inline]
680                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
681                 pub fn pow(self, exp: u32) -> Self {
682                     Wrapping(self.0.wrapping_pow(exp))
683                 }
684             }
685         }
686     )*)
687 }
688
689 wrapping_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
690
691 macro_rules! wrapping_int_impl_signed {
692     ($($t:ty)*) => ($(
693         impl Wrapping<$t> {
694             doc_comment! {
695                 concat!("Returns the number of leading zeros in the binary representation of `self`.
696
697 # Examples
698
699 Basic usage:
700
701 ```
702 #![feature(wrapping_int_impl)]
703 use std::num::Wrapping;
704
705 let n = Wrapping(", stringify!($t), "::max_value()) >> 2;
706
707 assert_eq!(n.leading_zeros(), 3);
708 ```"),
709                 #[inline]
710                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
711                 pub const fn leading_zeros(self) -> u32 {
712                     self.0.leading_zeros()
713                 }
714             }
715
716             doc_comment! {
717                 concat!("Computes the absolute value of `self`, wrapping around at
718 the boundary of the type.
719
720 The only case where such wrapping can occur is when one takes the absolute value of the negative
721 minimal value for the type this is a positive value that is too large to represent in the type. In
722 such a case, this function returns `MIN` itself.
723
724 # Examples
725
726 Basic usage:
727
728 ```
729 #![feature(wrapping_int_impl)]
730 use std::num::Wrapping;
731
732 assert_eq!(Wrapping(100", stringify!($t), ").abs(), Wrapping(100));
733 assert_eq!(Wrapping(-100", stringify!($t), ").abs(), Wrapping(100));
734 assert_eq!(Wrapping(", stringify!($t), "::min_value()).abs(), Wrapping(", stringify!($t),
735 "::min_value()));
736 assert_eq!(Wrapping(-128i8).abs().0 as u8, 128u8);
737 ```"),
738                 #[inline]
739                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
740                 pub fn abs(self) -> Wrapping<$t> {
741                     Wrapping(self.0.wrapping_abs())
742                 }
743             }
744
745             doc_comment! {
746                 concat!("Returns a number representing sign of `self`.
747
748  - `0` if the number is zero
749  - `1` if the number is positive
750  - `-1` if the number is negative
751
752 # Examples
753
754 Basic usage:
755
756 ```
757 #![feature(wrapping_int_impl)]
758 use std::num::Wrapping;
759
760 assert_eq!(Wrapping(10", stringify!($t), ").signum(), Wrapping(1));
761 assert_eq!(Wrapping(0", stringify!($t), ").signum(), Wrapping(0));
762 assert_eq!(Wrapping(-10", stringify!($t), ").signum(), Wrapping(-1));
763 ```"),
764                 #[inline]
765                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
766                 pub fn signum(self) -> Wrapping<$t> {
767                     Wrapping(self.0.signum())
768                 }
769             }
770
771             doc_comment! {
772                 concat!("Returns `true` if `self` is positive and `false` if the number is zero or
773 negative.
774
775 # Examples
776
777 Basic usage:
778
779 ```
780 #![feature(wrapping_int_impl)]
781 use std::num::Wrapping;
782
783 assert!(Wrapping(10", stringify!($t), ").is_positive());
784 assert!(!Wrapping(-10", stringify!($t), ").is_positive());
785 ```"),
786                 #[inline]
787                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
788                 pub const fn is_positive(self) -> bool {
789                     self.0.is_positive()
790                 }
791             }
792
793             doc_comment! {
794                 concat!("Returns `true` if `self` is negative and `false` if the number is zero or
795 positive.
796
797 # Examples
798
799 Basic usage:
800
801 ```
802 #![feature(wrapping_int_impl)]
803 use std::num::Wrapping;
804
805 assert!(Wrapping(-10", stringify!($t), ").is_negative());
806 assert!(!Wrapping(10", stringify!($t), ").is_negative());
807 ```"),
808                 #[inline]
809                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
810                 pub const fn is_negative(self) -> bool {
811                     self.0.is_negative()
812                 }
813             }
814         }
815     )*)
816 }
817
818 wrapping_int_impl_signed! { isize i8 i16 i32 i64 i128 }
819
820 macro_rules! wrapping_int_impl_unsigned {
821     ($($t:ty)*) => ($(
822         impl Wrapping<$t> {
823             doc_comment! {
824                 concat!("Returns the number of leading zeros in the binary representation of `self`.
825
826 # Examples
827
828 Basic usage:
829
830 ```
831 #![feature(wrapping_int_impl)]
832 use std::num::Wrapping;
833
834 let n = Wrapping(", stringify!($t), "::max_value()) >> 2;
835
836 assert_eq!(n.leading_zeros(), 2);
837 ```"),
838                 #[inline]
839                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
840                 pub const fn leading_zeros(self) -> u32 {
841                     self.0.leading_zeros()
842                 }
843             }
844
845             doc_comment! {
846                 concat!("Returns `true` if and only if `self == 2^k` for some `k`.
847
848 # Examples
849
850 Basic usage:
851
852 ```
853 #![feature(wrapping_int_impl)]
854 use std::num::Wrapping;
855
856 assert!(Wrapping(16", stringify!($t), ").is_power_of_two());
857 assert!(!Wrapping(10", stringify!($t), ").is_power_of_two());
858 ```"),
859                 #[inline]
860                 #[unstable(feature = "wrapping_int_impl", issue = "32463")]
861                 pub fn is_power_of_two(self) -> bool {
862                     self.0.is_power_of_two()
863                 }
864             }
865
866             doc_comment! {
867                 concat!("Returns the smallest power of two greater than or equal to `self`.
868
869 When return value overflows (i.e., `self > (1 << (N-1))` for type
870 `uN`), overflows to `2^N = 0`.
871
872 # Examples
873
874 Basic usage:
875
876 ```
877 #![feature(wrapping_next_power_of_two)]
878 use std::num::Wrapping;
879
880 assert_eq!(Wrapping(2", stringify!($t), ").next_power_of_two(), Wrapping(2));
881 assert_eq!(Wrapping(3", stringify!($t), ").next_power_of_two(), Wrapping(4));
882 assert_eq!(Wrapping(200_u8).next_power_of_two(), Wrapping(0));
883 ```"),
884                 #[inline]
885                 #[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
886                            reason = "needs decision on wrapping behaviour")]
887                 pub fn next_power_of_two(self) -> Self {
888                     Wrapping(self.0.wrapping_next_power_of_two())
889                 }
890             }
891         }
892     )*)
893 }
894
895 wrapping_int_impl_unsigned! { usize u8 u16 u32 u64 u128 }
896
897 mod shift_max {
898     #![allow(non_upper_case_globals)]
899
900     #[cfg(target_pointer_width = "16")]
901     mod platform {
902         pub const usize: u32 = super::u16;
903         pub const isize: u32 = super::i16;
904     }
905
906     #[cfg(target_pointer_width = "32")]
907     mod platform {
908         pub const usize: u32 = super::u32;
909         pub const isize: u32 = super::i32;
910     }
911
912     #[cfg(target_pointer_width = "64")]
913     mod platform {
914         pub const usize: u32 = super::u64;
915         pub const isize: u32 = super::i64;
916     }
917
918     pub const i8: u32 = (1 << 3) - 1;
919     pub const i16: u32 = (1 << 4) - 1;
920     pub const i32: u32 = (1 << 5) - 1;
921     pub const i64: u32 = (1 << 6) - 1;
922     pub const i128: u32 = (1 << 7) - 1;
923     pub use self::platform::isize;
924
925     pub const u8: u32 = i8;
926     pub const u16: u32 = i16;
927     pub const u32: u32 = i32;
928     pub const u64: u32 = i64;
929     pub const u128: u32 = i128;
930     pub use self::platform::usize;
931 }