]> git.lizzy.rs Git - rust.git/blob - library/core/src/num/uint_macros.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / library / core / src / num / uint_macros.rs
1 macro_rules! uint_impl {
2     ($SelfT:ty, $ActualT:ident, $SignedT:ident, $BITS:expr, $MaxV:expr,
3         $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
4         $reversed:expr, $le_bytes:expr, $be_bytes:expr,
5         $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
6         /// The smallest value that can be represented by this integer type.
7         ///
8         /// # Examples
9         ///
10         /// Basic usage:
11         ///
12         /// ```
13         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, 0);")]
14         /// ```
15         #[stable(feature = "assoc_int_consts", since = "1.43.0")]
16         pub const MIN: Self = 0;
17
18         /// The largest value that can be represented by this integer type,
19         #[doc = concat!("2<sup>", $BITS, "</sup> - 1.")]
20         ///
21         /// # Examples
22         ///
23         /// Basic usage:
24         ///
25         /// ```
26         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($MaxV), ");")]
27         /// ```
28         #[stable(feature = "assoc_int_consts", since = "1.43.0")]
29         pub const MAX: Self = !0;
30
31         /// The size of this integer type in bits.
32         ///
33         /// # Examples
34         ///
35         /// ```
36         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
37         /// ```
38         #[stable(feature = "int_bits_const", since = "1.53.0")]
39         pub const BITS: u32 = $BITS;
40
41         /// Converts a string slice in a given base to an integer.
42         ///
43         /// The string is expected to be an optional `+` sign
44         /// followed by digits.
45         /// Leading and trailing whitespace represent an error.
46         /// Digits are a subset of these characters, depending on `radix`:
47         ///
48         /// * `0-9`
49         /// * `a-z`
50         /// * `A-Z`
51         ///
52         /// # Panics
53         ///
54         /// This function panics if `radix` is not in the range from 2 to 36.
55         ///
56         /// # Examples
57         ///
58         /// Basic usage:
59         ///
60         /// ```
61         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
62         /// ```
63         #[stable(feature = "rust1", since = "1.0.0")]
64         pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
65             from_str_radix(src, radix)
66         }
67
68         /// Returns the number of ones in the binary representation of `self`.
69         ///
70         /// # Examples
71         ///
72         /// Basic usage:
73         ///
74         /// ```
75         #[doc = concat!("let n = 0b01001100", stringify!($SelfT), ";")]
76         ///
77         /// assert_eq!(n.count_ones(), 3);
78         /// ```
79         #[stable(feature = "rust1", since = "1.0.0")]
80         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
81         #[doc(alias = "popcount")]
82         #[doc(alias = "popcnt")]
83         #[must_use = "this returns the result of the operation, \
84                       without modifying the original"]
85         #[inline(always)]
86         pub const fn count_ones(self) -> u32 {
87             intrinsics::ctpop(self as $ActualT) as u32
88         }
89
90         /// Returns the number of zeros in the binary representation of `self`.
91         ///
92         /// # Examples
93         ///
94         /// Basic usage:
95         ///
96         /// ```
97         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 0);")]
98         /// ```
99         #[stable(feature = "rust1", since = "1.0.0")]
100         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
101         #[must_use = "this returns the result of the operation, \
102                       without modifying the original"]
103         #[inline(always)]
104         pub const fn count_zeros(self) -> u32 {
105             (!self).count_ones()
106         }
107
108         /// Returns the number of leading zeros in the binary representation of `self`.
109         ///
110         /// # Examples
111         ///
112         /// Basic usage:
113         ///
114         /// ```
115         #[doc = concat!("let n = ", stringify!($SelfT), "::MAX >> 2;")]
116         ///
117         /// assert_eq!(n.leading_zeros(), 2);
118         /// ```
119         #[stable(feature = "rust1", since = "1.0.0")]
120         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
121         #[must_use = "this returns the result of the operation, \
122                       without modifying the original"]
123         #[inline(always)]
124         pub const fn leading_zeros(self) -> u32 {
125             intrinsics::ctlz(self as $ActualT) as u32
126         }
127
128         /// Returns the number of trailing zeros in the binary representation
129         /// of `self`.
130         ///
131         /// # Examples
132         ///
133         /// Basic usage:
134         ///
135         /// ```
136         #[doc = concat!("let n = 0b0101000", stringify!($SelfT), ";")]
137         ///
138         /// assert_eq!(n.trailing_zeros(), 3);
139         /// ```
140         #[stable(feature = "rust1", since = "1.0.0")]
141         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
142         #[must_use = "this returns the result of the operation, \
143                       without modifying the original"]
144         #[inline(always)]
145         pub const fn trailing_zeros(self) -> u32 {
146             intrinsics::cttz(self) as u32
147         }
148
149         /// Returns the number of leading ones in the binary representation of `self`.
150         ///
151         /// # Examples
152         ///
153         /// Basic usage:
154         ///
155         /// ```
156         #[doc = concat!("let n = !(", stringify!($SelfT), "::MAX >> 2);")]
157         ///
158         /// assert_eq!(n.leading_ones(), 2);
159         /// ```
160         #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
161         #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
162         #[must_use = "this returns the result of the operation, \
163                       without modifying the original"]
164         #[inline(always)]
165         pub const fn leading_ones(self) -> u32 {
166             (!self).leading_zeros()
167         }
168
169         /// Returns the number of trailing ones in the binary representation
170         /// of `self`.
171         ///
172         /// # Examples
173         ///
174         /// Basic usage:
175         ///
176         /// ```
177         #[doc = concat!("let n = 0b1010111", stringify!($SelfT), ";")]
178         ///
179         /// assert_eq!(n.trailing_ones(), 3);
180         /// ```
181         #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
182         #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
183         #[must_use = "this returns the result of the operation, \
184                       without modifying the original"]
185         #[inline(always)]
186         pub const fn trailing_ones(self) -> u32 {
187             (!self).trailing_zeros()
188         }
189
190         /// Shifts the bits to the left by a specified amount, `n`,
191         /// wrapping the truncated bits to the end of the resulting integer.
192         ///
193         /// Please note this isn't the same operation as the `<<` shifting operator!
194         ///
195         /// # Examples
196         ///
197         /// Basic usage:
198         ///
199         /// ```
200         #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
201         #[doc = concat!("let m = ", $rot_result, ";")]
202         ///
203         #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
204         /// ```
205         #[stable(feature = "rust1", since = "1.0.0")]
206         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
207         #[must_use = "this returns the result of the operation, \
208                       without modifying the original"]
209         #[inline(always)]
210         pub const fn rotate_left(self, n: u32) -> Self {
211             intrinsics::rotate_left(self, n as $SelfT)
212         }
213
214         /// Shifts the bits to the right by a specified amount, `n`,
215         /// wrapping the truncated bits to the beginning of the resulting
216         /// integer.
217         ///
218         /// Please note this isn't the same operation as the `>>` shifting operator!
219         ///
220         /// # Examples
221         ///
222         /// Basic usage:
223         ///
224         /// ```
225         #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
226         #[doc = concat!("let m = ", $rot_op, ";")]
227         ///
228         #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
229         /// ```
230         #[stable(feature = "rust1", since = "1.0.0")]
231         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
232         #[must_use = "this returns the result of the operation, \
233                       without modifying the original"]
234         #[inline(always)]
235         pub const fn rotate_right(self, n: u32) -> Self {
236             intrinsics::rotate_right(self, n as $SelfT)
237         }
238
239         /// Reverses the byte order of the integer.
240         ///
241         /// # Examples
242         ///
243         /// Basic usage:
244         ///
245         /// ```
246         #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
247         /// let m = n.swap_bytes();
248         ///
249         #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
250         /// ```
251         #[stable(feature = "rust1", since = "1.0.0")]
252         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
253         #[must_use = "this returns the result of the operation, \
254                       without modifying the original"]
255         #[inline(always)]
256         pub const fn swap_bytes(self) -> Self {
257             intrinsics::bswap(self as $ActualT) as Self
258         }
259
260         /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
261         ///                 second least-significant bit becomes second most-significant bit, etc.
262         ///
263         /// # Examples
264         ///
265         /// Basic usage:
266         ///
267         /// ```
268         #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
269         /// let m = n.reverse_bits();
270         ///
271         #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
272         #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
273         /// ```
274         #[stable(feature = "reverse_bits", since = "1.37.0")]
275         #[rustc_const_stable(feature = "const_math", since = "1.37.0")]
276         #[must_use = "this returns the result of the operation, \
277                       without modifying the original"]
278         #[inline(always)]
279         pub const fn reverse_bits(self) -> Self {
280             intrinsics::bitreverse(self as $ActualT) as Self
281         }
282
283         /// Converts an integer from big endian to the target's endianness.
284         ///
285         /// On big endian this is a no-op. On little endian the bytes are
286         /// swapped.
287         ///
288         /// # Examples
289         ///
290         /// Basic usage:
291         ///
292         /// ```
293         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
294         ///
295         /// if cfg!(target_endian = "big") {
296         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
297         /// } else {
298         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
299         /// }
300         /// ```
301         #[stable(feature = "rust1", since = "1.0.0")]
302         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
303         #[must_use]
304         #[inline(always)]
305         pub const fn from_be(x: Self) -> Self {
306             #[cfg(target_endian = "big")]
307             {
308                 x
309             }
310             #[cfg(not(target_endian = "big"))]
311             {
312                 x.swap_bytes()
313             }
314         }
315
316         /// Converts an integer from little endian to the target's endianness.
317         ///
318         /// On little endian this is a no-op. On big endian the bytes are
319         /// swapped.
320         ///
321         /// # Examples
322         ///
323         /// Basic usage:
324         ///
325         /// ```
326         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
327         ///
328         /// if cfg!(target_endian = "little") {
329         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
330         /// } else {
331         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
332         /// }
333         /// ```
334         #[stable(feature = "rust1", since = "1.0.0")]
335         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
336         #[must_use]
337         #[inline(always)]
338         pub const fn from_le(x: Self) -> Self {
339             #[cfg(target_endian = "little")]
340             {
341                 x
342             }
343             #[cfg(not(target_endian = "little"))]
344             {
345                 x.swap_bytes()
346             }
347         }
348
349         /// Converts `self` to big endian from the target's endianness.
350         ///
351         /// On big endian this is a no-op. On little endian the bytes are
352         /// swapped.
353         ///
354         /// # Examples
355         ///
356         /// Basic usage:
357         ///
358         /// ```
359         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
360         ///
361         /// if cfg!(target_endian = "big") {
362         ///     assert_eq!(n.to_be(), n)
363         /// } else {
364         ///     assert_eq!(n.to_be(), n.swap_bytes())
365         /// }
366         /// ```
367         #[stable(feature = "rust1", since = "1.0.0")]
368         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
369         #[must_use = "this returns the result of the operation, \
370                       without modifying the original"]
371         #[inline(always)]
372         pub const fn to_be(self) -> Self { // or not to be?
373             #[cfg(target_endian = "big")]
374             {
375                 self
376             }
377             #[cfg(not(target_endian = "big"))]
378             {
379                 self.swap_bytes()
380             }
381         }
382
383         /// Converts `self` to little endian from the target's endianness.
384         ///
385         /// On little endian this is a no-op. On big endian the bytes are
386         /// swapped.
387         ///
388         /// # Examples
389         ///
390         /// Basic usage:
391         ///
392         /// ```
393         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
394         ///
395         /// if cfg!(target_endian = "little") {
396         ///     assert_eq!(n.to_le(), n)
397         /// } else {
398         ///     assert_eq!(n.to_le(), n.swap_bytes())
399         /// }
400         /// ```
401         #[stable(feature = "rust1", since = "1.0.0")]
402         #[rustc_const_stable(feature = "const_math", since = "1.32.0")]
403         #[must_use = "this returns the result of the operation, \
404                       without modifying the original"]
405         #[inline(always)]
406         pub const fn to_le(self) -> Self {
407             #[cfg(target_endian = "little")]
408             {
409                 self
410             }
411             #[cfg(not(target_endian = "little"))]
412             {
413                 self.swap_bytes()
414             }
415         }
416
417         /// Checked integer addition. Computes `self + rhs`, returning `None`
418         /// if overflow occurred.
419         ///
420         /// # Examples
421         ///
422         /// Basic usage:
423         ///
424         /// ```
425         #[doc = concat!(
426             "assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), ",
427             "Some(", stringify!($SelfT), "::MAX - 1));"
428         )]
429         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
430         /// ```
431         #[stable(feature = "rust1", since = "1.0.0")]
432         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
433         #[must_use = "this returns the result of the operation, \
434                       without modifying the original"]
435         #[inline]
436         pub const fn checked_add(self, rhs: Self) -> Option<Self> {
437             let (a, b) = self.overflowing_add(rhs);
438             if unlikely!(b) {None} else {Some(a)}
439         }
440
441         /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
442         /// cannot occur.
443         ///
444         /// # Safety
445         ///
446         /// This results in undefined behavior when
447         #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
448         /// i.e. when [`checked_add`] would return `None`.
449         ///
450         #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
451         #[unstable(
452             feature = "unchecked_math",
453             reason = "niche optimization path",
454             issue = "85122",
455         )]
456         #[must_use = "this returns the result of the operation, \
457                       without modifying the original"]
458         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
459         #[inline(always)]
460         pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
461             // SAFETY: the caller must uphold the safety contract for
462             // `unchecked_add`.
463             unsafe { intrinsics::unchecked_add(self, rhs) }
464         }
465
466         /// Checked addition with a signed integer. Computes `self + rhs`,
467         /// returning `None` if overflow occurred.
468         ///
469         /// # Examples
470         ///
471         /// Basic usage:
472         ///
473         /// ```
474         /// # #![feature(mixed_integer_ops)]
475         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_signed(2), Some(3));")]
476         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_signed(-2), None);")]
477         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_signed(3), None);")]
478         /// ```
479         #[unstable(feature = "mixed_integer_ops", issue = "87840")]
480         #[rustc_const_unstable(feature = "mixed_integer_ops", issue = "87840")]
481         #[must_use = "this returns the result of the operation, \
482                       without modifying the original"]
483         #[inline]
484         pub const fn checked_add_signed(self, rhs: $SignedT) -> Option<Self> {
485             let (a, b) = self.overflowing_add_signed(rhs);
486             if unlikely!(b) {None} else {Some(a)}
487         }
488
489         /// Checked integer subtraction. Computes `self - rhs`, returning
490         /// `None` if overflow occurred.
491         ///
492         /// # Examples
493         ///
494         /// Basic usage:
495         ///
496         /// ```
497         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub(1), Some(0));")]
498         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);")]
499         /// ```
500         #[stable(feature = "rust1", since = "1.0.0")]
501         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
502         #[must_use = "this returns the result of the operation, \
503                       without modifying the original"]
504         #[inline]
505         pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
506             let (a, b) = self.overflowing_sub(rhs);
507             if unlikely!(b) {None} else {Some(a)}
508         }
509
510         /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
511         /// cannot occur.
512         ///
513         /// # Safety
514         ///
515         /// This results in undefined behavior when
516         #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
517         /// i.e. when [`checked_sub`] would return `None`.
518         ///
519         #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
520         #[unstable(
521             feature = "unchecked_math",
522             reason = "niche optimization path",
523             issue = "85122",
524         )]
525         #[must_use = "this returns the result of the operation, \
526                       without modifying the original"]
527         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
528         #[inline(always)]
529         pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
530             // SAFETY: the caller must uphold the safety contract for
531             // `unchecked_sub`.
532             unsafe { intrinsics::unchecked_sub(self, rhs) }
533         }
534
535         /// Checked integer multiplication. Computes `self * rhs`, returning
536         /// `None` if overflow occurred.
537         ///
538         /// # Examples
539         ///
540         /// Basic usage:
541         ///
542         /// ```
543         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_mul(1), Some(5));")]
544         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
545         /// ```
546         #[stable(feature = "rust1", since = "1.0.0")]
547         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
548         #[must_use = "this returns the result of the operation, \
549                       without modifying the original"]
550         #[inline]
551         pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
552             let (a, b) = self.overflowing_mul(rhs);
553             if unlikely!(b) {None} else {Some(a)}
554         }
555
556         /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
557         /// cannot occur.
558         ///
559         /// # Safety
560         ///
561         /// This results in undefined behavior when
562         #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
563         /// i.e. when [`checked_mul`] would return `None`.
564         ///
565         #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
566         #[unstable(
567             feature = "unchecked_math",
568             reason = "niche optimization path",
569             issue = "85122",
570         )]
571         #[must_use = "this returns the result of the operation, \
572                       without modifying the original"]
573         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
574         #[inline(always)]
575         pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
576             // SAFETY: the caller must uphold the safety contract for
577             // `unchecked_mul`.
578             unsafe { intrinsics::unchecked_mul(self, rhs) }
579         }
580
581         /// Checked integer division. Computes `self / rhs`, returning `None`
582         /// if `rhs == 0`.
583         ///
584         /// # Examples
585         ///
586         /// Basic usage:
587         ///
588         /// ```
589         #[doc = concat!("assert_eq!(128", stringify!($SelfT), ".checked_div(2), Some(64));")]
590         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_div(0), None);")]
591         /// ```
592         #[stable(feature = "rust1", since = "1.0.0")]
593         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.52.0")]
594         #[must_use = "this returns the result of the operation, \
595                       without modifying the original"]
596         #[inline]
597         pub const fn checked_div(self, rhs: Self) -> Option<Self> {
598             if unlikely!(rhs == 0) {
599                 None
600             } else {
601                 // SAFETY: div by zero has been checked above and unsigned types have no other
602                 // failure modes for division
603                 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
604             }
605         }
606
607         /// Checked Euclidean division. Computes `self.div_euclid(rhs)`, returning `None`
608         /// if `rhs == 0`.
609         ///
610         /// # Examples
611         ///
612         /// Basic usage:
613         ///
614         /// ```
615         #[doc = concat!("assert_eq!(128", stringify!($SelfT), ".checked_div_euclid(2), Some(64));")]
616         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);")]
617         /// ```
618         #[stable(feature = "euclidean_division", since = "1.38.0")]
619         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
620         #[must_use = "this returns the result of the operation, \
621                       without modifying the original"]
622         #[inline]
623         pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
624             if unlikely!(rhs == 0) {
625                 None
626             } else {
627                 Some(self.div_euclid(rhs))
628             }
629         }
630
631
632         /// Checked integer remainder. Computes `self % rhs`, returning `None`
633         /// if `rhs == 0`.
634         ///
635         /// # Examples
636         ///
637         /// Basic usage:
638         ///
639         /// ```
640         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
641         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
642         /// ```
643         #[stable(feature = "wrapping", since = "1.7.0")]
644         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.52.0")]
645         #[must_use = "this returns the result of the operation, \
646                       without modifying the original"]
647         #[inline]
648         pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
649             if unlikely!(rhs == 0) {
650                 None
651             } else {
652                 // SAFETY: div by zero has been checked above and unsigned types have no other
653                 // failure modes for division
654                 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
655             }
656         }
657
658         /// Checked Euclidean modulo. Computes `self.rem_euclid(rhs)`, returning `None`
659         /// if `rhs == 0`.
660         ///
661         /// # Examples
662         ///
663         /// Basic usage:
664         ///
665         /// ```
666         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
667         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
668         /// ```
669         #[stable(feature = "euclidean_division", since = "1.38.0")]
670         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
671         #[must_use = "this returns the result of the operation, \
672                       without modifying the original"]
673         #[inline]
674         pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
675             if unlikely!(rhs == 0) {
676                 None
677             } else {
678                 Some(self.rem_euclid(rhs))
679             }
680         }
681
682         /// Returns the logarithm of the number with respect to an arbitrary base,
683         /// rounded down.
684         ///
685         /// This method might not be optimized owing to implementation details;
686         /// `log2` can produce results more efficiently for base 2, and `log10`
687         /// can produce results more efficiently for base 10.
688         ///
689         /// # Panics
690         ///
691         /// When the number is negative, zero, or if the base is not at least 2;
692         /// it panics in debug mode and the return value is 0 in release mode.
693         ///
694         /// # Examples
695         ///
696         /// ```
697         /// #![feature(int_log)]
698         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".log(5), 1);")]
699         /// ```
700         #[unstable(feature = "int_log", issue = "70887")]
701         #[must_use = "this returns the result of the operation, \
702                       without modifying the original"]
703         #[inline]
704         #[track_caller]
705         #[rustc_inherit_overflow_checks]
706         #[allow(arithmetic_overflow)]
707         pub const fn log(self, base: Self) -> u32 {
708             match self.checked_log(base) {
709                 Some(n) => n,
710                 None => {
711                     // In debug builds, trigger a panic on None.
712                     // This should optimize completely out in release builds.
713                     let _ = Self::MAX + 1;
714
715                     0
716                 },
717             }
718         }
719
720         /// Returns the base 2 logarithm of the number, rounded down.
721         ///
722         /// # Panics
723         ///
724         /// When the number is negative or zero it panics in debug mode and
725         /// the return value is 0 in release mode.
726         ///
727         /// # Examples
728         ///
729         /// ```
730         /// #![feature(int_log)]
731         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".log2(), 1);")]
732         /// ```
733         #[unstable(feature = "int_log", issue = "70887")]
734         #[must_use = "this returns the result of the operation, \
735                       without modifying the original"]
736         #[inline]
737         #[track_caller]
738         #[rustc_inherit_overflow_checks]
739         #[allow(arithmetic_overflow)]
740         pub const fn log2(self) -> u32 {
741             match self.checked_log2() {
742                 Some(n) => n,
743                 None => {
744                     // In debug builds, trigger a panic on None.
745                     // This should optimize completely out in release builds.
746                     let _ = Self::MAX + 1;
747
748                     0
749                 },
750             }
751         }
752
753         /// Returns the base 10 logarithm of the number, rounded down.
754         ///
755         /// # Panics
756         ///
757         /// When the number is negative or zero it panics in debug mode and the
758         /// return value is 0 in release mode.
759         ///
760         /// # Example
761         ///
762         /// ```
763         /// #![feature(int_log)]
764         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".log10(), 1);")]
765         /// ```
766         #[unstable(feature = "int_log", issue = "70887")]
767         #[must_use = "this returns the result of the operation, \
768                       without modifying the original"]
769         #[inline]
770         #[track_caller]
771         #[rustc_inherit_overflow_checks]
772         #[allow(arithmetic_overflow)]
773         pub const fn log10(self) -> u32 {
774             match self.checked_log10() {
775                 Some(n) => n,
776                 None => {
777                     // In debug builds, trigger a panic on None.
778                     // This should optimize completely out in release builds.
779                     let _ = Self::MAX + 1;
780
781                     0
782                 },
783             }
784         }
785
786         /// Returns the logarithm of the number with respect to an arbitrary base,
787         /// rounded down.
788         ///
789         /// Returns `None` if the number is zero, or if the base is not at least 2.
790         ///
791         /// This method might not be optimized owing to implementation details;
792         /// `checked_log2` can produce results more efficiently for base 2, and
793         /// `checked_log10` can produce results more efficiently for base 10.
794         ///
795         /// # Examples
796         ///
797         /// ```
798         /// #![feature(int_log)]
799         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_log(5), Some(1));")]
800         /// ```
801         #[unstable(feature = "int_log", issue = "70887")]
802         #[must_use = "this returns the result of the operation, \
803                       without modifying the original"]
804         #[inline]
805         pub const fn checked_log(self, base: Self) -> Option<u32> {
806             if self <= 0 || base <= 1 {
807                 None
808             } else {
809                 let mut n = 0;
810                 let mut r = self;
811
812                 // Optimization for 128 bit wide integers.
813                 if Self::BITS == 128 {
814                     let b = Self::log2(self) / (Self::log2(base) + 1);
815                     n += b;
816                     r /= base.pow(b as u32);
817                 }
818
819                 while r >= base {
820                     r /= base;
821                     n += 1;
822                 }
823                 Some(n)
824             }
825         }
826
827         /// Returns the base 2 logarithm of the number, rounded down.
828         ///
829         /// Returns `None` if the number is zero.
830         ///
831         /// # Examples
832         ///
833         /// ```
834         /// #![feature(int_log)]
835         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_log2(), Some(1));")]
836         /// ```
837         #[unstable(feature = "int_log", issue = "70887")]
838         #[must_use = "this returns the result of the operation, \
839                       without modifying the original"]
840         #[inline]
841         pub const fn checked_log2(self) -> Option<u32> {
842             if self <= 0 {
843                 None
844             } else {
845                 // SAFETY: We just checked that this number is positive
846                 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
847                 Some(log)
848             }
849         }
850
851         /// Returns the base 10 logarithm of the number, rounded down.
852         ///
853         /// Returns `None` if the number is zero.
854         ///
855         /// # Examples
856         ///
857         /// ```
858         /// #![feature(int_log)]
859         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_log10(), Some(1));")]
860         /// ```
861         #[unstable(feature = "int_log", issue = "70887")]
862         #[must_use = "this returns the result of the operation, \
863                       without modifying the original"]
864         #[inline]
865         pub const fn checked_log10(self) -> Option<u32> {
866             int_log10::$ActualT(self as $ActualT)
867         }
868
869         /// Checked negation. Computes `-self`, returning `None` unless `self ==
870         /// 0`.
871         ///
872         /// Note that negating any positive integer will overflow.
873         ///
874         /// # Examples
875         ///
876         /// Basic usage:
877         ///
878         /// ```
879         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".checked_neg(), Some(0));")]
880         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);")]
881         /// ```
882         #[stable(feature = "wrapping", since = "1.7.0")]
883         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
884         #[must_use = "this returns the result of the operation, \
885                       without modifying the original"]
886         #[inline]
887         pub const fn checked_neg(self) -> Option<Self> {
888             let (a, b) = self.overflowing_neg();
889             if unlikely!(b) {None} else {Some(a)}
890         }
891
892         /// Checked shift left. Computes `self << rhs`, returning `None`
893         /// if `rhs` is larger than or equal to the number of bits in `self`.
894         ///
895         /// # Examples
896         ///
897         /// Basic usage:
898         ///
899         /// ```
900         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
901         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);")]
902         /// ```
903         #[stable(feature = "wrapping", since = "1.7.0")]
904         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
905         #[must_use = "this returns the result of the operation, \
906                       without modifying the original"]
907         #[inline]
908         pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
909             let (a, b) = self.overflowing_shl(rhs);
910             if unlikely!(b) {None} else {Some(a)}
911         }
912
913         /// Unchecked shift left. Computes `self << rhs`, assuming that
914         /// `rhs` is less than the number of bits in `self`.
915         ///
916         /// # Safety
917         ///
918         /// This results in undefined behavior if `rhs` is larger than
919         /// or equal to the number of bits in `self`,
920         /// i.e. when [`checked_shl`] would return `None`.
921         ///
922         #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
923         #[unstable(
924             feature = "unchecked_math",
925             reason = "niche optimization path",
926             issue = "85122",
927         )]
928         #[must_use = "this returns the result of the operation, \
929                       without modifying the original"]
930         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
931         #[inline(always)]
932         pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
933             // SAFETY: the caller must uphold the safety contract for
934             // `unchecked_shl`.
935             unsafe { intrinsics::unchecked_shl(self, rhs) }
936         }
937
938         /// Checked shift right. Computes `self >> rhs`, returning `None`
939         /// if `rhs` is larger than or equal to the number of bits in `self`.
940         ///
941         /// # Examples
942         ///
943         /// Basic usage:
944         ///
945         /// ```
946         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
947         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);")]
948         /// ```
949         #[stable(feature = "wrapping", since = "1.7.0")]
950         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
951         #[must_use = "this returns the result of the operation, \
952                       without modifying the original"]
953         #[inline]
954         pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
955             let (a, b) = self.overflowing_shr(rhs);
956             if unlikely!(b) {None} else {Some(a)}
957         }
958
959         /// Unchecked shift right. Computes `self >> rhs`, assuming that
960         /// `rhs` is less than the number of bits in `self`.
961         ///
962         /// # Safety
963         ///
964         /// This results in undefined behavior if `rhs` is larger than
965         /// or equal to the number of bits in `self`,
966         /// i.e. when [`checked_shr`] would return `None`.
967         ///
968         #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
969         #[unstable(
970             feature = "unchecked_math",
971             reason = "niche optimization path",
972             issue = "85122",
973         )]
974         #[must_use = "this returns the result of the operation, \
975                       without modifying the original"]
976         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
977         #[inline(always)]
978         pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
979             // SAFETY: the caller must uphold the safety contract for
980             // `unchecked_shr`.
981             unsafe { intrinsics::unchecked_shr(self, rhs) }
982         }
983
984         /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
985         /// overflow occurred.
986         ///
987         /// # Examples
988         ///
989         /// Basic usage:
990         ///
991         /// ```
992         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_pow(5), Some(32));")]
993         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
994         /// ```
995         #[stable(feature = "no_panic_pow", since = "1.34.0")]
996         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
997         #[must_use = "this returns the result of the operation, \
998                       without modifying the original"]
999         #[inline]
1000         pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1001             if exp == 0 {
1002                 return Some(1);
1003             }
1004             let mut base = self;
1005             let mut acc: Self = 1;
1006
1007             while exp > 1 {
1008                 if (exp & 1) == 1 {
1009                     acc = try_opt!(acc.checked_mul(base));
1010                 }
1011                 exp /= 2;
1012                 base = try_opt!(base.checked_mul(base));
1013             }
1014
1015             // since exp!=0, finally the exp must be 1.
1016             // Deal with the final bit of the exponent separately, since
1017             // squaring the base afterwards is not necessary and may cause a
1018             // needless overflow.
1019
1020             Some(try_opt!(acc.checked_mul(base)))
1021         }
1022
1023         /// Saturating integer addition. Computes `self + rhs`, saturating at
1024         /// the numeric bounds instead of overflowing.
1025         ///
1026         /// # Examples
1027         ///
1028         /// Basic usage:
1029         ///
1030         /// ```
1031         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1032         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(127), ", stringify!($SelfT), "::MAX);")]
1033         /// ```
1034         #[stable(feature = "rust1", since = "1.0.0")]
1035         #[must_use = "this returns the result of the operation, \
1036                       without modifying the original"]
1037         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1038         #[inline(always)]
1039         pub const fn saturating_add(self, rhs: Self) -> Self {
1040             intrinsics::saturating_add(self, rhs)
1041         }
1042
1043         /// Saturating addition with a signed integer. Computes `self + rhs`,
1044         /// saturating at the numeric bounds instead of overflowing.
1045         ///
1046         /// # Examples
1047         ///
1048         /// Basic usage:
1049         ///
1050         /// ```
1051         /// # #![feature(mixed_integer_ops)]
1052         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_signed(2), 3);")]
1053         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_signed(-2), 0);")]
1054         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).saturating_add_signed(4), ", stringify!($SelfT), "::MAX);")]
1055         /// ```
1056         #[unstable(feature = "mixed_integer_ops", issue = "87840")]
1057         #[rustc_const_unstable(feature = "mixed_integer_ops", issue = "87840")]
1058         #[must_use = "this returns the result of the operation, \
1059                       without modifying the original"]
1060         #[inline]
1061         pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self {
1062             let (res, overflow) = self.overflowing_add(rhs as Self);
1063             if overflow == (rhs < 0) {
1064                 res
1065             } else if overflow {
1066                 Self::MAX
1067             } else {
1068                 0
1069             }
1070         }
1071
1072         /// Saturating integer subtraction. Computes `self - rhs`, saturating
1073         /// at the numeric bounds instead of overflowing.
1074         ///
1075         /// # Examples
1076         ///
1077         /// Basic usage:
1078         ///
1079         /// ```
1080         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(27), 73);")]
1081         #[doc = concat!("assert_eq!(13", stringify!($SelfT), ".saturating_sub(127), 0);")]
1082         /// ```
1083         #[stable(feature = "rust1", since = "1.0.0")]
1084         #[must_use = "this returns the result of the operation, \
1085                       without modifying the original"]
1086         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1087         #[inline(always)]
1088         pub const fn saturating_sub(self, rhs: Self) -> Self {
1089             intrinsics::saturating_sub(self, rhs)
1090         }
1091
1092         /// Saturating integer multiplication. Computes `self * rhs`,
1093         /// saturating at the numeric bounds instead of overflowing.
1094         ///
1095         /// # Examples
1096         ///
1097         /// Basic usage:
1098         ///
1099         /// ```
1100         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".saturating_mul(10), 20);")]
1101         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).saturating_mul(10), ", stringify!($SelfT),"::MAX);")]
1102         /// ```
1103         #[stable(feature = "wrapping", since = "1.7.0")]
1104         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1105         #[must_use = "this returns the result of the operation, \
1106                       without modifying the original"]
1107         #[inline]
1108         pub const fn saturating_mul(self, rhs: Self) -> Self {
1109             match self.checked_mul(rhs) {
1110                 Some(x) => x,
1111                 None => Self::MAX,
1112             }
1113         }
1114
1115         /// Saturating integer division. Computes `self / rhs`, saturating at the
1116         /// numeric bounds instead of overflowing.
1117         ///
1118         /// # Examples
1119         ///
1120         /// Basic usage:
1121         ///
1122         /// ```
1123         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1124         ///
1125         /// ```
1126         ///
1127         /// ```should_panic
1128         #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1129         ///
1130         /// ```
1131         #[stable(feature = "saturating_div", since = "1.58.0")]
1132         #[must_use = "this returns the result of the operation, \
1133                       without modifying the original"]
1134         #[inline]
1135         pub const fn saturating_div(self, rhs: Self) -> Self {
1136             // on unsigned types, there is no overflow in integer division
1137             self.wrapping_div(rhs)
1138         }
1139
1140         /// Saturating integer exponentiation. Computes `self.pow(exp)`,
1141         /// saturating at the numeric bounds instead of overflowing.
1142         ///
1143         /// # Examples
1144         ///
1145         /// Basic usage:
1146         ///
1147         /// ```
1148         #[doc = concat!("assert_eq!(4", stringify!($SelfT), ".saturating_pow(3), 64);")]
1149         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
1150         /// ```
1151         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1152         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1153         #[must_use = "this returns the result of the operation, \
1154                       without modifying the original"]
1155         #[inline]
1156         pub const fn saturating_pow(self, exp: u32) -> Self {
1157             match self.checked_pow(exp) {
1158                 Some(x) => x,
1159                 None => Self::MAX,
1160             }
1161         }
1162
1163         /// Wrapping (modular) addition. Computes `self + rhs`,
1164         /// wrapping around at the boundary of the type.
1165         ///
1166         /// # Examples
1167         ///
1168         /// Basic usage:
1169         ///
1170         /// ```
1171         #[doc = concat!("assert_eq!(200", stringify!($SelfT), ".wrapping_add(55), 255);")]
1172         #[doc = concat!("assert_eq!(200", stringify!($SelfT), ".wrapping_add(", stringify!($SelfT), "::MAX), 199);")]
1173         /// ```
1174         #[stable(feature = "rust1", since = "1.0.0")]
1175         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1176         #[must_use = "this returns the result of the operation, \
1177                       without modifying the original"]
1178         #[inline(always)]
1179         pub const fn wrapping_add(self, rhs: Self) -> Self {
1180             intrinsics::wrapping_add(self, rhs)
1181         }
1182
1183         /// Wrapping (modular) addition with a signed integer. Computes
1184         /// `self + rhs`, wrapping around at the boundary of the type.
1185         ///
1186         /// # Examples
1187         ///
1188         /// Basic usage:
1189         ///
1190         /// ```
1191         /// # #![feature(mixed_integer_ops)]
1192         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_add_signed(2), 3);")]
1193         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_add_signed(-2), ", stringify!($SelfT), "::MAX);")]
1194         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).wrapping_add_signed(4), 1);")]
1195         /// ```
1196         #[unstable(feature = "mixed_integer_ops", issue = "87840")]
1197         #[rustc_const_unstable(feature = "mixed_integer_ops", issue = "87840")]
1198         #[must_use = "this returns the result of the operation, \
1199                       without modifying the original"]
1200         #[inline]
1201         pub const fn wrapping_add_signed(self, rhs: $SignedT) -> Self {
1202             self.wrapping_add(rhs as Self)
1203         }
1204
1205         /// Wrapping (modular) subtraction. Computes `self - rhs`,
1206         /// wrapping around at the boundary of the type.
1207         ///
1208         /// # Examples
1209         ///
1210         /// Basic usage:
1211         ///
1212         /// ```
1213         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_sub(100), 0);")]
1214         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_sub(", stringify!($SelfT), "::MAX), 101);")]
1215         /// ```
1216         #[stable(feature = "rust1", since = "1.0.0")]
1217         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1218         #[must_use = "this returns the result of the operation, \
1219                       without modifying the original"]
1220         #[inline(always)]
1221         pub const fn wrapping_sub(self, rhs: Self) -> Self {
1222             intrinsics::wrapping_sub(self, rhs)
1223         }
1224
1225         /// Wrapping (modular) multiplication. Computes `self *
1226         /// rhs`, wrapping around at the boundary of the type.
1227         ///
1228         /// # Examples
1229         ///
1230         /// Basic usage:
1231         ///
1232         /// Please note that this example is shared between integer types.
1233         /// Which explains why `u8` is used here.
1234         ///
1235         /// ```
1236         /// assert_eq!(10u8.wrapping_mul(12), 120);
1237         /// assert_eq!(25u8.wrapping_mul(12), 44);
1238         /// ```
1239         #[stable(feature = "rust1", since = "1.0.0")]
1240         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1241         #[must_use = "this returns the result of the operation, \
1242                       without modifying the original"]
1243         #[inline(always)]
1244         pub const fn wrapping_mul(self, rhs: Self) -> Self {
1245             intrinsics::wrapping_mul(self, rhs)
1246         }
1247
1248         /// Wrapping (modular) division. Computes `self / rhs`.
1249         /// Wrapped division on unsigned types is just normal division.
1250         /// There's no way wrapping could ever happen.
1251         /// This function exists, so that all operations
1252         /// are accounted for in the wrapping operations.
1253         ///
1254         /// # Examples
1255         ///
1256         /// Basic usage:
1257         ///
1258         /// ```
1259         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
1260         /// ```
1261         #[stable(feature = "num_wrapping", since = "1.2.0")]
1262         #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1263         #[must_use = "this returns the result of the operation, \
1264                       without modifying the original"]
1265         #[inline(always)]
1266         pub const fn wrapping_div(self, rhs: Self) -> Self {
1267             self / rhs
1268         }
1269
1270         /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`.
1271         /// Wrapped division on unsigned types is just normal division.
1272         /// There's no way wrapping could ever happen.
1273         /// This function exists, so that all operations
1274         /// are accounted for in the wrapping operations.
1275         /// Since, for the positive integers, all common
1276         /// definitions of division are equal, this
1277         /// is exactly equal to `self.wrapping_div(rhs)`.
1278         ///
1279         /// # Examples
1280         ///
1281         /// Basic usage:
1282         ///
1283         /// ```
1284         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
1285         /// ```
1286         #[stable(feature = "euclidean_division", since = "1.38.0")]
1287         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1288         #[must_use = "this returns the result of the operation, \
1289                       without modifying the original"]
1290         #[inline(always)]
1291         pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
1292             self / rhs
1293         }
1294
1295         /// Wrapping (modular) remainder. Computes `self % rhs`.
1296         /// Wrapped remainder calculation on unsigned types is
1297         /// just the regular remainder calculation.
1298         /// There's no way wrapping could ever happen.
1299         /// This function exists, so that all operations
1300         /// are accounted for in the wrapping operations.
1301         ///
1302         /// # Examples
1303         ///
1304         /// Basic usage:
1305         ///
1306         /// ```
1307         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
1308         /// ```
1309         #[stable(feature = "num_wrapping", since = "1.2.0")]
1310         #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1311         #[must_use = "this returns the result of the operation, \
1312                       without modifying the original"]
1313         #[inline(always)]
1314         pub const fn wrapping_rem(self, rhs: Self) -> Self {
1315             self % rhs
1316         }
1317
1318         /// Wrapping Euclidean modulo. Computes `self.rem_euclid(rhs)`.
1319         /// Wrapped modulo calculation on unsigned types is
1320         /// just the regular remainder calculation.
1321         /// There's no way wrapping could ever happen.
1322         /// This function exists, so that all operations
1323         /// are accounted for in the wrapping operations.
1324         /// Since, for the positive integers, all common
1325         /// definitions of division are equal, this
1326         /// is exactly equal to `self.wrapping_rem(rhs)`.
1327         ///
1328         /// # Examples
1329         ///
1330         /// Basic usage:
1331         ///
1332         /// ```
1333         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
1334         /// ```
1335         #[stable(feature = "euclidean_division", since = "1.38.0")]
1336         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1337         #[must_use = "this returns the result of the operation, \
1338                       without modifying the original"]
1339         #[inline(always)]
1340         pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1341             self % rhs
1342         }
1343
1344         /// Wrapping (modular) negation. Computes `-self`,
1345         /// wrapping around at the boundary of the type.
1346         ///
1347         /// Since unsigned types do not have negative equivalents
1348         /// all applications of this function will wrap (except for `-0`).
1349         /// For values smaller than the corresponding signed type's maximum
1350         /// the result is the same as casting the corresponding signed value.
1351         /// Any larger values are equivalent to `MAX + 1 - (val - MAX - 1)` where
1352         /// `MAX` is the corresponding signed type's maximum.
1353         ///
1354         /// # Examples
1355         ///
1356         /// Basic usage:
1357         ///
1358         /// Please note that this example is shared between integer types.
1359         /// Which explains why `i8` is used here.
1360         ///
1361         /// ```
1362         /// assert_eq!(100i8.wrapping_neg(), -100);
1363         /// assert_eq!((-128i8).wrapping_neg(), -128);
1364         /// ```
1365         #[stable(feature = "num_wrapping", since = "1.2.0")]
1366         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1367         #[must_use = "this returns the result of the operation, \
1368                       without modifying the original"]
1369         #[inline(always)]
1370         pub const fn wrapping_neg(self) -> Self {
1371             (0 as $SelfT).wrapping_sub(self)
1372         }
1373
1374         /// Panic-free bitwise shift-left; yields `self << mask(rhs)`,
1375         /// where `mask` removes any high-order bits of `rhs` that
1376         /// would cause the shift to exceed the bitwidth of the type.
1377         ///
1378         /// Note that this is *not* the same as a rotate-left; the
1379         /// RHS of a wrapping shift-left is restricted to the range
1380         /// of the type, rather than the bits shifted out of the LHS
1381         /// being returned to the other end. The primitive integer
1382         /// types all implement a [`rotate_left`](Self::rotate_left) function,
1383         /// which may be what you want instead.
1384         ///
1385         /// # Examples
1386         ///
1387         /// Basic usage:
1388         ///
1389         /// ```
1390         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_shl(7), 128);")]
1391         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".wrapping_shl(128), 1);")]
1392         /// ```
1393         #[stable(feature = "num_wrapping", since = "1.2.0")]
1394         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1395         #[must_use = "this returns the result of the operation, \
1396                       without modifying the original"]
1397         #[inline(always)]
1398         pub const fn wrapping_shl(self, rhs: u32) -> Self {
1399             // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1400             // out of bounds
1401             unsafe {
1402                 intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1403             }
1404         }
1405
1406         /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`,
1407         /// where `mask` removes any high-order bits of `rhs` that
1408         /// would cause the shift to exceed the bitwidth of the type.
1409         ///
1410         /// Note that this is *not* the same as a rotate-right; the
1411         /// RHS of a wrapping shift-right is restricted to the range
1412         /// of the type, rather than the bits shifted out of the LHS
1413         /// being returned to the other end. The primitive integer
1414         /// types all implement a [`rotate_right`](Self::rotate_right) function,
1415         /// which may be what you want instead.
1416         ///
1417         /// # Examples
1418         ///
1419         /// Basic usage:
1420         ///
1421         /// ```
1422         #[doc = concat!("assert_eq!(128", stringify!($SelfT), ".wrapping_shr(7), 1);")]
1423         #[doc = concat!("assert_eq!(128", stringify!($SelfT), ".wrapping_shr(128), 128);")]
1424         /// ```
1425         #[stable(feature = "num_wrapping", since = "1.2.0")]
1426         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1427         #[must_use = "this returns the result of the operation, \
1428                       without modifying the original"]
1429         #[inline(always)]
1430         pub const fn wrapping_shr(self, rhs: u32) -> Self {
1431             // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1432             // out of bounds
1433             unsafe {
1434                 intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1435             }
1436         }
1437
1438         /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
1439         /// wrapping around at the boundary of the type.
1440         ///
1441         /// # Examples
1442         ///
1443         /// Basic usage:
1444         ///
1445         /// ```
1446         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(5), 243);")]
1447         /// assert_eq!(3u8.wrapping_pow(6), 217);
1448         /// ```
1449         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1450         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1451         #[must_use = "this returns the result of the operation, \
1452                       without modifying the original"]
1453         #[inline]
1454         pub const fn wrapping_pow(self, mut exp: u32) -> Self {
1455             if exp == 0 {
1456                 return 1;
1457             }
1458             let mut base = self;
1459             let mut acc: Self = 1;
1460
1461             while exp > 1 {
1462                 if (exp & 1) == 1 {
1463                     acc = acc.wrapping_mul(base);
1464                 }
1465                 exp /= 2;
1466                 base = base.wrapping_mul(base);
1467             }
1468
1469             // since exp!=0, finally the exp must be 1.
1470             // Deal with the final bit of the exponent separately, since
1471             // squaring the base afterwards is not necessary and may cause a
1472             // needless overflow.
1473             acc.wrapping_mul(base)
1474         }
1475
1476         /// Calculates `self` + `rhs`
1477         ///
1478         /// Returns a tuple of the addition along with a boolean indicating
1479         /// whether an arithmetic overflow would occur. If an overflow would
1480         /// have occurred then the wrapped value is returned.
1481         ///
1482         /// # Examples
1483         ///
1484         /// Basic usage
1485         ///
1486         /// ```
1487         ///
1488         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
1489         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (0, true));")]
1490         /// ```
1491         #[stable(feature = "wrapping", since = "1.7.0")]
1492         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1493         #[must_use = "this returns the result of the operation, \
1494                       without modifying the original"]
1495         #[inline(always)]
1496         pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1497             let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
1498             (a as Self, b)
1499         }
1500
1501         /// Calculates `self + rhs + carry` without the ability to overflow.
1502         ///
1503         /// Performs "ternary addition" which takes in an extra bit to add, and may return an
1504         /// additional bit of overflow. This allows for chaining together multiple additions
1505         /// to create "big integers" which represent larger values.
1506         ///
1507         /// # Examples
1508         ///
1509         /// Basic usage
1510         ///
1511         /// ```
1512         /// #![feature(bigint_helper_methods)]
1513         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, false), (7, false));")]
1514         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, true), (8, false));")]
1515         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), (0, true));")]
1516         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, true), (1, true));")]
1517         /// ```
1518         #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1519         #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1520         #[must_use = "this returns the result of the operation, \
1521                       without modifying the original"]
1522         #[inline]
1523         pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1524             // note: longer-term this should be done via an intrinsic, but this has been shown
1525             //   to generate optimal code for now, and LLVM doesn't have an equivalent intrinsic
1526             let (a, b) = self.overflowing_add(rhs);
1527             let (c, d) = a.overflowing_add(carry as $SelfT);
1528             (c, b || d)
1529         }
1530
1531         /// Calculates `self` + `rhs` with a signed `rhs`
1532         ///
1533         /// Returns a tuple of the addition along with a boolean indicating
1534         /// whether an arithmetic overflow would occur. If an overflow would
1535         /// have occurred then the wrapped value is returned.
1536         ///
1537         /// # Examples
1538         ///
1539         /// Basic usage:
1540         ///
1541         /// ```
1542         /// # #![feature(mixed_integer_ops)]
1543         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_signed(2), (3, false));")]
1544         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_signed(-2), (", stringify!($SelfT), "::MAX, true));")]
1545         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_signed(4), (1, true));")]
1546         /// ```
1547         #[unstable(feature = "mixed_integer_ops", issue = "87840")]
1548         #[rustc_const_unstable(feature = "mixed_integer_ops", issue = "87840")]
1549         #[must_use = "this returns the result of the operation, \
1550                       without modifying the original"]
1551         #[inline]
1552         pub const fn overflowing_add_signed(self, rhs: $SignedT) -> (Self, bool) {
1553             let (res, overflowed) = self.overflowing_add(rhs as Self);
1554             (res, overflowed ^ (rhs < 0))
1555         }
1556
1557         /// Calculates `self` - `rhs`
1558         ///
1559         /// Returns a tuple of the subtraction along with a boolean indicating
1560         /// whether an arithmetic overflow would occur. If an overflow would
1561         /// have occurred then the wrapped value is returned.
1562         ///
1563         /// # Examples
1564         ///
1565         /// Basic usage
1566         ///
1567         /// ```
1568         ///
1569         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
1570         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
1571         /// ```
1572         #[stable(feature = "wrapping", since = "1.7.0")]
1573         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1574         #[must_use = "this returns the result of the operation, \
1575                       without modifying the original"]
1576         #[inline(always)]
1577         pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1578             let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
1579             (a as Self, b)
1580         }
1581
1582         /// Calculates `self - rhs - borrow` without the ability to overflow.
1583         ///
1584         /// Performs "ternary subtraction" which takes in an extra bit to subtract, and may return
1585         /// an additional bit of overflow. This allows for chaining together multiple subtractions
1586         /// to create "big integers" which represent larger values.
1587         ///
1588         /// # Examples
1589         ///
1590         /// Basic usage
1591         ///
1592         /// ```
1593         /// #![feature(bigint_helper_methods)]
1594         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, false), (3, false));")]
1595         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, true), (2, false));")]
1596         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, false), (", stringify!($SelfT), "::MAX, true));")]
1597         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, true), (", stringify!($SelfT), "::MAX - 1, true));")]
1598         /// ```
1599         #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1600         #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1601         #[must_use = "this returns the result of the operation, \
1602                       without modifying the original"]
1603         #[inline]
1604         pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1605             // note: longer-term this should be done via an intrinsic, but this has been shown
1606             //   to generate optimal code for now, and LLVM doesn't have an equivalent intrinsic
1607             let (a, b) = self.overflowing_sub(rhs);
1608             let (c, d) = a.overflowing_sub(borrow as $SelfT);
1609             (c, b || d)
1610         }
1611
1612         /// Computes the absolute difference between `self` and `other`.
1613         ///
1614         /// # Examples
1615         ///
1616         /// Basic usage:
1617         ///
1618         /// ```
1619         /// #![feature(int_abs_diff)]
1620         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($SelfT), ");")]
1621         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($SelfT), ");")]
1622         /// ```
1623         #[unstable(feature = "int_abs_diff", issue = "89492")]
1624         #[must_use = "this returns the result of the operation, \
1625                       without modifying the original"]
1626         #[inline]
1627         pub const fn abs_diff(self, other: Self) -> Self {
1628             if mem::size_of::<Self>() == 1 {
1629                 // Trick LLVM into generating the psadbw instruction when SSE2
1630                 // is available and this function is autovectorized for u8's.
1631                 (self as i32).wrapping_sub(other as i32).abs() as Self
1632             } else {
1633                 if self < other {
1634                     other - self
1635                 } else {
1636                     self - other
1637                 }
1638             }
1639         }
1640
1641         /// Calculates the multiplication of `self` and `rhs`.
1642         ///
1643         /// Returns a tuple of the multiplication along with a boolean
1644         /// indicating whether an arithmetic overflow would occur. If an
1645         /// overflow would have occurred then the wrapped value is returned.
1646         ///
1647         /// # Examples
1648         ///
1649         /// Basic usage:
1650         ///
1651         /// Please note that this example is shared between integer types.
1652         /// Which explains why `u32` is used here.
1653         ///
1654         /// ```
1655         /// assert_eq!(5u32.overflowing_mul(2), (10, false));
1656         /// assert_eq!(1_000_000_000u32.overflowing_mul(10), (1410065408, true));
1657         /// ```
1658         #[stable(feature = "wrapping", since = "1.7.0")]
1659         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1660         #[must_use = "this returns the result of the operation, \
1661                           without modifying the original"]
1662         #[inline(always)]
1663         pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1664             let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
1665             (a as Self, b)
1666         }
1667
1668         /// Calculates the divisor when `self` is divided by `rhs`.
1669         ///
1670         /// Returns a tuple of the divisor along with a boolean indicating
1671         /// whether an arithmetic overflow would occur. Note that for unsigned
1672         /// integers overflow never occurs, so the second value is always
1673         /// `false`.
1674         ///
1675         /// # Panics
1676         ///
1677         /// This function will panic if `rhs` is 0.
1678         ///
1679         /// # Examples
1680         ///
1681         /// Basic usage
1682         ///
1683         /// ```
1684         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
1685         /// ```
1686         #[inline(always)]
1687         #[stable(feature = "wrapping", since = "1.7.0")]
1688         #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1689         #[must_use = "this returns the result of the operation, \
1690                       without modifying the original"]
1691         pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1692             (self / rhs, false)
1693         }
1694
1695         /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
1696         ///
1697         /// Returns a tuple of the divisor along with a boolean indicating
1698         /// whether an arithmetic overflow would occur. Note that for unsigned
1699         /// integers overflow never occurs, so the second value is always
1700         /// `false`.
1701         /// Since, for the positive integers, all common
1702         /// definitions of division are equal, this
1703         /// is exactly equal to `self.overflowing_div(rhs)`.
1704         ///
1705         /// # Panics
1706         ///
1707         /// This function will panic if `rhs` is 0.
1708         ///
1709         /// # Examples
1710         ///
1711         /// Basic usage
1712         ///
1713         /// ```
1714         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
1715         /// ```
1716         #[inline(always)]
1717         #[stable(feature = "euclidean_division", since = "1.38.0")]
1718         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1719         #[must_use = "this returns the result of the operation, \
1720                       without modifying the original"]
1721         pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1722             (self / rhs, false)
1723         }
1724
1725         /// Calculates the remainder when `self` is divided by `rhs`.
1726         ///
1727         /// Returns a tuple of the remainder after dividing along with a boolean
1728         /// indicating whether an arithmetic overflow would occur. Note that for
1729         /// unsigned integers overflow never occurs, so the second value is
1730         /// always `false`.
1731         ///
1732         /// # Panics
1733         ///
1734         /// This function will panic if `rhs` is 0.
1735         ///
1736         /// # Examples
1737         ///
1738         /// Basic usage
1739         ///
1740         /// ```
1741         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
1742         /// ```
1743         #[inline(always)]
1744         #[stable(feature = "wrapping", since = "1.7.0")]
1745         #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1746         #[must_use = "this returns the result of the operation, \
1747                       without modifying the original"]
1748         pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1749             (self % rhs, false)
1750         }
1751
1752         /// Calculates the remainder `self.rem_euclid(rhs)` as if by Euclidean division.
1753         ///
1754         /// Returns a tuple of the modulo after dividing along with a boolean
1755         /// indicating whether an arithmetic overflow would occur. Note that for
1756         /// unsigned integers overflow never occurs, so the second value is
1757         /// always `false`.
1758         /// Since, for the positive integers, all common
1759         /// definitions of division are equal, this operation
1760         /// is exactly equal to `self.overflowing_rem(rhs)`.
1761         ///
1762         /// # Panics
1763         ///
1764         /// This function will panic if `rhs` is 0.
1765         ///
1766         /// # Examples
1767         ///
1768         /// Basic usage
1769         ///
1770         /// ```
1771         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
1772         /// ```
1773         #[inline(always)]
1774         #[stable(feature = "euclidean_division", since = "1.38.0")]
1775         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1776         #[must_use = "this returns the result of the operation, \
1777                       without modifying the original"]
1778         pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1779             (self % rhs, false)
1780         }
1781
1782         /// Negates self in an overflowing fashion.
1783         ///
1784         /// Returns `!self + 1` using wrapping operations to return the value
1785         /// that represents the negation of this unsigned value. Note that for
1786         /// positive unsigned values overflow always occurs, but negating 0 does
1787         /// not overflow.
1788         ///
1789         /// # Examples
1790         ///
1791         /// Basic usage
1792         ///
1793         /// ```
1794         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".overflowing_neg(), (0, false));")]
1795         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!($SelfT), ", true));")]
1796         /// ```
1797         #[inline(always)]
1798         #[stable(feature = "wrapping", since = "1.7.0")]
1799         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1800         #[must_use = "this returns the result of the operation, \
1801                       without modifying the original"]
1802         pub const fn overflowing_neg(self) -> (Self, bool) {
1803             ((!self).wrapping_add(1), self != 0)
1804         }
1805
1806         /// Shifts self left by `rhs` bits.
1807         ///
1808         /// Returns a tuple of the shifted version of self along with a boolean
1809         /// indicating whether the shift value was larger than or equal to the
1810         /// number of bits. If the shift value is too large, then value is
1811         /// masked (N-1) where N is the number of bits, and this value is then
1812         /// used to perform the shift.
1813         ///
1814         /// # Examples
1815         ///
1816         /// Basic usage
1817         ///
1818         /// ```
1819         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".overflowing_shl(4), (0x10, false));")]
1820         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".overflowing_shl(132), (0x10, true));")]
1821         /// ```
1822         #[stable(feature = "wrapping", since = "1.7.0")]
1823         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1824         #[must_use = "this returns the result of the operation, \
1825                       without modifying the original"]
1826         #[inline(always)]
1827         pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1828             (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1829         }
1830
1831         /// Shifts self right by `rhs` bits.
1832         ///
1833         /// Returns a tuple of the shifted version of self along with a boolean
1834         /// indicating whether the shift value was larger than or equal to the
1835         /// number of bits. If the shift value is too large, then value is
1836         /// masked (N-1) where N is the number of bits, and this value is then
1837         /// used to perform the shift.
1838         ///
1839         /// # Examples
1840         ///
1841         /// Basic usage
1842         ///
1843         /// ```
1844         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
1845         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(132), (0x1, true));")]
1846         /// ```
1847         #[stable(feature = "wrapping", since = "1.7.0")]
1848         #[rustc_const_stable(feature = "const_wrapping_math", since = "1.32.0")]
1849         #[must_use = "this returns the result of the operation, \
1850                       without modifying the original"]
1851         #[inline(always)]
1852         pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1853             (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1854         }
1855
1856         /// Raises self to the power of `exp`, using exponentiation by squaring.
1857         ///
1858         /// Returns a tuple of the exponentiation along with a bool indicating
1859         /// whether an overflow happened.
1860         ///
1861         /// # Examples
1862         ///
1863         /// Basic usage:
1864         ///
1865         /// ```
1866         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(5), (243, false));")]
1867         /// assert_eq!(3u8.overflowing_pow(6), (217, true));
1868         /// ```
1869         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1870         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1871         #[must_use = "this returns the result of the operation, \
1872                       without modifying the original"]
1873         #[inline]
1874         pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1875             if exp == 0{
1876                 return (1,false);
1877             }
1878             let mut base = self;
1879             let mut acc: Self = 1;
1880             let mut overflown = false;
1881             // Scratch space for storing results of overflowing_mul.
1882             let mut r;
1883
1884             while exp > 1 {
1885                 if (exp & 1) == 1 {
1886                     r = acc.overflowing_mul(base);
1887                     acc = r.0;
1888                     overflown |= r.1;
1889                 }
1890                 exp /= 2;
1891                 r = base.overflowing_mul(base);
1892                 base = r.0;
1893                 overflown |= r.1;
1894             }
1895
1896             // since exp!=0, finally the exp must be 1.
1897             // Deal with the final bit of the exponent separately, since
1898             // squaring the base afterwards is not necessary and may cause a
1899             // needless overflow.
1900             r = acc.overflowing_mul(base);
1901             r.1 |= overflown;
1902
1903             r
1904         }
1905
1906         /// Raises self to the power of `exp`, using exponentiation by squaring.
1907         ///
1908         /// # Examples
1909         ///
1910         /// Basic usage:
1911         ///
1912         /// ```
1913         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".pow(5), 32);")]
1914         /// ```
1915         #[stable(feature = "rust1", since = "1.0.0")]
1916         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1917         #[must_use = "this returns the result of the operation, \
1918                       without modifying the original"]
1919         #[inline]
1920         #[rustc_inherit_overflow_checks]
1921         pub const fn pow(self, mut exp: u32) -> Self {
1922             if exp == 0 {
1923                 return 1;
1924             }
1925             let mut base = self;
1926             let mut acc = 1;
1927
1928             while exp > 1 {
1929                 if (exp & 1) == 1 {
1930                     acc = acc * base;
1931                 }
1932                 exp /= 2;
1933                 base = base * base;
1934             }
1935
1936             // since exp!=0, finally the exp must be 1.
1937             // Deal with the final bit of the exponent separately, since
1938             // squaring the base afterwards is not necessary and may cause a
1939             // needless overflow.
1940             acc * base
1941         }
1942
1943         /// Performs Euclidean division.
1944         ///
1945         /// Since, for the positive integers, all common
1946         /// definitions of division are equal, this
1947         /// is exactly equal to `self / rhs`.
1948         ///
1949         /// # Panics
1950         ///
1951         /// This function will panic if `rhs` is 0.
1952         ///
1953         /// # Examples
1954         ///
1955         /// Basic usage:
1956         ///
1957         /// ```
1958         #[doc = concat!("assert_eq!(7", stringify!($SelfT), ".div_euclid(4), 1); // or any other integer type")]
1959         /// ```
1960         #[stable(feature = "euclidean_division", since = "1.38.0")]
1961         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1962         #[must_use = "this returns the result of the operation, \
1963                       without modifying the original"]
1964         #[inline(always)]
1965         #[rustc_inherit_overflow_checks]
1966         pub const fn div_euclid(self, rhs: Self) -> Self {
1967             self / rhs
1968         }
1969
1970
1971         /// Calculates the least remainder of `self (mod rhs)`.
1972         ///
1973         /// Since, for the positive integers, all common
1974         /// definitions of division are equal, this
1975         /// is exactly equal to `self % rhs`.
1976         ///
1977         /// # Panics
1978         ///
1979         /// This function will panic if `rhs` is 0.
1980         ///
1981         /// # Examples
1982         ///
1983         /// Basic usage:
1984         ///
1985         /// ```
1986         #[doc = concat!("assert_eq!(7", stringify!($SelfT), ".rem_euclid(4), 3); // or any other integer type")]
1987         /// ```
1988         #[stable(feature = "euclidean_division", since = "1.38.0")]
1989         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1990         #[must_use = "this returns the result of the operation, \
1991                       without modifying the original"]
1992         #[inline(always)]
1993         #[rustc_inherit_overflow_checks]
1994         pub const fn rem_euclid(self, rhs: Self) -> Self {
1995             self % rhs
1996         }
1997
1998         /// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1999         ///
2000         /// This is the same as performing `self / rhs` for all unsigned integers.
2001         ///
2002         /// # Panics
2003         ///
2004         /// This function will panic if `rhs` is 0.
2005         ///
2006         /// # Examples
2007         ///
2008         /// Basic usage:
2009         ///
2010         /// ```
2011         /// #![feature(int_roundings)]
2012         #[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unstable_div_floor(4), 1);")]
2013         /// ```
2014         #[unstable(feature = "int_roundings", issue = "88581")]
2015         #[must_use = "this returns the result of the operation, \
2016                       without modifying the original"]
2017         #[inline(always)]
2018         #[rustc_inherit_overflow_checks]
2019         pub const fn unstable_div_floor(self, rhs: Self) -> Self {
2020             self / rhs
2021         }
2022
2023         /// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
2024         ///
2025         /// # Panics
2026         ///
2027         /// This function will panic if `rhs` is 0.
2028         ///
2029         /// # Examples
2030         ///
2031         /// Basic usage:
2032         ///
2033         /// ```
2034         /// #![feature(int_roundings)]
2035         #[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".unstable_div_ceil(4), 2);")]
2036         /// ```
2037         #[unstable(feature = "int_roundings", issue = "88581")]
2038         #[must_use = "this returns the result of the operation, \
2039                       without modifying the original"]
2040         #[inline]
2041         #[rustc_inherit_overflow_checks]
2042         pub const fn unstable_div_ceil(self, rhs: Self) -> Self {
2043             let d = self / rhs;
2044             let r = self % rhs;
2045             if r > 0 && rhs > 0 {
2046                 d + 1
2047             } else {
2048                 d
2049             }
2050         }
2051
2052         /// Calculates the smallest value greater than or equal to `self` that
2053         /// is a multiple of `rhs`.
2054         ///
2055         /// # Panics
2056         ///
2057         /// This function will panic if `rhs` is 0 or the operation results in overflow.
2058         ///
2059         /// # Examples
2060         ///
2061         /// Basic usage:
2062         ///
2063         /// ```
2064         /// #![feature(int_roundings)]
2065         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".unstable_next_multiple_of(8), 16);")]
2066         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".unstable_next_multiple_of(8), 24);")]
2067         /// ```
2068         #[unstable(feature = "int_roundings", issue = "88581")]
2069         #[must_use = "this returns the result of the operation, \
2070                       without modifying the original"]
2071         #[inline]
2072         #[rustc_inherit_overflow_checks]
2073         pub const fn unstable_next_multiple_of(self, rhs: Self) -> Self {
2074             match self % rhs {
2075                 0 => self,
2076                 r => self + (rhs - r)
2077             }
2078         }
2079
2080         /// Calculates the smallest value greater than or equal to `self` that
2081         /// is a multiple of `rhs`. Returns `None` is `rhs` is zero or the
2082         /// operation would result in overflow.
2083         ///
2084         /// # Examples
2085         ///
2086         /// Basic usage:
2087         ///
2088         /// ```
2089         /// #![feature(int_roundings)]
2090         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
2091         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
2092         #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
2093         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
2094         /// ```
2095         #[unstable(feature = "int_roundings", issue = "88581")]
2096         #[must_use = "this returns the result of the operation, \
2097                       without modifying the original"]
2098         #[inline]
2099         #[rustc_inherit_overflow_checks]
2100         pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
2101             match try_opt!(self.checked_rem(rhs)) {
2102                 0 => Some(self),
2103                 r => self.checked_add(try_opt!(rhs.checked_sub(r)))
2104             }
2105         }
2106
2107         /// Returns `true` if and only if `self == 2^k` for some `k`.
2108         ///
2109         /// # Examples
2110         ///
2111         /// Basic usage:
2112         ///
2113         /// ```
2114         #[doc = concat!("assert!(16", stringify!($SelfT), ".is_power_of_two());")]
2115         #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_power_of_two());")]
2116         /// ```
2117         #[must_use]
2118         #[stable(feature = "rust1", since = "1.0.0")]
2119         #[rustc_const_stable(feature = "const_is_power_of_two", since = "1.32.0")]
2120         #[inline(always)]
2121         pub const fn is_power_of_two(self) -> bool {
2122             self.count_ones() == 1
2123         }
2124
2125         // Returns one less than next power of two.
2126         // (For 8u8 next power of two is 8u8 and for 6u8 it is 8u8)
2127         //
2128         // 8u8.one_less_than_next_power_of_two() == 7
2129         // 6u8.one_less_than_next_power_of_two() == 7
2130         //
2131         // This method cannot overflow, as in the `next_power_of_two`
2132         // overflow cases it instead ends up returning the maximum value
2133         // of the type, and can return 0 for 0.
2134         #[inline]
2135         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2136         const fn one_less_than_next_power_of_two(self) -> Self {
2137             if self <= 1 { return 0; }
2138
2139             let p = self - 1;
2140             // SAFETY: Because `p > 0`, it cannot consist entirely of leading zeros.
2141             // That means the shift is always in-bounds, and some processors
2142             // (such as intel pre-haswell) have more efficient ctlz
2143             // intrinsics when the argument is non-zero.
2144             let z = unsafe { intrinsics::ctlz_nonzero(p) };
2145             <$SelfT>::MAX >> z
2146         }
2147
2148         /// Returns the smallest power of two greater than or equal to `self`.
2149         ///
2150         /// When return value overflows (i.e., `self > (1 << (N-1))` for type
2151         /// `uN`), it panics in debug mode and the return value is wrapped to 0 in
2152         /// release mode (the only situation in which method can return 0).
2153         ///
2154         /// # Examples
2155         ///
2156         /// Basic usage:
2157         ///
2158         /// ```
2159         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".next_power_of_two(), 2);")]
2160         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".next_power_of_two(), 4);")]
2161         /// ```
2162         #[stable(feature = "rust1", since = "1.0.0")]
2163         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2164         #[must_use = "this returns the result of the operation, \
2165                       without modifying the original"]
2166         #[inline]
2167         #[rustc_inherit_overflow_checks]
2168         pub const fn next_power_of_two(self) -> Self {
2169             self.one_less_than_next_power_of_two() + 1
2170         }
2171
2172         /// Returns the smallest power of two greater than or equal to `n`. If
2173         /// the next power of two is greater than the type's maximum value,
2174         /// `None` is returned, otherwise the power of two is wrapped in `Some`.
2175         ///
2176         /// # Examples
2177         ///
2178         /// Basic usage:
2179         ///
2180         /// ```
2181         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_next_power_of_two(), Some(2));")]
2182         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".checked_next_power_of_two(), Some(4));")]
2183         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_power_of_two(), None);")]
2184         /// ```
2185         #[inline]
2186         #[stable(feature = "rust1", since = "1.0.0")]
2187         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2188         #[must_use = "this returns the result of the operation, \
2189                       without modifying the original"]
2190         pub const fn checked_next_power_of_two(self) -> Option<Self> {
2191             self.one_less_than_next_power_of_two().checked_add(1)
2192         }
2193
2194         /// Returns the smallest power of two greater than or equal to `n`. If
2195         /// the next power of two is greater than the type's maximum value,
2196         /// the return value is wrapped to `0`.
2197         ///
2198         /// # Examples
2199         ///
2200         /// Basic usage:
2201         ///
2202         /// ```
2203         /// #![feature(wrapping_next_power_of_two)]
2204         ///
2205         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".wrapping_next_power_of_two(), 2);")]
2206         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_next_power_of_two(), 4);")]
2207         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_next_power_of_two(), 0);")]
2208         /// ```
2209         #[unstable(feature = "wrapping_next_power_of_two", issue = "32463",
2210                    reason = "needs decision on wrapping behaviour")]
2211         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2212         #[must_use = "this returns the result of the operation, \
2213                       without modifying the original"]
2214         pub const fn wrapping_next_power_of_two(self) -> Self {
2215             self.one_less_than_next_power_of_two().wrapping_add(1)
2216         }
2217
2218         /// Return the memory representation of this integer as a byte array in
2219         /// big-endian (network) byte order.
2220         ///
2221         #[doc = $to_xe_bytes_doc]
2222         ///
2223         /// # Examples
2224         ///
2225         /// ```
2226         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
2227         #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
2228         /// ```
2229         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2230         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2231         #[must_use = "this returns the result of the operation, \
2232                       without modifying the original"]
2233         #[inline]
2234         pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
2235             self.to_be().to_ne_bytes()
2236         }
2237
2238         /// Return the memory representation of this integer as a byte array in
2239         /// little-endian byte order.
2240         ///
2241         #[doc = $to_xe_bytes_doc]
2242         ///
2243         /// # Examples
2244         ///
2245         /// ```
2246         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
2247         #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
2248         /// ```
2249         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2250         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2251         #[must_use = "this returns the result of the operation, \
2252                       without modifying the original"]
2253         #[inline]
2254         pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
2255             self.to_le().to_ne_bytes()
2256         }
2257
2258         /// Return the memory representation of this integer as a byte array in
2259         /// native byte order.
2260         ///
2261         /// As the target platform's native endianness is used, portable code
2262         /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
2263         /// instead.
2264         ///
2265         #[doc = $to_xe_bytes_doc]
2266         ///
2267         /// [`to_be_bytes`]: Self::to_be_bytes
2268         /// [`to_le_bytes`]: Self::to_le_bytes
2269         ///
2270         /// # Examples
2271         ///
2272         /// ```
2273         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
2274         /// assert_eq!(
2275         ///     bytes,
2276         ///     if cfg!(target_endian = "big") {
2277         #[doc = concat!("        ", $be_bytes)]
2278         ///     } else {
2279         #[doc = concat!("        ", $le_bytes)]
2280         ///     }
2281         /// );
2282         /// ```
2283         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2284         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2285         #[must_use = "this returns the result of the operation, \
2286                       without modifying the original"]
2287         // SAFETY: const sound because integers are plain old datatypes so we can always
2288         // transmute them to arrays of bytes
2289         #[inline]
2290         pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2291             // SAFETY: integers are plain old datatypes so we can always transmute them to
2292             // arrays of bytes
2293             unsafe { mem::transmute(self) }
2294         }
2295
2296         /// Create a native endian integer value from its representation
2297         /// as a byte array in big endian.
2298         ///
2299         #[doc = $from_xe_bytes_doc]
2300         ///
2301         /// # Examples
2302         ///
2303         /// ```
2304         #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
2305         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2306         /// ```
2307         ///
2308         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2309         ///
2310         /// ```
2311         /// use std::convert::TryInto;
2312         ///
2313         #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2314         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2315         ///     *input = rest;
2316         #[doc = concat!("    ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
2317         /// }
2318         /// ```
2319         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2320         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2321         #[must_use]
2322         #[inline]
2323         pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2324             Self::from_be(Self::from_ne_bytes(bytes))
2325         }
2326
2327         /// Create a native endian integer value from its representation
2328         /// as a byte array in little endian.
2329         ///
2330         #[doc = $from_xe_bytes_doc]
2331         ///
2332         /// # Examples
2333         ///
2334         /// ```
2335         #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
2336         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2337         /// ```
2338         ///
2339         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2340         ///
2341         /// ```
2342         /// use std::convert::TryInto;
2343         ///
2344         #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2345         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2346         ///     *input = rest;
2347         #[doc = concat!("    ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
2348         /// }
2349         /// ```
2350         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2351         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2352         #[must_use]
2353         #[inline]
2354         pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2355             Self::from_le(Self::from_ne_bytes(bytes))
2356         }
2357
2358         /// Create a native endian integer value from its memory representation
2359         /// as a byte array in native endianness.
2360         ///
2361         /// As the target platform's native endianness is used, portable code
2362         /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
2363         /// appropriate instead.
2364         ///
2365         /// [`from_be_bytes`]: Self::from_be_bytes
2366         /// [`from_le_bytes`]: Self::from_le_bytes
2367         ///
2368         #[doc = $from_xe_bytes_doc]
2369         ///
2370         /// # Examples
2371         ///
2372         /// ```
2373         #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
2374         #[doc = concat!("    ", $be_bytes, "")]
2375         /// } else {
2376         #[doc = concat!("    ", $le_bytes, "")]
2377         /// });
2378         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2379         /// ```
2380         ///
2381         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2382         ///
2383         /// ```
2384         /// use std::convert::TryInto;
2385         ///
2386         #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2387         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2388         ///     *input = rest;
2389         #[doc = concat!("    ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
2390         /// }
2391         /// ```
2392         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2393         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2394         #[must_use]
2395         // SAFETY: const sound because integers are plain old datatypes so we can always
2396         // transmute to them
2397         #[inline]
2398         pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2399             // SAFETY: integers are plain old datatypes so we can always transmute to them
2400             unsafe { mem::transmute(bytes) }
2401         }
2402
2403         /// New code should prefer to use
2404         #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
2405         ///
2406         /// Returns the smallest value that can be represented by this integer type.
2407         #[stable(feature = "rust1", since = "1.0.0")]
2408         #[rustc_promotable]
2409         #[inline(always)]
2410         #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2411         #[rustc_deprecated(since = "TBD", reason = "replaced by the `MIN` associated constant on this type")]
2412         pub const fn min_value() -> Self { Self::MIN }
2413
2414         /// New code should prefer to use
2415         #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
2416         ///
2417         /// Returns the largest value that can be represented by this integer type.
2418         #[stable(feature = "rust1", since = "1.0.0")]
2419         #[rustc_promotable]
2420         #[inline(always)]
2421         #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2422         #[rustc_deprecated(since = "TBD", reason = "replaced by the `MAX` associated constant on this type")]
2423         pub const fn max_value() -> Self { Self::MAX }
2424     }
2425 }